summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/rep051.tcl
blob: 1446e890018c16f91609f367b58368fea5468049 (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
234
235
236
237
238
239
240
241
242
243
# See the file LICENSE for redistribution information.
#
# Copyright (c) 2001-2009 Oracle.  All rights reserved.
#
# $Id$
#
# TEST	rep051
# TEST	Test of compaction with replication.
# TEST
# TEST	Run rep_test in a replicated master environment.
# TEST	Delete a large number of entries and compact with -freespace.
# TEST	Propagate the changes to the client and make sure client and
# TEST	master match.

proc rep051 { method { niter 1000 } { tnum "051" } args } {
	source ./include.tcl
	global databases_in_memory
	global repfiles_in_memory

	if { $is_windows9x_test == 1 } {
		puts "Skipping replication test on Win9x platform."
		return
	}

	# Compaction is an option for btree and recno databases only.
	if { $checking_valid_methods } {
		set test_methods {}
		foreach method $valid_methods {
			if { [is_btree $method] == 1 || \
			    [is_recno $method] == 1 } {
				lappend test_methods $method
			}
		}
		return $test_methods
	}
	if { [is_hash $method] == 1 || [is_queue $method] == 1 } {
		puts "Skipping test$tnum for method $method."
		return
	}

	# Set up for on-disk or in-memory databases.
	set msg "using on-disk databases"
	if { $databases_in_memory } {
		set msg "using named in-memory databases"
		if { [is_queueext $method] } { 
			puts -nonewline "Skipping rep$tnum for method "
			puts "$method with named in-memory databases."
			return
		}
	}

	set msg2 "and on-disk replication files"
	if { $repfiles_in_memory } {
		set msg2 "and in-memory replication files"
	}

	# Run tests with and without recovery.  If we're doing testing
	# of in-memory logging, skip the combination of recovery
	# and in-memory logging -- it doesn't make sense.
	set logsets [create_logsets 2]
	set saved_args $args

	foreach recopt $test_recopts {
		foreach l $logsets {
			set logindex [lsearch -exact $l "in-memory"]
			if { $recopt == "-recover" && $logindex != -1 } {
				puts "Skipping test \
				    with -recover for in-memory logs."
				continue
			}
			set envargs ""
			set args $saved_args
			puts "Rep$tnum: Replication with\
			    compaction ($method $recopt) $msg $msg2."
			puts "Rep$tnum: Master logs are [lindex $l 0]"
			puts "Rep$tnum: Client logs are [lindex $l 1]"
			rep051_sub $method \
			    $niter $tnum $envargs $l $recopt $args
		}
	}
}

proc rep051_sub { method niter tnum envargs logset recargs largs } {
	source ./include.tcl
	global databases_in_memory
	global repfiles_in_memory
	global rep_verbose
	global verbose_type

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

	set repmemargs ""
	if { $repfiles_in_memory } {
		set repmemargs "-rep_inmem_files "
	}

	env_cleanup $testdir

	replsetup $testdir/MSGQUEUEDIR

	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]

	set verify_subset \
	    [expr { $m_logtype == "in-memory" || $c_logtype == "in-memory" }]

	# In-memory logs require a large log buffer, and cannot
	# be used with -txn nosync.  Adjust the args for master
	# and client.
	# This test has a long transaction, allocate a larger log 
	# buffer for in-memory test.
	set m_logargs [adjust_logargs $m_logtype [expr 2 * [expr 1024 * 1024]]]
	set c_logargs [adjust_logargs $c_logtype [expr 2 * [expr 1024 * 1024]]]
	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 $verbargs \
	    -log_max 1000000 $envargs $m_logargs $recargs $repmemargs \
	    -home $masterdir -errpfx MASTER $m_txnargs -rep_master \
	    -rep_transport \[list 1 replsend\]"
	set masterenv [eval $env_cmd(M)]

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

	# Bring the client online by processing the startup messages.
	set envlist "{$masterenv 1} {$clientenv 2}"
	process_msgs $envlist

	# Explicitly create the db handle so we can do deletes,
	# and also to make the page size small.
	if { $databases_in_memory } {
		set dbname { "" "test.db" }
	} else { 
		set dbname "test.db"
	} 

	set omethod [convert_method $method]
	set db [eval {berkdb_open_noerr -env $masterenv -auto_commit\
	    -pagesize 512 -create -mode 0644} $largs $omethod $dbname]
 	error_check_good db_open [is_valid_db $db] TRUE

	# Run rep_test in the master and update client.
	puts "\tRep$tnum.a:\
	    Running rep_test in replicated env ($envargs $recargs)."

	eval rep_test $method $masterenv $db $niter 0 0 0 $largs
	process_msgs $envlist

	# Verify that contents match.
	puts "\tRep$tnum.b: Verifying client database contents."
	rep_verify $masterdir $masterenv\
	    $clientdir $clientenv $verify_subset 1 1

	# Delete most entries.  Since some of our methods renumber,
	# delete starting at $niter and working down to 0.
	puts "\tRep$tnum.c: Remove most entries, by cursor."
	set count [expr $niter - 1]
	set n 20
	set t [$masterenv txn]
	error_check_good txn [is_valid_txn $t $masterenv] TRUE
	set txn "-txn $t"

	set dbc [eval {$db cursor} $txn]

	# Leave every nth item.
	set dbt [$dbc get -first]
	while { $count > 0 } {
		if { [expr $count % $n] != 0 } {
			error_check_good dbc_del [$dbc del] 0
		}
		set dbt [$dbc get -next]
		incr count -1
	}

	error_check_good dbc_close [$dbc close] 0
	error_check_good t_commit [$t commit] 0

	# Open read-only handle on client, so we can call $db stat.
	set client_db \
	    [eval {berkdb_open_noerr} -env $clientenv -rdonly $dbname]
 	error_check_good client_open [is_valid_db $client_db] TRUE

	# Check database size on both client and master.
	process_msgs $envlist
	set master_pages_before [stat_field $db stat "Page count"]
	set client_pages_before [stat_field $client_db stat "Page count"]
	error_check_good \
	    pages_match_before $client_pages_before $master_pages_before

	# Compact database.
	puts "\tRep$tnum.d: Compact database."
	set t [$masterenv txn]
	error_check_good txn [is_valid_txn $t $masterenv] TRUE
	set txn "-txn $t"

	set ret [eval {$db compact} $txn {-freespace}]

	error_check_good t_commit [$t commit] 0
	error_check_good db_sync [$db sync] 0

	# There will be fewer pages in use after the compact -freespace call.
	set master_pages_after [stat_field $db stat "Page count"]
	set page_reduction [expr $master_pages_before - $master_pages_after]
	error_check_good page_reduction [expr $page_reduction > 0] 1
	
	# Process messages so the client sees the reduction in pages used.
	process_msgs $envlist

	set client_pages_after [stat_field $client_db stat "Page count"]
	error_check_good \
	    pages_match_after $client_pages_after $master_pages_after

	# Close client handle.
	error_check_good client_handle [$client_db close] 0

	# Reverify.
	puts "\tRep$tnum.b: Verifying client database contents."
	rep_verify $masterdir $masterenv\
	    $clientdir $clientenv $verify_subset 1 1

	# Clean up.
	error_check_good db_close [$db close] 0
	error_check_good masterenv_close [$masterenv close] 0
	error_check_good clientenv_close [$clientenv close] 0
	replclose $testdir/MSGQUEUEDIR
}