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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
|
# See the file LICENSE for redistribution information.
#
# Copyright (c) 2001-2009 Oracle. All rights reserved.
#
# $Id$
#
# Replication testing utilities
# Environment handle for the env containing the replication "communications
# structure" (really a CDB environment).
# The test environment consists of a queue and a # directory (environment)
# per replication site. The queue is used to hold messages destined for a
# particular site and the directory will contain the environment for the
# site. So the environment looks like:
# $testdir
# ___________|______________________________
# / | \ \
# MSGQUEUEDIR MASTERDIR CLIENTDIR.0 ... CLIENTDIR.N-1
# | | ... |
# 1 2 .. N+1
#
# The master is site 1 in the MSGQUEUEDIR and clients 1-N map to message
# queues 2 - N+1.
#
# The globals repenv(1-N) contain the environment handles for the sites
# with a given id (i.e., repenv(1) is the master's environment.
# queuedbs is an array of DB handles, one per machine ID/machine ID pair,
# for the databases that contain messages from one machine to another.
# We omit the cases where the "from" and "to" machines are the same.
# Since tcl does not have real two-dimensional arrays, we use this
# naming convention: queuedbs(1.2) has the handle for the database
# containing messages to machid 1 from machid 2.
#
global queuedbs
global machids
global perm_response_list
set perm_response_list {}
global perm_sent_list
set perm_sent_list {}
global elect_timeout
unset -nocomplain elect_timeout
set elect_timeout(default) 5000000
global electable_pri
set electable_pri 5
set drop 0
global anywhere
set anywhere 0
global rep_verbose
set rep_verbose 0
global verbose_type
set verbose_type "rep"
# To run a replication test with verbose messages, type
# 'run_verbose' and then the usual test command string enclosed
# in double quotes or curly braces. For example:
#
# run_verbose "rep001 btree"
#
# run_verbose {run_repmethod btree test001}
#
# To run a replication test with one of the subsets of verbose
# messages, use the same syntax with 'run_verbose_elect',
# 'run_verbose_lease', etc.
proc run_verbose { commandstring } {
global verbose_type
set verbose_type "rep"
run_verb $commandstring
}
proc run_verbose_elect { commandstring } {
global verbose_type
set verbose_type "rep_elect"
run_verb $commandstring
}
proc run_verbose_lease { commandstring } {
global verbose_type
set verbose_type "rep_lease"
run_verb $commandstring
}
proc run_verbose_misc { commandstring } {
global verbose_type
set verbose_type "rep_misc"
run_verb $commandstring
}
proc run_verbose_msgs { commandstring } {
global verbose_type
set verbose_type "rep_msgs"
run_verb $commandstring
}
proc run_verbose_sync { commandstring } {
global verbose_type
set verbose_type "rep_sync"
run_verb $commandstring
}
proc run_verbose_test { commandstring } {
global verbose_type
set verbose_type "rep_test"
run_verb $commandstring
}
proc run_verbose_repmgr_misc { commandstring } {
global verbose_type
set verbose_type "repmgr_misc"
run_verb $commandstring
}
proc run_verb { commandstring } {
global rep_verbose
global verbose_type
set rep_verbose 1
if { [catch {
eval $commandstring
flush stdout
flush stderr
} res] != 0 } {
global errorInfo
set rep_verbose 0
set fnl [string first "\n" $errorInfo]
set theError [string range $errorInfo 0 [expr $fnl - 1]]
if {[string first FAIL $errorInfo] == -1} {
error "FAIL:[timestamp]\
run_verbose: $commandstring: $theError"
} else {
error $theError;
}
}
set rep_verbose 0
}
# Databases are on-disk by default for replication testing.
# Some replication tests have been converted to run with databases
# in memory instead.
global databases_in_memory
set databases_in_memory 0
proc run_inmem_db { test method } {
run_inmem $test $method 1 0 0 0
}
# Replication files are on-disk by default for replication testing.
# Some replication tests have been converted to run with rep files
# in memory instead.
global repfiles_in_memory
set repfiles_in_memory 0
proc run_inmem_rep { test method } {
run_inmem $test $method 0 0 1 0
}
# Region files are on-disk by default for replication testing.
# Replication tests can force the region files in-memory by setting
# the -private flag when opening an env.
global env_private
set env_private 0
proc run_env_private { test method } {
run_inmem $test $method 0 0 0 1
}
# Logs are on-disk by default for replication testing.
# Mixed-mode log testing provides a mixture of on-disk and
# in-memory logging, or even all in-memory. When testing on a
# 1-master/1-client test, we try all four options. On a test
# with more clients, we still try four options, randomly
# selecting whether the later clients are on-disk or in-memory.
#
global mixed_mode_logging
set mixed_mode_logging 0
proc create_logsets { nsites } {
global mixed_mode_logging
global logsets
global rand_init
error_check_good set_random_seed [berkdb srand $rand_init] 0
if { $mixed_mode_logging == 0 || $mixed_mode_logging == 2 } {
if { $mixed_mode_logging == 0 } {
set logmode "on-disk"
} else {
set logmode "in-memory"
}
set loglist {}
for { set i 0 } { $i < $nsites } { incr i } {
lappend loglist $logmode
}
set logsets [list $loglist]
}
if { $mixed_mode_logging == 1 } {
set set1 {on-disk on-disk}
set set2 {on-disk in-memory}
set set3 {in-memory on-disk}
set set4 {in-memory in-memory}
# Start with nsites at 2 since we already set up
# the master and first client.
for { set i 2 } { $i < $nsites } { incr i } {
foreach set { set1 set2 set3 set4 } {
if { [berkdb random_int 0 1] == 0 } {
lappend $set "on-disk"
} else {
lappend $set "in-memory"
}
}
}
set logsets [list $set1 $set2 $set3 $set4]
}
return $logsets
}
proc run_inmem_log { test method } {
run_inmem $test $method 0 1 0 0
}
# Run_mixedmode_log is a little different from the other run_inmem procs:
# it provides a mixture of in-memory and on-disk logging on the different
# hosts in a replication group.
proc run_mixedmode_log { test method {display 0} {run 1} \
{outfile stdout} {largs ""} } {
global mixed_mode_logging
set mixed_mode_logging 1
set prefix [string range $test 0 2]
if { $prefix != "rep" } {
puts "Skipping mixed-mode log testing for non-rep test."
set mixed_mode_logging 0
return
}
eval run_method $method $test $display $run $outfile $largs
# Reset to default values after run.
set mixed_mode_logging 0
}
# The procs run_inmem_db, run_inmem_log, run_inmem_rep, and run_env_private
# put databases, logs, rep files, or region files in-memory. (Setting up
# an env with the -private flag puts region files in memory.)
# The proc run_inmem allows you to put any or all of these in-memory
# at the same time.
proc run_inmem { test method\
{dbinmem 1} {logsinmem 1} {repinmem 1} {envprivate 1} } {
set prefix [string range $test 0 2]
if { $prefix != "rep" } {
puts "Skipping in-memory testing for non-rep test."
return
}
global databases_in_memory
global mixed_mode_logging
global repfiles_in_memory
global env_private
global test_names
if { $dbinmem } {
if { [is_substr $test_names(rep_inmem) $test] == 0 } {
puts "Test $test does not support in-memory databases."
puts "Putting databases on-disk."
set databases_in_memory 0
} else {
set databases_in_memory 1
}
}
if { $logsinmem } {
set mixed_mode_logging 2
}
if { $repinmem } {
set repfiles_in_memory 1
}
if { $envprivate } {
set env_private 1
}
if { [catch {eval run_method $method $test} res] } {
set databases_in_memory 0
set mixed_mode_logging 0
set repfiles_in_memory 0
set env_private 0
puts "FAIL: $res"
}
set databases_in_memory 0
set mixed_mode_logging 0
set repfiles_in_memory 0
set env_private 0
}
# The proc run_diskless runs run_inmem with its default values.
# It's useful to have this name to remind us of its testing purpose,
# which is to mimic a diskless host.
proc run_diskless { test method } {
run_inmem $test $method 1 1 1 1
}
# Open the master and client environments; store these in the global repenv
# Return the master's environment: "-env masterenv"
proc repl_envsetup { envargs largs test {nclients 1} {droppct 0} { oob 0 } } {
source ./include.tcl
global clientdir
global drop drop_msg
global masterdir
global repenv
global rep_verbose
global verbose_type
set verbargs ""
if { $rep_verbose == 1 } {
set verbargs " -verbose {$verbose_type on}"
}
env_cleanup $testdir
replsetup $testdir/MSGQUEUEDIR
set masterdir $testdir/MASTERDIR
file mkdir $masterdir
if { $droppct != 0 } {
set drop 1
set drop_msg [expr 100 / $droppct]
} else {
set drop 0
}
for { set i 0 } { $i < $nclients } { incr i } {
set clientdir($i) $testdir/CLIENTDIR.$i
file mkdir $clientdir($i)
}
# Open a master.
repladd 1
#
# Set log smaller than default to force changing files,
# but big enough so that the tests that use binary files
# as keys/data can run. Increase the size of the log region --
# sdb004 needs this, now that subdatabase names are stored
# in the env region.
#
set logmax [expr 3 * 1024 * 1024]
set lockmax 40000
set logregion 2097152
set ma_cmd "berkdb_env_noerr -create -log_max $logmax $envargs \
-cachesize { 0 4194304 1 } -log_regionmax $logregion \
-lock_max_objects $lockmax -lock_max_locks $lockmax \
-errpfx $masterdir $verbargs \
-home $masterdir -txn nosync -rep_master -rep_transport \
\[list 1 replsend\]"
set masterenv [eval $ma_cmd]
error_check_good master_env [is_valid_env $masterenv] TRUE
set repenv(master) $masterenv
# Open clients
for { set i 0 } { $i < $nclients } { incr i } {
set envid [expr $i + 2]
repladd $envid
set cl_cmd "berkdb_env_noerr -create $envargs -txn nosync \
-cachesize { 0 10000000 0 } -log_regionmax $logregion \
-lock_max_objects $lockmax -lock_max_locks $lockmax \
-errpfx $clientdir($i) $verbargs \
-home $clientdir($i) -rep_client -rep_transport \
\[list $envid replsend\]"
set clientenv [eval $cl_cmd]
error_check_good client_env [is_valid_env $clientenv] TRUE
set repenv($i) $clientenv
}
set repenv($i) NULL
append largs " -env $masterenv "
# Process startup messages
repl_envprocq $test $nclients $oob
# Clobber replication's 30-second anti-archive timer, which
# will have been started by client sync-up internal init, in
# case the test we're about to run wants to do any log
# archiving, or database renaming and/or removal.
$masterenv test force noarchive_timeout
return $largs
}
# Process all incoming messages. Iterate until there are no messages left
# in anyone's queue so that we capture all message exchanges. We verify that
# the requested number of clients matches the number of client environments
# we have. The oob parameter indicates if we should process the queue
# with out-of-order delivery. The replprocess procedure actually does
# the real work of processing the queue -- this routine simply iterates
# over the various queues and does the initial setup.
proc repl_envprocq { test { nclients 1 } { oob 0 }} {
global repenv
global drop
set masterenv $repenv(master)
for { set i 0 } { 1 } { incr i } {
if { $repenv($i) == "NULL"} {
break
}
}
error_check_good i_nclients $nclients $i
berkdb debug_check
puts -nonewline "\t$test: Processing master/$i client queues"
set rand_skip 0
if { $oob } {
puts " out-of-order"
} else {
puts " in order"
}
set droprestore $drop
while { 1 } {
set nproced 0
if { $oob } {
set rand_skip [berkdb random_int 2 10]
}
incr nproced [replprocessqueue $masterenv 1 $rand_skip]
for { set i 0 } { $i < $nclients } { incr i } {
set envid [expr $i + 2]
if { $oob } {
set rand_skip [berkdb random_int 2 10]
}
set n [replprocessqueue $repenv($i) \
$envid $rand_skip]
incr nproced $n
}
if { $nproced == 0 } {
# Now that we delay requesting records until
# we've had a few records go by, we should always
# see that the number of requests is lower than the
# number of messages that were enqueued.
for { set i 0 } { $i < $nclients } { incr i } {
set clientenv $repenv($i)
set queued [stat_field $clientenv rep_stat \
"Total log records queued"]
error_check_bad queued_stats \
$queued -1
set requested [stat_field $clientenv rep_stat \
"Log records requested"]
error_check_bad requested_stats \
$requested -1
#
# Set to 100 usecs. An average ping
# to localhost should be a few 10s usecs.
#
$clientenv rep_request 100 400
}
# If we were dropping messages, we might need
# to flush the log so that we get everything
# and end up in the right state.
if { $drop != 0 } {
set drop 0
$masterenv rep_flush
berkdb debug_check
puts "\t$test: Flushing Master"
} else {
break
}
}
}
# Reset the clients back to the default state in case we
# have more processing to do.
for { set i 0 } { $i < $nclients } { incr i } {
set clientenv $repenv($i)
$clientenv rep_request 40000 1280000
}
set drop $droprestore
}
# Verify that the directories in the master are exactly replicated in
# each of the client environments.
proc repl_envver0 { test method { nclients 1 } } {
global clientdir
global masterdir
global repenv
# Verify the database in the client dir.
# First dump the master.
set t1 $masterdir/t1
set t2 $masterdir/t2
set t3 $masterdir/t3
set omethod [convert_method $method]
#
# We are interested in the keys of whatever databases are present
# in the master environment, so we just call a no-op check function
# since we have no idea what the contents of this database really is.
# We just need to walk the master and the clients and make sure they
# have the same contents.
#
set cwd [pwd]
cd $masterdir
set stat [catch {glob test*.db} dbs]
cd $cwd
if { $stat == 1 } {
return
}
foreach testfile $dbs {
open_and_dump_file $testfile $repenv(master) $masterdir/t2 \
repl_noop dump_file_direction "-first" "-next"
if { [string compare [convert_method $method] -recno] != 0 } {
filesort $t2 $t3
file rename -force $t3 $t2
}
for { set i 0 } { $i < $nclients } { incr i } {
puts "\t$test: Verifying client $i database $testfile contents."
open_and_dump_file $testfile $repenv($i) \
$t1 repl_noop dump_file_direction "-first" "-next"
if { [string compare $omethod "-recno"] != 0 } {
filesort $t1 $t3
} else {
catch {file copy -force $t1 $t3} ret
}
error_check_good diff_files($t2,$t3) [filecmp $t2 $t3] 0
}
}
}
# Remove all the elements from the master and verify that these
# deletions properly propagated to the clients.
proc repl_verdel { test method { nclients 1 } } {
global clientdir
global masterdir
global repenv
# Delete all items in the master.
set cwd [pwd]
cd $masterdir
set stat [catch {glob test*.db} dbs]
cd $cwd
if { $stat == 1 } {
return
}
foreach testfile $dbs {
puts "\t$test: Deleting all items from the master."
set txn [$repenv(master) txn]
error_check_good txn_begin [is_valid_txn $txn \
$repenv(master)] TRUE
set db [eval berkdb_open -txn $txn -env $repenv(master) \
$testfile]
error_check_good reopen_master [is_valid_db $db] TRUE
set dbc [$db cursor -txn $txn]
error_check_good reopen_master_cursor \
[is_valid_cursor $dbc $db] TRUE
for { set dbt [$dbc get -first] } { [llength $dbt] > 0 } \
{ set dbt [$dbc get -next] } {
error_check_good del_item [$dbc del] 0
}
error_check_good dbc_close [$dbc close] 0
error_check_good txn_commit [$txn commit] 0
error_check_good db_close [$db close] 0
repl_envprocq $test $nclients
# Check clients.
for { set i 0 } { $i < $nclients } { incr i } {
puts "\t$test: Verifying client database $i is empty."
set db [eval berkdb_open -env $repenv($i) $testfile]
error_check_good reopen_client($i) \
[is_valid_db $db] TRUE
set dbc [$db cursor]
error_check_good reopen_client_cursor($i) \
[is_valid_cursor $dbc $db] TRUE
error_check_good client($i)_empty \
[llength [$dbc get -first]] 0
error_check_good dbc_close [$dbc close] 0
error_check_good db_close [$db close] 0
}
}
}
# Replication "check" function for the dump procs that expect to
# be able to verify the keys and data.
proc repl_noop { k d } {
return
}
# Close all the master and client environments in a replication test directory.
proc repl_envclose { test envargs } {
source ./include.tcl
global clientdir
global encrypt
global masterdir
global repenv
global drop
if { [lsearch $envargs "-encrypta*"] !=-1 } {
set encrypt 1
}
# In order to make sure that we have fully-synced and ready-to-verify
# databases on all the clients, do a checkpoint on the master and
# process messages in order to flush all the clients.
set drop 0
berkdb debug_check
puts "\t$test: Checkpointing master."
error_check_good masterenv_ckp [$repenv(master) txn_checkpoint] 0
# Count clients.
for { set ncli 0 } { 1 } { incr ncli } {
if { $repenv($ncli) == "NULL" } {
break
}
$repenv($ncli) rep_request 100 100
}
repl_envprocq $test $ncli
error_check_good masterenv_close [$repenv(master) close] 0
verify_dir $masterdir "\t$test: " 0 0 1
for { set i 0 } { $i < $ncli } { incr i } {
error_check_good client($i)_close [$repenv($i) close] 0
verify_dir $clientdir($i) "\t$test: " 0 0 1
}
replclose $testdir/MSGQUEUEDIR
}
# Replnoop is a dummy function to substitute for replsend
# when replication is off.
proc replnoop { control rec fromid toid flags lsn } {
return 0
}
proc replclose { queuedir } {
global queueenv queuedbs machids
foreach m $machids {
set db $queuedbs($m)
error_check_good dbr_close [$db close] 0
}
error_check_good qenv_close [$queueenv close] 0
set machids {}
}
# Create a replication group for testing.
proc replsetup { queuedir } {
global queueenv queuedbs machids
file mkdir $queuedir
set max_locks 20000
set queueenv [berkdb_env \
-create -txn nosync -lock_max_locks $max_locks -home $queuedir]
error_check_good queueenv [is_valid_env $queueenv] TRUE
if { [info exists queuedbs] } {
unset queuedbs
}
set machids {}
return $queueenv
}
# Send function for replication.
proc replsend { control rec fromid toid flags lsn } {
global queuedbs queueenv machids
global drop drop_msg
global perm_sent_list
global anywhere
set permflags [lsearch $flags "perm"]
if { [llength $perm_sent_list] != 0 && $permflags != -1 } {
# puts "replsend sent perm message, LSN $lsn"
lappend perm_sent_list $lsn
}
#
# If we are testing with dropped messages, then we drop every
# $drop_msg time. If we do that just return 0 and don't do
# anything.
#
if { $drop != 0 } {
incr drop
if { $drop == $drop_msg } {
set drop 1
return 0
}
}
# XXX
# -1 is DB_BROADCAST_EID
if { $toid == -1 } {
set machlist $machids
} else {
if { [info exists queuedbs($toid)] != 1 } {
error "replsend: machid $toid not found"
}
set m NULL
if { $anywhere != 0 } {
#
# If we can send this anywhere, send it to the first
# id we find that is neither toid or fromid.
#
set anyflags [lsearch $flags "any"]
if { $anyflags != -1 } {
foreach m $machids {
if { $m == $fromid || $m == $toid } {
continue
}
set machlist [list $m]
break
}
}
}
#
# If we didn't find a different site, then we must
# fallback to the toid.
#
if { $m == "NULL" } {
set machlist [list $toid]
}
}
foreach m $machlist {
# do not broadcast to self.
if { $m == $fromid } {
continue
}
set db $queuedbs($m)
set txn [$queueenv txn]
$db put -txn $txn -append [list $control $rec $fromid]
error_check_good replsend_commit [$txn commit] 0
}
queue_logcheck
return 0
}
#
# If the message queue log files are getting too numerous, checkpoint
# and archive them. Some tests are so large (particularly from
# run_repmethod) that they can consume far too much disk space.
proc queue_logcheck { } {
global queueenv
set logs [$queueenv log_archive -arch_log]
set numlogs [llength $logs]
if { $numlogs > 10 } {
$queueenv txn_checkpoint
$queueenv log_archive -arch_remove
}
}
# Discard all the pending messages for a particular site.
proc replclear { machid } {
global queuedbs queueenv
if { [info exists queuedbs($machid)] != 1 } {
error "FAIL: replclear: machid $machid not found"
}
set db $queuedbs($machid)
set txn [$queueenv txn]
set dbc [$db cursor -txn $txn]
for { set dbt [$dbc get -rmw -first] } { [llength $dbt] > 0 } \
{ set dbt [$dbc get -rmw -next] } {
error_check_good replclear($machid)_del [$dbc del] 0
}
error_check_good replclear($machid)_dbc_close [$dbc close] 0
error_check_good replclear($machid)_txn_commit [$txn commit] 0
}
# Add a machine to a replication environment.
proc repladd { machid } {
global queueenv queuedbs machids
if { [info exists queuedbs($machid)] == 1 } {
error "FAIL: repladd: machid $machid already exists"
}
set queuedbs($machid) [berkdb open -auto_commit \
-env $queueenv -create -recno -renumber repqueue$machid.db]
error_check_good repqueue_create [is_valid_db $queuedbs($machid)] TRUE
lappend machids $machid
}
# Acquire a handle to work with an existing machine's replication
# queue. This is for situations where more than one process
# is working with a message queue. In general, having more than one
# process handle the queue is wrong. However, in order to test some
# things, we need two processes (since Tcl doesn't support threads). We
# go to great pain in the test harness to make sure this works, but we
# don't let customers do it.
proc repljoin { machid } {
global queueenv queuedbs machids
set queuedbs($machid) [berkdb open -auto_commit \
-env $queueenv repqueue$machid.db]
error_check_good repqueue_create [is_valid_db $queuedbs($machid)] TRUE
lappend machids $machid
}
# Process a queue of messages, skipping every "skip_interval" entry.
# We traverse the entire queue, but since we skip some messages, we
# may end up leaving things in the queue, which should get picked up
# on a later run.
proc replprocessqueue { dbenv machid { skip_interval 0 } { hold_electp NONE } \
{ dupmasterp NONE } { errp NONE } } {
global queuedbs queueenv errorCode
global perm_response_list
global startup_done
# hold_electp is a call-by-reference variable which lets our caller
# know we need to hold an election.
if { [string compare $hold_electp NONE] != 0 } {
upvar $hold_electp hold_elect
}
set hold_elect 0
# dupmasterp is a call-by-reference variable which lets our caller
# know we have a duplicate master.
if { [string compare $dupmasterp NONE] != 0 } {
upvar $dupmasterp dupmaster
}
set dupmaster 0
# errp is a call-by-reference variable which lets our caller
# know we have gotten an error (that they expect).
if { [string compare $errp NONE] != 0 } {
upvar $errp errorp
}
set errorp 0
set nproced 0
set txn [$queueenv txn]
# If we are running separate processes, the second process has
# to join an existing message queue.
if { [info exists queuedbs($machid)] == 0 } {
repljoin $machid
}
set dbc [$queuedbs($machid) cursor -txn $txn]
error_check_good process_dbc($machid) \
[is_valid_cursor $dbc $queuedbs($machid)] TRUE
for { set dbt [$dbc get -first] } \
{ [llength $dbt] != 0 } \
{ } {
set data [lindex [lindex $dbt 0] 1]
set recno [lindex [lindex $dbt 0] 0]
# If skip_interval is nonzero, we want to process messages
# out of order. We do this in a simple but slimy way--
# continue walking with the cursor without processing the
# message or deleting it from the queue, but do increment
# "nproced". The way this proc is normally used, the
# precise value of nproced doesn't matter--we just don't
# assume the queues are empty if it's nonzero. Thus,
# if we contrive to make sure it's nonzero, we'll always
# come back to records we've skipped on a later call
# to replprocessqueue. (If there really are no records,
# we'll never get here.)
#
# Skip every skip_interval'th record (and use a remainder other
# than zero so that we're guaranteed to really process at least
# one record on every call).
if { $skip_interval != 0 } {
if { $nproced % $skip_interval == 1 } {
incr nproced
set dbt [$dbc get -next]
continue
}
}
# We need to remove the current message from the queue,
# because we're about to end the transaction and someone
# else processing messages might come in and reprocess this
# message which would be bad.
error_check_good queue_remove [$dbc del] 0
# We have to play an ugly cursor game here: we currently
# hold a lock on the page of messages, but rep_process_message
# might need to lock the page with a different cursor in
# order to send a response. So save the next recno, close
# the cursor, and then reopen and reset the cursor.
# If someone else is processing this queue, our entry might
# have gone away, and we need to be able to handle that.
error_check_good dbc_process_close [$dbc close] 0
error_check_good txn_commit [$txn commit] 0
set ret [catch {$dbenv rep_process_message \
[lindex $data 2] [lindex $data 0] [lindex $data 1]} res]
# Save all ISPERM and NOTPERM responses so we can compare their
# LSNs to the LSN in the log. The variable perm_response_list
# holds the entire response so we can extract responses and
# LSNs as needed.
#
if { [llength $perm_response_list] != 0 && \
([is_substr $res ISPERM] || [is_substr $res NOTPERM]) } {
lappend perm_response_list $res
}
if { $ret != 0 } {
if { [string compare $errp NONE] != 0 } {
set errorp "$dbenv $machid $res"
} else {
error "FAIL:[timestamp]\
rep_process_message returned $res"
}
}
incr nproced
# Now, re-establish the cursor position. We fetch the
# current record number. If there is something there,
# that is the record for the next iteration. If there
# is nothing there, then we've consumed the last item
# in the queue.
set txn [$queueenv txn]
set dbc [$queuedbs($machid) cursor -txn $txn]
set dbt [$dbc get -set_range $recno]
if { $ret == 0 } {
set rettype [lindex $res 0]
set retval [lindex $res 1]
#
# Do nothing for 0 and NEWSITE
#
if { [is_substr $rettype STARTUPDONE] } {
set startup_done 1
}
if { [is_substr $rettype HOLDELECTION] } {
set hold_elect 1
}
if { [is_substr $rettype DUPMASTER] } {
set dupmaster "1 $dbenv $machid"
}
if { [is_substr $rettype NOTPERM] || \
[is_substr $rettype ISPERM] } {
set lsnfile [lindex $retval 0]
set lsnoff [lindex $retval 1]
}
}
if { $errorp != 0 } {
# Break also on an error, caller wants to handle it.
break
}
if { $hold_elect == 1 } {
# Break also on a HOLDELECTION, for the same reason.
break
}
if { $dupmaster == 1 } {
# Break also on a DUPMASTER, for the same reason.
break
}
}
error_check_good dbc_close [$dbc close] 0
error_check_good txn_commit [$txn commit] 0
# Return the number of messages processed.
return $nproced
}
set run_repl_flag "-run_repl"
proc extract_repl_args { args } {
global run_repl_flag
for { set arg [lindex $args [set i 0]] } \
{ [string length $arg] > 0 } \
{ set arg [lindex $args [incr i]] } {
if { [string compare $arg $run_repl_flag] == 0 } {
return [lindex $args [expr $i + 1]]
}
}
return ""
}
proc delete_repl_args { args } {
global run_repl_flag
set ret {}
for { set arg [lindex $args [set i 0]] } \
{ [string length $arg] > 0 } \
{ set arg [lindex $args [incr i]] } {
if { [string compare $arg $run_repl_flag] != 0 } {
lappend ret $arg
} else {
incr i
}
}
return $ret
}
global elect_serial
global elections_in_progress
set elect_serial 0
# Start an election in a sub-process.
proc start_election \
{ pfx qdir envstring nsites nvotes pri timeout {err "none"} {crash 0}} {
source ./include.tcl
global elect_serial elections_in_progress machids
global rep_verbose
set filelist {}
set ret [catch {glob $testdir/ELECTION*.$elect_serial} result]
if { $ret == 0 } {
set filelist [concat $filelist $result]
}
foreach f $filelist {
fileremove -f $f
}
set oid [open $testdir/ELECTION_SOURCE.$elect_serial w]
puts $oid "source $test_path/test.tcl"
puts $oid "set elected_event 0"
puts $oid "set elected_env \"NONE\""
puts $oid "set is_repchild 1"
puts $oid "replsetup $qdir"
foreach i $machids { puts $oid "repladd $i" }
puts $oid "set env_cmd \{$envstring\}"
if { $rep_verbose == 1 } {
puts $oid "set dbenv \[eval \$env_cmd -errfile \
/dev/stdout -errpfx $pfx \]"
} else {
puts $oid "set dbenv \[eval \$env_cmd -errfile \
$testdir/ELECTION_ERRFILE.$elect_serial -errpfx $pfx \]"
}
puts $oid "\$dbenv test abort $err"
puts $oid "set res \[catch \{\$dbenv rep_elect $nsites \
$nvotes $pri $timeout\} ret\]"
puts $oid "set r \[open \$testdir/ELECTION_RESULT.$elect_serial w\]"
puts $oid "if \{\$res == 0 \} \{"
puts $oid "puts \$r \"SUCCESS \$ret\""
puts $oid "\} else \{"
puts $oid "puts \$r \"ERROR \$ret\""
puts $oid "\}"
#
# This loop calls rep_elect a second time with the error cleared.
# We don't want to do that if we are simulating a crash.
if { $err != "none" && $crash != 1 } {
puts $oid "\$dbenv test abort none"
puts $oid "set res \[catch \{\$dbenv rep_elect $nsites \
$nvotes $pri $timeout\} ret\]"
puts $oid "if \{\$res == 0 \} \{"
puts $oid "puts \$r \"SUCCESS \$ret\""
puts $oid "\} else \{"
puts $oid "puts \$r \"ERROR \$ret\""
puts $oid "\}"
}
puts $oid "if \{ \$elected_event == 1 \} \{"
puts $oid "puts \$r \"ELECTED \$elected_env\""
puts $oid "\}"
puts $oid "close \$r"
close $oid
set t [open "|$tclsh_path >& $testdir/ELECTION_OUTPUT.$elect_serial" w]
if { $rep_verbose } {
set t [open "|$tclsh_path" w]
}
puts $t "source ./include.tcl"
puts $t "source $testdir/ELECTION_SOURCE.$elect_serial"
flush $t
set elections_in_progress($elect_serial) $t
return $elect_serial
}
#
# If we are doing elections during upgrade testing, set
# upgrade to 1. Doing that sets the priority to the
# test priority in rep_elect, which will simulate a
# 0-priority but electable site.
#
proc setpriority { priority nclients winner {start 0} {upgrade 0} } {
global electable_pri
upvar $priority pri
for { set i $start } { $i < [expr $nclients + $start] } { incr i } {
if { $i == $winner } {
set pri($i) 100
} else {
if { $upgrade } {
set pri($i) $electable_pri
} else {
set pri($i) 10
}
}
}
}
# run_election has the following arguments:
# Arrays:
# ecmd Array of the commands for setting up each client env.
# cenv Array of the handles to each client env.
# errcmd Array of where errors should be forced.
# priority Array of the priorities of each client env.
# crash If an error is forced, should we crash or recover?
# The upvar command takes care of making these arrays available to
# the procedure.
#
# Ordinary variables:
# qdir Directory where the message queue is located.
# msg Message prefixed to the output.
# elector This client calls the first election.
# nsites Number of sites in the replication group.
# nvotes Number of votes required to win the election.
# nclients Number of clients participating in the election.
# win The expected winner of the election.
# reopen Should the new master (i.e. winner) be closed
# and reopened as a client?
# dbname Name of the underlying database. The caller
# should send in "NULL" if the database has not
# yet been created.
# ignore Should the winner ignore its own election?
# If ignore is 1, the winner is not made master.
# timeout_ok We expect that this election will not succeed
# in electing a new master (perhaps because there
# already is a master).
proc run_election { ecmd celist errcmd priority crsh\
qdir msg elector nsites nvotes nclients win reopen\
dbname {ignore 0} {timeout_ok 0} } {
global elect_timeout elect_serial
global is_hp_test
global is_windows_test
global rand_init
upvar $ecmd env_cmd
upvar $celist cenvlist
upvar $errcmd err_cmd
upvar $priority pri
upvar $crsh crash
set elect_timeout(default) 15000000
# Windows and HP-UX require a longer timeout.
if { $is_windows_test == 1 || $is_hp_test == 1 } {
set elect_timeout(default) [expr $elect_timeout(default) * 2]
}
set long_timeout $elect_timeout(default)
#
# Initialize tries based on the default timeout.
# We use tries to loop looking for messages because
# as sites are sleeping waiting for their timeout
# to expire we need to keep checking for messages.
#
set tries [expr [expr $long_timeout * 4] / 1000000]
#
# Retry indicates whether the test should retry the election
# if it gets a timeout. This is primarily used for the
# varied timeout election test because we expect short timeouts
# to timeout when interacting with long timeouts and the
# short timeout sites need to call elections again.
#
set retry 0
foreach pair $cenvlist {
set id [lindex $pair 1]
set i [expr $id - 2]
set elect_pipe($i) INVALID
#
# Array get should return us a list of 1 element:
# { {$i timeout_value} }
# If that doesn't exist, use the default.
#
set this_timeout [array get elect_timeout $i]
if { [llength $this_timeout] } {
set e_timeout($i) [lindex $this_timeout 1]
#
# Set number of tries based on the biggest
# timeout we see in this group if using
# varied timeouts.
#
set retry 1
if { $e_timeout($i) > $long_timeout } {
set long_timeout $e_timeout($i)
set tries [expr $long_timeout / 1000000]
}
} else {
set e_timeout($i) $elect_timeout(default)
}
replclear $id
}
#
# XXX
# We need to somehow check for the warning if nvotes is not
# a majority. Problem is that warning will go into the child
# process' output. Furthermore, we need a mechanism that can
# handle both sending the output to a file and sending it to
# /dev/stderr when debugging without failing the
# error_check_good check.
#
puts "\t\t$msg.1: Election with nsites=$nsites,\
nvotes=$nvotes, nclients=$nclients"
puts "\t\t$msg.2: First elector is $elector,\
expected winner is $win (eid [expr $win + 2])"
incr elect_serial
set pfx "CHILD$elector.$elect_serial"
set elect_pipe($elector) [start_election \
$pfx $qdir $env_cmd($elector) $nsites $nvotes $pri($elector) \
$e_timeout($elector) $err_cmd($elector) $crash($elector)]
tclsleep 2
set got_newmaster 0
set max_retry $tries
# If we're simulating a crash, skip the while loop and
# just give the initial election a chance to complete.
set crashing 0
for { set i 0 } { $i < $nclients } { incr i } {
if { $crash($i) == 1 } {
set crashing 1
}
}
global elected_event
global elected_env
set elected_event 0
set c_elected_event 0
set elected_env "NONE"
set orig_tries $tries
if { $crashing == 1 } {
tclsleep 10
} else {
set retry_cnt 0
while { 1 } {
set nproced 0
set he 0
set winning_envid -1
set c_winning_envid -1
foreach pair $cenvlist {
set he 0
set unavail 0
set envid [lindex $pair 1]
set i [expr $envid - 2]
set clientenv($i) [lindex $pair 0]
# If the "elected" event is received by the
# child process, the env set up in that child
# is the elected env.
set child_done [check_election $elect_pipe($i)\
unavail c_elected_event c_elected_env]
if { $c_elected_event != 0 } {
set elected_event 1
set c_winning_envid $envid
set c_elected_event 0
}
incr nproced [replprocessqueue \
$clientenv($i) $envid 0 he]
# puts "Tries $tries:\
# Processed queue for client $i, $nproced msgs he $he unavail $unavail"
# Check for completed election. If it's the
# first time we've noticed it, deal with it.
if { $elected_event == 1 && \
$got_newmaster == 0 } {
set got_newmaster 1
# Find env id of winner.
if { $c_winning_envid != -1 } {
set winning_envid \
$c_winning_envid
set c_winning_envid -1
} else {
foreach pair $cenvlist {
if { [lindex $pair 0]\
== $elected_env } {
set winning_envid \
[lindex $pair 1]
break
}
}
}
# Make sure it's the expected winner.
error_check_good right_winner \
$winning_envid [expr $win + 2]
# Reconfigure winning env as master.
if { $ignore == 0 } {
$clientenv($i) errpfx \
NEWMASTER
error_check_good \
make_master($i) \
[$clientenv($i) \
rep_start -master] 0
# Don't hold another election
# yet if we are setting up a
# new master. This could
# cause the new master to
# declare itself a client
# during internal init.
set he 0
}
# Occasionally force new log records
# to be written, unless the database
# has not yet been created.
set write [berkdb random_int 1 10]
if { $write == 1 && $dbname != "NULL" } {
set db [eval berkdb_open_noerr \
-env $clientenv($i) \
-auto_commit $dbname]
error_check_good dbopen \
[is_valid_db $db] TRUE
error_check_good dbclose \
[$db close] 0
}
}
# If the previous election failed with a
# timeout and we need to retry because we
# are testing varying site timeouts, force
# a hold election to start a new one.
if { $unavail && $retry && $retry_cnt < $max_retry} {
incr retry_cnt
puts "\t\t$msg.2.b: Client $i timed\
out. Retry $retry_cnt\
of max $max_retry"
set he 1
set tries $orig_tries
}
if { $he == 1 && $got_newmaster == 0 } {
#
# Only close down the election pipe if the
# previously created one is done and
# waiting for new commands, otherwise
# if we try to close it while it's in
# progress we hang this main tclsh.
#
if { $elect_pipe($i) != "INVALID" && \
$child_done == 1 } {
close_election $elect_pipe($i)
set elect_pipe($i) "INVALID"
}
# puts "Starting election on client $i"
if { $elect_pipe($i) == "INVALID" } {
incr elect_serial
set pfx "CHILD$i.$elect_serial"
set elect_pipe($i) [start_election \
$pfx $qdir \
$env_cmd($i) $nsites \
$nvotes $pri($i) $e_timeout($i)]
set got_hold_elect($i) 1
}
}
}
# We need to wait around to make doubly sure that the
# election has finished...
if { $nproced == 0 } {
incr tries -1
#
# If we have a newmaster already, set tries
# down to just allow straggling messages to
# be processed. Tries could be a very large
# number if we have long timeouts.
#
if { $got_newmaster != 0 && $tries > 10 } {
set tries 10
}
if { $tries == 0 } {
break
} else {
tclsleep 1
}
} else {
set tries $tries
}
}
# If we did get a new master, its identity was checked
# at that time. But we still have to make sure that we
# didn't just time out.
if { $got_newmaster == 0 && $timeout_ok == 0 } {
error "FAIL: Did not elect new master."
}
}
cleanup_elections
#
# Make sure we've really processed all the post-election
# sync-up messages. If we're simulating a crash, don't process
# any more messages.
#
if { $crashing == 0 } {
process_msgs $cenvlist
}
if { $reopen == 1 } {
puts "\t\t$msg.3: Closing new master and reopening as client"
error_check_good log_flush [$clientenv($win) log_flush] 0
error_check_good newmaster_close [$clientenv($win) close] 0
set clientenv($win) [eval $env_cmd($win)]
error_check_good cl($win) [is_valid_env $clientenv($win)] TRUE
set newelector "$clientenv($win) [expr $win + 2]"
set cenvlist [lreplace $cenvlist $win $win $newelector]
if { $crashing == 0 } {
process_msgs $cenvlist
}
}
}
proc check_election { id unavailp elected_eventp elected_envp } {
source ./include.tcl
if { $id == "INVALID" } {
return 0
}
upvar $unavailp unavail
upvar $elected_eventp elected_event
upvar $elected_envp elected_env
set unavail 0
set elected_event 0
set elected_env "NONE"
set res [catch {open $testdir/ELECTION_RESULT.$id} nmid]
if { $res != 0 } {
return 0
}
while { [gets $nmid val] != -1 } {
# puts "result $id: $val"
set str [lindex $val 0]
if { [is_substr $val UNAVAIL] } {
set unavail 1
}
if { [is_substr $val ELECTED] } {
set elected_event 1
set elected_env [lindex $val 1]
}
}
close $nmid
return 1
}
proc close_election { i } {
global elections_in_progress
global noenv_messaging
global qtestdir
if { $noenv_messaging == 1 } {
set testdir $qtestdir
}
set t $elections_in_progress($i)
puts $t "replclose \$testdir/MSGQUEUEDIR"
puts $t "\$dbenv close"
close $t
unset elections_in_progress($i)
}
proc cleanup_elections { } {
global elect_serial elections_in_progress
for { set i 0 } { $i <= $elect_serial } { incr i } {
if { [info exists elections_in_progress($i)] != 0 } {
close_election $i
}
}
set elect_serial 0
}
#
# This is essentially a copy of test001, but it only does the put/get
# loop AND it takes an already-opened db handle.
#
proc rep_test { method env repdb {nentries 10000} \
{start 0} {skip 0} {needpad 0} args } {
source ./include.tcl
global databases_in_memory
#
# Open the db if one isn't given. Close before exit.
#
if { $repdb == "NULL" } {
if { $databases_in_memory == 1 } {
set testfile { "" "test.db" }
} else {
set testfile "test.db"
}
set largs [convert_args $method $args]
set omethod [convert_method $method]
set db [eval {berkdb_open_noerr} -env $env -auto_commit\
-create -mode 0644 $omethod $largs $testfile]
error_check_good reptest_db [is_valid_db $db] TRUE
} else {
set db $repdb
}
puts "\t\tRep_test: $method $nentries key/data pairs starting at $start"
set did [open $dict]
# The "start" variable determines the record number to start
# with, if we're using record numbers. The "skip" variable
# determines which dictionary entry to start with. In normal
# use, skip is equal to start.
if { $skip != 0 } {
for { set count 0 } { $count < $skip } { incr count } {
gets $did str
}
}
set pflags ""
set gflags ""
set txn ""
if { [is_record_based $method] == 1 } {
append gflags " -recno"
}
puts "\t\tRep_test.a: put/get loop"
# Here is the loop where we put and get each key/data pair
set count 0
# Checkpoint 10 times during the run, but not more
# frequently than every 5 entries.
set checkfreq [expr $nentries / 10]
# Abort occasionally during the run.
set abortfreq [expr $nentries / 15]
while { [gets $did str] != -1 && $count < $nentries } {
if { [is_record_based $method] == 1 } {
global kvals
set key [expr $count + 1 + $start]
if { 0xffffffff > 0 && $key > 0xffffffff } {
set key [expr $key - 0x100000000]
}
if { $key == 0 || $key - 0xffffffff == 1 } {
incr key
incr count
}
set kvals($key) [pad_data $method $str]
} else {
set key $str
set str [reverse $str]
}
#
# We want to make sure we send in exactly the same
# length data so that LSNs match up for some tests
# in replication (rep021).
#
if { [is_fixed_length $method] == 1 && $needpad } {
#
# Make it something visible and obvious, 'A'.
#
set p 65
set str [make_fixed_length $method $str $p]
set kvals($key) $str
}
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
set ret [eval \
{$db put} $txn $pflags {$key [chop_data $method $str]}]
error_check_good put $ret 0
error_check_good txn [$t commit] 0
if { $checkfreq < 5 } {
set checkfreq 5
}
if { $abortfreq < 3 } {
set abortfreq 3
}
#
# Do a few aborted transactions to test that
# aborts don't get processed on clients and the
# master handles them properly. Just abort
# trying to delete the key we just added.
#
if { $count % $abortfreq == 0 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set ret [$db del -txn $t $key]
error_check_good txn [$t abort] 0
}
if { $count % $checkfreq == 0 } {
error_check_good txn_checkpoint($count) \
[$env txn_checkpoint] 0
}
incr count
}
close $did
if { $repdb == "NULL" } {
error_check_good rep_close [$db close] 0
}
}
#
# This is essentially a copy of rep_test, but it only does the put/get
# loop in a long running txn to an open db. We use it for bulk testing
# because we want to fill the bulk buffer some before sending it out.
# Bulk buffer gets transmitted on every commit.
#
proc rep_test_bulk { method env repdb {nentries 10000} \
{start 0} {skip 0} {useoverflow 0} args } {
source ./include.tcl
global overflowword1
global overflowword2
global databases_in_memory
if { [is_fixed_length $method] && $useoverflow == 1 } {
puts "Skipping overflow for fixed length method $method"
return
}
#
# Open the db if one isn't given. Close before exit.
#
if { $repdb == "NULL" } {
if { $databases_in_memory == 1 } {
set testfile { "" "test.db" }
} else {
set testfile "test.db"
}
set largs [convert_args $method $args]
set omethod [convert_method $method]
set db [eval {berkdb_open_noerr -env $env -auto_commit -create \
-mode 0644} $largs $omethod $testfile]
error_check_good reptest_db [is_valid_db $db] TRUE
} else {
set db $repdb
}
#
# If we are using an env, then testfile should just be the db name.
# Otherwise it is the test directory and the name.
# If we are not using an external env, then test setting
# the database cache size and using multiple caches.
puts \
"\t\tRep_test_bulk: $method $nentries key/data pairs starting at $start"
set did [open $dict]
# The "start" variable determines the record number to start
# with, if we're using record numbers. The "skip" variable
# determines which dictionary entry to start with. In normal
# use, skip is equal to start.
if { $skip != 0 } {
for { set count 0 } { $count < $skip } { incr count } {
gets $did str
}
}
set pflags ""
set gflags ""
set txn ""
if { [is_record_based $method] == 1 } {
append gflags " -recno"
}
puts "\t\tRep_test_bulk.a: put/get loop in 1 txn"
# Here is the loop where we put and get each key/data pair
set count 0
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
set pid [pid]
while { [gets $did str] != -1 && $count < $nentries } {
if { [is_record_based $method] == 1 } {
global kvals
set key [expr $count + 1 + $start]
if { 0xffffffff > 0 && $key > 0xffffffff } {
set key [expr $key - 0x100000000]
}
if { $key == 0 || $key - 0xffffffff == 1 } {
incr key
incr count
}
set kvals($key) [pad_data $method $str]
if { [is_fixed_length $method] == 0 } {
set str [repeat $str 100]
}
} else {
set key $str.$pid
set str [repeat $str 100]
}
#
# For use for overflow test.
#
if { $useoverflow == 0 } {
if { [string length $overflowword1] < \
[string length $str] } {
set overflowword2 $overflowword1
set overflowword1 $str
}
} else {
if { $count == 0 } {
set len [string length $overflowword1]
set word $overflowword1
} else {
set len [string length $overflowword2]
set word $overflowword1
}
set rpt [expr 1024 * 1024 / $len]
incr rpt
set str [repeat $word $rpt]
}
set ret [eval \
{$db put} $txn $pflags {$key [chop_data $method $str]}]
error_check_good put $ret 0
incr count
}
error_check_good txn [$t commit] 0
error_check_good txn_checkpoint [$env txn_checkpoint] 0
close $did
if { $repdb == "NULL" } {
error_check_good rep_close [$db close] 0
}
}
proc rep_test_upg { method env repdb {nentries 10000} \
{start 0} {skip 0} {needpad 0} {inmem 0} args } {
source ./include.tcl
#
# Open the db if one isn't given. Close before exit.
#
if { $repdb == "NULL" } {
if { $inmem == 1 } {
set testfile { "" "test.db" }
} else {
set testfile "test.db"
}
set largs [convert_args $method $args]
set omethod [convert_method $method]
set db [eval {berkdb_open_noerr} -env $env -auto_commit\
-create -mode 0644 $omethod $largs $testfile]
error_check_good reptest_db [is_valid_db $db] TRUE
} else {
set db $repdb
}
set pid [pid]
puts "\t\tRep_test_upg($pid): $method $nentries key/data pairs starting at $start"
set did [open $dict]
# The "start" variable determines the record number to start
# with, if we're using record numbers. The "skip" variable
# determines which dictionary entry to start with. In normal
# use, skip is equal to start.
if { $skip != 0 } {
for { set count 0 } { $count < $skip } { incr count } {
gets $did str
}
}
set pflags ""
set gflags ""
set txn ""
if { [is_record_based $method] == 1 } {
append gflags " -recno"
}
puts "\t\tRep_test.a: put/get loop"
# Here is the loop where we put and get each key/data pair
set count 0
# Checkpoint 10 times during the run, but not more
# frequently than every 5 entries.
set checkfreq [expr $nentries / 10]
# Abort occasionally during the run.
set abortfreq [expr $nentries / 15]
while { [gets $did str] != -1 && $count < $nentries } {
if { [is_record_based $method] == 1 } {
global kvals
set key [expr $count + 1 + $start]
if { 0xffffffff > 0 && $key > 0xffffffff } {
set key [expr $key - 0x100000000]
}
if { $key == 0 || $key - 0xffffffff == 1 } {
incr key
incr count
}
set kvals($key) [pad_data $method $str]
} else {
#
# With upgrade test, we run the same test several
# times with the same database. We want to have
# some overwritten records and some new records.
# Therefore append our pid to half the keys.
#
if { $count % 2 } {
set key $str.$pid
} else {
set key $str
}
set str [reverse $str]
}
#
# We want to make sure we send in exactly the same
# length data so that LSNs match up for some tests
# in replication (rep021).
#
if { [is_fixed_length $method] == 1 && $needpad } {
#
# Make it something visible and obvious, 'A'.
#
set p 65
set str [make_fixed_length $method $str $p]
set kvals($key) $str
}
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
# puts "rep_test_upg: put $count of $nentries: key $key, data $str"
set ret [eval \
{$db put} $txn $pflags {$key [chop_data $method $str]}]
error_check_good put $ret 0
error_check_good txn [$t commit] 0
if { $checkfreq < 5 } {
set checkfreq 5
}
if { $abortfreq < 3 } {
set abortfreq 3
}
#
# Do a few aborted transactions to test that
# aborts don't get processed on clients and the
# master handles them properly. Just abort
# trying to delete the key we just added.
#
if { $count % $abortfreq == 0 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set ret [$db del -txn $t $key]
error_check_good txn [$t abort] 0
}
if { $count % $checkfreq == 0 } {
error_check_good txn_checkpoint($count) \
[$env txn_checkpoint] 0
}
incr count
}
close $did
if { $repdb == "NULL" } {
error_check_good rep_close [$db close] 0
}
}
proc rep_test_upg.check { key data } {
#
# If the key has the pid attached, strip it off before checking.
# If the key does not have the pid attached, then it is a recno
# and we're done.
#
set i [string first . $key]
if { $i != -1 } {
set key [string replace $key $i end]
}
error_check_good "key/data mismatch" $data [reverse $key]
}
proc rep_test_upg.recno.check { key data } {
#
# If we're a recno database we better not have a pid in the key.
# Otherwise we're done.
#
set i [string first . $key]
error_check_good pid $i -1
}
#
# This is the basis for a number of simple repmgr test cases. It creates
# an appointed master and two clients, calls rep_test to process some records
# and verifies the resulting databases. The following parameters control
# runtime options:
# niter - number of records to process
# inmemdb - put databases in-memory (0, 1)
# inmemlog - put logs in-memory (0, 1)
# peer - make the second client a peer of the first client (0, 1)
# bulk - use bulk processing (0, 1)
# inmemrep - put replication files in-memory (0, 1)
#
proc basic_repmgr_test { method niter tnum inmemdb inmemlog peer bulk \
inmemrep largs } {
global testdir
global rep_verbose
global verbose_type
global overflowword1
global overflowword2
global databases_in_memory
set overflowword1 "0"
set overflowword2 "0"
set nsites 3
# Set databases_in_memory for this test, preserving original value.
if { $inmemdb } {
set restore_dbinmem $databases_in_memory
set databases_in_memory 1
}
set verbargs ""
if { $rep_verbose == 1 } {
set verbargs " -verbose {$verbose_type on} "
}
env_cleanup $testdir
set ports [available_ports $nsites]
set masterdir $testdir/MASTERDIR
set clientdir $testdir/CLIENTDIR
set clientdir2 $testdir/CLIENTDIR2
file mkdir $masterdir
file mkdir $clientdir
file mkdir $clientdir2
# In-memory logs require a large log buffer, and cannot
# be used with -txn nosync. Adjust the args.
if { $inmemlog } {
set logtype "in-memory"
} else {
set logtype "on-disk"
}
set logargs [adjust_logargs $logtype]
set txnargs [adjust_txnargs $logtype]
# Determine in-memory replication argument for environments.
if { $inmemrep } {
set repmemarg "-rep_inmem_files "
} else {
set repmemarg ""
}
# Use different connection retry timeout values to handle any
# collisions from starting sites at the same time by retrying
# at different times.
# Open a master.
puts "\tRepmgr$tnum.a: Start an appointed master."
set ma_envcmd "berkdb_env_noerr -create $logargs $verbargs \
-errpfx MASTER -home $masterdir $txnargs -rep -thread \
-lock_max_locks 10000 -lock_max_objects 10000 $repmemarg"
set masterenv [eval $ma_envcmd]
$masterenv repmgr -ack all -nsites $nsites \
-timeout {conn_retry 20000000} \
-local [list localhost [lindex $ports 0]] \
-start master
# Open first client
puts "\tRepmgr$tnum.b: Start first client."
set cl_envcmd "berkdb_env_noerr -create $verbargs $logargs \
-errpfx CLIENT -home $clientdir $txnargs -rep -thread \
-lock_max_locks 10000 -lock_max_objects 10000 $repmemarg"
set clientenv [eval $cl_envcmd]
$clientenv repmgr -ack all -nsites $nsites \
-timeout {conn_retry 10000000} \
-local [list localhost [lindex $ports 1]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 2]] \
-start client
await_startup_done $clientenv
# Open second client
puts "\tRepmgr$tnum.c: Start second client."
set cl2_envcmd "berkdb_env_noerr -create $verbargs $logargs \
-errpfx CLIENT2 -home $clientdir2 $txnargs -rep -thread \
-lock_max_locks 10000 -lock_max_objects 10000 $repmemarg"
set clientenv2 [eval $cl2_envcmd]
if { $peer } {
$clientenv2 repmgr -ack all -nsites $nsites \
-timeout {conn_retry 5000000} \
-local [list localhost [lindex $ports 2]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 1] peer] \
-start client
} else {
$clientenv2 repmgr -ack all -nsites $nsites \
-timeout {conn_retry 5000000} \
-local [list localhost [lindex $ports 2]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 1]] \
-start client
}
await_startup_done $clientenv2
#
# Use of -ack all guarantees replication complete before repmgr send
# function returns and rep_test finishes.
#
puts "\tRepmgr$tnum.d: Run some transactions at master."
if { $bulk } {
# Turn on bulk processing on master.
error_check_good set_bulk [$masterenv rep_config {bulk on}] 0
eval rep_test_bulk $method $masterenv NULL $niter 0 0 0 $largs
# Must turn off bulk because some configs (debug_rop/wop)
# generate log records when verifying databases.
error_check_good set_bulk [$masterenv rep_config {bulk off}] 0
} else {
eval rep_test $method $masterenv NULL $niter 0 0 0 $largs
}
puts "\tRepmgr$tnum.e: Verifying client database contents."
rep_verify $masterdir $masterenv $clientdir $clientenv 1 1 1
rep_verify $masterdir $masterenv $clientdir2 $clientenv2 1 1 1
# For in-memory replication, verify replication files not there.
if { $inmemrep } {
puts "\tRepmgr$tnum.f: Verify no replication files on disk."
no_rep_files_on_disk $masterdir
no_rep_files_on_disk $clientdir
no_rep_files_on_disk $clientdir2
}
# Restore original databases_in_memory value.
if { $inmemdb } {
set databases_in_memory $restore_dbinmem
}
error_check_good client2_close [$clientenv2 close] 0
error_check_good client_close [$clientenv close] 0
error_check_good masterenv_close [$masterenv close] 0
}
#
# This is the basis for simple repmgr election test cases. It opens three
# clients of different priorities and makes sure repmgr elects the
# expected master. Then it shuts the master down and makes sure repmgr
# elects the expected remaining client master. Then it makes sure the former
# master can join as a client. The following parameters control
# runtime options:
# niter - number of records to process
# inmemrep - put replication files in-memory (0, 1)
#
proc basic_repmgr_election_test { method niter tnum inmemrep largs } {
global rep_verbose
global testdir
global verbose_type
set nsites 3
set verbargs ""
if { $rep_verbose == 1 } {
set verbargs " -verbose {$verbose_type on} "
}
env_cleanup $testdir
set ports [available_ports $nsites]
set clientdir $testdir/CLIENTDIR
set clientdir2 $testdir/CLIENTDIR2
set clientdir3 $testdir/CLIENTDIR3
file mkdir $clientdir
file mkdir $clientdir2
file mkdir $clientdir3
# Determine in-memory replication argument for environments.
if { $inmemrep } {
set repmemarg "-rep_inmem_files "
} else {
set repmemarg ""
}
# Use different connection retry timeout values to handle any
# collisions from starting sites at the same time by retrying
# at different times.
puts "\tRepmgr$tnum.a: Start three clients."
# Open first client
set cl_envcmd "berkdb_env_noerr -create $verbargs \
-errpfx CLIENT -home $clientdir -txn -rep -thread $repmemarg"
set clientenv [eval $cl_envcmd]
$clientenv repmgr -ack all -nsites $nsites -pri 100 \
-timeout {conn_retry 20000000} \
-local [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 1]] \
-remote [list localhost [lindex $ports 2]] \
-start elect
# Open second client
set cl2_envcmd "berkdb_env_noerr -create $verbargs \
-errpfx CLIENT2 -home $clientdir2 -txn -rep -thread $repmemarg"
set clientenv2 [eval $cl2_envcmd]
$clientenv2 repmgr -ack all -nsites $nsites -pri 30 \
-timeout {conn_retry 10000000} \
-local [list localhost [lindex $ports 1]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 2]] \
-start elect
# Open third client
set cl3_envcmd "berkdb_env_noerr -create $verbargs \
-errpfx CLIENT3 -home $clientdir3 -txn -rep -thread $repmemarg"
set clientenv3 [eval $cl3_envcmd]
$clientenv3 repmgr -ack all -nsites $nsites -pri 20 \
-timeout {conn_retry 5000000} \
-local [list localhost [lindex $ports 2]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 1]] \
-start elect
puts "\tRepmgr$tnum.b: Elect first client master."
await_expected_master $clientenv
set masterenv $clientenv
set masterdir $clientdir
await_startup_done $clientenv2
await_startup_done $clientenv3
#
# Use of -ack all guarantees replication complete before repmgr send
# function returns and rep_test finishes.
#
puts "\tRepmgr$tnum.c: Run some transactions at master."
eval rep_test $method $masterenv NULL $niter 0 0 0 $largs
puts "\tRepmgr$tnum.d: Verify client database contents."
rep_verify $masterdir $masterenv $clientdir2 $clientenv2 1 1 1
rep_verify $masterdir $masterenv $clientdir3 $clientenv3 1 1 1
puts "\tRepmgr$tnum.e: Shut down master, elect second client master."
error_check_good client_close [$clientenv close] 0
await_expected_master $clientenv2
set masterenv $clientenv2
await_startup_done $clientenv3
puts "\tRepmgr$tnum.f: Restart former master as client."
# Open -recover to clear env region, including startup_done value.
set clientenv [eval $cl_envcmd -recover]
$clientenv repmgr -ack all -nsites $nsites -pri 100 \
-timeout {conn_retry 20000000} \
-local [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 1]] \
-remote [list localhost [lindex $ports 2]] \
-start client
await_startup_done $clientenv
puts "\tRepmgr$tnum.g: Run some transactions at new master."
eval rep_test $method $masterenv NULL $niter $niter 0 0 $largs
puts "\tRepmgr$tnum.h: Verify client database contents."
set masterdir $clientdir2
rep_verify $masterdir $masterenv $clientdir $clientenv 1 1 1
rep_verify $masterdir $masterenv $clientdir3 $clientenv3 1 1 1
# For in-memory replication, verify replication files not there.
if { $inmemrep } {
puts "\tRepmgr$tnum.i: Verify no replication files on disk."
no_rep_files_on_disk $clientdir
no_rep_files_on_disk $clientdir2
no_rep_files_on_disk $clientdir3
}
error_check_good client3_close [$clientenv3 close] 0
error_check_good client_close [$clientenv close] 0
error_check_good client2_close [$clientenv2 close] 0
}
#
# This is the basis for simple repmgr internal init test cases. It starts
# an appointed master and two clients, processing transactions between each
# additional site. Then it verifies all expected transactions are
# replicated. The following parameters control runtime options:
# niter - number of records to process
# inmemrep - put replication files in-memory (0, 1)
#
proc basic_repmgr_init_test { method niter tnum inmemrep largs } {
global rep_verbose
global testdir
global verbose_type
set nsites 3
set verbargs ""
if { $rep_verbose == 1 } {
set verbargs " -verbose {$verbose_type on} "
}
env_cleanup $testdir
set ports [available_ports $nsites]
set masterdir $testdir/MASTERDIR
set clientdir $testdir/CLIENTDIR
set clientdir2 $testdir/CLIENTDIR2
file mkdir $masterdir
file mkdir $clientdir
file mkdir $clientdir2
# Determine in-memory replication argument for environments.
if { $inmemrep } {
set repmemarg "-rep_inmem_files "
} else {
set repmemarg ""
}
# Use different connection retry timeout values to handle any
# collisions from starting sites at the same time by retrying
# at different times.
# Open a master.
puts "\tRepmgr$tnum.a: Start a master."
set ma_envcmd "berkdb_env_noerr -create $verbargs \
-errpfx MASTER -home $masterdir -txn -rep -thread $repmemarg"
set masterenv [eval $ma_envcmd]
$masterenv repmgr -ack all -nsites $nsites \
-timeout {conn_retry 20000000} \
-local [list localhost [lindex $ports 0]] \
-start master
puts "\tRepmgr$tnum.b: Run some transactions at master."
eval rep_test $method $masterenv NULL $niter 0 0 0 $largs
# Open first client
puts "\tRepmgr$tnum.c: Start first client."
set cl_envcmd "berkdb_env_noerr -create $verbargs \
-errpfx CLIENT -home $clientdir -txn -rep -thread $repmemarg"
set clientenv [eval $cl_envcmd]
$clientenv repmgr -ack all -nsites $nsites \
-timeout {conn_retry 10000000} \
-local [list localhost [lindex $ports 1]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 2]] \
-start client
await_startup_done $clientenv
#
# Use of -ack all guarantees replication complete before repmgr send
# function returns and rep_test finishes.
#
puts "\tRepmgr$tnum.d: Run some more transactions at master."
eval rep_test $method $masterenv NULL $niter $niter 0 0 $largs
# Open second client
puts "\tRepmgr$tnum.e: Start second client."
set cl_envcmd "berkdb_env_noerr -create $verbargs \
-errpfx CLIENT2 -home $clientdir2 -txn -rep -thread $repmemarg"
set clientenv2 [eval $cl_envcmd]
$clientenv2 repmgr -ack all -nsites $nsites \
-timeout {conn_retry 5000000} \
-local [list localhost [lindex $ports 2]] \
-remote [list localhost [lindex $ports 0]] \
-remote [list localhost [lindex $ports 1]] \
-start client
await_startup_done $clientenv2
puts "\tRepmgr$tnum.f: Verifying client database contents."
rep_verify $masterdir $masterenv $clientdir $clientenv 1 1 1
rep_verify $masterdir $masterenv $clientdir2 $clientenv2 1 1 1
# For in-memory replication, verify replication files not there.
if { $inmemrep } {
puts "\tRepmgr$tnum.g: Verify no replication files on disk."
no_rep_files_on_disk $masterdir
no_rep_files_on_disk $clientdir
no_rep_files_on_disk $clientdir2
}
error_check_good client2_close [$clientenv2 close] 0
error_check_good client_close [$clientenv close] 0
error_check_good masterenv_close [$masterenv close] 0
}
#
# Verify that no replication files are present in a given directory.
# This checks for the gen, egen, internal init, temp db and page db
# files.
#
proc no_rep_files_on_disk { dir } {
error_check_good nogen [file exists "$dir/__db.rep.gen"] 0
error_check_good noegen [file exists "$dir/__db.rep.egen"] 0
error_check_good noinit [file exists "$dir/__db.rep.init"] 0
error_check_good notmpdb [file exists "$dir/__db.rep.db"] 0
error_check_good nopgdb [file exists "$dir/__db.reppg.db"] 0
}
proc process_msgs { elist {perm_response 0} {dupp NONE} {errp NONE} \
{upg 0} } {
if { $perm_response == 1 } {
global perm_response_list
set perm_response_list {{}}
}
if { [string compare $dupp NONE] != 0 } {
upvar $dupp dupmaster
set dupmaster 0
} else {
set dupmaster NONE
}
if { [string compare $errp NONE] != 0 } {
upvar $errp errorp
set errorp 0
set var_name errorp
} else {
set errorp NONE
set var_name NONE
}
set upgcount 0
while { 1 } {
set nproced 0
incr nproced [proc_msgs_once $elist dupmaster $var_name]
#
# If we're running the upgrade test, we are running only
# our own env, we need to loop a bit to allow the other
# upgrade procs to run and reply to our messages.
#
if { $upg == 1 && $upgcount < 10 } {
tclsleep 2
incr upgcount
continue
}
if { $nproced == 0 } {
break
} else {
set upgcount 0
}
}
}
proc proc_msgs_once { elist {dupp NONE} {errp NONE} } {
global noenv_messaging
if { [string compare $dupp NONE] != 0 } {
upvar $dupp dupmaster
set dupmaster 0
} else {
set dupmaster NONE
}
if { [string compare $errp NONE] != 0 } {
upvar $errp errorp
set errorp 0
set var_name errorp
} else {
set errorp NONE
set var_name NONE
}
set nproced 0
foreach pair $elist {
set envname [lindex $pair 0]
set envid [lindex $pair 1]
#
# If we need to send in all the other args
# puts "Call replpq with on $envid"
if { $noenv_messaging } {
incr nproced [replprocessqueue_noenv $envname $envid \
0 NONE dupmaster $var_name]
} else {
incr nproced [replprocessqueue $envname $envid \
0 NONE dupmaster $var_name]
}
#
# If the user is expecting to handle an error and we get
# one, return the error immediately.
#
if { $dupmaster != 0 && $dupmaster != "NONE" } {
return 0
}
if { $errorp != 0 && $errorp != "NONE" } {
# puts "Returning due to error $errorp"
return 0
}
}
return $nproced
}
proc rep_verify { masterdir masterenv clientdir clientenv \
{compare_shared_portion 0} {match 1} {logcompare 1} \
{dbname "test.db"} {datadir ""} } {
global util_path
global encrypt
global passwd
global databases_in_memory
global repfiles_in_memory
global env_private
# Whether a named database is in-memory or on-disk, only the
# the name itself is passed in. Here we do the syntax adjustment
# from "test.db" to { "" "test.db" } for in-memory databases.
#
if { $databases_in_memory && $dbname != "NULL" } {
set dbname " {} $dbname "
}
# Check locations of dbs, repfiles, region files.
if { $dbname != "NULL" } {
check_db_location $masterenv $dbname $datadir
check_db_location $clientenv $dbname $datadir
}
if { $repfiles_in_memory } {
no_rep_files_on_disk $masterdir
no_rep_files_on_disk $clientdir
}
if { $env_private } {
no_region_files_on_disk $masterdir
no_region_files_on_disk $clientdir
}
# The logcompare flag indicates whether to compare logs.
# Sometimes we run a test where rep_verify is run twice with
# no intervening processing of messages. If that test is
# on a build with debug_rop enabled, the master's log is
# altered by the first rep_verify, and the second rep_verify
# will fail.
# To avoid this, skip the log comparison on the second rep_verify
# by specifying logcompare == 0.
#
if { $logcompare } {
set msg "Logs and databases"
} else {
set msg "Databases ($dbname)"
}
if { $match } {
puts "\t\tRep_verify: $clientdir: $msg should match"
} else {
puts "\t\tRep_verify: $clientdir: $msg should not match"
}
# Check that master and client logs and dbs are identical.
# Logs first, if specified ...
#
# If compare_shared_portion is set, run db_printlog on the log
# subset that both client and master have. Either the client or
# the master may have more (earlier) log files, due to internal
# initialization, in-memory log wraparound, or other causes.
#
if { $logcompare } {
error_check_good logcmp \
[logcmp $masterenv $clientenv $compare_shared_portion] 0
if { $dbname == "NULL" } {
return
}
}
# ... now the databases.
#
# We're defensive here and throw an error if a database does
# not exist. If opening the first database succeeded but the
# second failed, we close the first before reporting the error.
#
if { [catch {eval {berkdb_open_noerr} -env $masterenv\
-rdonly $dbname} db1] } {
error "FAIL:\
Unable to open first db $dbname in rep_verify: $db1"
}
if { [catch {eval {berkdb_open_noerr} -env $clientenv\
-rdonly $dbname} db2] } {
error_check_good close_db1 [$db1 close] 0
error "FAIL:\
Unable to open second db $dbname in rep_verify: $db2"
}
# db_compare uses the database handles to do the comparison, and
# we pass in the $mumbledir/$dbname string as a label to make it
# easier to identify the offending database in case of failure.
# Therefore this will work for both in-memory and on-disk databases.
if { $match } {
error_check_good [concat comparedbs. $dbname] [db_compare \
$db1 $db2 $masterdir/$dbname $clientdir/$dbname] 0
} else {
error_check_bad comparedbs [db_compare \
$db1 $db2 $masterdir/$dbname $clientdir/$dbname] 0
}
error_check_good db1_close [$db1 close] 0
error_check_good db2_close [$db2 close] 0
}
proc rep_event { env eventlist } {
global startup_done
global elected_event
global elected_env
set event [lindex $eventlist 0]
# puts "rep_event: Got event $event on env $env"
set eventlength [llength $eventlist]
if { $event == "startupdone" } {
error_check_good event_nodata $eventlength 1
set startup_done 1
}
if { $event == "elected" } {
error_check_good event_nodata $eventlength 1
set elected_event 1
set elected_env $env
}
if { $event == "newmaster" } {
error_check_good eiddata $eventlength 2
set event_newmasterid [lindex $eventlist 1]
}
return
}
# Return a list of TCP port numbers that are not currently in use on
# the local system. Note that this doesn't actually reserve the
# ports, so it's possible that by the time the caller tries to use
# them, another process could have taken one of them. But for our
# purposes that's unlikely enough that this is still useful: it's
# still better than trying to find hard-coded port numbers that will
# always be available.
#
proc available_ports { n } {
set ports {}
set socks {}
while {[incr n -1] >= 0} {
set sock [socket -server Unused -myaddr localhost 0]
set port [lindex [fconfigure $sock -sockname] 2]
lappend socks $sock
lappend ports $port
}
foreach sock $socks {
close $sock
}
return $ports
}
# Wait (a limited amount of time) for an arbitrary condition to become true,
# polling once per second. If time runs out we throw an error: a successful
# return implies the condition is indeed true.
#
proc await_condition { cond { limit 20 } } {
for {set i 0} {$i < $limit} {incr i} {
if {[uplevel 1 [list expr $cond]]} {
return
}
tclsleep 1
}
error "FAIL: condition \{$cond\} not achieved in $limit seconds."
}
proc await_startup_done { env { limit 20 } } {
await_condition {[stat_field $env rep_stat "Startup complete"]} $limit
}
# Wait (a limited amount of time) for an election to yield the expected
# environment as winner.
#
proc await_expected_master { env { limit 20 } } {
await_condition {[stat_field $env rep_stat "Role"] == "master"} $limit
}
proc do_leaseop { env db method key envlist { domsgs 1 } } {
global alphabet
#
# Put a txn to the database. Process messages to envlist
# if directed to do so. Read data on the master, ignoring
# leases (should always succeed).
#
set num [berkdb random_int 1 100]
set data $alphabet.$num
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
set ret [eval \
{$db put} $txn {$key [chop_data $method $data]}]
error_check_good put $ret 0
error_check_good txn [$t commit] 0
if { $domsgs } {
process_msgs $envlist
}
#
# Now make sure we can successfully read on the master
# if we ignore leases. That should always work. The
# caller will do any lease related calls and checks
# that are specific to the test.
#
set kd [$db get -nolease $key]
set curs [$db cursor]
set ckd [$curs get -nolease -set $key]
$curs close
error_check_good kd [llength $kd] 1
error_check_good ckd [llength $ckd] 1
}
#
# Get the given key, expecting status depending on whether leases
# are currently expected to be valid or not.
#
proc check_leaseget { db key getarg status } {
set stat [catch {eval {$db get} $getarg $key} kd]
if { $status != 0 } {
error_check_good get_result $stat 1
error_check_good kd_check \
[is_substr $kd $status] 1
} else {
error_check_good get_result_good $stat $status
error_check_good dbkey [lindex [lindex $kd 0] 0] $key
}
set curs [$db cursor]
set stat [catch {eval {$curs get} $getarg -set $key} kd]
if { $status != 0 } {
error_check_good get_result2 $stat 1
error_check_good kd_check \
[is_substr $kd $status] 1
} else {
error_check_good get_result2_good $stat $status
error_check_good dbckey [lindex [lindex $kd 0] 0] $key
}
$curs close
}
# Simple utility to check a client database for expected values. It does not
# handle dup keys.
#
proc verify_client_data { env db items } {
set dbp [berkdb open -env $env $db]
foreach i $items {
foreach {key expected_value} $i {
set results [$dbp get $key]
error_check_good result_length [llength $results] 1
set value [lindex $results 0 1]
error_check_good expected_value $value $expected_value
}
}
$dbp close
}
proc make_dbconfig { dir cnfs } {
global rep_verbose
set f [open "$dir/DB_CONFIG" "w"]
foreach line $cnfs {
puts $f $line
}
if {$rep_verbose} {
puts $f "set_verbose DB_VERB_REPLICATION"
}
close $f
}
proc open_site_prog { cmds } {
set site_prog [setup_site_prog]
set s [open "| $site_prog" "r+"]
fconfigure $s -buffering line
set synced yes
foreach cmd $cmds {
puts $s $cmd
if {[lindex $cmd 0] == "start"} {
gets $s
set synced yes
} else {
set synced no
}
}
if {! $synced} {
puts $s "echo done"
gets $s
}
return $s
}
proc setup_site_prog { } {
source ./include.tcl
# Generate the proper executable name for the system.
if { $is_windows_test } {
set repsite_executable db_repsite.exe
} else {
set repsite_executable db_repsite
}
# Check whether the executable exists.
if { [file exists $util_path/$repsite_executable] == 0 } {
error "Skipping: db_repsite executable\
not found. Is it built?"
} else {
set site_prog $util_path/$repsite_executable
}
return $site_prog
}
proc next_expected_lsn { env } {
return [stat_field $env rep_stat "Next LSN expected"]
}
proc lsn_file { lsn } {
if { [llength $lsn] != 2 } {
error "not a valid LSN: $lsn"
}
return [lindex $lsn 0]
}
proc assert_rep_flag { dir flag value } {
global util_path
set stat [exec $util_path/db_stat -N -RA -h $dir]
set present [is_substr $stat $flag]
error_check_good expected.flag.$flag $present $value
}
|