forked from orioledb/orioledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincomplete_split_test.py
409 lines (302 loc) · 11.9 KB
/
incomplete_split_test.py
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
#!/usr/bin/env python3
# coding: utf-8
import unittest
import testgres
from .base_test import BaseTest
from .base_test import ThreadQueryExecutor
from testgres.enums import NodeStatus
from testgres.connection import NodeConnection
from .base_test import wait_stopevent
import string
import random
class SplitTest(BaseTest):
def setUp(self):
super().setUp()
self.node.append_conf('postgresql.conf',
"orioledb.main_buffers = 8MB\n"
"log_min_messages = notice\n")
self.node.start() # start PostgreSQL
self.node.safe_psql('postgres',
"""
CREATE EXTENSION IF NOT EXISTS orioledb;
CREATE TABLE IF NOT EXISTS o_split (
id text NOT NULL,
PRIMARY KEY (id)
) USING orioledb;
TRUNCATE o_split;
""")
self.connections = []
def tearDown(self):
# stops node if tests fails
if self.node.status() == NodeStatus.Running:
self.stopAll() # just comment it if node should not stops on fails
pass
super().tearDown()
def test_incomplete_leaf_insert_fix(self):
node = self.node
self.insertToSplitTable(node, 10, 50, 4)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 11)
self.failedInsertToSplitTable(con1, 51, 90, 4)
# insert fails
# BTree has incomplete split pages
self.checkSplitTable(11, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
self.insertToSplitTable(con2, 51, 90, 4)
# second insert is successfully
# BTree has not incomplete split pages
self.checkSplitTable(21, True)
self.stopAll()
def test_incomplete_leaf_insert_checkpoint_fix_middle(self):
node = self.node
self.insertToSplitTable(node, 1, 100, 1)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 100)
self.failedInsertToSplitTable(con1, 510, 515, 1)
# insert fails
# BTree has incomplete split pages
self.checkSplitTable(100, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con2.execute("CHECKPOINT;")
# checkpoint fixes split and has autonomous page writes
# btree_check() will fail
self.checkSplitTable(100, False)
con2.execute("CHECKPOINT;")
# After second checkpoint there is no incomplete split pages and lost blocks in BTree
self.checkSplitTable(100, True)
self.stopAll()
def test_incomplete_leaf_insert_checkpoint_fix_end(self):
node = self.node
self.insertToSplitTable(node, 1, 58, 1)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 58)
self.failedInsertToSplitTable(con1, 91, 99, 1)
# insert fails
# BTree has incomplete split pages
self.checkSplitTable(58, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con2.execute("CHECKPOINT;")
# checkpoint fixes split and has autonomous page writes
# btree_check() will fail
self.checkSplitTable(58, False)
con2.execute("CHECKPOINT;")
# After second checkpoint there is no incomplete split pages and lost blocks in BTree
self.checkSplitTable(58, True)
self.stopAll()
def test_incomplete_leaf_checkpoint_fix(self):
node = self.node
self.insertToSplitTable(node, 10, 50, 4)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 11)
self.failedInsertToSplitTable(con1, 51, 90, 4)
# insert fails
# BTree has incomplete split pages
self.checkSplitTable(11, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con2.begin()
con2.execute("CHECKPOINT;")
con2.commit()
# checkpoint fixes split and has autonomous page writes
# btree_check() will fail
self.checkSplitTable(11, False)
con2.execute("CHECKPOINT;")
# After second checkpoint there is no incomplete split pages and lost blocks
self.checkSplitTable(11, True)
self.stopAll()
node.start()
self.checkSplitTable(11, True)
def test_incomplete_node_insert_fix(self):
node = self.node
self.insertToSplitTable(node, 1, 400, 4)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', '$.level == 1');")
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 100)
self.failedInsertToSplitTable(con1, 1650, 1685, 1)
# insert fails
# BTree has incomplete split pages
self.checkSplitTable(100, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
self.insertToSplitTable(con2, 1645, 1654, 1)
# second insert is successfully
# BTree has not incomplete split pages
self.checkSplitTable(110, True)
def test_incomplete_node_checkpoint_fix(self):
node = self.node
self.insertToSplitTable(node, 1, 400, 4)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', '$.level == 1');")
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 100)
self.failedInsertToSplitTable(con1, 1650, 1685, 1)
# insert fails
# BTree has incomplete split pages
self.checkSplitTable(100, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con2.begin()
con2.execute("CHECKPOINT;")
con2.commit()
con2.execute("CHECKPOINT;")
# After second checkpoint there is no incomplete split pages and lost blocks in BTree
self.checkSplitTable(100, True)
self.stopAll()
node.start()
self.checkSplitTable(100, True)
def test_incomplete_root_fix(self):
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.failedInsertToSplitTable(con1, 1, 20, 1)
# BTree has no incomplete split pages (fixes by call of refind_page in undo_callback)
self.checkSplitTable(0, True)
self.insertToSplitTable(con2, 1, 20, 1)
self.checkSplitTable(20, True)
def test_incomplete_split_sequence(self):
node = self.node
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
self.insertToSplitTable(con1, 1, 400, 4)
self.assertEqual(node.execute("SELECT COUNT(*) FROM o_split;")[0][0], 100)
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', '$.level == 1');")
self.failedInsertToSplitTable(con1, 1650, 1685, 1)
# insert fails, now incomplete split is at a node
# BTree has incomplete split pages
self.checkSplitTable(100, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.failedInsertToSplitTable(con1, 1550, 1585, 1)
# another insert fails now incomplete split is at a leaf and the node
# BTree has incomplete split pages
self.checkSplitTable(100, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
self.insertToSplitTable(con2, 1480, 1480, 1)
# fixes all splits
self.checkSplitTable(101, True)
def test_incomplete_split_sequence2(self):
node = self.node
con1 = self.createConnection()
con1_pid = con1.pid
con1.execute("SET orioledb.enable_stopevents = true;")
self.insertToSplitTable(con1, 1, 1000, 4);
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
# incomplete split on leaf
self.failedInsertToSplitTable(con1, 10100, 10130, 1)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con1.execute("SET orioledb.enable_stopevents = true;")
con2.execute("SELECT pg_stopevent_set('page_read', '$.level == 0 && $pid == %d');" % (con1_pid,))
# this thread fix incomplete split on leaf and than root
# breakpoint helps us to avoid root split fix first (???)
t1 = ThreadQueryExecutor(con1, "INSERT INTO o_split "
"(SELECT repeat('x', 708) || id FROM generate_series(10100, 10130, 1) id);")
t1.start()
wait_stopevent(node, con1_pid)
con3 = self.createConnection()
con3_pid = con3.pid
# makes an incomplete split of root
con3.execute("SET orioledb.enable_stopevents = true;")
con2.execute("SELECT pg_stopevent_set('split_fail', '$.level == 2 && $pid == %d');" % (con3_pid,))
# this breakpoint helps us to avoid root split fix on apply_undo()
con2.execute("SELECT pg_stopevent_set('before_apply_undo', '$.commit == false');")
t3 = ThreadQueryExecutor(con3, "INSERT INTO o_split "
"(SELECT repeat('x', 708) || id FROM generate_series(90000, 99999, 4) id);")
t3.start()
wait_stopevent(node, con3_pid)
con2.execute("SELECT pg_stopevent_reset('page_read');")
t1.join()
con2.execute("SELECT pg_stopevent_reset('before_apply_undo');")
con2.execute("SELECT pg_stopevent_reset('split_fail');")
try:
t3.join()
self.assertTrue(False)
except:
self.assertTrue(True)
def test_rightlink_traverse(self):
node = self.node
self.insertToSplitTable(node, 5, 15, 1)
con1 = self.createConnection()
con1.execute("SET orioledb.enable_stopevents = true;")
con2 = self.createConnection()
con2.execute("SELECT pg_stopevent_set('split_fail', 'true');")
self.failedInsertToSplitTable(con1, 100, 105, 1)
self.checkSplitTable(11, False)
con2.execute("SELECT pg_stopevent_reset('split_fail');")
con1.rollback()
con1.execute("SET orioledb.enable_stopevents = true;")
con2.execute("SELECT pg_stopevent_set('page_read', '$.level == 0 && $pid == %d');" % (con1.pid,))
t1 = ThreadQueryExecutor(con1,
"SELECT COUNT(*) FROM o_split "
"WHERE id > repeat('x', 708) || 14;")
t1.start()
self.insertToSplitTable(node, 100, 105, 1)
node.safe_psql('postgres',
"CREATE TABLE IF NOT EXISTS o_eviction (\n"
" key integer NOT NULL,\n"
" val integer NOT NULL,\n"
" PRIMARY KEY (key)\n"
") USING orioledb;\n"
"CREATE UNIQUE INDEX o_eviction_ix1 ON o_eviction (val);")
n = 100000
con3 = self.createConnection()
con3.execute(
"INSERT INTO o_eviction\n"
" (SELECT id, %s - id FROM generate_series(%s, %s, 1) id);\n" %
(str(n), str(1), str(n)))
con3.commit()
con2.execute("SELECT pg_stopevent_reset('page_read');")
result = t1.join()
self.assertEqual(result[0][0], 6)
self.stopAll()
def createConnection(self):
connection = self.node.connect()
self.connections.append(connection)
return connection
def stopAll(self):
for con in self.connections:
con.close()
self.connections = []
self.node.stop()
def checkSplitTable(self, expected_count, expected_check_result):
con = self.node.connect()
self.assertEqual(con.execute("SELECT COUNT(*) FROM o_split;")[0][0],
expected_count)
self.assertEqual(con.execute("SELECT orioledb_tbl_check('o_split'::regclass)")[0][0],
expected_check_result)
con.close()
# insert data to the split table
def insertToSplitTable(self, node_or_con, insert_from, insert_to, step):
if isinstance(node_or_con, NodeConnection):
node_or_con.begin()
try:
node_or_con.execute("INSERT INTO o_split"
" (SELECT repeat('x', 708) || id\n"
"FROM generate_series(%d, %d, %d) id);"
% (insert_from, insert_to, step))
finally:
if isinstance(node_or_con, NodeConnection):
node_or_con.commit()
# this insert must fails with exception
def failedInsertToSplitTable(self, con, insert_from, insert_to, step):
try:
self.insertToSplitTable(con, insert_from, insert_to, step)
self.assertTrue(False)
except AssertionError:
raise
except Exception:
self.assertTrue(True);