forked from orioledb/orioledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplication_test.py
612 lines (515 loc) · 18.2 KB
/
replication_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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/bin/env python3
# coding: utf-8
import unittest
import testgres
import time
import os
import re
import subprocess
from .base_test import BaseTest
def catchup_orioledb(replica):
replica.catchup()
replica.poll_query_until("SELECT orioledb_recovery_synchronized();", expected = True)
class ReplicationTest(BaseTest):
def test_replication_simple(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.execute("CREATE EXTENSION orioledb;")
master.execute("CREATE TABLE o_test\n"
" (id integer NOT NULL)\n"
"USING orioledb;")
master.execute("INSERT INTO o_test VALUES (1);")
# wait for synchronization
catchup_orioledb(replica)
self.assertEqual(1, replica.execute("SELECT * FROM o_test;")[0][0])
replica.poll_query_until("SELECT orioledb_has_retained_undo();",
expected = False)
def test_replication_in_progress(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("""CREATE EXTENSION orioledb;
CREATE TABLE o_test (
id integer NOT NULL,
val text,
PRIMARY KEY (id)
) USING orioledb;""")
replica.catchup()
master.safe_psql("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
master.safe_psql("""DELETE FROM o_test WHERE id > 5000;""")
master.safe_psql("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(5001, 7500, 1) id);""")
replica.catchup()
# ensure that we do not see in-progress transaction on the replica
count = replica.execute("""SELECT COUNT(*) FROM o_test;""")[0][0]
self.assertTrue(count == 0 or count == 10000 or count == 5000 or count == 7500)
count = replica.execute("""SELECT count(*) FROM (SELECT * FROM o_test ORDER BY id DESC) id;""")[0][0]
self.assertTrue(count == 0 or count == 10000 or count == 5000 or count == 7500)
replica.poll_query_until("SELECT orioledb_has_retained_undo();",
expected = False)
def test_replication_drop(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("""CREATE EXTENSION orioledb;
CREATE TABLE o_test (
id integer NOT NULL,
val text
) USING orioledb;""")
master.safe_psql("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
master.execute("DROP TABLE o_test;")
# check that DROP executed on master
self.assertTrue(self.all_tables_dropped(master))
# wait for synchronization
catchup_orioledb(replica)
# check that DROP replicated
self.assertTrue(self.all_tables_dropped(replica))
self.assertFalse(replica.execute("SELECT orioledb_has_retained_undo();")[0][0])
def test_replication_create_drop_commit(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("CREATE EXTENSION orioledb;")
con = master.connect()
con.begin()
con.execute("""CREATE TABLE o_test (
id integer NOT NULL,
val text
) USING orioledb;""")
con.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
con.execute("DROP TABLE o_test;")
con.commit()
con.close()
# check that DROP executed on master
self.assertTrue(self.all_tables_dropped(master))
# wait for synchronization
catchup_orioledb(replica)
# check that DROP replicated
self.assertTrue(self.all_tables_dropped(replica))
self.assertFalse(replica.execute("SELECT orioledb_has_retained_undo();")[0][0])
def test_replication_create_rollback(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("CREATE EXTENSION orioledb;")
with master.connect() as con1:
con1.execute("""CREATE TABLE o_test (
id integer NOT NULL,
val text
) USING orioledb;""")
con1.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
con1.rollback()
# check that table dropped after rollback
self.assertTrue(self.all_tables_dropped(master))
# wait for synchronization
catchup_orioledb(replica)
# check that drop replicated
self.assertTrue(self.all_tables_dropped(replica))
self.assertFalse(replica.execute("SELECT orioledb_has_retained_undo();")[0][0])
def test_replication_create_truncate_commit(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("CREATE EXTENSION orioledb;")
with master.connect() as con:
con.begin()
con.execute("""CREATE TABLE o_test (
id integer NOT NULL,
val text
) USING orioledb;""")
con.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
con.execute("TRUNCATE o_test;")
con.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
con.commit()
self.assertEqual(1, self.get_tbl_count(master))
# wait for synchronization
catchup_orioledb(replica)
replica.safe_psql("SELECT * FROM o_test;")
self.assertEqual(1, self.get_tbl_count(replica))
self.assertTrue(self.has_only_one_relnode(replica))
self.assertFalse(replica.execute("SELECT orioledb_has_retained_undo();")[0][0])
def test_replication_drop_truncate_rollback(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("""CREATE EXTENSION orioledb;
CREATE TABLE o_test (
id integer NOT NULL,
val text
) USING orioledb;""")
# wait for synchronization
replica.catchup()
replica.safe_psql("SELECT * FROM o_test;")
with master.connect() as con1:
con1.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
con1.execute("DROP TABLE o_test;")
con1.rollback()
catchup_orioledb(replica)
replica.safe_psql("SELECT * FROM o_test;")
self.assertTrue(self.has_only_one_relnode(replica))
self.assertEqual(1, self.get_tbl_count(replica))
with master.connect() as con1:
con1.execute("TRUNCATE o_test;")
con1.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
con1.rollback()
# wait for synchronization
catchup_orioledb(replica)
replica.safe_psql("SELECT * FROM o_test;")
self.assertTrue(self.has_only_one_relnode(replica))
self.assertEqual(1, self.get_tbl_count(replica))
self.assertFalse(replica.execute("SELECT orioledb_has_retained_undo();")[0][0])
def test_replication_non_transactional_truncate(self):
node = self.node
node.start()
with self.node as master:
with self.getReplica().start() as replica:
with master.connect() as con1:
con1.begin()
con1.execute("""
CREATE EXTENSION IF NOT EXISTS orioledb;
CREATE TABLE o_test_1(
val_1 int
) USING orioledb;
INSERT INTO o_test_1 VALUES (1);
TRUNCATE TABLE o_test_1;
TRUNCATE TABLE o_test_1;
INSERT INTO o_test_1 VALUES (1);
""")
con1.commit()
catchup_orioledb(replica)
def test_replication_non_root_eviction(self):
with self.node as master:
self.node.append_conf('postgresql.conf',
"log_min_messages = notice\n"
"max_worker_processes = 64\n"
"orioledb.undo_buffers = 256MB\n"
"orioledb.recovery_pool_size = 50\n")
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql('postgres',
"CREATE EXTENSION IF NOT EXISTS orioledb;\n"
"CREATE TABLE IF NOT EXISTS o_test (\n"
" key integer NOT NULL,\n"
" val integer NOT NULL,\n"
" val2 text NOT NULL,\n"
" PRIMARY KEY (key)\n"
") USING orioledb;\n")
n = 500000
con = master.connect()
con.begin()
con.execute("INSERT INTO o_test (SELECT id, %s - id, repeat('x', 1000) FROM generate_series(%s, %s, 1) id);" %
(str(n), str(1), str(n)))
con.commit()
con.close()
# wait for synchronization
catchup_orioledb(replica)
replica.poll_query_until("SELECT orioledb_has_retained_undo();",
expected = False)
self.assertEqual(500000, replica.execute("SELECT count(*) FROM (SELECT * FROM o_test ORDER BY key) x;")[0][0])
def test_replication_root_eviction(self):
INDEX_NOT_LOADED = "Index o_evicted_pkey: not loaded"
with self.node as master:
master.append_conf('postgresql.conf',
"orioledb.main_buffers = 8MB\n"
"checkpoint_timeout = 86400\n"
"max_wal_size = 1GB\n"
"enable_seqscan = off\n"
"orioledb.recovery_pool_size = 1\n"
"orioledb.debug_disable_bgwriter = true\n")
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql('postgres', """
CREATE EXTENSION IF NOT EXISTS orioledb;
CREATE TABLE IF NOT EXISTS o_test (
key integer NOT NULL,
val integer NOT NULL,
val2 integer NOT NULL,
val3 integer NOT NULL,
val4 integer NOT NULL,
PRIMARY KEY (key)
) USING orioledb;
CREATE TABLE IF NOT EXISTS o_evicted (
key SERIAL NOT NULL,
val int NOT NULL,
PRIMARY KEY (key)
) USING orioledb;
INSERT INTO o_evicted (val)
SELECT val id FROM generate_series(1001, 1500, 1) val;
""")
self.assertEqual(500, master.execute("SELECT count(*) FROM o_evicted;")[0][0])
n = 400000
step = 1000
self.assertNotEqual(
master.execute("SELECT orioledb_tbl_structure('o_evicted'::regclass, 'e');")[0][0].split('\n')[0],
INDEX_NOT_LOADED)
with master.connect('postgres') as con:
for i in range(1, n, step):
con.execute("""
INSERT INTO o_test
(SELECT id, %s - id, %s - id,
%s - id, %s - id FROM
generate_series(%s, %s, 1) id);
""" % (str(n), str(i), str(i), str(i), str(i),
str(i + step - 1)))
con.commit()
self.assertEqual(n, master.execute(
"SELECT count(*) FROM o_test;")[0][0])
self.assertEqual(n, master.execute(
"SELECT count(*) FROM o_test;")[0][0])
self.assertEqual(
master.execute("SELECT orioledb_tbl_structure('o_evicted'::regclass, 'e');")[0][0].split('\n')[0],
INDEX_NOT_LOADED)
# wait for synchronization
catchup_orioledb(replica)
replica.poll_query_until(
"SELECT orioledb_has_retained_undo();",
expected = False)
self.assertEqual(500, replica.execute(
"SELECT count(*) FROM o_evicted;")[0][0])
self.assertNotEqual(
replica.execute("SELECT orioledb_tbl_structure('o_evicted'::regclass, 'e');")[0][0].split('\n')[0],
INDEX_NOT_LOADED)
self.assertEqual(n, replica.execute(
"SELECT count(*) FROM o_test;")[0][0])
self.assertEqual(n, replica.execute(
"SELECT count(*) FROM o_test;")[0][0])
self.assertEqual(n, replica.execute(
"SELECT count(*) FROM o_test;")[0][0])
self.assertEqual(n, replica.execute(
"SELECT count(*) FROM o_test;")[0][0])
self.assertEqual(
replica.execute("SELECT orioledb_tbl_structure('o_evicted'::regclass, 'e');")[0][0].split('\n')[0],
INDEX_NOT_LOADED)
self.assertEqual(500, replica.execute(
"SELECT count(*) FROM o_evicted;")[0][0])
self.assertNotEqual(
replica.execute("SELECT orioledb_tbl_structure('o_evicted'::regclass, 'e');")[0][0].split('\n')[0],
INDEX_NOT_LOADED)
def test_replica_checkpoint(self):
with self.node as master:
master.start()
# create a backup
with self.getReplica().start() as replica:
master.safe_psql("""CREATE EXTENSION orioledb;
CREATE TABLE o_test (
id integer NOT NULL,
val text,
PRIMARY KEY (id)
) USING orioledb;""")
replica.catchup()
con = master.connect()
con.begin()
con.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(1, 10000, 1) id);""")
replica.catchup()
replica.safe_psql('CHECKPOINT;')
replica.stop()
replica.start()
con.execute("""INSERT INTO o_test (
SELECT id, id || 'val'
FROM
generate_series(10001, 20000, 1) id);""")
con.commit()
con.close()
replica.catchup()
# ensure that we do not see in-progress transaction on the replica
replica.poll_query_until("SELECT orioledb_has_retained_undo();",
expected = False)
count = replica.execute("""SELECT COUNT(*) FROM o_test;""")[0][0]
self.assertEqual(count, 20000)
def test_remote_apply(self):
with self.node as master:
master.append_conf(filename='postgresql.conf',
synchronous_commit = 'remote_apply',
default_table_access_method = 'orioledb')
master.start()
# create a backup
with self.getReplica() as replica:
master.safe_psql("""CREATE EXTENSION orioledb;""")
pgbench = master.pgbench(options=["-i", "-s", "1"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
self.assertEqual(pgbench.wait(), 0)
replica.start()
master.stop()
master.append_conf(filename='postgresql.conf',
synchronous_standby_names = 'FIRST 1 (replica)')
master.start()
pgbench = master.pgbench(options=["--client", "16",
"--jobs", "4",
"--protocol", "prepared",
"--progress", "10",
"--time", "20"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
self.assertEqual(pgbench.wait(), 0)
pgbench = master.pgbench(options=["--client", "16",
"--jobs", "4",
"--protocol", "prepared",
"--progress", "10",
"--time", "20"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
self.assertEqual(pgbench.wait(), 0)
def test_replication_column_ddl(self):
with self.node as master:
master.start()
with self.getReplica().start() as replica:
master.execute("CREATE EXTENSION orioledb;")
master.execute("CREATE TABLE o_test\n"
" (id integer NOT NULL)\n"
"USING orioledb;")
master.execute("INSERT INTO o_test VALUES (1);")
master.execute("ALTER TABLE o_test ADD COLUMN val int;")
master.execute("ALTER TABLE o_test RENAME COLUMN val TO val_2;")
master.execute("ALTER TABLE o_test DROP COLUMN val_2")
# wait for synchronization
catchup_orioledb(replica)
self.assertEqual(1, replica.execute("SELECT * FROM o_test;")[0][0])
replica.poll_query_until("SELECT orioledb_has_retained_undo();",
expected = False)
def test_replication_create_table_add_column_same_trx(self):
node = self.node
node.start()
with self.node as master:
with self.getReplica().start() as replica:
master.safe_psql("""
CREATE EXTENSION orioledb;
""")
with master.connect() as con:
con.begin()
con.execute("""
CREATE TABLE o_test_1 (
a int
) USING orioledb;
ALTER TABLE o_test_1 ADD COLUMN b serial;
INSERT INTO o_test_1 VALUES (1), (2), (3);
""")
con.commit()
catchup_orioledb(replica)
self.assertEqual(master.execute("SELECT * FROM o_test_1"),
[(1, 1), (2, 2), (3, 3)])
self.assertEqual(replica.execute("SELECT * FROM o_test_1"),
[(1, 1), (2, 2), (3, 3)])
def test_replication_default_domain(self):
node = self.node
node.start()
with self.node as master:
with self.getReplica().start() as replica:
with master.connect() as con1:
con1.begin()
con1.execute("""
CREATE EXTENSION IF NOT EXISTS orioledb;
CREATE DOMAIN d1 int DEFAULT 1;
CREATE TABLE o_test_1 (
val_1 d1 DEFAULT 2
) USING orioledb;
""")
con1.commit()
self.catchup_orioledb(replica)
def test_replication_primary_column_after_dropped(self):
node = self.node
node.start()
with self.node as master:
with self.getReplica() as replica:
replica.append_conf('orioledb.recovery_pool_size = 1')
replica.start()
node.safe_psql("CREATE EXTENSION IF NOT EXISTS orioledb;")
with master.connect() as con1:
con1.begin()
con1.execute("""
CREATE TABLE o_test_1 (
val_1 int,
val_2 int,
PRIMARY KEY (val_2)
) USING orioledb;
""")
con1.execute("ALTER TABLE o_test_1 DROP COLUMN val_1;")
con1.execute("INSERT INTO o_test_1 VALUES (1), (3);")
con1.commit()
self.assertEqual([(1,),(3,)],
master.execute("""
SELECT * FROM o_test_1 ORDER BY val_2;
"""))
catchup_orioledb(replica)
self.assertEqual([(1,),(3,)],
replica.execute("""
SELECT * FROM o_test_1 ORDER BY val_2;
"""))
def test_replication_workers_synchronize_shutdown_concurrent(self):
node = self.node
node.start()
with self.node as master:
with self.getReplica().start() as replica:
with master.connect() as con1:
con1.begin()
con1.execute("""
CREATE EXTENSION IF NOT EXISTS orioledb;
CREATE TEMP TABLE o_test_1 (
val_1 text COLLATE "C"
) USING orioledb;
CREATE INDEX ON o_test_1(val_1);
INSERT INTO o_test_1
VALUES ('a'), ('b'), ('c'), ('d');
""")
con1.commit()
self.catchup_orioledb(replica)
def has_only_one_relnode(self, node):
orioledb_files = self.get_orioledb_files(node)
oid_list = [re.match(r'(\d+_\d+).*', x).group(1) for x
in orioledb_files]
if len(list(set(oid_list))) != 1:
print(oid_list)
return len(list(set(oid_list))) == 1
def get_tbl_count(self, node):
return node.execute('postgres',
'SELECT count(*) FROM orioledb_table_oids();')[0][0]
def get_orioledb_files(self, node):
orioledb_dir = node.data_dir + "/orioledb_data"
all_files = []
for f in os.listdir(orioledb_dir):
m = re.match(r'(\d+)_(\d+).*', f)
if m and int(m.group(1)) > 1:
# do not check o_tables BTree files
all_files.append(f)
return all_files
def all_tables_dropped(self, node):
return len(self.get_orioledb_files(node)) == 0