summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/support/timestamp.c
blob: 8acdbcb56b26a39af3d0b35428075b9429df77ce (plain)
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
/*-
 * Copyright (c) 2014-present MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __wt_timestamp_to_string --
 *     Convert a timestamp to the MongoDB string representation.
 */
char *
__wt_timestamp_to_string(wt_timestamp_t ts, char *ts_string)
{
    WT_IGNORE_RET(__wt_snprintf(ts_string, WT_TS_INT_STRING_SIZE, "(%" PRIu32 ", %" PRIu32 ")",
      (uint32_t)((ts >> 32) & 0xffffffff), (uint32_t)(ts & 0xffffffff)));
    return (ts_string);
}

/*
 * __wt_time_point_to_string --
 *     Converts a time point to a standard string representation.
 */
char *
__wt_time_point_to_string(
  wt_timestamp_t ts, wt_timestamp_t durable_ts, uint64_t txn_id, char *tp_string)
{
    char ts_string[WT_TS_INT_STRING_SIZE];

    WT_IGNORE_RET(__wt_snprintf(tp_string, WT_TP_STRING_SIZE, "%s/%s/%" PRIu64,
      __wt_timestamp_to_string(ts, ts_string), __wt_timestamp_to_string(durable_ts, ts_string),
      txn_id));
    return (tp_string);
}

/*
 * __wt_time_window_to_string --
 *     Converts a time window to a standard string representation.
 */
char *
__wt_time_window_to_string(WT_TIME_WINDOW *tw, char *tw_string)
{
    char ts_string[4][WT_TS_INT_STRING_SIZE];

    WT_IGNORE_RET(__wt_snprintf(tw_string, WT_TIME_STRING_SIZE,
      "start: %s/%s/%" PRIu64 " stop: %s/%s/%" PRIu64 "%s",
      __wt_timestamp_to_string(tw->durable_start_ts, ts_string[0]),
      __wt_timestamp_to_string(tw->start_ts, ts_string[1]), tw->start_txn,
      __wt_timestamp_to_string(tw->durable_stop_ts, ts_string[2]),
      __wt_timestamp_to_string(tw->stop_ts, ts_string[3]), tw->stop_txn,
      tw->prepare ? ", prepared" : ""));
    return (tw_string);
}

/*
 * __wt_time_aggregate_to_string --
 *     Converts a time aggregate to a standard string representation.
 */
char *
__wt_time_aggregate_to_string(WT_TIME_AGGREGATE *ta, char *ta_string)
{
    char ts_string[4][WT_TS_INT_STRING_SIZE];

    WT_IGNORE_RET(__wt_snprintf(ta_string, WT_TIME_STRING_SIZE,
      "newest durable: %s/%s oldest start: %s/%" PRIu64 " newest stop %s/%" PRIu64 "%s",
      __wt_timestamp_to_string(ta->newest_start_durable_ts, ts_string[0]),
      __wt_timestamp_to_string(ta->newest_stop_durable_ts, ts_string[1]),
      __wt_timestamp_to_string(ta->oldest_start_ts, ts_string[2]), ta->newest_txn,
      __wt_timestamp_to_string(ta->newest_stop_ts, ts_string[3]), ta->newest_stop_txn,
      ta->prepare ? ", prepared" : ""));
    return (ta_string);
}

/*
 * __wt_timestamp_to_hex_string --
 *     Convert a timestamp to hex string representation.
 */
void
__wt_timestamp_to_hex_string(wt_timestamp_t ts, char *hex_timestamp)
{
    char *p, v;

    if (ts == 0) {
        hex_timestamp[0] = '0';
        hex_timestamp[1] = '\0';
        return;
    }
    if (ts == WT_TS_MAX) {
#define WT_TS_MAX_HEX_STRING "ffffffffffffffff"
        (void)memcpy(hex_timestamp, WT_TS_MAX_HEX_STRING, strlen(WT_TS_MAX_HEX_STRING) + 1);
        return;
    }

    for (p = hex_timestamp; ts != 0; ts >>= 4)
        *p++ = (char)__wt_hex((u_char)(ts & 0x0f));
    *p = '\0';

    /* Reverse the string. */
    for (--p; p > hex_timestamp;) {
        v = *p;
        *p-- = *hex_timestamp;
        *hex_timestamp++ = v;
    }
}

