1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/*-
* Copyright (c) 2014-2016 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __nsnap_destroy --
* Destroy a named snapshot structure.
*/
static void
__nsnap_destroy(WT_SESSION_IMPL *session, WT_NAMED_SNAPSHOT *nsnap)
{
__wt_free(session, nsnap->name);
__wt_free(session, nsnap->snapshot);
__wt_free(session, nsnap);
}
/*
* __nsnap_drop_one --
* Drop a single named snapshot. The named snapshot lock must be held
* write locked.
*/
static int
__nsnap_drop_one(WT_SESSION_IMPL *session, WT_CONFIG_ITEM *name)
{
WT_DECL_RET;
WT_NAMED_SNAPSHOT *found;
WT_TXN_GLOBAL *txn_global;
txn_global = &S2C(session)->txn_global;
TAILQ_FOREACH(found, &txn_global->nsnaph, q)
if (WT_STRING_MATCH(found->name, name->str, name->len))
break;
if (found == NULL)
return (WT_NOTFOUND);
/* Bump the global ID if we are removing the first entry */
if (found == TAILQ_FIRST(&txn_global->nsnaph))
txn_global->nsnap_oldest_id = (TAILQ_NEXT(found, q) != NULL) ?
TAILQ_NEXT(found, q)->snap_min : WT_TXN_NONE;
TAILQ_REMOVE(&txn_global->nsnaph, found, q);
__nsnap_destroy(session, found);
WT_STAT_FAST_CONN_INCR(session, txn_snapshots_dropped);
return (ret);
}
/*
* __nsnap_drop_to --
* Drop named snapshots, if the name is NULL all snapshots will be
* dropped. The named snapshot lock must be held write locked.
*/
static int
__nsnap_drop_to(WT_SESSION_IMPL *session, WT_CONFIG_ITEM *name, bool inclusive)
{
WT_DECL_RET;
WT_NAMED_SNAPSHOT *last, *nsnap, *prev;
WT_TXN_GLOBAL *txn_global;
uint64_t new_nsnap_oldest;
last = nsnap = prev = NULL;
txn_global = &S2C(session)->txn_global;
if (TAILQ_EMPTY(&txn_global->nsnaph)) {
if (name == NULL)
return (0);
/*
* Dropping specific snapshots when there aren't any it's an
* error.
*/
WT_RET_MSG(session, EINVAL,
"Named snapshot '%.*s' for drop not found",
(int)name->len, name->str);
}
/*
* The new ID will be none if we are removing all named snapshots
* which is the default behavior of this loop.
*/
new_nsnap_oldest = WT_TXN_NONE;
if (name != NULL) {
TAILQ_FOREACH(last, &txn_global->nsnaph, q) {
if (WT_STRING_MATCH(last->name, name->str, name->len))
break;
prev = last;
}
if (last == NULL)
WT_RET_MSG(session, EINVAL,
"Named snapshot '%.*s' for drop not found",
(int)name->len, name->str);
if (!inclusive) {
/* We are done if a drop before points to the head */
if (prev == 0)
return (0);
last = prev;
}
if (TAILQ_NEXT(last, q) != NULL)
new_nsnap_oldest = TAILQ_NEXT(last, q)->snap_min;
}
do {
nsnap = TAILQ_FIRST(&txn_global->nsnaph);
WT_ASSERT(session, nsnap != NULL);
TAILQ_REMOVE(&txn_global->nsnaph, nsnap, q);
__nsnap_destroy(session, nsnap);
WT_STAT_FAST_CONN_INCR(session, txn_snapshots_dropped);
/* Last will be NULL in the all case so it will never match */
} while (nsnap != last && !TAILQ_EMPTY(&txn_global->nsnaph));
/* Now that the queue of named snapshots is updated, update the ID */
txn_global->nsnap_oldest_id = new_nsnap_oldest;
return (ret);
}
/*
* __wt_txn_named_snapshot_begin --
* Begin an named in-memory snapshot.
*/
int
__wt_txn_named_snapshot_begin(WT_SESSION_IMPL *session, const char *cfg[])
{
WT_CONFIG_ITEM cval;
WT_DECL_RET;
WT_NAMED_SNAPSHOT *nsnap, *nsnap_new;
WT_TXN *txn;
WT_TXN_GLOBAL *txn_global;
const char *txn_cfg[] =
{ WT_CONFIG_BASE(session, WT_SESSION_begin_transaction),
"isolation=snapshot", NULL };
bool started_txn;
started_txn = false;
nsnap_new = NULL;
txn_global = &S2C(session)->txn_global;
txn = &session->txn;
WT_RET(__wt_config_gets_def(session, cfg, "name", 0, &cval));
WT_ASSERT(session, cval.len != 0);
if (!F_ISSET(txn, WT_TXN_RUNNING)) {
WT_RET(__wt_txn_begin(session, txn_cfg));
started_txn = true;
}
F_SET(txn, WT_TXN_READONLY);
/* Save a copy of the transaction's snapshot. */
WT_ERR(__wt_calloc_one(session, &nsnap_new));
nsnap = nsnap_new;
WT_ERR(__wt_strndup(session, cval.str, cval.len, &nsnap->name));
nsnap->snap_min = txn->snap_min;
nsnap->snap_max = txn->snap_max;
if (txn->snapshot_count > 0) {
WT_ERR(__wt_calloc_def(
session, txn->snapshot_count, &nsnap->snapshot));
memcpy(nsnap->snapshot, txn->snapshot,
txn->snapshot_count * sizeof(*nsnap->snapshot));
}
nsnap->snapshot_count = txn->snapshot_count;
/* Update the list. */
/*
* The semantic is that a new snapshot with the same name as an
* existing snapshot will replace the old one.
*/
WT_ERR_NOTFOUND_OK(__nsnap_drop_one(session, &cval));
if (TAILQ_EMPTY(&txn_global->nsnaph))
txn_global->nsnap_oldest_id = nsnap_new->snap_min;
TAILQ_INSERT_TAIL(&txn_global->nsnaph, nsnap_new, q);
WT_STAT_FAST_CONN_INCR(session, txn_snapshots_created);
nsnap_new = NULL;
err: if (started_txn)
WT_TRET(__wt_txn_rollback(session, NULL));
else if (ret == 0)
F_SET(txn, WT_TXN_NAMED_SNAPSHOT);
if (nsnap_new != NULL)
__nsnap_destroy(session, nsnap_new);
return (ret);
}
/*
* __wt_txn_named_snapshot_drop --
* Drop named snapshots
*/
int
__wt_txn_named_snapshot_drop(WT_SESSION_IMPL *session, const char *cfg[])
{
WT_CONFIG objectconf;
WT_CONFIG_ITEM all_config, k, names_config, to_config, before_config, v;
WT_DECL_RET;
WT_RET(__wt_config_gets_def(session, cfg, "drop.all", 0, &all_config));
WT_RET(__wt_config_gets_def(
session, cfg, "drop.names", 0, &names_config));
WT_RET(__wt_config_gets_def(session, cfg, "drop.to", 0, &to_config));
WT_RET(__wt_config_gets_def(
session, cfg, "drop.before", 0, &before_config));
if (all_config.val != 0)
WT_RET(__nsnap_drop_to(session, NULL, true));
else if (before_config.len != 0)
WT_RET(__nsnap_drop_to(session, &before_config, false));
else if (to_config.len != 0)
WT_RET(__nsnap_drop_to(session, &to_config, true));
/* We are done if there are no named drops */
if (names_config.len != 0) {
WT_RET(__wt_config_subinit(
session, &objectconf, &names_config));
while ((ret = __wt_config_next(&objectconf, &k, &v)) == 0) {
ret = __nsnap_drop_one(session, &k);
if (ret != 0)
WT_RET_MSG(session, EINVAL,
"Named snapshot '%.*s' for drop not found",
(int)k.len, k.str);
}
if (ret == WT_NOTFOUND)
ret = 0;
}
return (ret);
}
/*
* __wt_txn_named_snapshot_get --
* Lookup a named snapshot for a transaction.
*/
int
__wt_txn_named_snapshot_get(WT_SESSION_IMPL *session, WT_CONFIG_ITEM *nameval)
{
WT_NAMED_SNAPSHOT *nsnap;
WT_TXN *txn;
WT_TXN_GLOBAL *txn_global;
WT_TXN_STATE *txn_state;
txn = &session->txn;
txn_global = &S2C(session)->txn_global;
txn_state = WT_SESSION_TXN_STATE(session);
txn->isolation = WT_ISO_SNAPSHOT;
if (session->ncursors > 0)
WT_RET(__wt_session_copy_values(session));
__wt_readlock(session, txn_global->nsnap_rwlock);
TAILQ_FOREACH(nsnap, &txn_global->nsnaph, q)
if (WT_STRING_MATCH(nsnap->name, nameval->str, nameval->len)) {
txn->snap_min = txn_state->snap_min = nsnap->snap_min;
txn->snap_max = nsnap->snap_max;
if ((txn->snapshot_count = nsnap->snapshot_count) != 0)
memcpy(txn->snapshot, nsnap->snapshot,
nsnap->snapshot_count *
sizeof(*nsnap->snapshot));
F_SET(txn, WT_TXN_HAS_SNAPSHOT);
break;
}
__wt_readunlock(session, txn_global->nsnap_rwlock);
if (nsnap == NULL)
WT_RET_MSG(session, EINVAL,
"Named snapshot '%.*s' not found",
(int)nameval->len, nameval->str);
/* Flag that this transaction is opened on a named snapshot */
F_SET(txn, WT_TXN_NAMED_SNAPSHOT);
return (0);
}
/*
* __wt_txn_named_snapshot_config --
* Check the configuration for a named snapshot
*/
int
__wt_txn_named_snapshot_config(WT_SESSION_IMPL *session,
const char *cfg[], bool *has_create, bool *has_drops)
{
WT_CONFIG_ITEM cval;
WT_CONFIG_ITEM all_config, names_config, to_config, before_config;
WT_TXN *txn;
txn = &session->txn;
*has_create = *has_drops = false;
/* Verify that the name is legal. */
WT_RET(__wt_config_gets_def(session, cfg, "name", 0, &cval));
if (cval.len != 0) {
if (WT_STRING_MATCH("all", cval.str, cval.len))
WT_RET_MSG(session, EINVAL,
"Can't create snapshot with reserved \"all\" name");
WT_RET(__wt_name_check(session, cval.str, cval.len));
if (F_ISSET(txn, WT_TXN_RUNNING) &&
txn->isolation != WT_ISO_SNAPSHOT)
WT_RET_MSG(session, EINVAL,
"Can't create a named snapshot from a running "
"transaction that isn't snapshot isolation");
else if (F_ISSET(txn, WT_TXN_RUNNING) && txn->mod_count != 0)
WT_RET_MSG(session, EINVAL,
"Can't create a named snapshot from a running "
"transaction that has made updates");
*has_create = true;
}
/* Verify that the drop configuration is sane. */
WT_RET(__wt_config_gets_def(session, cfg, "drop.all", 0, &all_config));
WT_RET(__wt_config_gets_def(
session, cfg, "drop.names", 0, &names_config));
WT_RET(__wt_config_gets_def(session, cfg, "drop.to", 0, &to_config));
WT_RET(__wt_config_gets_def(
session, cfg, "drop.before", 0, &before_config));
/* Avoid more work if no drops are configured. */
if (all_config.val != 0 || names_config.len != 0 ||
before_config.len != 0 || to_config.len != 0) {
if (before_config.len != 0 && to_config.len != 0)
WT_RET_MSG(session, EINVAL,
"Illegal configuration; named snapshot drop can't "
"specify both before and to options");
if (all_config.val != 0 && (names_config.len != 0 ||
to_config.len != 0 || before_config.len != 0))
WT_RET_MSG(session, EINVAL,
"Illegal configuration; named snapshot drop can't "
"specify all and any other options");
*has_drops = true;
}
if (!*has_create && !*has_drops)
WT_RET_MSG(session, EINVAL,
"WT_SESSION::snapshot API called without any drop or "
"name option");
return (0);
}
/*
* __wt_txn_named_snapshot_destroy --
* Destroy all named snapshots on connection close
*/
int
__wt_txn_named_snapshot_destroy(WT_SESSION_IMPL *session)
{
WT_NAMED_SNAPSHOT *nsnap;
WT_TXN_GLOBAL *txn_global;
txn_global = &S2C(session)->txn_global;
txn_global->nsnap_oldest_id = WT_TXN_NONE;
while ((nsnap = TAILQ_FIRST(&txn_global->nsnaph)) != NULL) {
TAILQ_REMOVE(&txn_global->nsnaph, nsnap, q);
__nsnap_destroy(session, nsnap);
}
return (0);
}
|