1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2004,2008 Oracle.  All rights reserved.
4#
5# $Id: rep031.tcl,v 12.25 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	rep031
8# TEST	Test of internal initialization and blocked operations.
9# TEST
10# TEST	One master, one client.
11# TEST	Put one more record to the master.
12# TEST  Test that internal initialization blocks:
13# TEST  log_archive, rename, remove, fileid_reset, lsn_reset.
14# TEST	Sleep 30+ seconds.
15# TEST  Test that blocked operations are now unblocked.
16#
17proc rep031 { method { niter 200 } { tnum "031" } args } {
18
19	source ./include.tcl
20	if { $is_windows9x_test == 1 } {
21		puts "Skipping replication test on Win 9x platform."
22		return
23	}
24
25	# There is nothing method-sensitive in this test, so
26	# skip for all except btree.
27	if { $checking_valid_methods } {
28		set test_methods { btree }
29		return $test_methods
30	}
31	if { [is_btree $method] != 1 } {
32		puts "Skipping rep031 for method $method."
33		return
34	}
35
36	set args [convert_args $method $args]
37	set logsets [create_logsets 2]
38
39	# This test needs to set its own pagesize.
40	set pgindex [lsearch -exact $args "-pagesize"]
41        if { $pgindex != -1 } {
42                puts "Rep$tnum: skipping for specific pagesizes"
43                return
44        }
45
46	# Run the body of the test with and without recovery,
47	# and with and without cleaning.  Skip recovery with in-memory
48	# logging - it doesn't make sense.
49	set cleanopts { clean noclean }
50	foreach r $test_recopts {
51		foreach c $cleanopts {
52			foreach l $logsets {
53				set logindex [lsearch -exact $l "in-memory"]
54				if { $r == "-recover" && $logindex != -1 } {
55					puts "Skipping rep$tnum for -recover\
56					    with in-memory logs."
57					continue
58				}
59				puts "Rep$tnum ($method $r $c $args): Test of\
60				    internal init and blocked operations."
61				puts "Rep$tnum: Master logs are [lindex $l 0]"
62				puts "Rep$tnum: Client logs are [lindex $l 1]"
63				rep031_sub $method $niter $tnum $l $r $c $args
64			}
65		}
66	}
67}
68
69proc rep031_sub { method niter tnum logset recargs clean largs } {
70	source ./include.tcl
71	global rep_verbose
72	global verbose_type
73
74	set verbargs ""
75	if { $rep_verbose == 1 } {
76		set verbargs " -verbose {$verbose_type on} "
77	}
78
79	env_cleanup $testdir
80
81	replsetup $testdir/MSGQUEUEDIR
82
83	set masterdir $testdir/MASTERDIR
84	set clientdir $testdir/CLIENTDIR
85
86	file mkdir $masterdir
87	file mkdir $clientdir
88
89	# Log size is small so we quickly create more than one.
90	# The documentation says that the log file must be at least
91	# four times the size of the in-memory log buffer.
92	set pagesize 4096
93	append largs " -pagesize $pagesize "
94	set log_max [expr $pagesize * 8]
95
96	set m_logtype [lindex $logset 0]
97	set c_logtype [lindex $logset 1]
98
99	# In-memory logs cannot be used with -txn nosync.
100	set m_logargs [adjust_logargs $m_logtype]
101	set c_logargs [adjust_logargs $c_logtype]
102	set m_txnargs [adjust_txnargs $m_logtype]
103	set c_txnargs [adjust_txnargs $c_logtype]
104
105	# Open a master.
106	repladd 1
107	set ma_envcmd "berkdb_env_noerr -create $m_txnargs \
108	    $m_logargs -log_max $log_max $verbargs \
109	    -home $masterdir -rep_transport \[list 1 replsend\]"
110	set masterenv [eval $ma_envcmd $recargs -rep_master]
111
112	# Open a client
113	repladd 2
114	set cl_envcmd "berkdb_env_noerr -create $c_txnargs \
115	    $c_logargs -log_max $log_max $verbargs \
116	    -home $clientdir -rep_transport \[list 2 replsend\]"
117	set clientenv [eval $cl_envcmd $recargs -rep_client]
118
119	# Bring the clients online by processing the startup messages.
120	set envlist "{$masterenv 1} {$clientenv 2}"
121	process_msgs $envlist
122
123	$masterenv test force noarchive_timeout
124
125	# Run rep_test in the master (and update client).
126	puts "\tRep$tnum.a: Running rep_test in replicated env."
127	set start 0
128	eval rep_test $method $masterenv NULL $niter $start $start 0 0 $largs
129	incr start $niter
130	process_msgs $envlist
131
132	puts "\tRep$tnum.b: Close client."
133	# Find out what exists on the client.  We need to loop until
134	# the first master log file > last client log file.
135	if { $c_logtype != "in-memory" } {
136		set res [eval exec $util_path/db_archive -l -h $clientdir]
137	}
138	set last_client_log [get_logfile $clientenv last]
139	error_check_good client_close [$clientenv close] 0
140
141	set stop 0
142	while { $stop == 0 } {
143		# Run rep_test in the master (don't update client).
144		puts "\tRep$tnum.c: Running rep_test in replicated env."
145	 	eval rep_test \
146		    $method $masterenv NULL $niter $start $start 0 0 $largs
147		incr start $niter
148		replclear 2
149
150		puts "\tRep$tnum.d: Run db_archive on master."
151		if { $m_logtype != "in-memory"} {
152			set res \
153			    [eval exec $util_path/db_archive -d -h $masterdir]
154		}
155		set first_master_log [get_logfile $masterenv first]
156		if { $first_master_log > $last_client_log } {
157			set stop 1
158		}
159	}
160
161	puts "\tRep$tnum.e: Reopen client ($clean)."
162	if { $clean == "clean" } {
163		env_cleanup $clientdir
164	}
165	set clientenv [eval $cl_envcmd $recargs -rep_client]
166	error_check_good client_env [is_valid_env $clientenv] TRUE
167	set envlist "{$masterenv 1} {$clientenv 2}"
168	process_msgs $envlist 0 NONE err
169	if { $clean == "noclean" } {
170		puts "\tRep$tnum.e.1: Trigger log request"
171		#
172		# When we don't clean, starting the client doesn't
173		# trigger any events.  We need to generate some log
174		# records so that the client requests the missing
175		# logs and that will trigger it.
176		#
177		set entries 10
178		eval rep_test \
179		    $method $masterenv NULL $entries $start $start 0 0 $largs
180		incr start $entries
181		process_msgs $envlist 0 NONE err
182	}
183
184	#
185	# We have now forced an internal initialization.  Verify it is correct.
186	#
187	puts "\tRep$tnum.f: Verify logs and databases"
188	rep_verify $masterdir $masterenv $clientdir $clientenv 1
189
190	#
191	# Internal initializations disable certain operations on the master for
192	# 30 seconds after the last init-related message is received
193	# by the master.  Those operations are dbremove, dbrename and
194	# log_archive (with removal).
195	#
196	puts "\tRep$tnum.g: Try to remove and rename the database."
197
198	set dbname "test.db"
199	set old $dbname
200	set new $dbname.new
201	set stat [catch {$masterenv dbrename -auto_commit $old $new} ret]
202	error_check_good rename_fail $stat 1
203	error_check_good rename_err [is_substr $ret "invalid"] 1
204	set stat [catch {$masterenv dbremove -auto_commit $old} ret]
205	error_check_good remove_fail $stat 1
206	error_check_good remove_err [is_substr $ret "invalid"] 1
207
208	puts "\tRep$tnum.h: Try to reset LSNs and fileid on the database."
209	set stat [catch {$masterenv id_reset $old} ret]
210	error_check_good id_reset $stat 1
211	error_check_good id_err [is_substr $ret "invalid"] 1
212	set stat [catch {$masterenv lsn_reset $old} ret]
213	error_check_good lsn_reset $stat 1
214	error_check_good lsn_err [is_substr $ret "invalid"] 1
215
216	#
217	# Need entries big enough to generate additional log files.
218	# However, db_archive will not return an error, it will
219	# just retain the log file.
220	#
221	puts "\tRep$tnum.i: Run rep_test to generate more logs."
222	set entries 200
223	eval rep_test $method $masterenv NULL $entries $start $start 0 0 $largs
224	incr start $entries
225	process_msgs $envlist 0 NONE err
226
227	# Test lockout of archiving only in on-disk case.
228	if { $m_logtype != "in-memory" } {
229		puts "\tRep$tnum.j: Try to db_archive."
230		set res [eval exec $util_path/db_archive -l -h $masterdir]
231		set first [lindex $res 0]
232		set res [eval exec $util_path/db_archive -d -h $masterdir]
233		set res [eval exec $util_path/db_archive -l -h $masterdir]
234		error_check_bad log.gone [lsearch -exact $res $first] -1
235
236		puts "\tRep$tnum.j.0: Try to log_archive in master env."
237		set res [$masterenv log_archive -arch_remove]
238		set res [eval exec $util_path/db_archive -l -h $masterdir]
239		error_check_bad log.gone0 [lsearch -exact $res $first] -1
240
241		# We can't open a second handle on the env in HP-UX.
242		if { $is_hp_test != 1 } {
243			puts "\tRep$tnum.j.1: Log_archive in new non-rep env."
244			set newenv [berkdb_env_noerr -txn nosync \
245			    -log_max $log_max -home $masterdir]
246			error_check_good newenv [is_valid_env $newenv] TRUE
247			set res [$newenv log_archive -arch_remove]
248			set res [eval exec \
249			    $util_path/db_archive -l -h $masterdir]
250			error_check_bad \
251			    log.gone1 [lsearch -exact $res $first] -1
252		}
253	}
254
255	set timeout 30
256	#
257	# Sleep timeout+2 seconds - The timeout is 60 seconds, but we need
258	# to sleep a bit longer to make sure we cross the timeout.
259	#
260	set to [expr $timeout + 2]
261	puts "\tRep$tnum.k: Wait $to seconds to timeout"
262	tclsleep $to
263	puts "\tRep$tnum.l: Retry blocked operations after wait"
264	set stat [catch {$masterenv id_reset $old} ret]
265	error_check_good id_reset_work $stat 0
266	set stat [catch {$masterenv lsn_reset $old} ret]
267	error_check_good lsn_reset_work $stat 0
268	set stat [catch {$masterenv dbrename -auto_commit $old $new} ret]
269	error_check_good rename_work $stat 0
270	set stat [catch {$masterenv dbremove -auto_commit $new} ret]
271	error_check_good remove_work $stat 0
272	process_msgs $envlist 0 NONE err
273
274	if { $m_logtype != "in-memory" } {
275		# Remove files via the 2nd non-rep env, check via db_archive.
276		if { $is_hp_test != 1 } {
277			set res [$newenv log_archive -arch_remove]
278			set res \
279			    [eval exec $util_path/db_archive -l -h $masterdir]
280			error_check_good \
281			    log.gone [lsearch -exact $res $first] -1
282			error_check_good newenv_close [$newenv close] 0
283		} else {
284			set res [$masterenv log_archive -arch_remove]
285			set res \
286			    [eval exec $util_path/db_archive -l -h $masterdir]
287			error_check_good \
288			    log.gone [lsearch -exact $res $first] -1
289		}
290	}
291
292	error_check_good masterenv_close [$masterenv close] 0
293	error_check_good clientenv_close [$clientenv close] 0
294	replclose $testdir/MSGQUEUEDIR
295}
296