/*
 * __wt_verbose_timestamp --
 *     Output a verbose message along with the specified timestamp.
 */
void
__wt_verbose_timestamp(WT_SESSION_IMPL *session, wt_timestamp_t ts, const char *msg)
{
    char ts_string[WT_TS_INT_STRING_SIZE];

    __wt_verbose(
      session, WT_VERB_TIMESTAMP, "Timestamp %s: %s", __wt_timestamp_to_string(ts, ts_string), msg);
}

#define WT_TIME_VALIDATE_RET(session, ...)        \
    do {                                          \
        if (silent)                               \
            return (EINVAL);                      \
        WT_RET_MSG(session, EINVAL, __VA_ARGS__); \
    } while (0)

/*
 * __time_stable --
 *     Return the stable timestamp for the system.
 */
static wt_timestamp_t
__time_stable(WT_SESSION_IMPL *session)
{
    WT_TXN_GLOBAL *txn_global;

    txn_global = &S2C(session)->txn_global;

    return (txn_global->has_stable_timestamp ? txn_global->stable_timestamp :
                                               txn_global->recovery_timestamp);
}

#undef WT_TIME_ERROR
#define WT_TIME_ERROR(tag)                                             \
    WT_TIME_VALIDATE_RET(session,                                      \
      "aggregate time window has " tag                                 \
      " the stable point with an empty parent aggregate time window; " \
      "stable time %s, time window %s",                                \
      __wt_timestamp_to_string(stable, ts_string), __wt_time_aggregate_to_string(ta, time_string))

/*
 * __time_aggregate_validate_parent_stable --
 *     Aggregated time window validation against a stable point.
 */
static int
__time_aggregate_validate_parent_stable(
  WT_SESSION_IMPL *session, WT_TIME_AGGREGATE *ta, bool silent)
{
    wt_timestamp_t stable;
    char time_string[WT_TIME_STRING_SIZE], ts_string[WT_TS_INT_STRING_SIZE];

    stable = __time_stable(session);

    if (ta->newest_start_durable_ts > stable)
        WT_TIME_ERROR("a newest start durable time after");
    if (ta->newest_stop_durable_ts > stable)
        WT_TIME_ERROR("a newest stop durable time after");
    if (ta->oldest_start_ts > stable)
        WT_TIME_ERROR("an oldest start time after");
    if (ta->newest_stop_ts != WT_TS_MAX && ta->newest_stop_ts > stable)
        WT_TIME_ERROR("a newest stop time after");

    return (0);
}

/*
 * __time_aggregate_validate_parent --
 *     Aggregated time window validation against a parent.
 */
