1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2004,2008 Oracle.  All rights reserved.
4#
5# $Id: rep038.tcl,v 12.23 2008/04/16 13:52:52 sue Exp $
6#
7# TEST	rep038
8# TEST	Test of internal initialization and ongoing master updates.
9# TEST
10# TEST	One master, one client.
11# TEST	Generate several log files.
12# TEST	Remove old master log files.
13# TEST	Delete client files and restart client.
14# TEST	Put more records on master while initialization is in progress.
15#
16proc rep038 { method { niter 200 } { tnum "038" } args } {
17
18	source ./include.tcl
19	if { $is_windows9x_test == 1 } {
20		puts "Skipping replication test on Win 9x platform."
21		return
22	}
23
24	# Valid for all access methods.
25	if { $checking_valid_methods } {
26		return "ALL"
27	}
28
29	set args [convert_args $method $args]
30
31	# This test needs to set its own pagesize.
32	set pgindex [lsearch -exact $args "-pagesize"]
33        if { $pgindex != -1 } {
34                puts "Rep$tnum: skipping for specific pagesizes"
35                return
36        }
37
38	set logsets [create_logsets 2]
39
40	# Run the body of the test with and without recovery,
41	# and with various options, such as in-memory databases,
42	# forcing an archive during the middle of init, and normal.
43	# Skip recovery with in-memory logging - it doesn't make sense.
44	set testopts { in-memdb normal archive }
45	foreach r $test_recopts {
46		foreach t $testopts {
47			foreach l $logsets {
48				set logindex [lsearch -exact $l "in-memory"]
49				if { $t == "in-memdb" && \
50				    [is_queueext $method]} {
51					puts "Skipping rep$tnum for queueext\
52					    with in-memory databases."
53					continue
54				}
55				if { $r == "-recover" && $logindex != -1 } {
56					puts "Skipping rep$tnum for -recover\
57					    with in-memory logs."
58					continue
59				}
60				puts "Rep$tnum ($method $t $r $args):\
61				    Test of internal init with new records."
62				puts "Rep$tnum: Master logs are [lindex $l 0]"
63				puts "Rep$tnum: Client logs are [lindex $l 1]"
64				rep038_sub $method $niter $tnum $l $r $t $args
65			}
66		}
67	}
68}
69
70proc rep038_sub { method niter tnum logset recargs testopt largs } {
71	global testdir
72	global util_path
73	global rep_verbose
74	global verbose_type
75
76	set verbargs ""
77	if { $rep_verbose == 1 } {
78		set verbargs " -verbose {$verbose_type on} "
79	}
80
81	env_cleanup $testdir
82
83	replsetup $testdir/MSGQUEUEDIR
84
85	set masterdir $testdir/MASTERDIR
86	set clientdir $testdir/CLIENTDIR
87
88	file mkdir $masterdir
89	file mkdir $clientdir
90
91	# Log size is small so we quickly create more than one.
92	# The documentation says that the log file must be at least
93	# four times the size of the in-memory log buffer.
94	set pagesize 4096
95	append largs " -pagesize $pagesize "
96	set log_max [expr $pagesize * 8]
97
98	set m_logtype [lindex $logset 0]
99	set c_logtype [lindex $logset 1]
100
101	# In-memory logs cannot be used with -txn nosync.
102	set m_logargs [adjust_logargs $m_logtype]
103	set c_logargs [adjust_logargs $c_logtype]
104	set m_txnargs [adjust_txnargs $m_logtype]
105	set c_txnargs [adjust_txnargs $c_logtype]
106
107	# Open a master.
108	repladd 1
109	set ma_envcmd "berkdb_env_noerr -create $m_txnargs \
110	    $m_logargs -log_max $log_max -errpfx MASTER $verbargs \
111	    -home $masterdir -rep_transport \[list 1 replsend\]"
112	set masterenv [eval $ma_envcmd $recargs -rep_master]
113	$masterenv rep_limit 0 0
114
115	# Run rep_test in the master only.
116	puts "\tRep$tnum.a: Running rep_test in replicated env."
117	set start 0
118	if { $testopt == "in-memdb" } {
119		set testfile { "" "test.db" }
120	} else {
121		set testfile "test.db"
122	}
123	set omethod [convert_method $method]
124	set dbargs [convert_args $method $largs]
125	set mdb [eval {berkdb_open} -env $masterenv -auto_commit\
126          	-create -mode 0644 $omethod $dbargs $testfile ]
127	error_check_good reptest_db [is_valid_db $mdb] TRUE
128
129	set stop 0
130	while { $stop == 0 } {
131		# Run rep_test in the master beyond the first log file.
132		eval rep_test\
133		    $method $masterenv $mdb $niter $start $start
134		incr start $niter
135
136		puts "\tRep$tnum.a.1: Run db_archive on master."
137		if { $m_logtype == "on-disk" } {
138			set res \
139			    [eval exec $util_path/db_archive -d -h $masterdir]
140		}
141		#
142		# Make sure we have moved beyond the first log file.
143		#
144		set first_master_log [get_logfile $masterenv first]
145		if { $first_master_log > 1 } {
146			set stop 1
147		}
148
149	}
150
151	puts "\tRep$tnum.b: Open client."
152	repladd 2
153	set cl_envcmd "berkdb_env_noerr -create $c_txnargs \
154	    $c_logargs -log_max $log_max -errpfx CLIENT $verbargs \
155	    -home $clientdir -rep_transport \[list 2 replsend\]"
156	set clientenv [eval $cl_envcmd $recargs -rep_client]
157	$clientenv rep_limit 0 0
158	set envlist "{$masterenv 1} {$clientenv 2}"
159	#
160	# We want to simulate a master continually getting new
161	# records while an update is going on.  Simulate that
162	# for several iterations and then let the messages finish
163	# all their processing.
164	#
165	set loop 10
166	set i 0
167	set entries 100
168	set archived 0
169	set start $niter
170	set init 0
171	while { $i < $loop } {
172		set nproced 0
173		set start [expr $start + $entries]
174		eval rep_test \
175		    $method $masterenv $mdb $entries $start $start
176		incr start $entries
177		incr nproced [proc_msgs_once $envlist NONE err]
178		error_check_bad nproced $nproced 0
179		#
180		# If we are testing archiving, we need to make sure that
181		# the first_lsn for internal init (the last log file we
182		# have when we first enter init) is no longer available.
183		# So, the first time through we record init_log, and then
184		# on subsequent iterations we'll wait for the last log
185		# to move further.  Force a checkpoint and archive.
186		#
187		if { $testopt == "archive" && $archived == 0 } {
188			set clstat [exec $util_path/db_stat \
189			    -N -r -R A -h $clientdir]
190			if { $init == 0 && \
191			    [is_substr $clstat "REP_F_RECOVER_PAGE"] } {
192				set init_log [get_logfile $masterenv last]
193				set init 1
194			}
195			if { $init == 0 && \
196			    [is_substr $clstat "REP_F_RECOVER_LOG"] } {
197				set init_log [get_logfile $masterenv last]
198				set init 1
199			}
200			set last_master_log [get_logfile $masterenv last]
201			set first_master_log [get_logfile $masterenv first]
202			if { $init && $m_logtype == "on-disk" && \
203			    $last_master_log > $init_log } {
204				$masterenv txn_checkpoint -force
205				$masterenv test force noarchive_timeout
206				set res [eval exec  $util_path/db_archive \
207				    -d -h $masterdir]
208				set newlog [get_logfile $masterenv first]
209				set archived 1
210				error_check_good logs \
211				    [expr $newlog > $init_log] 1
212			} elseif { $init && $m_logtype == "in-memory" && \
213			    $first_master_log > $init_log } {
214				$masterenv txn_checkpoint -force
215				$masterenv test force noarchive_timeout
216				set archived 1
217			}
218		}
219		incr i
220	}
221	set cdb [eval {berkdb_open_noerr} -env $clientenv -auto_commit\
222	    -create -mode 0644 $omethod $dbargs $testfile]
223	error_check_good reptest_db [is_valid_db $cdb] TRUE
224	process_msgs $envlist
225
226	puts "\tRep$tnum.c: Verify logs and databases"
227	if { $testopt == "in-memdb" } {
228		rep038_verify_inmem $masterenv $clientenv $mdb $cdb
229	} else {
230		rep_verify $masterdir $masterenv $clientdir $clientenv 1
231	}
232
233	# Add records to the master and update client.
234	puts "\tRep$tnum.d: Add more records and check again."
235	eval rep_test $method $masterenv $mdb $entries $start $start
236	incr start $entries
237	process_msgs $envlist 0 NONE err
238	if { $testopt == "in-memdb" } {
239		rep038_verify_inmem $masterenv $clientenv $mdb $cdb
240	} else {
241		rep_verify $masterdir $masterenv $clientdir $clientenv 1
242	}
243
244	# Make sure log file are on-disk or not as expected.
245	check_log_location $masterenv
246	check_log_location $clientenv
247
248	error_check_good mdb_close [$mdb close] 0
249	error_check_good cdb_close [$cdb close] 0
250	error_check_good masterenv_close [$masterenv close] 0
251	error_check_good clientenv_close [$clientenv close] 0
252	replclose $testdir/MSGQUEUEDIR
253}
254
255proc rep038_verify_inmem { masterenv clientenv mdb cdb } {
256	#
257	# Can't use rep_verify to compare the logs because each
258	# commit record from db_printlog shows the database name
259	# as text on the master and as the file uid on the client
260	# because the client cannot find the "file".
261	#
262	# !!! Check the LSN first.  Otherwise the DB->stat for the
263	# number of records will write a log record on the master if
264	# the build is configured for debug_rop.  Work around that issue.
265	#
266	set mlsn [stat_field $masterenv rep_stat "Next LSN expected"]
267	set clsn [stat_field $clientenv rep_stat "Next LSN expected"]
268	error_check_good lsn $mlsn $clsn
269
270	set mrecs [stat_field $mdb stat "Number of records"]
271	set crecs [stat_field $cdb stat "Number of records"]
272	error_check_good recs $mrecs $crecs
273}
274