summaryrefslogtreecommitdiff
path: root/db/test/rep010.tcl
blob: 99563ab88b4c3408b6da25ad7cd580e4f4b00ca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# See the file LICENSE for redistribution information.
#
# Copyright (c) 2003,2007 Oracle.  All rights reserved.
#
# $Id: rep010.tcl,v 12.14 2007/05/17 18:17:21 bostic Exp $
#
# TEST  rep010
# TEST	Replication and ISPERM
# TEST
# TEST	With consecutive message processing, make sure every
# TEST	DB_REP_PERMANENT is responded to with an ISPERM when
# TEST	processed.  With gaps in the processing, make sure
# TEST	every DB_REP_PERMANENT is responded to with an ISPERM
# TEST	or a NOTPERM.  Verify in both cases that the LSN returned
# TEST	with ISPERM is found in the log.
proc rep010 { method { niter 100 } { tnum "010" } args } {

	source ./include.tcl
	if { $is_windows9x_test == 1 } {
		puts "Skipping replication test on Win 9x platform."
		return
	}

	# Run for all access methods.
	if { $checking_valid_methods } {
		return "ALL"
	}

	set args [convert_args $method $args]
	set logsets [create_logsets 2]

	# Run the body of the test with and without recovery.
	foreach r $test_recopts {
		foreach l $logsets {
			set logindex [lsearch -exact $l "in-memory"]
			if { $r == "-recover" && $logindex != -1 } {
				puts "Rep$tnum: Skipping\
				    for in-memory logs with -recover."
				continue
			}
			puts "Rep$tnum ($method $r): Replication and ISPERM."
			puts "Rep$tnum: Master logs are [lindex $l 0]"
			puts "Rep$tnum: Client logs are [lindex $l 1]"
			rep010_sub $method $niter $tnum $l $r $args
		}
	}
}

proc rep010_sub { method niter tnum logset recargs largs } {
	source ./include.tcl
	global rand_init
	berkdb srand $rand_init
	global perm_sent_list
	global rep_verbose

	set verbargs ""
	if { $rep_verbose == 1 } {
		set verbargs " -verbose {rep on} "
	}

	env_cleanup $testdir
	set omethod [convert_method $method]

	replsetup $testdir/MSGQUEUEDIR
	set perm_sent_list {{}}

	set masterdir $testdir/MASTERDIR
	set clientdir $testdir/CLIENTDIR

	file mkdir $masterdir
	file mkdir $clientdir

	set m_logtype [lindex $logset 0]
	set c_logtype [lindex $logset 1]

	# In-memory logs require a large log buffer, and cannot
	# be used with -txn nosync.
	set m_logargs [adjust_logargs $m_logtype]
	set c_logargs [adjust_logargs $c_logtype]
	set m_txnargs [adjust_txnargs $m_logtype]
	set c_txnargs [adjust_txnargs $c_logtype]

	# Open a master.
	repladd 1
	set env_cmd(M) "berkdb_env_noerr -create -log_max 1000000 \
	    $m_logargs $verbargs -errpfx MASTER \
	    -home $masterdir $m_txnargs -rep_master \
	    -rep_transport \[list 1 replsend\]"
	set masterenv [eval $env_cmd(M) $recargs]

	# Open a client
	repladd 2
	set env_cmd(C) "berkdb_env_noerr -create -home $clientdir \
	    $c_txnargs $c_logargs $verbargs -rep_client -errpfx CLIENT \
	    -rep_transport \[list 2 replsend\]"
	set clientenv [eval $env_cmd(C) $recargs]

	# Bring the client online.  Since that now involves internal init, we
	# have to avoid the special rep010_process_msgs here, because otherwise
	# we would hang trying to open a log cursor.
	#
	process_msgs "{$masterenv 1} {$clientenv 2}"

	# Open database in master, propagate to client.
	set dbname rep010.db
	set db1 [eval {berkdb_open_noerr -create} $omethod -auto_commit \
	    -env $masterenv $largs $dbname]
	rep010_process_msgs $masterenv $clientenv 1

	puts "\tRep$tnum.a: Process messages with no gaps."
	# Feed operations one at a time to master and immediately
	# update client.
	for { set i 1 } { $i <= $niter } { incr i } {
		set t [$masterenv txn]
		error_check_good db_put \
		    [eval $db1 put -txn $t $i [chop_data $method data$i]] 0
		error_check_good txn_commit [$t commit] 0
		rep010_process_msgs $masterenv $clientenv 1
	}

	# Replace data.
	for { set i 1 } { $i <= $niter } { incr i } {
		set t [$masterenv txn]
		set ret \
		    [$db1 get -get_both -txn $t $i [pad_data $method data$i]]
		error_check_good db_put \
		    [$db1 put -txn $t $i [chop_data $method newdata$i]] 0
		error_check_good txn_commit [$t commit] 0
		rep010_process_msgs $masterenv $clientenv 1
	}

	# Try some aborts.  These do not write permanent messages.
	for { set i 1 } { $i <= $niter } { incr i } {
		set t [$masterenv txn]
		error_check_good db_put [$db1 put -txn $t $i abort$i] 0
		error_check_good txn_abort [$t abort] 0
		rep010_process_msgs $masterenv $clientenv 0
	}

	puts "\tRep$tnum.b: Process messages with gaps."
	# To test gaps in message processing, run and commit a whole
	# bunch of transactions, then process the messages with skips.
	for { set i 1 } { $i <= $niter } { incr i } {
		set t [$masterenv txn]
		error_check_good db_put [$db1 put -txn $t $i data$i] 0
		error_check_good txn_commit [$t commit] 0
	}
	set skip [berkdb random_int 2 8]
	rep010_process_msgs $masterenv $clientenv 1 $skip

	# Clean up.
	error_check_good db1_close [$db1 close] 0
	error_check_good masterenv_close [$masterenv close] 0
	error_check_good clientenv_close [$clientenv close] 0

	replclose $testdir/MSGQUEUEDIR
}

