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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*-
* Copyright (c) 2014-2015 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* WT_META_TRACK -- A tracked metadata operation: a non-transactional log,
* maintained to make it easy to unroll simple metadata and filesystem
* operations.
*/
typedef struct __wt_meta_track {
enum {
WT_ST_EMPTY, /* Unused slot */
WT_ST_CHECKPOINT, /* Complete a checkpoint */
WT_ST_DROP_COMMIT, /* Drop post commit */
WT_ST_FILEOP, /* File operation */
WT_ST_LOCK, /* Lock a handle */
WT_ST_REMOVE, /* Remove a metadata entry */
WT_ST_SET /* Reset a metadata entry */
} op;
char *a, *b; /* Strings */
WT_DATA_HANDLE *dhandle; /* Locked handle */
bool created; /* Handle on newly created file */
} WT_META_TRACK;
/*
* __meta_track_next --
* Extend the list of operations we're tracking, as necessary, and
* optionally return the next slot.
*/
static int
__meta_track_next(WT_SESSION_IMPL *session, WT_META_TRACK **trkp)
{
size_t offset, sub_off;
if (session->meta_track_next == NULL)
session->meta_track_next = session->meta_track;
offset = WT_PTRDIFF(session->meta_track_next, session->meta_track);
sub_off = WT_PTRDIFF(session->meta_track_sub, session->meta_track);
if (offset == session->meta_track_alloc) {
WT_RET(__wt_realloc(session, &session->meta_track_alloc,
WT_MAX(2 * session->meta_track_alloc,
20 * sizeof(WT_META_TRACK)), &session->meta_track));
/* Maintain positions in the new chunk of memory. */
session->meta_track_next =
(uint8_t *)session->meta_track + offset;
if (session->meta_track_sub != NULL)
session->meta_track_sub =
(uint8_t *)session->meta_track + sub_off;
}
WT_ASSERT(session, session->meta_track_next != NULL);
if (trkp != NULL) {
*trkp = session->meta_track_next;
session->meta_track_next = *trkp + 1;
}
return (0);
}
/*
* __meta_track_clear --
* Clear the structure.
*/
static void
__meta_track_clear(WT_SESSION_IMPL *session, WT_META_TRACK *trk)
{
__wt_free(session, trk->a);
__wt_free(session, trk->b);
memset(trk, 0, sizeof(WT_META_TRACK));
}
/*
* __wt_meta_track_discard --
* Cleanup metadata tracking when closing a session.
*/
void
__wt_meta_track_discard(WT_SESSION_IMPL *session)
{
__wt_free(session, session->meta_track);
session->meta_track_next = NULL;
session->meta_track_alloc = 0;
}
/*
* __wt_meta_track_on --
* Turn on metadata operation tracking.
*/
int
__wt_meta_track_on(WT_SESSION_IMPL *session)
{
if (session->meta_track_nest++ == 0)
WT_RET(__meta_track_next(session, NULL));
return (0);
}
/*
* __meta_track_apply --
* Apply the changes in a metadata tracking record.
*/
static int
__meta_track_apply(WT_SESSION_IMPL *session, WT_META_TRACK *trk)
{
WT_BM *bm;
WT_BTREE *btree;
WT_DECL_RET;
int tret;
switch (trk->op) {
case WT_ST_EMPTY: /* Unused slot */
break;
case WT_ST_CHECKPOINT: /* Checkpoint, see above */
btree = trk->dhandle->handle;
bm = btree->bm;
WT_WITH_DHANDLE(session, trk->dhandle,
WT_TRET(bm->checkpoint_resolve(bm, session)));
break;
case WT_ST_DROP_COMMIT:
if ((tret = __wt_remove_if_exists(session, trk->a)) != 0) {
__wt_err(session, tret,
"metadata remove dropped file %s",
trk->a);
WT_TRET(tret);
}
break;
case WT_ST_LOCK:
WT_WITH_DHANDLE(session, trk->dhandle,
WT_TRET(__wt_session_release_btree(session)));
break;
case WT_ST_FILEOP:
case WT_ST_REMOVE:
case WT_ST_SET:
break;
WT_ILLEGAL_VALUE(session);
}
__meta_track_clear(session, trk);
return (ret);
}
/*
* __meta_track_unroll --
* Undo the changes in a metadata tracking record.
*/
static int
__meta_track_unroll(WT_SESSION_IMPL *session, WT_META_TRACK *trk)
{
WT_DECL_RET;
int tret;
switch (trk->op) {
case WT_ST_EMPTY: /* Unused slot */
break;
case WT_ST_CHECKPOINT: /* Checkpoint, see above */
break;
case WT_ST_DROP_COMMIT:
break;
case WT_ST_LOCK: /* Handle lock, see above */
if (trk->created)
F_SET(trk->dhandle, WT_DHANDLE_DISCARD);
WT_WITH_DHANDLE(session, trk->dhandle,
WT_TRET(__wt_session_release_btree(session)));
break;
case WT_ST_FILEOP: /* File operation */
/*
* For renames, both a and b are set.
* For creates, a is NULL.
* For removes, b is NULL.
*/
if (trk->a != NULL && trk->b != NULL &&
(tret = __wt_rename(session,
trk->b + strlen("file:"),
trk->a + strlen("file:"))) != 0) {
__wt_err(session, tret,
"metadata unroll rename %s to %s",
trk->b, trk->a);
WT_TRET(tret);
} else if (trk->a == NULL) {
if ((tret = __wt_remove(session,
trk->b + strlen("file:"))) != 0) {
__wt_err(session, tret,
"metadata unroll create %s",
trk->b);
WT_TRET(tret);
}
}
/*
* We can't undo removes yet: that would imply
* some kind of temporary rename and remove in
* roll forward.
*/
break;
case WT_ST_REMOVE: /* Remove trk.a */
if ((tret = __wt_metadata_remove(session, trk->a)) != 0) {
__wt_err(session, tret,
"metadata unroll remove: %s",
trk->a);
WT_TRET(tret);
}
break;
case WT_ST_SET: /* Set trk.a to trk.b */
if ((tret = __wt_metadata_update(
session, trk->a, trk->b)) != 0) {
__wt_err(session, tret,
"metadata unroll update %s to %s",
trk->a, trk->b);
WT_TRET(tret);
}
break;
WT_ILLEGAL_VALUE(session);
}
__meta_track_clear(session, trk);
return (ret);
}
/*
* __wt_meta_track_find_handle --
* Check if we have already seen a handle.
*/
int
__wt_meta_track_find_handle(
WT_SESSION_IMPL *session, const char *name, const char *checkpoint)
{
WT_META_TRACK *trk, *trk_orig;
WT_ASSERT(session,
WT_META_TRACKING(session) && session->meta_track_nest > 0);
trk_orig = session->meta_track;
trk = session->meta_track_next;
while (--trk >= trk_orig) {
if (trk->op != WT_ST_LOCK)
continue;
if (strcmp(trk->dhandle->name, name) == 0 &&
((trk->dhandle->checkpoint == NULL && checkpoint == NULL) ||
(trk->dhandle->checkpoint != NULL &&
strcmp(trk->dhandle->checkpoint, checkpoint) == 0)))
return (0);
}
return (WT_NOTFOUND);
}
/*
* __wt_meta_track_off --
* Turn off metadata operation tracking, unrolling on error.
*/
int
__wt_meta_track_off(WT_SESSION_IMPL *session, bool need_sync, bool unroll)
{
WT_DECL_RET;
WT_META_TRACK *trk, *trk_orig;
WT_ASSERT(session,
WT_META_TRACKING(session) && session->meta_track_nest > 0);
trk_orig = session->meta_track;
trk = session->meta_track_next;
/* If it was a nested transaction, there is nothing to do. */
if (--session->meta_track_nest != 0)
return (0);
/* Turn off tracking for unroll. */
session->meta_track_next = session->meta_track_sub = NULL;
/*
* If there were no operations logged, return now and avoid unnecessary
* metadata checkpoints. For example, this happens if attempting to
* create a data source that already exists (or drop one that doesn't).
*/
if (trk == trk_orig)
return (0);
if (unroll) {
while (--trk >= trk_orig)
WT_TRET(__meta_track_unroll(session, trk));
/* Unroll operations don't need to flush the metadata. */
return (ret);
}
/*
* If we don't have the metadata handle (e.g, we're in the process of
* creating the metadata), we can't sync it.
*/
if (!need_sync || session->meta_dhandle == NULL)
goto done;
/* If we're logging, make sure the metadata update was flushed. */
if (FLD_ISSET(S2C(session)->log_flags, WT_CONN_LOG_ENABLED)) {
WT_WITH_DHANDLE(session, session->meta_dhandle,
ret = __wt_txn_checkpoint_log(
session, false, WT_TXN_LOG_CKPT_SYNC, NULL));
WT_RET(ret);
} else {
WT_WITH_DHANDLE(session, session->meta_dhandle,
ret = __wt_checkpoint(session, NULL));
WT_RET(ret);
WT_WITH_DHANDLE(session, session->meta_dhandle,
ret = __wt_checkpoint_sync(session, NULL));
WT_RET(ret);
}
done: /* Apply any tracked operations post-commit. */
for (; trk_orig < trk; trk_orig++)
WT_TRET(__meta_track_apply(session, trk_orig));
return (ret);
}
/*
* __wt_meta_track_sub_on --
* Start a group of operations that can be committed independent of the
* main transaction.
*/
int
__wt_meta_track_sub_on(WT_SESSION_IMPL *session)
{
WT_ASSERT(session, session->meta_track_sub == NULL);
session->meta_track_sub = session->meta_track_next;
return (0);
}
/*
* __wt_meta_track_sub_off --
* Commit a group of operations independent of the main transaction.
*/
int
__wt_meta_track_sub_off(WT_SESSION_IMPL *session)
{
WT_DECL_RET;
WT_META_TRACK *trk, *trk_orig;
if (!WT_META_TRACKING(session) || session->meta_track_sub == NULL)
return (0);
trk_orig = session->meta_track_sub;
trk = session->meta_track_next;
/* Turn off tracking for unroll. */
session->meta_track_next = session->meta_track_sub = NULL;
while (--trk >= trk_orig)
WT_TRET(__meta_track_apply(session, trk));
session->meta_track_next = trk_orig;
return (ret);
}
/*
* __wt_meta_track_checkpoint --
* Track a handle involved in a checkpoint.
*/
int
__wt_meta_track_checkpoint(WT_SESSION_IMPL *session)
{
WT_META_TRACK *trk;
WT_ASSERT(session, session->dhandle != NULL);
WT_RET(__meta_track_next(session, &trk));
trk->op = WT_ST_CHECKPOINT;
trk->dhandle = session->dhandle;
return (0);
}
/*
* __wt_meta_track_insert --
* Track an insert operation.
*/
int
__wt_meta_track_insert(WT_SESSION_IMPL *session, const char *key)
{
WT_META_TRACK *trk;
WT_RET(__meta_track_next(session, &trk));
trk->op = WT_ST_REMOVE;
WT_RET(__wt_strdup(session, key, &trk->a));
return (0);
}
/*
* __wt_meta_track_update --
* Track a metadata update operation.
*/
int
__wt_meta_track_update(WT_SESSION_IMPL *session, const char *key)
{
WT_DECL_RET;
WT_META_TRACK *trk;
WT_RET(__meta_track_next(session, &trk));
trk->op = WT_ST_SET;
WT_RET(__wt_strdup(session, key, &trk->a));
/*
* If there was a previous value, keep it around -- if not, then this
* "update" is really an insert.
*/
if ((ret =
__wt_metadata_search(session, key, &trk->b)) == WT_NOTFOUND) {
trk->op = WT_ST_REMOVE;
ret = 0;
}
return (ret);
}
/*
* __wt_meta_track_fileop --
* Track a filesystem operation.
*/
int
__wt_meta_track_fileop(
WT_SESSION_IMPL *session, const char *olduri, const char *newuri)
{
WT_META_TRACK *trk;
WT_RET(__meta_track_next(session, &trk));
trk->op = WT_ST_FILEOP;
if (olduri != NULL)
WT_RET(__wt_strdup(session, olduri, &trk->a));
if (newuri != NULL)
WT_RET(__wt_strdup(session, newuri, &trk->b));
return (0);
}
/*
* __wt_meta_track_drop --
* Track a file drop, where the remove is deferred until commit.
*/
int
__wt_meta_track_drop(
WT_SESSION_IMPL *session, const char *filename)
{
WT_META_TRACK *trk;
WT_RET(__meta_track_next(session, &trk));
trk->op = WT_ST_DROP_COMMIT;
WT_RET(__wt_strdup(session, filename, &trk->a));
return (0);
}
/*
* __wt_meta_track_handle_lock --
* Track a locked handle.
*/
int
__wt_meta_track_handle_lock(WT_SESSION_IMPL *session, bool created)
{
WT_META_TRACK *trk;
WT_ASSERT(session, session->dhandle != NULL);
WT_RET(__meta_track_next(session, &trk));
trk->op = WT_ST_LOCK;
trk->dhandle = session->dhandle;
trk->created = created;
return (0);
}
|