summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/test_prepare20.py
blob: 0b541d4073073b25b9ef759b5e544f7fea5eeacf (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
#!/usr/bin/env python
#
# Public Domain 2014-present MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.


# test_prepare19.py
# Check that we can use an application-level log to replay unstable transactions.

import wiredtiger, wttest
from wtscenario import make_scenarios
from helper import simulate_crash_restart

class test_prepare19(wttest.WiredTigerTestCase):
    # Do write the logs immediately, but don't waste time fsyncing them.
    conn_config = 'log=(enabled),transaction_sync=(enabled=true,method=none)'

    format_values = [
        ('integer-row', dict(key_format='i', usestrings=True, value_format='S')),
        ('column', dict(key_format='r', usestrings=False, value_format='S')),
        ('column-fix', dict(key_format='r', usestrings=False, value_format='8t')),
    ]
    ckpt_values = [
        ('none', dict(first_ckpt=None, second_ckpt=None)),
        ('1-21', dict(first_ckpt=21, second_ckpt=None)),
        ('1-23', dict(first_ckpt=23, second_ckpt=None)),
        ('1-26', dict(first_ckpt=26, second_ckpt=None)),
        ('2-21', dict(first_ckpt=None, second_ckpt=21)),
        ('2-23', dict(first_ckpt=None, second_ckpt=23)),
        ('2-26', dict(first_ckpt=None, second_ckpt=26)),
        ('2-31', dict(first_ckpt=None, second_ckpt=31)),
        ('2-33', dict(first_ckpt=None, second_ckpt=33)),
        ('2-36', dict(first_ckpt=None, second_ckpt=36)),
    ]
    commit_values = [
        ('commit', dict(commit_before_crash=True)),
        ('nocommit', dict(commit_before_crash=False)),
    ]
    scenarios = make_scenarios(format_values, ckpt_values, commit_values)

    # Application-level log infrastructure. This is real simpleminded and not
    # expected to do very much other than make sure the basic premise works.

    nullvalue = None

    # Some operation codes for the application log.
    BEGIN=1
    WRITE=2
    PREPARETIME=3
    PREPARE=4
    COMMITTIME=5
    DURABLETIME=6
    COMMIT=7

    lsession = None
    lcursor = None
    lsn = 1
    txnid = 1

    log_replays = 0

    def log_open(self, log_uri):
        self.lsession = self.conn.open_session()
        self.lcursor = self.lsession.open_cursor(log_uri)

    def log_begin(self):
        self.session.begin_transaction()
        self.lsession.begin_transaction()
        self.lcursor[self.lsn] = (self.BEGIN, self.txnid, self.nullvalue, self.nullvalue)
        self.lsn += 1

    def log_write(self, dcursor, k, newv):
        oldv = dcursor[k]
        self.lcursor[self.lsn] = (self.WRITE, k, oldv, newv)
        self.lsn += 1
        dcursor[k] = newv

    def log_prepare(self, prepare_ts):
        self.lcursor[self.lsn] = (self.PREPARETIME, prepare_ts, self.nullvalue, self.nullvalue)
        self.lsn += 1
        self.lsession.commit_transaction()
        self.session.prepare_transaction('prepare_timestamp=' + self.timestamp_str(prepare_ts))
        self.lsession.begin_transaction()
        self.lcursor[self.lsn] = (self.PREPARE, self.txnid, self.nullvalue, self.nullvalue)
        self.lsn += 1
        self.lsession.commit_transaction()

    def log_precommit(self, commit_ts, durable_ts):
        self.lsession.begin_transaction()
        self.lcursor[self.lsn] = (self.COMMITTIME, commit_ts, self.nullvalue, self.nullvalue)
        self.lsn += 1
        self.lcursor[self.lsn] = (self.DURABLETIME, durable_ts, self.nullvalue, self.nullvalue)
        self.lsn += 1
        self.lsession.commit_transaction()

    def log_commit(self, commit_ts, durable_ts):
        self.session.commit_transaction('commit_timestamp=' + self.timestamp_str(commit_ts) +
            ',durable_timestamp=' + self.timestamp_str(durable_ts))
        self.lsession.begin_transaction()
        self.lcursor[self.lsn] = (self.COMMIT, self.txnid, self.nullvalue, self.nullvalue)
        self.lsn += 1
        self.lsession.commit_transaction()
        self.txnid += 1

    def log_replay(self, dcursor):
        stabletime = int('0x' + self.conn.query_timestamp('get=stable_timestamp'), 16)
        #self.prout("stable {}".format(stabletime))
        # First pass: find prepared txns. Ignore (thus abort) any that didn't even prepare.
        txns = {}
        self.lcursor.reset()
        for lsn, op, k, oldv, newv in self.lcursor:
            if op == self.BEGIN:
                # "key" is the txnid
                txns[k] = False
            elif op == self.PREPARE or op == self.COMMIT:
                # "key" is the txnid
                txns[k] = True
        # Second pass: replay values from non-aborted txns.
        self.lcursor.reset()
        writing = False
        committime = None
        durabletime = None
        for lsn, op, k, oldv, newv in self.lcursor:
            if op == self.BEGIN:
                # "key" is the txnid
                if txns[k]:
                    self.session.begin_transaction('roundup_timestamps=(prepared=true)')
                    writing = True
                    #self.prout("begin {}".format(k))
                else:
                    writing = False
            elif op == self.WRITE:
                if writing and dcursor[k] == oldv:
                    dcursor[k] = newv
                    self.log_replays += 1
                    #self.prout("write {} {}".format(k, str(newv)[0:2]))
            elif op == self.PREPARETIME:
                if writing:
                    preparetime = k
                    #self.prout("preparetime {}".format(k))
            elif op == self.PREPARE:
                if writing:
                    self.session.prepare_transaction(
                        'prepare_timestamp=' + self.timestamp_str(preparetime))
                    #self.prout("prepare {}".format(k))
            elif op == self.COMMITTIME:
                if writing:
                    committime = k
                    #self.prout("committime {}".format(k))
            elif op == self.DURABLETIME:
                if writing:
                    durabletime = k
                    if durabletime <= stabletime:
                        durabletime = stabletime + 1
                    #self.prout("durabletime {} -> {}".format(k, durabletime))
            elif op == self.COMMIT:
                if writing:
                    self.session.commit_transaction(
                        'commit_timestamp=' + self.timestamp_str(committime) +
                        ',durable_timestamp=' + self.timestamp_str(durabletime))
                    #self.prout("commit {}".format(k))
                writing = False
                committime = None
                durabletime = None
            else:
                self.assertTrue(False)
        # If we have a prepared and uncommitted transaction, commit it.
        # (Note that we don't handle the case where we write values and don't finish preparing.)
        if committime is not None and durabletime is not None:
            self.assertTrue(writing)
            self.session.commit_transaction(
                'commit_timestamp=' + self.timestamp_str(committime) +
                ',durable_timestamp=' + self.timestamp_str(durabletime))
            #self.prout("final commit")

    # Now the test.

    def test_prepare19(self):
        data_uri = 'file:prepare19data'
        log_uri = 'file:prepare19log'

        # Create one table for data and another to be an application-level log.
        # The log's format is application-lsn -> operation, key, oldvalue, newvalue

        self.session.create(data_uri, 'log=(enabled=false),key_format={},value_format={}'.format(
            self.key_format, self.value_format))

        self.session.create(log_uri, 'key_format=r,value_format=ii{}{}'.format(
            self.value_format, self.value_format))

        nrows = 1000
        if self.value_format == '8t':
            value_a = 97
            value_b = 98
            value_c = 99
            self.nullvalue = 255
        else:
            value_a = 'aaaaa' * 100
            value_b = 'bbbbb' * 100
            value_c = 'ccccc' * 100
            self.nullvalue = ''

        dcursor = self.session.open_cursor(data_uri)

        # Write some baseline data first and make it stable.
        self.session.begin_transaction()
        for i in range(1, nrows + 1):
            dcursor[i] = value_a
        self.session.commit_transaction('commit_timestamp=' + self.timestamp_str(10))
        self.conn.set_timestamp('stable_timestamp=' + self.timestamp_str(10))
        self.conn.set_timestamp('oldest_timestamp=' + self.timestamp_str(10))
        self.session.checkpoint()

        self.log_open(log_uri)

        # Write and log a transaction.
        self.log_begin()
        for i in range(1, nrows + 1):
            self.log_write(dcursor, i, value_b)
        self.log_prepare(20)
        self.log_precommit(22, 25)
        self.log_commit(22, 25)

        # Optionally checkpoint here.
        if self.first_ckpt is not None:
            self.conn.set_timestamp('stable_timestamp=' + self.timestamp_str(self.first_ckpt))
            self.session.checkpoint()

        # Write and log another transaction.
        self.log_begin()
        for i in range(1, nrows + 1):
            self.log_write(dcursor, i, value_c)
        self.log_prepare(30)

        # Optionally commit now.
        self.log_precommit(32, 35)
        if self.commit_before_crash:
            self.log_commit(32, 35)

        # Optionally checkpoint here.
        if self.second_ckpt is not None:
            self.conn.set_timestamp('stable_timestamp=' + self.timestamp_str(self.second_ckpt))
            # Use the log session to checkpoint: checkpointing from the main session
            # will fail if we just prepared without committing.
            self.lsession.checkpoint()

        # Now crash.
        simulate_crash_restart(self, ".", "RESTART")
        dcursor = self.session.open_cursor(data_uri)
        self.log_open(log_uri)

        # Replay the application log.
        self.log_replay(dcursor)

        # Make everything stable.
        self.conn.set_timestamp('stable_timestamp=' + self.timestamp_str(100))
        self.session.checkpoint()

        # We should now see all the data.
        self.session.begin_transaction('read_timestamp=' + self.timestamp_str(15))
        for i in range(1, nrows + 1):
            self.assertEqual(dcursor[i], value_a)
        self.session.rollback_transaction()
        self.session.begin_transaction('read_timestamp=' + self.timestamp_str(25))
        for i in range(1, nrows + 1):
            self.assertEqual(dcursor[i], value_b)
        self.session.rollback_transaction()
        self.session.begin_transaction('read_timestamp=' + self.timestamp_str(35))
        for i in range(1, nrows + 1):
            self.assertEqual(dcursor[i], value_c)
        self.session.rollback_transaction()

        # Assert we replayed the right number of writes from the application log.
        ckpt = self.first_ckpt if self.second_ckpt is None else self.second_ckpt
        if ckpt is None or ckpt < 25:
            self.assertEqual(self.log_replays, nrows * 2)
        elif ckpt < 35 or not self.commit_before_crash:
            self.assertEqual(self.log_replays, nrows)
        else:
            self.assertEqual(self.log_replays, 0)