1# Copyright (c) 2014, 2020 Oracle and/or its affiliates.  All rights reserved.
2#
3# See the file LICENSE for license information.
4#
5# $Id$
6#
7# TEST	rep116
8# TEST	Test of instant internal initialization, where internal init is started
9# TEST	instantly if a file delete is found while walking back through the
10# TEST	logs during the verify step.
11# TEST
12# TEST	One master, one client.
13# TEST	Generate several log files.
14# TEST	Remove old master and client log files.
15# TEST	Create a network partition between the master and client,
16# TEST	and restart the client as a master.
17# TEST	Delete a database or blob file on the client, then close the client and
18# TEST	have it rejoin the master.  Assert that the deleted file is present on
19# TEST	the client.
20#
21proc rep116 { method { niter 200 } { tnum "116" } args } {
22
23	source ./include.tcl
24	global databases_in_memory
25	global env_private
26	global repfiles_in_memory
27
28	if { $checking_valid_methods } {
29		return "ALL"
30	}
31
32	set args [convert_args $method $args]
33	set saved_args $args
34
35	set msg "and on-disk replication files"
36	if { $repfiles_in_memory } {
37		set msg "and in-memory replication files"
38	}
39
40	set msg2 ""
41	if { $env_private } {
42		set msg2 "with private env"
43	}
44
45	if { $databases_in_memory } {
46		puts "Skipping rep$tnum for in-memory databases."
47		return
48	}
49
50	# Delete database 1, database 2, a blob file, or nothing
51	set del_opt { "db1" "db2" "blob" "none" }
52
53	foreach r $test_recopts {
54		foreach o $del_opt {
55			set envargs ""
56			set args $saved_args
57			puts "Rep$tnum ($method $envargs $r deleting $o \
58			    $args): Test of internal initialization $msg \
59			    $msg2."
60			rep116_sub $method $niter $tnum $envargs $r $o $args
61		}
62	}
63}
64
65proc rep116_sub { method niter tnum envargs recargs del_opts largs } {
66	global testdir
67	global util_path
68	global env_private
69	global repfiles_in_memory
70	global rep_verbose
71	global verbose_type
72
73	set verbargs ""
74	if { $rep_verbose == 1 } {
75		set verbargs " -verbose {$verbose_type on} "
76	}
77
78	set repmemargs ""
79	if { $repfiles_in_memory } {
80		set repmemargs "-rep_inmem_files "
81	}
82
83	set privargs ""
84	if { $env_private } {
85		set privargs " -private "
86	}
87
88	set blobargs ""
89	set num 10
90    	if { [can_support_blobs $method $largs] == 1 } {
91		set blobargs "-blob_threshold 100"
92	} else {
93		if { $del_opts == "blob" } {
94puts "\tRep$tnum: Skipping blob file delete test, blobs not supported."
95		return
96		}
97		set num 50
98	}
99
100	env_cleanup $testdir
101
102	replsetup $testdir/MSGQUEUEDIR
103
104	set masterdir $testdir/MASTERDIR
105	set clientdir $testdir/CLIENTDIR
106
107	file mkdir $masterdir
108	file mkdir $clientdir
109
110	# Log size is small so we quickly create more than one.
111	# The documentation says that the log file must be at least
112	# four times the size of the in-memory log buffer.
113	set pagesize 4096
114	append largs " -pagesize $pagesize "
115	set log_max [expr $pagesize * 8]
116
117	set file1 "test1.db"
118	set file2 "test2.db"
119
120	# Open a master.
121	puts "\tRep$tnum.a: Opening the master and client."
122	repladd 1
123	set ma_envcmd "berkdb_env_noerr -create -txn $repmemargs \
124	    $privargs -log_max $log_max $envargs $verbargs \
125	    -errpfx MASTER -home $masterdir \
126	    $blobargs -rep_transport \[list 1 replsend\]"
127	set masterenv [eval $ma_envcmd $recargs -rep_master]
128
129	# Open a client
130	repladd 2
131	set cl_envcmd "berkdb_env_noerr -create -txn $repmemargs \
132	    $privargs -log_max $log_max $envargs $verbargs \
133	    -errpfx CLIENT -home $clientdir \
134	    $blobargs -rep_transport \[list 2 replsend\]"
135	set clientenv [eval $cl_envcmd $recargs -rep_client]
136
137	# Bring the clients online by processing the startup messages.
138	set envlist "{$masterenv 1} {$clientenv 2}"
139	process_msgs $envlist
140
141	puts "\tRep$tnum.b: Creating database 1 and database 2"
142	# Create two databases.  db1 does not support blobs, db2 may support
143	# blobs if the environment supports them.  Replicate the new dbs.
144	set omethod [convert_method $method]
145	set oargs [convert_args $method $largs]
146	set oflags " -create -auto_commit -blob_threshold 0 -env \
147	    $masterenv $omethod "
148	set db1 [eval {berkdb_open_noerr} $oflags $oargs $file1]
149	error_check_good db1open [is_valid_db $db1] TRUE
150	eval rep_test $method $masterenv $db1 $num 0 0 0 $largs
151	process_msgs $envlist
152
153	set oflags " -env $masterenv $omethod $blobargs -create -auto_commit "
154	set db2 [eval {berkdb_open_noerr} $oflags $oargs $file2]
155	error_check_good db2open [is_valid_db $db2] TRUE
156	eval rep_test $method $masterenv $db2 $num 0 0 0 $largs
157	process_msgs $envlist
158
159	# Clobber replication's 30-second anti-archive timer, which will have
160	# been started by client sync-up internal init, so that we can do a
161	# log_archive in a moment.
162	#
163	$masterenv test force noarchive_timeout
164
165	puts "\tRep$tnum.c: Run db_archive on master."
166	$masterenv log_flush
167	set res [eval exec $util_path/db_archive -l -h $masterdir]
168	error_check_bad log.1.present [lsearch -exact $res log.0000000001] -1
169	set res [eval exec $util_path/db_archive -d -h $masterdir]
170	set res [eval exec $util_path/db_archive -l -h $masterdir]
171	error_check_good log.1.gone [lsearch -exact $res log.0000000001] -1
172	process_msgs $envlist
173
174	puts "\tRep$tnum.d: Run db_archive on client."
175	$clientenv log_flush
176	set res [eval exec $util_path/db_archive -l -h $clientdir]
177	error_check_bad log.1.present [lsearch -exact $res log.0000000001] -1
178	set res [eval exec $util_path/db_archive -d -h $clientdir]
179	set res [eval exec $util_path/db_archive -l -h $clientdir]
180	error_check_good log.1.gone [lsearch -exact $res log.0000000001] -1
181	process_msgs $envlist
182
183	puts "\tRep$tnum.e: Close the client and reopen as a master."
184	error_check_good clientenv_close [$clientenv close] 0
185	set clientenv [eval $cl_envcmd $recargs -rep_master]
186
187	# Perform the delete on the client.
188	if { $del_opts == "db1" } {
189		puts "\tRep$tnum.f: Client deletes database 1."
190		error_check_good remove_1 \
191		    [eval {$clientenv dbremove -auto_commit} $file1] 0
192	} elseif { $del_opts == "db2" } {
193		puts "\tRep$tnum.f: Client deletes database 2."
194		error_check_good remove_2 \
195		    [eval {$clientenv dbremove -auto_commit} $file2] 0
196	} elseif { $del_opts == "blob" } {
197		puts "\tRep$tnum.f: Client deletes a blob record."
198		# Blobs are inserted at the end, so delete the last record
199		set oflags " -env $clientenv $omethod -auto_commit "
200		set db2_cli [eval {berkdb_open_noerr} $oflags $oargs $file2]
201		set txn [eval $clientenv txn]
202		set dbc [eval $db2_cli cursor -txn $txn]
203		set ret [eval $dbc get -last]
204		error_check_good blob_del [eval $dbc del] 0
205		error_check_good cursor_close [$dbc close] 0
206		error_check_good txn_commit [$txn commit] 0
207		error_check_good close_db2_cli [$db2_cli close] 0
208	} elseif { $del_opts == "none" } {
209		puts "\tRep$tnum.f: Client does nothing."
210	}
211	eval rep_test $method $masterenv $db1 10 0 0 0 $largs
212	$clientenv log_flush
213	$masterenv log_flush
214
215	puts "\tRep$tnum.g: Close the client and master and reopen."
216	error_check_good clientenv_close [$clientenv close] 0
217	error_check_good db1_close [$db1 close] 0
218	error_check_good db2_close [$db2 close] 0
219	error_check_good masterenv_close [$masterenv close] 0
220	# Clear any messages in the message queues
221	replclear 1
222	replclear 2
223
224	set clientenv [eval $cl_envcmd -recover -rep_client]
225	set masterenv [eval $ma_envcmd -recover -rep_master]
226	set envlist "{$masterenv 1} {$clientenv 2}"
227	process_msgs $envlist
228
229	puts "\tRep$tnum.g: Verify the databases and files exist."
230	# Verify the databases are all still there and the same
231	rep_verify $masterdir $masterenv $clientdir $clientenv 0 1 0 $file1
232	rep_verify $masterdir $masterenv $clientdir $clientenv 0 1 0 $file2
233
234	error_check_good masterenv_close [$masterenv close] 0
235	error_check_good clientenv_close [$clientenv close] 0
236	replclose $testdir/MSGQUEUEDIR
237}
238
239