1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2007-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	rep076
8# TEST	Replication elections - what happens if elected client
9# TEST  does not become master?
10# TEST
11# TEST	Set up a master and 3 clients.  Take down master, run election.
12# TEST	The elected client will ignore the fact that it's been elected,
13# TEST	so we still have 2 clients.
14# TEST
15# TEST	Run another election, a regular election that allows the winner
16# TEST	to become master, and make sure it goes okay.  We do this both
17# TEST	for the client that ignored its election and for the other client.
18# TEST
19# TEST	This simulates what would happen if, say, we had a temporary
20# TEST	network partition and lost the winner.
21#
22proc rep076 { method args } {
23	source ./include.tcl
24
25	global mixed_mode_logging
26	global databases_in_memory
27	global repfiles_in_memory
28
29	set tnum "076"
30	if { $is_windows9x_test == 1 } {
31		puts "Skipping replication test on Win 9x platform."
32		return
33	}
34
35	# Run for btree only.
36	if { $checking_valid_methods } {
37		set test_methods { btree }
38		return $test_methods
39	}
40	if { [is_btree $method] == 0 } {
41		puts "Rep$tnum: Skipping for method $method."
42		return
43	}
44
45	# Set up for on-disk or in-memory databases.
46	set msg "using on-disk databases"
47	if { $databases_in_memory } {
48		set msg "using named in-memory databases"
49		if { [is_queueext $method] } {
50			puts -nonewline "Skipping rep$tnum for method "
51			puts "$method with named in-memory databases."
52			return
53		}
54	}
55
56	set msg2 "and on-disk replication files"
57	if { $repfiles_in_memory } {
58		set msg2 "and in-memory replication files"
59	}
60
61	set nclients 3
62	set logsets [create_logsets [expr $nclients + 1]]
63	set winsets { { 1 1 } { 1 2 } }
64	foreach l $logsets {
65		foreach w $winsets {
66			puts "Rep$tnum ($method): Replication elections -\
67			    elected client ignores election $msg $msg2."
68			puts "Rep$tnum: Master logs are [lindex $l 0]"
69			for { set i 0 } { $i < $nclients } { incr i } {
70				puts "Rep$tnum: Client $i logs are\
71				    [lindex $l [expr $i + 1]]"
72			}
73			rep076_sub $method $nclients $tnum $l $w $args
74		}
75	}
76}
77
78proc rep076_sub { method nclients tnum logset winset largs } {
79	source ./include.tcl
80	global machids
81	global databases_in_memory
82	global repfiles_in_memory
83	global rep_verbose
84	global verbose_type
85
86	set verbargs ""
87	if { $rep_verbose == 1 } {
88		set verbargs " -verbose {$verbose_type on} "
89	}
90
91	set repmemargs ""
92	if { $repfiles_in_memory } {
93		set repmemargs "-rep_inmem_files "
94	}
95
96	env_cleanup $testdir
97
98	set qdir $testdir/MSGQUEUEDIR
99	replsetup $qdir
100
101	set masterdir $testdir/MASTERDIR
102	file mkdir $masterdir
103	set m_logtype [lindex $logset 0]
104	set m_logargs [adjust_logargs $m_logtype]
105	set m_txnargs [adjust_txnargs $m_logtype]
106
107	for { set i 0 } { $i < $nclients } { incr i } {
108		set clientdir($i) $testdir/CLIENTDIR.$i
109		file mkdir $clientdir($i)
110		set c_logtype($i) [lindex $logset [expr $i + 1]]
111		set c_logargs($i) [adjust_logargs $c_logtype($i)]
112		set c_txnargs($i) [adjust_txnargs $c_logtype($i)]
113	}
114
115	# Open a master.
116	set envlist {}
117	repladd 1
118	set env_cmd(M) "berkdb_env -create -log_max 1000000 $verbargs \
119	    -event rep_event $repmemargs \
120	    -home $masterdir $m_txnargs $m_logargs -rep_master \
121	    -errpfx MASTER -rep_transport \[list 1 replsend\]"
122	set masterenv [eval $env_cmd(M)]
123	lappend envlist "$masterenv 1"
124
125	# Open the clients.
126	for { set i 0 } { $i < $nclients } { incr i } {
127		set envid [expr $i + 2]
128		repladd $envid
129		set env_cmd($i) "berkdb_env_noerr -create $verbargs \
130		    -event rep_event $repmemargs \
131		    -home $clientdir($i) $c_txnargs($i) $c_logargs($i) \
132		    -rep_client -rep_transport \[list $envid replsend\]"
133		set clientenv($i) [eval $env_cmd($i)]
134		error_check_good \
135		    client_env($i) [is_valid_env $clientenv($i)] TRUE
136		lappend envlist "$clientenv($i) $envid"
137	}
138
139	# Bring the clients online by processing the startup messages.
140	process_msgs $envlist
141
142	# Run a modified test001 in the master.
143	puts "\tRep$tnum.a: Running rep_test in replicated env."
144	set niter 10
145	eval rep_test $method $masterenv NULL $niter 0 0 0 $largs
146	process_msgs $envlist
147
148	# Close master.
149	error_check_good masterenv_close [$masterenv close] 0
150	set envlist [lreplace $envlist 0 0]
151
152	# Winner1 is set up to win the first election, winner2
153	# the second.
154	set m "Rep$tnum.b"
155	set winner1 [lindex $winset 0]
156	set winner2 [lindex $winset 1]
157	set elector 1
158	set nsites $nclients
159	set nvotes $nclients
160	setpriority pri $nclients $winner1
161	if { $databases_in_memory } {
162		set dbname { "" "test.db" }
163	} else {
164		set dbname "test.db"
165	}
166
167	foreach pair $envlist {
168		set i [expr [lindex $pair 1] - 2]
169		replclear [expr $i + 2]
170		set err_cmd($i) "none"
171		set crash($i) 0
172		if { $rep_verbose == 1 } {
173			$clientenv($i) errpfx CLIENT$i
174			$clientenv($i) verbose $verbose_type on
175			$clientenv($i) errfile /dev/stderr
176			set env_cmd($i) [concat $env_cmd($i) \
177			    "-errpfx CLIENT$i -errfile /dev/stderr"]
178		}
179	}
180
181
182	# Run election where winner will ignore its election and
183	# not be made master.
184	puts "\tRep$tnum: First winner ignores its election."
185	run_election env_cmd envlist err_cmd pri crash $qdir $m\
186	    $elector $nsites $nvotes $nclients $winner1 0 $dbname 1
187
188	# Run second election where winner accepts its election and
189	# is made master.
190	puts "\tRep$tnum: Second winner accepts its election."
191	setpriority pri $nclients $winner2
192	run_election env_cmd envlist err_cmd pri crash $qdir $m\
193	    $elector $nsites $nvotes $nclients $winner2 0 $dbname
194
195	# Clean up.
196	foreach pair $envlist {
197		set cenv [lindex $pair 0]
198		error_check_good cenv_close [$cenv close] 0
199	}
200
201	replclose $testdir/MSGQUEUEDIR
202}
203
204