1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2001,2008 Oracle.  All rights reserved.
4#
5# $Id: rep019.tcl,v 12.16 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST  rep019
8# TEST	Replication and multiple clients at same LSN.
9# TEST  Have several clients at the same LSN.  Run recovery at
10# TEST  different times.  Declare a client master and after sync-up
11# TEST  verify all client logs are identical.
12#
13proc rep019 { method { nclients 3 } { tnum "019" } args } {
14
15	source ./include.tcl
16	if { $is_windows9x_test == 1 } {
17		puts "Skipping replication test on Win 9x platform."
18		return
19	}
20
21	# Run for all access methods.
22	if { $checking_valid_methods } {
23		return "ALL"
24	}
25
26	# This test needs to use recovery, so mixed-mode testing
27	# isn't appropriate.
28	global mixed_mode_logging
29	if { $mixed_mode_logging > 0 } {
30		puts "Rep$tnum: Skipping for mixed-mode logging."
31		return
32	}
33	set args [convert_args $method $args]
34
35	# Run the body of the test with and without recovery.
36	foreach r $test_recopts {
37		puts "Rep$tnum ($method $r):\
38		    Replication and $nclients recovered clients in sync."
39		rep019_sub $method $nclients $tnum $r $args
40	}
41}
42
43proc rep019_sub { method nclients tnum recargs largs } {
44	global testdir
45	global util_path
46	global rep_verbose
47	global verbose_type
48
49	set verbargs ""
50	if { $rep_verbose == 1 } {
51		set verbargs " -verbose {$verbose_type on} "
52	}
53
54	set orig_tdir $testdir
55	env_cleanup $testdir
56
57	replsetup $testdir/MSGQUEUEDIR
58
59	set niter 100
60	set masterdir $testdir/MASTERDIR
61	file mkdir $masterdir
62
63	# Open a master.
64	repladd 1
65	set ma_envcmd "berkdb_env_noerr -create -txn nosync $verbargs \
66	    -home $masterdir -rep_master -errpfx MASTER \
67	    -rep_transport \[list 1 replsend\]"
68	set menv [eval $ma_envcmd $recargs]
69
70	for {set i 0} {$i < $nclients} {incr i} {
71		set clientdir($i) $testdir/CLIENTDIR.$i
72		file mkdir $clientdir($i)
73		set id($i) [expr 2 + $i]
74		repladd $id($i)
75		set cl_envcmd($i) "berkdb_env_noerr -create -txn nosync \
76		    -home $clientdir($i) $verbargs -errpfx CLIENT.$i \
77		    -rep_client -rep_transport \[list $id($i) replsend\]"
78		set clenv($i) [eval $cl_envcmd($i) $recargs]
79		error_check_good client_env [is_valid_env $clenv($i)] TRUE
80	}
81	set testfile "test$tnum.db"
82	set omethod [convert_method $method]
83	set masterdb [eval {berkdb_open_noerr -env $menv -auto_commit \
84	    -create -mode 0644} $largs $omethod $testfile]
85	error_check_good dbopen [is_valid_db $masterdb] TRUE
86
87	# Bring the clients online by processing the startup messages.
88	set envlist {}
89	lappend envlist "$menv 1"
90	for { set i 0 } { $i < $nclients } { incr i } {
91		lappend envlist "$clenv($i) $id($i)"
92	}
93	process_msgs $envlist
94
95	# Run a modified test001 in the master (and update clients).
96	puts "\tRep$tnum.a: Running test001 in replicated env."
97	eval rep_test $method $menv $masterdb $niter 0 0 0 0 $largs
98	process_msgs $envlist
99
100	error_check_good mdb_cl [$masterdb close] 0
101	# Process any close messages.
102	process_msgs $envlist
103
104	error_check_good menv_cl [$menv close] 0
105	puts "\tRep$tnum.b: Close all envs and run recovery in clients."
106	for {set i 0} {$i < $nclients} {incr i} {
107		error_check_good cl$i.close [$clenv($i) close] 0
108		set hargs($i) "-h $clientdir($i)"
109	}
110	foreach sleep {2 1 0} {
111		for {set i 0} {$i < $nclients} {incr i} {
112			set stat [catch {eval exec $util_path/db_recover \
113			    $hargs($i)} result]
114			error_check_good stat $stat 0
115			#
116			# Need to sleep to make sure recovery's checkpoint
117			# records have different timestamps.
118			tclsleep $sleep
119		}
120	}
121
122	puts "\tRep$tnum.c: Reopen clients and declare one master."
123	for {set i 0} {$i < $nclients} {incr i} {
124		set clenv($i) [eval $cl_envcmd($i) $recargs]
125		error_check_good client_env [is_valid_env $clenv($i)] TRUE
126	}
127	error_check_good master0 [$clenv(0) rep_start -master] 0
128
129	puts "\tRep$tnum.d: Sync up with other clients."
130	while { 1 } {
131		set nproced 0
132
133		for {set i 0} {$i < $nclients} {incr i} {
134			incr nproced [replprocessqueue $clenv($i) $id($i)]
135		}
136
137		if { $nproced == 0 } {
138			break
139		}
140	}
141	puts "\tRep$tnum.e: Verify client logs match."
142	set i 0
143	error_check_good cl$i.close [$clenv($i) close] 0
144	set stat [catch {eval exec $util_path/db_printlog \
145	    $hargs($i) >& $clientdir($i)/prlog} result]
146	#
147	# Note we start the loop at 1 here and compare against client0
148	# which became the master.
149	#
150	for {set i 1} {$i < $nclients} {incr i} {
151		error_check_good cl$i.close [$clenv($i) close] 0
152		fileremove -f $clientdir($i)/prlog
153		set stat [catch {eval exec $util_path/db_printlog \
154		    $hargs($i) >> $clientdir($i)/prlog} result]
155		error_check_good stat_prlog $stat 0
156		error_check_good log_cmp(0,$i) \
157		    [filecmp $clientdir(0)/prlog $clientdir($i)/prlog] 0
158	}
159
160	replclose $testdir/MSGQUEUEDIR
161	set testdir $orig_tdir
162	return
163}
164
165