proc rep010_process_msgs { masterenv clientenv check {skip_interval 0} } {
	global perm_response_list
	global perm_sent_list

	set perm_response_list {{}}

	while { 1 } {
		set nproced 0

		incr nproced [replprocessqueue $masterenv 1 $skip_interval]
		incr nproced [replprocessqueue $clientenv 2 $skip_interval]

		# In this test, the ISPERM and NOTPERM messages are
		# sent by the client back to the master.  Verify that we
		# get ISPERM when the client is caught up to the master
		# (i.e. last client LSN in the log matches the LSN returned
		# with the ISPERM), and that when we get NOTPERM, the client
		# is not caught up.

		# Create a list of the LSNs in the client log.
		set lsnlist {}
		set logc [$clientenv log_cursor]
		error_check_good logc \
		    [is_valid_logc $logc $clientenv] TRUE
		for { set logrec [$logc get -first] } \
		    { [llength $logrec] != 0 } \
		    { set logrec [$logc get -next] } {
			lappend lsnlist [lindex [lindex $logrec 0] 1]
		}
		set lastloglsn [lindex $lsnlist end]

		# Parse perm_response_list to find the LSN returned with
		# ISPERM or NOTPERM.
		set response [lindex $perm_response_list end]
		set permtype [lindex $response 0]
		set messagelsn [lindex [lindex $response 1] 1]

		if { [llength $response] != 0 } {
			if { $permtype == "NOTPERM" } {
				# If we got a NOTPERM, the returned LSN has to
				# be greater than the last LSN in the log.
				error_check_good notpermlsn \
				    [expr $messagelsn > $lastloglsn] 1
			} elseif { $permtype == "ISPERM" } {
				# If we got an ISPERM, the returned LSN has to
				# be in the log.
				error_check_bad \
				    ispermlsn [lsearch $lsnlist $messagelsn] -1
			} else {
				puts "FAIL: unexpected message type $permtype"
			}
		}

		error_check_good logc_close [$logc close] 0

		# If we've finished processing all the messages, check
		# that the last received permanent message LSN matches the
		# last sent permanent message LSN.
		if { $nproced == 0 } {
			if { $check != 0 } {
				set last_sent [lindex $perm_sent_list end]
				set last_rec_msg \
				    [lindex $perm_response_list end]
				set last_received [lindex $last_rec_msg 1]
				error_check_good last_message \
				    $last_sent $last_received
			}

			# If we check correctly; empty out the lists
			set perm_response_list {{}}
			set perm_sent_list {{}}
			break
		}
	}
}