static int
__time_aggregate_validate_parent(
  WT_SESSION_IMPL *session, WT_TIME_AGGREGATE *ta, WT_TIME_AGGREGATE *parent, bool silent)
{
    char time_string[2][WT_TIME_STRING_SIZE];

    if (ta->newest_start_durable_ts > parent->newest_start_durable_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest start durable time after its parent's; time "
          "aggregate %s, parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (ta->newest_stop_durable_ts > parent->newest_stop_durable_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest stop durable time after its parent's; time aggregate "
          "%s, parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (ta->oldest_start_ts < parent->oldest_start_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has an oldest start time before its parent's; time aggregate %s, "
          "parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (ta->newest_txn > parent->newest_txn)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest transaction after its parent's; time "
          "aggregate %s, parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (ta->newest_stop_ts > parent->newest_stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest stop time after its parent's; time aggregate %s, "
          "parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (ta->newest_stop_txn > parent->newest_stop_txn)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest stop transaction after its parent's; time aggregate "
          "%s, parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (ta->prepare && !parent->prepare)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window is prepared but its parent is not; time aggregate %s, parent %s",
          __wt_time_aggregate_to_string(ta, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    return (0);
}

/*
 * __wt_time_aggregate_validate --
 *     Aggregated time window validation.
 */
int
__wt_time_aggregate_validate(
  WT_SESSION_IMPL *session, WT_TIME_AGGREGATE *ta, WT_TIME_AGGREGATE *parent, bool silent)
{
    char time_string[2][WT_TIME_STRING_SIZE];

    if (ta->oldest_start_ts > ta->newest_stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has an oldest start time after its newest stop time; time "
          "aggregate %s",
          __wt_time_aggregate_to_string(ta, time_string[0]));

    if (ta->newest_txn > ta->newest_stop_txn)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest transaction after its newest stop "
          "transaction; time aggregate %s",
          __wt_time_aggregate_to_string(ta, time_string[0]));

    if (ta->oldest_start_ts > ta->newest_start_durable_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has an oldest start time after its newest start durable time; "
          "time aggregate %s",
          __wt_time_aggregate_to_string(ta, time_string[0]));

    if (ta->newest_stop_ts != WT_TS_MAX && ta->newest_stop_ts > ta->newest_stop_durable_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest stop time after its newest stop durable time; time "
          "aggregate %s",
          __wt_time_aggregate_to_string(ta, time_string[0]));

    /*
     * In the case of out of order timestamps, we assign the start point to the stop point and
     * newest start durable timestamp may be larger than newest stop timestamp. Check whether start
     * and stop are equal first.
     */
    if (ta->newest_start_durable_ts != ta->newest_stop_durable_ts &&
      ta->newest_start_durable_ts > ta->newest_stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest stop durable time after its newest stop time; time "
          "aggregate %s",
          __wt_time_aggregate_to_string(ta, time_string[0]));

    if (ta->newest_stop_durable_ts != WT_TS_NONE &&
      ta->newest_stop_durable_ts < ta->oldest_start_ts)
        WT_TIME_VALIDATE_RET(session,
          "aggregate time window has a newest stop durable time before its oldest start time; time "
          "aggregate %s",
          __wt_time_aggregate_to_string(ta, time_string[0]));

    /*
     * Optionally validate the time window against a parent's time window.
     *
     * If no aggregated time window is set, it can be for one of two reasons: there really isn't any
     * time window information, or there was a downgrade/upgrade to a previous release which doesn't
     * write time window information. We can't tell the difference, but in either case, everything
     * should be stable.
     */
    if (parent == NULL || WT_IS_METADATA(session->dhandle))
        return (0);
    return (WT_TIME_AGGREGATE_IS_EMPTY(parent) ?
        __time_aggregate_validate_parent_stable(session, ta, silent) :
        __time_aggregate_validate_parent(session, ta, parent, silent));
}

#undef WT_TIME_ERROR
#define WT_TIME_ERROR(tag)                                             \
    WT_TIME_VALIDATE_RET(session,                                      \
      "time window has " tag                                           \
      " the stable point with an empty parent aggregate time window; " \
      "stable time %s, time window %s",                                \
      __wt_timestamp_to_string(stable, ts_string), __wt_time_window_to_string(tw, time_string))

/*
 * __time_value_validate_parent_stable --
 *     Value time window validation against a stable point.
 */
static int
__time_value_validate_parent_stable(WT_SESSION_IMPL *session, WT_TIME_WINDOW *tw, bool silent)
{
    wt_timestamp_t stable;
    char time_string[WT_TIME_STRING_SIZE], ts_string[WT_TS_INT_STRING_SIZE];

    stable = __time_stable(session);

    if (tw->durable_start_ts > stable)
        WT_TIME_ERROR("a durable start time after");
    if (tw->start_ts > stable)
        WT_TIME_ERROR("a start time after");
    if (tw->durable_stop_ts > stable)
        WT_TIME_ERROR("a durable stop time after");
    if (tw->stop_ts != WT_TS_MAX && tw->stop_ts > stable)
        WT_TIME_ERROR("a stop time after");

    return (0);
}

/*
 * __time_value_validate_parent --
 *     Value time window validation against a parent.
 */
static int
__time_value_validate_parent(
  WT_SESSION_IMPL *session, WT_TIME_WINDOW *tw, WT_TIME_AGGREGATE *parent, bool silent)
{
    char time_string[2][WT_TIME_STRING_SIZE];

    if (parent->newest_start_durable_ts != WT_TS_NONE &&
      tw->durable_start_ts > parent->newest_start_durable_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a durable start time after its parent's newest durable start "
          "time; time window %s, parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (tw->start_ts < parent->oldest_start_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a start time before its parent's oldest start time; time window "
          "%s, parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (tw->start_txn > parent->newest_txn)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a start transaction after its parent's newest transaction; "
          "time window %s, parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (parent->newest_stop_durable_ts != WT_TS_NONE &&
      tw->durable_stop_ts > parent->newest_stop_durable_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a durable stop time after its parent's newest durable stop time; "
          "time window %s, parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (tw->stop_ts > parent->newest_stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a stop time after its parent's newest stop time; time window %s, "
          "parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (tw->stop_txn > parent->newest_stop_txn)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a stop transaction after its parent's newest stop transaction; "
          "time window %s, parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    if (tw->prepare && !parent->prepare)
        WT_TIME_VALIDATE_RET(session,
          "value time window is prepared but its parent is not; time window %s, parent %s",
          __wt_time_window_to_string(tw, time_string[0]),
          __wt_time_aggregate_to_string(parent, time_string[1]));

    return (0);
}

/*
 * __wt_time_value_validate --
 *     Value time window validation.
 */
int
__wt_time_value_validate(
  WT_SESSION_IMPL *session, WT_TIME_WINDOW *tw, WT_TIME_AGGREGATE *parent, bool silent)
{
    char time_string[2][WT_TIME_STRING_SIZE];

    if (tw->start_ts > tw->stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a start time after its stop time; time window %s",
          __wt_time_window_to_string(tw, time_string[0]));

    if (tw->start_txn > tw->stop_txn)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a start transaction after its stop transaction; time window %s",
          __wt_time_window_to_string(tw, time_string[0]));

    if (tw->start_ts > tw->durable_start_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a start time after its durable start time; time window %s",
          __wt_time_window_to_string(tw, time_string[0]));

    if (tw->stop_ts != WT_TS_MAX && tw->stop_ts > tw->durable_stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a stop time after its durable stop time; time window %s",
          __wt_time_window_to_string(tw, time_string[0]));

    /*
     * In the case of out of order timestamps, we assign start time point to the stop point and
     * durable start timestamp may be larger than stop timestamp. Check whether start and stop are
     * equal first.
     */
    if (tw->durable_start_ts != tw->durable_stop_ts && tw->durable_start_ts > tw->stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a durable start time after its stop time; time window %s",
          __wt_time_window_to_string(tw, time_string[0]));

    if (tw->durable_stop_ts != WT_TS_NONE && tw->durable_start_ts > tw->durable_stop_ts)
        WT_TIME_VALIDATE_RET(session,
          "value time window has a durable start time after its durable stop time; time window %s",
          __wt_time_window_to_string(tw, time_string[0]));

    /*
     * Optionally validate the time window against a parent's time window.
     *
     * If no aggregated time window is set, it can be for one of two reasons: there really isn't any
     * time window information, or there was a downgrade/upgrade to a previous release which doesn't
     * write time window information. We can't tell the difference, but in either case, everything
     * should be stable.
     */
    if (parent == NULL || WT_IS_METADATA(session->dhandle))
        return (0);
    return (WT_TIME_AGGREGATE_IS_EMPTY(parent) ?
        __time_value_validate_parent_stable(session, tw, silent) :
        __time_value_validate_parent(session, tw, parent, silent));
}