1
# See the file LICENSE for redistribution information.
3
# Copyright (c) 2005, 2010 Oracle and/or its affiliates. All rights reserved.
9
# TEST Replication with versions.
11
# TEST Mimic an application where a database is set up in the
12
# TEST background and then put into a replication group for use.
13
# TEST The "version database" identifies the current live
14
# TEST version, the database against which queries are made.
15
# TEST For example, the version database might say the current
16
# TEST version is 3, and queries would then be sent to db.3.
17
# TEST Version 4 is prepared for use while version 3 is in use.
18
# TEST When version 4 is complete, the version database is updated
19
# TEST to point to version 4 so queries can be directed there.
21
# TEST This test has a master and two clients. One client swaps
22
# TEST roles with the master, and the other client runs constantly
23
# TEST in another process.
25
proc rep045 { method { tnum "045" } args } {
28
global databases_in_memory
29
global repfiles_in_memory
31
# Valid for all access methods.
32
if { $checking_valid_methods } {
36
set args [convert_args $method $args]
37
set logsets [create_logsets 3]
39
# Set up for on-disk or in-memory databases.
40
set msg "using on-disk databases"
41
if { $databases_in_memory } {
42
set msg "using named in-memory databases"
43
if { [is_queueext $method] } {
44
puts -nonewline "Skipping rep$tnum for method "
45
puts "$method with named in-memory databases."
50
set msg2 "and on-disk replication files"
51
if { $repfiles_in_memory } {
52
set msg2 "and in-memory replication files"
56
set logindex [lsearch -exact $l "in-memory"]
57
puts "Rep$tnum ($method): Replication with version\
58
databases $msg $msg2."
59
puts "Rep$tnum: Master logs are [lindex $l 0]"
60
puts "Rep$tnum: Client 0 logs are [lindex $l 1]"
61
puts "Rep$tnum: Client 1 logs are [lindex $l 2]"
62
rep045_sub $method $tnum $l $args
66
proc rep045_sub { method tnum logset largs } {
68
set orig_tdir $testdir
69
global databases_in_memory
70
global repfiles_in_memory
75
if { $rep_verbose == 1 } {
76
set verbargs " -verbose {$verbose_type on} "
80
if { $repfiles_in_memory } {
81
set repmemargs "-rep_inmem_files "
84
set masterdir $testdir/MASTERDIR
85
set clientdir0 $testdir/CLIENTDIR0
86
set clientdir1 $testdir/CLIENTDIR1
89
replsetup $testdir/MSGQUEUEDIR
91
file mkdir $clientdir0
92
file mkdir $clientdir1
94
set m_logtype [lindex $logset 0]
95
set c_logtype [lindex $logset 1]
96
set c2_logtype [lindex $logset 2]
98
# In-memory logs require a large log buffer, and cannot
99
# be used with -txn nosync.
100
set m_logargs [adjust_logargs $m_logtype]
101
set c_logargs [adjust_logargs $c_logtype]
102
set c2_logargs [adjust_logargs $c2_logtype]
103
set m_txnargs [adjust_txnargs $m_logtype]
104
set c_txnargs [adjust_txnargs $c_logtype]
105
set c2_txnargs [adjust_txnargs $c2_logtype]
107
set omethod [convert_method $method]
111
set envcmd(M0) "berkdb_env_noerr -create $m_txnargs \
112
$m_logargs -errpfx ENV.M0 $verbargs $repmemargs \
113
-errfile /dev/stderr -lock_detect default \
114
-home $masterdir -rep_transport \[list 1 replsend\]"
115
set menv [eval $envcmd(M0) -rep_master]
119
set envcmd(C0) "berkdb_env_noerr -create $c_txnargs \
120
$c_logargs -errpfx ENV.C0 $verbargs $repmemargs \
121
-errfile /dev/stderr -lock_detect default \
122
-home $clientdir0 -rep_transport \[list 2 replsend\]"
123
set cenv0 [eval $envcmd(C0) -rep_client]
125
# Open second client.
127
set envcmd(C1) "berkdb_env_noerr -create $c2_txnargs \
128
$c2_logargs -errpfx ENV.C1 $verbargs $repmemargs \
129
-errfile /dev/stderr -lock_detect default \
130
-home $clientdir1 -rep_transport \[list 3 replsend\]"
131
set cenv1 [eval $envcmd(C1) -rep_client]
133
# Bring the clients online by processing the startup messages.
134
set envlist "{$menv 1} {$cenv0 2} {$cenv1 3}"
135
process_msgs $envlist
137
# Clobber replication's 30-second anti-archive timer, which will have
138
# been started by client sync-up internal init, so that we can do a
139
# db_remove in a moment.
141
$menv test force noarchive_timeout
143
puts "\tRep$tnum.a: Initialize version database."
144
# Set up variables so we cycle through version numbers 1
145
# through maxversion several times.
146
if { $databases_in_memory } {
147
set vname { "" "version.db" }
149
set vname "version.db"
157
# The version db is always btree.
158
set vdb [eval {berkdb_open_noerr -env $menv -create \
159
-auto_commit -mode 0644} -btree $vname]
160
error_check_good init_version [$vdb put VERSION $version] 0
161
error_check_good vdb_close [$vdb close] 0
162
process_msgs $envlist
164
# Start up a separate process that constantly reads data
165
# from the current official version.
166
puts "\tRep$tnum.b: Spawn a child tclsh to do client work."
167
set pid [exec $tclsh_path $test_path/wrap.tcl \
168
rep045script.tcl $testdir/rep045script.log \
169
$clientdir1 $vname $databases_in_memory &]
171
# Main loop: update query database, process messages (or don't,
172
# simulating a failure), announce the new version, process
173
# messages (or don't), and swap masters.
175
for { set i 1 } { $i < $iter } { incr i } {
177
# If database.N exists on disk, clean it up.
178
if { $databases_in_memory } {
179
set dbname { "" "db.$version" }
181
set dbname "db.$version"
183
if { [file exists $masterdir/$dbname] == 1 } {
184
puts "\tRep$tnum.c.$i: Removing old version $version."
185
error_check_good dbremove \
186
[$menv dbremove -auto_commit $dbname] 0
189
puts "\tRep$tnum.c.$i: Set up query database $version."
190
set db [eval berkdb_open_noerr -create -env $menv\
191
-auto_commit -mode 0644 $largs $omethod $dbname]
192
error_check_good db_open [is_valid_db $db] TRUE
193
eval rep_test $method $menv $db $nentries $start $start 0 $largs
195
error_check_good db_close [$db close] 0
197
# We alternate between processing the messages and
198
# clearing the messages to simulate a failure.
200
set process [expr $i % 2]
201
if { $process == 1 } {
202
process_msgs $envlist
208
# Announce new version.
209
puts "\tRep$tnum.d.$i: Announce new version $version."
210
set vdb [eval {berkdb_open_noerr -env $menv \
211
-auto_commit -mode 0644} $vname]
212
error_check_good update_version [$vdb put VERSION $version] 0
213
error_check_good vdb_close [$vdb close] 0
215
# Process messages or simulate failure.
216
if { $process == 1 } {
217
process_msgs $envlist
223
# Switch master, update envlist.
224
puts "\tRep$tnum.e.$i: Switch masters."
225
set envlist [switch_master $envlist]
227
# Update values for next iteration.
228
set menv [lindex [lindex $envlist 0] 0]
229
set cenv0 [lindex [lindex $envlist 1] 0]
231
if { $version > $maxversion } {
236
# Signal to child that we are done.
237
set vdb [eval {berkdb_open_noerr -env $menv \
238
-auto_commit -mode 0644} $vname]
239
error_check_good version_done [$vdb put VERSION DONE] 0
240
error_check_good vdb_close [$vdb close] 0
241
process_msgs $envlist
243
# Watch for child to finish.
246
puts "\tRep$tnum.f: Clean up."
247
error_check_good menv_close [$menv close] 0
248
error_check_good cenv0_close [$cenv0 close] 0
249
error_check_good cenv1_close [$cenv1 close] 0
251
replclose $testdir/MSGQUEUEDIR
253
# Check for failures in child's log file.
254
set errstrings [eval findfail $testdir/rep045script.log]
255
foreach str $errstrings {
256
puts "FAIL: error message in log file: $str"
259
set testdir $orig_tdir
263
proc switch_master { envlist } {
264
# Find env handles and machine ids.
265
set menv [lindex [lindex $envlist 0] 0]
266
set mid [lindex [lindex $envlist 0] 1]
267
set cenv [lindex [lindex $envlist 1] 0]
268
set cid [lindex [lindex $envlist 1] 1]
269
set cenv1 [lindex [lindex $envlist 2] 0]
270
set cid1 [lindex [lindex $envlist 2] 1]
272
# Downgrade master, upgrade client.
273
error_check_good master_downgrade [$menv rep_start -client] 0
274
error_check_good client_upgrade [$cenv rep_start -master] 0
275
process_msgs $envlist
277
# Adjust envlist. The former client env is the new master,
279
set newenvlist "{$cenv $cid} {$menv $mid} {$cenv1 $cid1}"