2 # May you do good and not evil.
3 # May you find forgiveness for yourself and forgive others.
4 # May you share freely, never taking more than you give.
6 #***********************************************************************
7 # This file implements regression tests for Berkeley DB specific replication
11 set testdir [file dirname $argv0]/../../lang/sql/sqlite/test
12 source $testdir/tester.tcl
13 source $testdir/../../../../test/sql/bdb_util.tcl
15 global site1addr site2addr site3addr site1dir site2dir site3dir
17 # The SQL test system is sequential, but some HA operations like elections
18 # and client synchronization are parallel operations across more than one
19 # SQL database/replication site. SQL HA test cases should observe the
20 # following rules to avoid problems:
21 # - Wait the appropriate *_delay for replication operations (see below).
22 # - If you stop and restart one or more sites, you must do an initial
23 # query on each restarted site to cause it to rejoin the replication
24 # group. If there is still a master running, a restarted site will
25 # do a client synchronization. If there is no master, a restarted
26 # site and all other running sites will perform an election.
27 # - Any test case that runs an election after initial startup should
28 # create DB_CONFIG files on all sites that:
29 # * Set priorities to help make election winner deterministic.
30 # * Turn off 2SITE_STRICT so that there will not be an excessive
31 # wait for each site before a quorum is reached.
32 # * These DB_CONFIG files should usually be created after initial
33 # startup because during initial startup, turning off 2SITE_STRICT
34 # on an overloaded or slow machine can cause clients to call their
35 # own elections instead of finding the initial master.
37 # Delays are needed in the test to allow time for replication operations
38 # among different sites:
39 # replication_delay: Time for master transactions to appear on clients,
40 # including group membership operations such as
42 # client_sync_delay: Time for a client to synchronize with a master
43 # after client has been shut down.
44 # election_delay: Time for an election to be held and possibly retried
45 # after master has been shut down.
46 set replication_delay 3000
47 set client_sync_delay 5000
48 set election_delay 12000
51 ## Test cases replication-1.*
52 ## Verify basic replication with a simple 2-site configuration.
56 # Initialize and start replication on master site1.
58 pragma replication_local_site='$site1addr';
60 do_test replication-1.0 {
62 pragma replication_initial_master=on;
63 pragma replication=on;
65 } {1 {Replication started}}
67 # Insert initial data on master.
68 do_test replication-1.1 {
70 create table reptab (a);
71 insert into reptab values (1);
76 # Initialize and start replication on client site2.
78 pragma replication_local_site='$site2addr';
79 pragma replication_remote_site='$site1addr';
81 do_test replication-1.2 {
83 pragma replication=on;
85 } {{Replication started}}
87 after $replication_delay
89 # Make sure client got initial master insert.
90 do_test replication-1.3 {
96 # Make sure client database is readonly.
97 do_test replication-1.4 {
99 insert into reptab values (9999);
101 } {1 {attempt to write a readonly database}}
103 # Insert more data on master.
104 do_test replication-1.5 {
106 insert into reptab values (2);
107 select * from reptab order by a;
110 after $replication_delay
112 # Make sure client got additional master data.
113 do_test replication-1.6 {
115 select * from reptab order by a;
119 # Perform more operations on master.
120 do_test replication-1.7 {
122 insert into reptab values (3);
123 update reptab set a = 999 where a = 1;
124 delete from reptab where a = 2;
125 select * from reptab order by a;
128 after $replication_delay
130 # Make sure client got additional operations.
131 do_test replication-1.8 {
133 select * from reptab order by a;
137 # Remove client site2 from master.
138 do_test replication-1.9 {
140 execsql "pragma replication_remove_site='$site2addr';"
141 } {{Replication site removed}}
142 after $replication_delay
150 ## Test cases replication-2.*
151 ## Verify Use Cases from 5.2 spec.
154 ## Cases 2.0.* test Use Case 1: Distributed Read at 3 Sites.
158 pragma replication_local_site='$site1addr';
159 pragma replication_initial_master=ON;
160 pragma replication=ON;
162 create table university (
172 create table country(
181 pragma replication_local_site='$site2addr';
182 pragma replication_remote_site='$site1addr';
183 pragma replication=ON;
186 pragma replication_local_site='$site3addr';
187 pragma replication_remote_site='$site1addr';
188 pragma replication=ON;
191 do_test replication-2.0.1 {
193 insert into country values ("Greenland","gl", 0, 0, 0, 2);
194 insert into university values (26, "University College London",
195 "ucl.edu", "uk", "Europe", 18, 39, 47, 30);
196 update country set top_1000 = 1 where abbr = "gl";
197 select * from country where abbr = "gl";
199 } {Greenland gl 0 0 0 1}
201 after $replication_delay
203 do_test replication-2.0.2 {
205 select * from university where region = "Europe";
206 select count(*) from country where top_100 > 0;
208 } {26 {University College London} ucl.edu uk Europe 18 39 47 30 0}
210 do_test replication-2.0.3 {
212 insert into country values ("Antarctica","an", 0, 0, 0, 0);
214 } {1 {attempt to write a readonly database}}
220 ## Cases 2.1.* test Use Case 2: 2-site Failover.
224 pragma replication_local_site='$site1addr';
225 pragma replication_initial_master=ON;
226 pragma replication=ON;
227 create table stock_quote (company_name text(40), price real);
230 pragma replication_local_site='$site2addr';
231 pragma replication_remote_site='$site1addr';
232 pragma replication=ON;
235 # Perform some writes on master.
236 do_test replication-2.1.1 {
238 insert into stock_quote values ("General Electric", 20.25);
239 insert into stock_quote values ("Nabisco", 24.75);
240 insert into stock_quote values ("United Healthcare", 31.00);
241 update stock_quote set price=25.25 where company_name = "Nabisco";
245 after $replication_delay
247 # Perform reads on client.
248 do_test replication-2.1.2 {
250 select * from stock_quote where price < 30.00;
251 select price from stock_quote where company_name = "United Healthcare";
253 } {{General Electric} 20.25 Nabisco 25.25 31.0}
255 # Stop the initial master.
256 do_test replication-2.1.3 {
260 after $election_delay
262 # The remaining client does not accept write operations until the other site
263 # rejoins the replication group.
265 do_test replication-2.1.4 {
267 insert into stock_quote values ("Prudential", 17.25);
269 } {1 {attempt to write a readonly database}}
271 # Restart site, will rejoin replication group. The repmgr will be started.
272 sqlite3 db $site1dir/rep.db
274 # Initial query to trigger election and client sync up.
275 execsql { select * from stock_quote where price < 21.00; } db
277 after $election_delay
279 # Assuming the site became master, perform some writes. If it is not the master,
280 # these writes will not succeed and must be performed at the other site.
281 do_test replication-2.1.5 {
283 insert into stock_quote values ("Raytheon", 9.25);
284 insert into stock_quote values ("Cadbury", 7.75);
288 after $replication_delay
290 do_test replication-2.1.6 {
292 select * from stock_quote where price < 21.00;
294 } {{General Electric} 20.25 Raytheon 9.25 Cadbury 7.75}
297 ## Test cases replication-3.*
298 ## Verify replication pragma syntax and usage.
301 ## Cases 3.0.* test replication_local_site by itself and internal host:port
305 do_test replication-3.0.0 {
307 pragma replication_local_site="badvalue";
309 } {1 {Format of value must be host:port}}
311 do_test replication-3.0.1 {
313 pragma replication_local_site="badvalue";
315 } {1 {Format of value must be host:port}}
317 do_test replication-3.0.2 {
319 pragma replication_local_site=":1234";
321 } {1 {Format of value must be host:port}}
323 do_test replication-3.0.3 {
325 pragma replication_local_site="1234";
327 } {1 {Format of value must be host:port}}
329 do_test replication-3.0.4 {
331 pragma replication_local_site=":";
333 } {1 {Format of value must be host:port}}
335 do_test replication-3.0.5 {
337 pragma replication_local_site="testhost:1234";
338 pragma replication_local_site;
340 } {testhost:1234 testhost:1234}
342 ## Cases 3.1.* test replication_initial_master by itself.
344 do_test replication-3.1.0 {
346 pragma replication_initial_master=true;
347 pragma replication_initial_master;
351 do_test replication-3.1.1 {
353 pragma replication_initial_master=false;
354 pragma replication_initial_master;
358 ## Cases 3.2.* test replication_remote_site by itself.
361 do_test replication-3.2.0 {
363 pragma replication_remote_site="badvalue";
365 } {1 {Format of value must be host:port}}
367 do_test replication-3.2.1 {
369 pragma replication_remote_site="testhost2:54321";
370 pragma replication_remote_site;
372 } {testhost2:54321 testhost2:54321}
374 ## Cases 3.3.* test interactions between replication pragmas.
377 do_test replication-3.3.0 {
379 pragma replication=on;
381 } {1 {Must specify local site before starting replication}}
384 pragma replication_local_site='$site1addr';
386 do_test replication-3.3.1 {
388 pragma replication=on;
390 } {1 {Must either be initial master or specify a remote site}}
394 ## Cases 3.4.* test replication_remove_site.
398 pragma replication_local_site='$site1addr';
399 pragma replication_initial_master=ON;
400 pragma replication=ON;
403 pragma replication_local_site='$site2addr';
404 pragma replication_remote_site='$site1addr';
405 pragma replication=ON;
408 pragma replication_local_site='$site3addr';
409 pragma replication_remote_site='$site1addr';
410 pragma replication=ON;
412 after $replication_delay
414 ## Cases 3.4.0.* remove a remote client site.
415 do_test replication-3.4.0 {
416 execsql "pragma replication_remove_site='$site2addr';"
417 } {{Replication site removed}}
419 after $replication_delay
421 ## Cases 3.4.1.* remove local client site.
422 do_test replication-3.4.1 {
424 execsql "pragma replication_remove_site='$site3addr';"
425 } {{Replication site removed}}
427 after $replication_delay
431 ## Cases 3.4.2.* attempt to remove non-existent site (no error).
432 do_test replication-3.4.2 {
434 pragma replication_remove_site="localhost:1234";
436 } {{Replication site removed}}
438 ## Cases 3.4.3.* attempt to remove master site (error).
439 do_test replication-3.4.3 {
440 catchsql "pragma replication_remove_site='$site1addr';"
441 } {1 {Replication site not removed}}
446 pragma replication_local_site='$site1addr';
447 pragma replication_initial_master=ON;
448 pragma replication=ON;
451 pragma replication_local_site='$site2addr';
452 pragma replication_remote_site='$site1addr';
453 pragma replication=ON;
456 ## Cases 3.4.4.* attempt to remove site before group membership database
457 ## is available (no error).
458 do_test replication-3.4.4 {
460 execsql "pragma replication_remove_site='$site2addr';"
461 } {{Replication site removed}}
465 ## Cases 3.4.5.* test removing and then adding a site back into
466 ## the replication group.
467 do_test replication-3.4.5.0 {
468 execsql "pragma replication_remove_site='$site2addr';"
469 } {{Replication site removed}}
471 after $replication_delay
474 create table reptab (a);
475 insert into reptab values (1);
478 after $replication_delay
480 # Removing a site does not necessarily stop all replication immediately,
481 # so reptab may or may not exist on db2.
484 sqlite3 db2 $site2dir/rep.db
486 after $client_sync_delay
489 insert into reptab values (2);
492 after $replication_delay
494 # Make sure db2 rejoined the replication group and is caught up.
495 do_test replication-3.4.5.1 {
497 select * from reptab;
501 ## Cases 3.5.* test replication_verbose_output and replication_verbose_file,
502 ## including the immediate effect of turning them on and off.
503 do_test replication-3.5.0 {
504 execsql { pragma replication_verbose_output=ON; } db
505 execsql { pragma replication_verbose_file=sqloutput; } db
512 do_test replication-3.5.1 {
513 expr [file size sqloutput] > 0
516 ## Cases 3.6.* test changing replication_local_site after replication is
521 pragma replication_local_site='$site1addr';
522 pragma replication_initial_master=ON;
523 pragma replication=ON;
526 do_test replication-3.6 {
528 pragma replication_local_site='$site2addr';
530 } {1 {Cannot change local site after replication is turned on}}
534 ## Cases 3.7.* test running replication pragmas in a database that was
535 ## created without DB_INIT_REP (error).
538 do_test replication-3.7.0 {
539 catchsql "pragma replication_remove_site='$site2addr';"
540 } {1 {Replication site not removed}}
542 execsql { create table reptab (a); } db
547 ## Test cases replication-4.*
548 ## Verify replication startup, shutdown and election scenarios.
551 ## Cases 4.0.* test a 2-site replication group starting up both sites,
552 ## shutting down and restarting the client, and verifying that replication
556 # Initialize and start replication on master site1.
558 pragma replication_local_site='$site1addr';
560 do_test replication-4.0.0 {
562 pragma replication_initial_master=on;
563 pragma replication=on;
565 } {1 {Replication started}}
567 # Initialize and start replication on client site2.
569 pragma replication_local_site='$site2addr';
572 pragma replication_remote_site='$site1addr';
574 do_test replication-4.0.1 {
576 pragma replication=on;
578 } {{Replication started}}
580 # Insert initial data on master.
581 do_test replication-4.0.2 {
583 create table reptab (a);
584 insert into reptab values (1);
585 select * from reptab;
588 after $replication_delay
590 # Make sure client got initial master insert.
591 do_test replication-4.0.3 {
593 select * from reptab;
597 # Shut down and reopen client site.
599 sqlite3 db2 $site2dir/rep.db
601 # Insert more data on master.
602 do_test replication-4.0.4 {
604 insert into reptab values (2);
605 select * from reptab order by a;
609 # Client query to trigger environment open and client synchronization after
611 execsql {select * from reptab order by a;} db2
612 after $client_sync_delay
614 # Make sure client got additional master data.
615 do_test replication-4.0.5 {
617 select * from reptab order by a;
621 # Insert more data on master.
622 do_test replication-4.0.6 {
624 insert into reptab values (3);
625 select * from reptab order by a;
628 after $replication_delay
630 # Make sure client got additional master data.
631 do_test replication-4.0.7 {
633 select * from reptab order by a;
640 ## Cases 4.1.* test a 2-site replication group starting up both sites,
641 ## shutting down first the client then the master and restarting the
642 ## master then the client and verifying that replication continues.
645 # Initialize and start replication on master site1.
647 pragma replication_local_site='$site1addr';
649 do_test replication-4.1.0 {
651 pragma replication_initial_master=on;
652 pragma replication=on;
654 } {1 {Replication started}}
656 # Initialize and start replication on client site2.
658 pragma replication_local_site='$site2addr';
661 pragma replication_remote_site='$site1addr';
663 do_test replication-4.1.1 {
665 pragma replication=on;
667 } {{Replication started}}
669 # Insert initial data on master.
670 do_test replication-4.1.2 {
672 create table reptab (a);
673 insert into reptab values (1);
674 select * from reptab;
677 after $replication_delay
679 # Make sure client got initial master insert.
680 do_test replication-4.1.3 {
682 select * from reptab;
686 # Set site priorities to make later election winner deterministic.
687 # Turn off 2site_strict to enable sites to restart and join repgroup
688 # sequentially after full shutdown, otherwise each initial site will wait
689 # a very long time for its election until a quorum of sites is available.
690 file mkdir $site1dir/rep.db-journal
691 set s1config [open $site1dir/rep.db-journal/DB_CONFIG w]
692 puts $s1config "rep_set_priority 100"
693 puts $s1config "rep_set_config db_repmgr_conf_2site_strict off"
695 file mkdir $site2dir/rep.db-journal
696 set s2config [open $site2dir/rep.db-journal/DB_CONFIG w]
697 puts $s2config "rep_set_priority 50"
698 puts $s2config "rep_set_config db_repmgr_conf_2site_strict off"
701 # Shut down and reopen master and client sites.
704 sqlite3 db $site1dir/rep.db
705 sqlite3 db2 $site2dir/rep.db
707 # Execute queries on each site to trigger environment opens after shutdown.
708 # This will throw the sites into an election.
709 execsql {select * from reptab order by a;} db
710 execsql {select * from reptab order by a;} db2
711 after $election_delay
713 # Insert more data on master.
714 do_test replication-4.1.4 {
716 insert into reptab values (2);
717 select * from reptab order by a;
720 after $replication_delay
722 # Make sure client got additional master data.
723 do_test replication-4.1.5 {
725 select * from reptab order by a;
729 # Insert more data on master.
730 do_test replication-4.1.6 {
732 insert into reptab values (3);
733 select * from reptab order by a;
736 after $replication_delay
738 # Make sure client got additional master data.
739 do_test replication-4.1.7 {
741 select * from reptab order by a;
748 ## Cases 4.2.* test a 2-site replication group starting up both sites,
749 ## shutting down first the master then the client and restarting the
750 ## client then the master and verifying that replication continues.
753 # Initialize and start replication on master site1.
755 pragma replication_local_site='$site1addr';
757 do_test replication-4.2.0 {
759 pragma replication_initial_master=on;
760 pragma replication=on;
762 } {1 {Replication started}}
764 # Initialize and start replication on client site2.
766 pragma replication_local_site='$site2addr';
769 pragma replication_remote_site='$site1addr';
771 do_test replication-4.2.1 {
773 pragma replication=on;
775 } {{Replication started}}
777 # Insert initial data on master.
778 do_test replication-4.2.2 {
780 create table reptab (a);
781 insert into reptab values (1);
782 select * from reptab;
785 after $replication_delay
787 # Make sure client got initial master insert.
788 do_test replication-4.2.3 {
790 select * from reptab;
794 # Set site priorities to make later election winner deterministic.
795 # Turn off 2site_strict to enable sites to restart and join repgroup
796 # sequentially after full shutdown, otherwise each initial site will wait
797 # a very long time for its election until a quorum of sites is available.
798 file mkdir $site1dir/rep.db-journal
799 set s1config [open $site1dir/rep.db-journal/DB_CONFIG w]
800 puts $s1config "rep_set_priority 100"
801 puts $s1config "rep_set_config db_repmgr_conf_2site_strict off"
803 file mkdir $site2dir/rep.db-journal
804 set s2config [open $site2dir/rep.db-journal/DB_CONFIG w]
805 puts $s2config "rep_set_priority 50"
806 puts $s2config "rep_set_config db_repmgr_conf_2site_strict off"
809 # Shut down and reopen master and client sites.
812 sqlite3 db $site1dir/rep.db
813 sqlite3 db2 $site2dir/rep.db
815 # Execute queries on each site to trigger environment opens after shutdown.
816 # This will throw the sites into an election.
817 execsql {select * from reptab order by a;} db
818 execsql {select * from reptab order by a;} db2
819 after $election_delay
821 # Insert more data on master.
822 do_test replication-4.2.4 {
824 insert into reptab values (2);
825 select * from reptab order by a;
828 after $replication_delay
830 # Make sure client got additional master data.
831 do_test replication-4.2.5 {
833 select * from reptab order by a;
837 # Insert more data on master.
838 do_test replication-4.2.6 {
840 insert into reptab values (3);
841 select * from reptab order by a;
844 after $replication_delay
846 # Make sure client got additional master data.
847 do_test replication-4.2.7 {
849 select * from reptab order by a;
856 ## Cases 4.3.* test that a 2-site replication group, using DB_CONFIG to turn
857 ## off the 2SITE_STRICT setting, can shut down the master and have the client
858 ## take over as master. Verify write operations on the new master and then
859 ## have the original master rejoin as a client.
863 pragma replication_local_site='$site1addr';
864 pragma replication_initial_master=ON;
865 pragma replication=ON;
866 create table reptab(a);
869 pragma replication_local_site='$site2addr';
870 pragma replication_remote_site='$site1addr';
871 pragma replication=ON;
874 execsql { insert into reptab values (1); } db
876 after $replication_delay
878 # Set priorities to guarantee initial master will win election when rejoining
880 set s1config [open $site1dir/rep.db-journal/DB_CONFIG w]
881 puts $s1config "rep_set_priority 150"
884 # Shut down master, client won't take over as master.
887 after $election_delay
889 do_test replication-4.3.1 {
891 insert into reptab values (1);
892 select * from reptab order by a;
894 } {1 {attempt to write a readonly database}}
897 sqlite3 db $site1dir/rep.db
899 execsql {select * from reptab order by a;} db
901 after $client_sync_delay
903 # Make sure initial master is still master.
904 do_test replication-4.3.2 {
906 insert into reptab values (2);
907 select * from reptab order by a;
910 after $replication_delay
912 # Turn off 2SITE_STRICT on both sites.
913 set s1config [open $site1dir/rep.db-journal/DB_CONFIG a]
914 puts $s1config "rep_set_config db_repmgr_conf_2site_strict off"
916 set s2config [open $site2dir/rep.db-journal/DB_CONFIG a]
917 puts $s2config "rep_set_config db_repmgr_conf_2site_strict off"
920 # Shut down both sites.
924 # Make sure previous client can now become master.
925 sqlite3 db2 $site2dir/rep.db
927 after $election_delay
929 do_test replication-4.3.3 {
931 insert into reptab values (3);
932 select * from reptab order by a;
938 ## Cases 4.4.* test a 3-site replication group that shuts down the initial
939 ## master, verifies that a client takes over as master and then that the
940 ## original master can rejoin as a client. Use DB_CONFIG files to set
941 ## site priorities to determine which client takes over as master.
945 file mkdir $site1dir/rep.db-journal
946 set s1config [open $site1dir/rep.db-journal/DB_CONFIG w]
947 puts $s1config "rep_set_priority 1"
949 file mkdir $site2dir/rep.db-journal
950 set s2config [open $site2dir/rep.db-journal/DB_CONFIG w]
951 puts $s2config "rep_set_priority 1"
953 file mkdir $site3dir/rep.db-journal
954 set s3config [open $site3dir/rep.db-journal/DB_CONFIG w]
955 puts $s3config "rep_set_priority 100"
959 pragma replication_local_site='$site1addr';
960 pragma replication_initial_master=ON;
961 pragma replication=ON;
962 create table reptab(a);
965 pragma replication_local_site='$site2addr';
966 pragma replication_remote_site='$site1addr';
967 pragma replication=ON;
970 pragma replication_local_site='$site3addr';
971 pragma replication_remote_site='$site1addr';
972 pragma replication=ON;
975 after $client_sync_delay
980 after $election_delay
982 # Site 3 will be elected as master
983 do_test replication-4.4.0 {
985 insert into reptab values (1);
986 select * from reptab order by a;
990 # Restart previous master as client.
991 sqlite3 db $site1dir/rep.db
992 execsql { select * from reptab order by a; } db
993 after $client_sync_delay
995 do_test replication-4.4.1 {
997 insert into reptab values (10);
999 } {1 {attempt to write a readonly database}}
1001 do_test replication-4.4.2 {
1003 select * from reptab order by a;
1012 ## Test cases replication-5.*
1013 ## Verify errors for each SQL operation that can modify the database
1014 ## on a replication client.
1019 pragma replication_local_site='$site1addr';
1020 pragma replication_initial_master=ON;
1021 pragma replication=ON;
1022 create table reptab(a);
1025 pragma replication_local_site='$site2addr';
1026 pragma replication_remote_site='$site1addr';
1027 pragma replication=ON;
1030 after $client_sync_delay
1032 do_test replication-5.0 {
1034 create table reptab_tmp as select a from reptab;
1036 } {1 {attempt to write a readonly database}}
1038 do_test replication-5.1 {
1042 } {1 {attempt to write a readonly database}}
1044 do_test replication-5.2 {
1046 insert into reptab values (1);
1048 } {1 {attempt to write a readonly database}}
1050 do_test replication-5.3 {
1052 create view testview AS select * from reptab;
1054 } {1 {attempt to write a readonly database}}
1056 do_test replication-5.4 {
1058 create index testindex on reptab(a);
1060 } {1 {attempt to write a readonly database}}
1062 do_test replication-5.5 {
1064 CREATE TRIGGER delete_a DELETE ON reptab
1066 INSERT INTO delete_a (a) values (old.a);
1069 } {1 {attempt to write a readonly database}}
1071 do_test replication-5.6 {
1073 delete from reptab where a=1;
1075 } {1 {attempt to write a readonly database}}
1077 do_test replication-5.7 {
1079 update reptab set a=2 where rowid = 1;
1081 } {1 {attempt to write a readonly database}}
1087 ## Test cases replication-6.*
1088 ## Verify adding replication to an existing database and permanently turning
1092 ## Cases 6.0.* test turning replication on in an existing database.
1095 # Create database without replication.
1096 do_test replication-6.0.1 {
1098 create table reptab(a);
1099 insert into reptab values (1);
1100 select * from reptab;
1104 # Turn on replication on existing database.
1106 pragma replication_local_site='$site1addr';
1107 pragma replication_initial_master=ON;
1108 pragma replication=ON;
1113 pragma replication_local_site='$site2addr';
1114 pragma replication_remote_site='$site1addr';
1115 pragma replication=ON;
1118 pragma replication_local_site='$site3addr';
1119 pragma replication_remote_site='$site1addr';
1120 pragma replication=ON;
1123 after $client_sync_delay
1125 # Do more operations on the master and ensure replication is occurring.
1126 do_test replication-6.0.2 {
1128 insert into reptab values (2);
1129 select * from reptab order by a;
1132 after $replication_delay
1133 do_test replication-6.0.3 {
1135 select * from reptab order by a;
1138 do_test replication-6.0.4 {
1140 select * from reptab order by a;
1144 # Remove db2 from replication group.
1145 do_test replication-6.0.5 {
1146 execsql "pragma replication_remove_site='$site2addr';"
1147 } {{Replication site removed}}
1150 # Turn off replication on db2 client.
1151 do_test replication-6.0.6 {
1154 pragma replication=OFF;
1156 } {{Replication stopped}}
1159 # Do more operations on the master and ensure replication is occurring
1160 # only to db3 remaining client.
1161 do_test replication-6.0.7 {
1163 insert into reptab values (3);
1164 select * from reptab order by a;
1167 after $replication_delay
1168 do_test replication-6.0.8 {
1170 select * from reptab order by a;
1173 do_test replication-6.0.9 {
1175 select * from reptab order by a;
1179 # Make sure db2 is no longer a client and can accept new data.
1180 do_test replication-6.0.10 {
1182 insert into reptab values (222);
1183 select * from reptab order by a;
1187 # Make sure db2 is usable after turning off replication and restarting.
1189 sqlite3 db2 $site2dir/rep.db
1190 do_test replication-6.0.11 {
1192 insert into reptab values (223);
1193 select * from reptab order by a;
1201 ## Cases 6.1.* test error paths starting replication on an existing database.
1204 # Create database without replication.
1205 do_test replication-6.1.1 {
1207 create table reptab(a);
1208 insert into reptab values (1);
1209 select * from reptab;
1213 # Test error when starting without a local site.
1214 do_test replication-6.1.2 {
1216 pragma replication=on;
1218 } {1 {Must specify local site before starting replication}}
1220 # Test turning off replication when it isn't already on.
1221 do_test replication-6.1.3 {
1223 pragma replication=off;
1225 } {1 {Replication is not currently running}}
1227 # Set wrong local site address that will be changed before it is used.
1229 pragma replication_local_site='$site2addr';
1232 # Test error when not starting as initial master.
1233 do_test replication-6.1.4 {
1235 pragma replication=on;
1237 } {1 {Must be initial master to start replication on an existing database}}
1239 # Change local site address before replication started.
1241 pragma replication_local_site='$site1addr';
1244 do_test replication-6.1.5 {
1246 pragma replication_initial_master=on;
1247 pragma replication=on;
1249 } {1 {Replication started}}
1251 # Test error turning on replication when it is already on.
1252 do_test replication-6.1.6 {
1254 pragma replication=on;
1256 } {1 {Replication is already running}}
1258 # Test error changing local site address after replication started.
1259 do_test replication-6.1.7 {
1260 catchsql "pragma replication_local_site='$site1addr';"
1261 } {1 {Cannot change local site after replication is turned on}}
1266 ## Test cases replication-7.*
1267 ## Tests for miscellaneous fixes and conditions.
1270 ## Cases 7.0.* test the error path where the initial election for a
1271 ## replication group fails to complete.
1274 # Initialize and start replication on master site1.
1276 pragma replication_local_site='$site1addr';
1278 do_test replication-7.0.0 {
1280 pragma replication_initial_master=on;
1281 pragma replication=on;
1283 } {1 {Replication started}}
1285 # Initialize and start replication on client site2.
1287 pragma replication_local_site='$site2addr';
1290 pragma replication_remote_site='$site1addr';
1292 do_test replication-7.0.1 {
1294 pragma replication=on;
1296 } {{Replication started}}
1298 # Insert initial data on master.
1299 do_test replication-7.0.2 {
1301 create table reptab (a);
1302 insert into reptab values (1);
1303 select * from reptab;
1306 after $replication_delay
1308 # Make sure client got initial master insert.
1309 do_test replication-7.0.3 {
1311 select * from reptab;
1315 # Shut down master and client sites.
1319 # Set tiny values for election timeout and election retry so that election
1320 # takes minimal time to fail.
1321 file mkdir $site1dir/rep.db-journal
1322 set s1config [open $site1dir/rep.db-journal/DB_CONFIG w]
1323 puts $s1config "rep_set_timeout db_rep_election_timeout 1"
1324 puts $s1config "rep_set_timeout db_rep_election_retry 1"
1327 sqlite3 db $site1dir/rep.db
1329 # Redirect to a file the many expected messages from the election attempt.
1330 set outfile $site1dir/bdbrep.out
1331 execsql "pragma bdbsql_error_file='$outfile';"
1333 # Try and fail to elect a new master without a quorum.
1334 do_test replication-7.0.4 {
1335 catchsql { select * from reptab order by a; } db
1336 } {1 {unable to open database file}}
1340 ## Cases 7.1.* test the error path for an unsuccessful insert on a client
1341 ## running alone, and that the master can rejoin the replication group and
1342 ## replication can resume afterwards.
1346 pragma replication_local_site='$site1addr';
1347 pragma replication_initial_master=ON;
1348 pragma replication=ON;
1349 create table reptab(a);
1352 pragma replication_local_site='$site2addr';
1353 pragma replication_remote_site='$site1addr';
1354 pragma replication=ON;
1357 # Shut down master, client cannot take over as master.
1360 after $election_delay
1362 # Try an unsuccessful write operation on the client.
1363 do_test replication-7.1.1 {
1365 insert into reptab values (999);
1367 } {1 {attempt to write a readonly database}}
1369 # Make sure a query on the client doesn't crash here.
1370 do_test replication-7.1.2 {
1372 select * from reptab order by a;
1377 sqlite3 db $site1dir/rep.db
1378 execsql {select * from reptab order by a;} db
1380 after $election_delay
1382 do_test replication-7.1.3 {
1384 insert into reptab values (1);
1385 select * from reptab order by a;
1389 after $replication_delay
1391 do_test replication-7.1.4 {
1393 select * from reptab order by a;