1 /*-
2  * Public Domain 2014-2018 MongoDB, Inc.
3  * Public Domain 2008-2014 WiredTiger, Inc.
4  *
5  * This is free and unencumbered software released into the public domain.
6  *
7  * Anyone is free to copy, modify, publish, use, compile, sell, or
8  * distribute this software, either in source code form or as a compiled
9  * binary, for any purpose, commercial or non-commercial, and by any
10  * means.
11  *
12  * In jurisdictions that recognize copyright laws, the author or authors
13  * of this software dedicate any and all copyright interest in the
14  * software to the public domain. We make this dedication for the benefit
15  * of the public at large and to the detriment of our heirs and
16  * successors. We intend this dedication to be an overt act of
17  * relinquishment in perpetuity of all present and future rights to this
18  * software under copyright law.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 #include "format.h"
30 
31 /*
32  * lrt --
33  *	Start a long-running transaction.
34  */
35 WT_THREAD_RET
lrt(void * arg)36 lrt(void *arg)
37 {
38 	WT_CONNECTION *conn;
39 	WT_CURSOR *cursor;
40 	WT_ITEM key, value;
41 	WT_SESSION *session;
42 	size_t buf_len, buf_size;
43 	uint64_t keyno, saved_keyno;
44 	uint8_t bitfield;
45 	u_int period;
46 	int pinned, ret;
47 	void *buf;
48 
49 	(void)(arg);			/* Unused parameter */
50 
51 	saved_keyno = 0;		/* [-Werror=maybe-uninitialized] */
52 
53 	key_gen_init(&key);
54 	val_gen_init(&value);
55 
56 	buf = NULL;
57 	buf_len = buf_size = 0;
58 
59 	/* Open a session and cursor. */
60 	conn = g.wts_conn;
61 	testutil_check(conn->open_session(conn, NULL, NULL, &session));
62 	/*
63 	 * open_cursor can return EBUSY if concurrent with a metadata
64 	 * operation, retry in that case.
65 	 */
66 	while ((ret = session->open_cursor(
67 	    session, g.uri, NULL, NULL, &cursor)) == EBUSY)
68 		__wt_yield();
69 	testutil_check(ret);
70 
71 	for (pinned = 0;;) {
72 		if (pinned) {
73 			/* Re-read the record at the end of the table. */
74 			while ((ret = read_row_worker(cursor,
75 			    saved_keyno, &key, &value, false)) == WT_ROLLBACK)
76 				;
77 			if (ret != 0)
78 				testutil_die(ret,
79 				    "read_row_worker %" PRIu64, saved_keyno);
80 
81 			/* Compare the previous value with the current one. */
82 			if (g.type == FIX) {
83 				ret = cursor->get_value(cursor, &bitfield);
84 				value.data = &bitfield;
85 				value.size = 1;
86 			} else
87 				ret = cursor->get_value(cursor, &value);
88 			if (ret != 0)
89 				testutil_die(ret,
90 				    "cursor.get_value: %" PRIu64, saved_keyno);
91 
92 			if (buf_size != value.size ||
93 			    memcmp(buf, value.data, value.size) != 0)
94 				testutil_die(0, "mismatched start/stop values");
95 
96 			/* End the transaction. */
97 			testutil_check(
98 			    session->commit_transaction(session, NULL));
99 
100 			/* Reset the cursor, releasing our pin. */
101 			testutil_check(cursor->reset(cursor));
102 			pinned = 0;
103 		} else {
104 			/*
105 			 * Test named snapshots: create a snapshot, wait to
106 			 * give the transaction state time to move forward,
107 			 * then start a transaction with the named snapshot,
108 			 * drop it, then commit the transaction. This exercises
109 			 * most of the named snapshot logic under load.
110 			 */
111 			testutil_check(session->snapshot(session, "name=test"));
112 			__wt_sleep(1, 0);
113 			/*
114 			 * Keep trying to start a new transaction if it's
115 			 * timing out - we know there aren't any resources
116 			 * pinned so it should succeed eventually.
117 			 */
118 			while ((ret = session->begin_transaction(
119 			    session, "snapshot=test")) == WT_CACHE_FULL)
120 				;
121 			testutil_check(ret);
122 			testutil_check(session->snapshot(
123 			    session, "drop=(all)"));
124 			testutil_check(session->commit_transaction(
125 			    session, NULL));
126 
127 			/*
128 			 * Begin transaction: without an explicit transaction,
129 			 * the snapshot is only kept around while a cursor is
130 			 * positioned. As soon as the cursor loses its position
131 			 * a new snapshot will be allocated.
132 			 */
133 			while ((ret = session->begin_transaction(
134 			    session, "snapshot=snapshot")) == WT_CACHE_FULL)
135 				;
136 			testutil_check(ret);
137 
138 			/* Read a record at the end of the table. */
139 			do {
140 				saved_keyno = mmrand(NULL,
141 				    (u_int)(g.key_cnt - g.key_cnt / 10),
142 				    (u_int)g.key_cnt);
143 				while ((ret = read_row_worker(cursor,
144 				    saved_keyno,
145 				    &key, &value, false)) == WT_ROLLBACK)
146 					;
147 			} while (ret == WT_NOTFOUND);
148 			if (ret != 0)
149 				testutil_die(ret,
150 				    "read_row_worker %" PRIu64, saved_keyno);
151 
152 			/* Copy the cursor's value. */
153 			if (g.type == FIX) {
154 				ret = cursor->get_value(cursor, &bitfield);
155 				value.data = &bitfield;
156 				value.size = 1;
157 			} else
158 				ret = cursor->get_value(cursor, &value);
159 			if (ret != 0)
160 				testutil_die(ret,
161 				    "cursor.get_value: %" PRIu64, saved_keyno);
162 			if (buf_len < value.size)
163 				buf = drealloc(buf, buf_len = value.size);
164 			memcpy(buf, value.data, buf_size = value.size);
165 
166 			/*
167 			 * Move the cursor to an early record in the table,
168 			 * hopefully allowing the page with the record just
169 			 * retrieved to be evicted from memory.
170 			 */
171 			do {
172 				keyno = mmrand(NULL, 1, (u_int)g.key_cnt / 5);
173 				while ((ret = read_row_worker(cursor,
174 				    keyno, &key, &value, false)) == WT_ROLLBACK)
175 					;
176 			} while (ret == WT_NOTFOUND);
177 			if (ret != 0)
178 				testutil_die(ret,
179 				    "read_row_worker %" PRIu64, keyno);
180 
181 			pinned = 1;
182 		}
183 
184 		/* Sleep for some number of seconds. */
185 		period = mmrand(NULL, 1, 10);
186 
187 		/* Sleep for short periods so we don't make the run wait. */
188 		while (period > 0 && !g.workers_finished) {
189 			--period;
190 			__wt_sleep(1, 0);
191 		}
192 		if (g.workers_finished)
193 			break;
194 	}
195 
196 	testutil_check(session->close(session, NULL));
197 
198 	key_gen_teardown(&key);
199 	val_gen_teardown(&value);
200 	free(buf);
201 
202 	return (WT_THREAD_RET_VALUE);
203 }
204