commit 201449acded532f07ce07e372b9cd131e39262cb
Author: nehakhatri5 <neha.khatri@mongodb.com>
Date:   Tue Sep 11 12:13:11 2018 +1000

    WT-4214 Simplify timestamp handling for test programs.

diff --git a/test/csuite/timestamp_abort/main.c b/test/csuite/timestamp_abort/main.c
index 5aec69cf0..c318620af 100644
--- a/test/csuite/timestamp_abort/main.c
+++ b/test/csuite/timestamp_abort/main.c
@@ -66,7 +66,6 @@ static char home[1024];			/* Program working dir */
 #define	PREPARE_FREQ	5
 #define	PREPARE_YIELD	(PREPARE_FREQ * 10)
 #define	RECORDS_FILE	"records-%" PRIu32
-#define	STABLE_PERIOD	100
 
 static const char * table_pfx = "table";
 static const char * const uri_local = "local";
@@ -101,6 +100,13 @@ typedef struct {
 	uint32_t info;
 } THREAD_DATA;
 
+/*
+ * We get the last committed timestamp periodically in order to update
+ * the oldest timestamp, that requires locking out transactional ops
+ * that set a timestamp.
+ */
+pthread_rwlock_t ts_lock;
+
 static void handler(int)
     WT_GCC_FUNC_DECL_ATTRIBUTE((noreturn));
 static void usage(void)
@@ -120,56 +126,34 @@ usage(void)
 static WT_THREAD_RET
 thread_ts_run(void *arg)
 {
+	WT_DECL_RET;
 	WT_SESSION *session;
 	THREAD_DATA *td;
-	uint64_t i, last_ts, oldest_ts, this_ts;
 	char tscfg[64];
+	char ts_buf[2 * WT_TIMESTAMP_SIZE +1];
 
 	td = (THREAD_DATA *)arg;
-	last_ts = 0;
 
 	testutil_check(td->conn->open_session(td->conn, NULL, NULL, &session));
 	/*
-	 * Every N records we will record our stable timestamp into the stable
-	 * table. That will define our threshold where we expect to find records
-	 * after recovery.
+	 * Every 10 seconds we will record our stable timestamp into the stable
+	 * table.
 	 */
 	for (;;) {
-		oldest_ts = UINT64_MAX;
-		/*
-		 * For the timestamp thread, the info field contains the number
-		 * of worker threads.
-		 */
-		for (i = 0; i < td->info; ++i) {
-			/*
-			 * We need to let all threads get started, so if we find
-			 * any thread still with a zero timestamp we go to
-			 * sleep.
-			 */
-			this_ts = th_ts[i];
-			if (this_ts == 0)
-				goto ts_wait;
-			else if (this_ts < oldest_ts)
-				oldest_ts = this_ts;
-		}
-
-		if (oldest_ts != UINT64_MAX &&
-		    oldest_ts - last_ts > STABLE_PERIOD) {
-			/*
-			 * Set both the oldest and stable timestamp so that we
-			 * don't need to maintain read availability at older
-			 * timestamps.
-			 */
+		__wt_sleep(10, 0);
+		testutil_check(pthread_rwlock_wrlock(&ts_lock));
+		ret = td->conn->query_timestamp(
+		    td->conn, ts_buf, "get=all_committed");
+		testutil_assert(ret == 0 || ret == WT_NOTFOUND);
+		if (ret == 0) {
 			testutil_check(__wt_snprintf(
 			    tscfg, sizeof(tscfg),
-			    "oldest_timestamp=%" PRIx64
-			    ",stable_timestamp=%" PRIx64,
-			    oldest_ts, oldest_ts));
+			    "oldest_timestamp=%s,stable_timestamp=%s",
+			    ts_buf, ts_buf));
 			testutil_check(
 			    td->conn->set_timestamp(td->conn, tscfg));
-			last_ts = oldest_ts;
-		} else
-ts_wait:		__wt_sleep(0, 1000);
+		}
+		testutil_check(pthread_rwlock_unlock(&ts_lock));
 	}
 	/* NOTREACHED */
 }
@@ -328,10 +312,12 @@ thread_run(void *arg)
 		 * transactions, it must be set after the prepare.
 		 */
 		if (use_ts && !use_prep) {
+			testutil_check(pthread_rwlock_wrlock(&ts_lock));
 			testutil_check(__wt_snprintf(tscfg, sizeof(tscfg),
 			    "commit_timestamp=%" PRIx64, stable_ts));
 			testutil_check(
 			    session->timestamp_transaction(session, tscfg));
+			testutil_check(pthread_rwlock_unlock(&ts_lock));
 		}
 		cur_coll->set_key(cur_coll, kname);
 		cur_local->set_key(cur_local, kname);
@@ -364,11 +350,13 @@ thread_run(void *arg)
 			 * on the regular session.
 			 */
 			if (use_prep && i % PREPARE_FREQ == 0) {
+				testutil_check(pthread_rwlock_wrlock(&ts_lock));
 				testutil_check(__wt_snprintf(
 				    tscfg, sizeof(tscfg),
 				    "prepare_timestamp=%" PRIx64, stable_ts));
 				testutil_check(session->prepare_transaction(
 				    session, tscfg));
+				testutil_check(pthread_rwlock_unlock(&ts_lock));
 				if (i % PREPARE_YIELD == 0)
 					__wt_yield();
 			}
@@ -380,17 +368,22 @@ thread_run(void *arg)
 				testutil_check(
 				    session->commit_transaction(session, NULL));
 			else {
+				testutil_check(pthread_rwlock_wrlock(&ts_lock));
 				testutil_check(
 				    __wt_snprintf(tscfg, sizeof(tscfg),
 				    "commit_timestamp=%" PRIx64, stable_ts));
 				testutil_check(
 				    session->commit_transaction(session,
 				    tscfg));
+				testutil_check(pthread_rwlock_unlock(&ts_lock));
 			}
-			if (use_prep)
+			if (use_prep) {
+				testutil_check(pthread_rwlock_wrlock(&ts_lock));
 				testutil_check(
 				    oplog_session->commit_transaction(
 				    oplog_session, tscfg));
+				testutil_check(pthread_rwlock_unlock(&ts_lock));
+			}
 			/*
 			 * Update the thread's last-committed timestamp.
 			 * Don't let the compiler re-order this statement,
@@ -780,6 +773,7 @@ main(int argc, char *argv[])
 	count = 0;
 	absent_coll = absent_local = absent_oplog = 0;
 	fatal = false;
+	testutil_check(pthread_rwlock_init(&ts_lock, NULL));
 	for (i = 0; i < nth; ++i) {
 		initialize_rep(&c_rep[i]);
 		initialize_rep(&l_rep[i]);
@@ -945,6 +939,7 @@ main(int argc, char *argv[])
 		    absent_oplog, count);
 		fatal = true;
 	}
+	testutil_check(pthread_rwlock_destroy(&ts_lock));
 	if (fatal)
 		return (EXIT_FAILURE);
 	printf("%" PRIu64 " records verified\n", count);
