1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: recd001.tcl,v 12.9 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	recd001
8# TEST	Per-operation recovery tests for non-duplicate, non-split
9# TEST	messages.  Makes sure that we exercise redo, undo, and do-nothing
10# TEST	condition.  Any test that appears with the message (change state)
11# TEST	indicates that we've already run the particular test, but we are
12# TEST	running it again so that we can change the state of the data base
13# TEST	to prepare for the next test (this applies to all other recovery
14# TEST	tests as well).
15# TEST
16# TEST	These are the most basic recovery tests.  We do individual recovery
17# TEST	tests for each operation in the access method interface.  First we
18# TEST	create a file and capture the state of the database (i.e., we copy
19# TEST	it.  Then we run a transaction containing a single operation.  In
20# TEST	one test, we abort the transaction and compare the outcome to the
21# TEST	original copy of the file.  In the second test, we restore the
22# TEST	original copy of the database and then run recovery and compare
23# TEST	this against the actual database.
24proc recd001 { method {select 0} args } {
25	global fixed_len
26	source ./include.tcl
27
28	set envargs ""
29	set zero_idx [lsearch -exact $args "-zero_log"]
30	if { $zero_idx != -1 } {
31		set args [lreplace $args $zero_idx $zero_idx]
32		set envargs "-zero_log"
33	}
34
35	set orig_fixed_len $fixed_len
36	set opts [convert_args $method $args]
37	set omethod [convert_method $method]
38
39	puts "Recd001: $method operation/transaction tests ($envargs)"
40
41	# Create the database and environment.
42	env_cleanup $testdir
43
44	# The recovery tests were originally written to
45	# do a command, abort, do it again, commit, and then
46	# repeat the sequence with another command.  Each command
47	# tends to require that the previous command succeeded and
48	# left the database a certain way.  To avoid cluttering up the
49	# op_recover interface as well as the test code, we create two
50	# databases;  one does abort and then commit for each op, the
51	# other does prepare, prepare-abort, and prepare-commit for each
52	# op.  If all goes well, this allows each command to depend
53	# exactly one successful iteration of the previous command.
54	set testfile recd001.db
55	set testfile2 recd001-2.db
56
57	set flags "-create -txn -home $testdir $envargs"
58
59	# For queue databases, we end up locking all records from one
60	# to the end of the queue, which depends on the default pagesize.
61	# Assume that page sizes default to 16KB or less, then we need 4K
62	# locks.
63	if { [is_record_based $method] == 1 } {
64		set flags "$flags -lock_max_locks 5000 -lock_max_objects 5000"
65	}
66
67	puts "\tRecd001.a.0: creating environment"
68	set env_cmd "berkdb_env $flags"
69	set dbenv [eval $env_cmd]
70	error_check_good dbenv [is_valid_env $dbenv] TRUE
71
72	#
73	# We need to create a database to get the pagesize (either
74	# the default or whatever might have been specified).
75	# Then remove it so we can compute fixed_len and create the
76	# real database.
77	set oflags "-create $omethod -mode 0644 \
78	    -env $dbenv $opts $testfile"
79	set db [eval {berkdb_open} $oflags]
80	error_check_good db_open [is_valid_db $db] TRUE
81	set stat [$db stat]
82	#
83	# Compute the fixed_len based on the pagesize being used.
84	# We want the fixed_len to be 1/4 the pagesize.
85	#
86	set pg [get_pagesize $stat]
87	error_check_bad get_pagesize $pg -1
88	set fixed_len [expr $pg / 4]
89	error_check_good db_close [$db close] 0
90	error_check_good dbremove [berkdb dbremove -env $dbenv $testfile] 0
91
92	# Convert the args again because fixed_len is now real.
93	# Create the databases and close the environment.
94	# cannot specify db truncate in txn protected env!!!
95	set opts [convert_args $method ""]
96	set omethod [convert_method $method]
97	set oflags "-create $omethod -mode 0644 \
98	    -env $dbenv $opts $testfile"
99	set db [eval {berkdb_open} $oflags]
100	error_check_good db_open [is_valid_db $db] TRUE
101	error_check_good db_close [$db close] 0
102
103	set oflags "-create $omethod -mode 0644 \
104	    -env $dbenv $opts $testfile2"
105	set db [eval {berkdb_open} $oflags]
106	error_check_good db_open [is_valid_db $db] TRUE
107	error_check_good db_close [$db close] 0
108
109	error_check_good env_close [$dbenv close] 0
110
111	puts "\tRecd001.a.1: Verify db_printlog can read logfile"
112	set tmpfile $testdir/printlog.out
113	set stat [catch {exec $util_path/db_printlog -h $testdir \
114	    > $tmpfile} ret]
115	error_check_good db_printlog $stat 0
116	fileremove $tmpfile
117
118	# List of recovery tests: {CMD MSG} pairs.
119	set rlist {
120	{ {DB put -txn TXNID $key $data}	"Recd001.b: put"}
121	{ {DB del -txn TXNID $key}		"Recd001.c: delete"}
122	{ {DB put -txn TXNID $bigkey $data}	"Recd001.d: big key put"}
123	{ {DB del -txn TXNID $bigkey}		"Recd001.e: big key delete"}
124	{ {DB put -txn TXNID $key $bigdata}	"Recd001.f: big data put"}
125	{ {DB del -txn TXNID $key}		"Recd001.g: big data delete"}
126	{ {DB put -txn TXNID $key $data}	"Recd001.h: put (change state)"}
127	{ {DB put -txn TXNID $key $newdata}	"Recd001.i: overwrite"}
128	{ {DB put -txn TXNID -partial "$off $len" $key $partial_grow}
129	  "Recd001.j: partial put growing"}
130	{ {DB put -txn TXNID $key $newdata}	"Recd001.k: overwrite (fix)"}
131	{ {DB put -txn TXNID -partial "$off $len" $key $partial_shrink}
132	  "Recd001.l: partial put shrinking"}
133	{ {DB put -txn TXNID -append $data}	"Recd001.m: put -append"}
134	{ {DB get -txn TXNID -consume}		"Recd001.n: db get -consume"}
135	}
136
137	# These are all the data values that we're going to need to read
138	# through the operation table and run the recovery tests.
139
140	if { [is_record_based $method] == 1 } {
141		set key 1
142	} else {
143		set key recd001_key
144	}
145	set data recd001_data
146	set newdata NEWrecd001_dataNEW
147	set off 3
148	set len 12
149
150	set partial_grow replacement_record_grow
151	set partial_shrink xxx
152	if { [is_fixed_length $method] == 1 } {
153		set len [string length $partial_grow]
154		set partial_shrink $partial_grow
155	}
156	set bigdata [replicate $key $fixed_len]
157	if { [is_record_based $method] == 1 } {
158		set bigkey $fixed_len
159	} else {
160		set bigkey [replicate $key $fixed_len]
161	}
162
163	foreach pair $rlist {
164		set cmd [subst [lindex $pair 0]]
165		set msg [lindex $pair 1]
166		if { $select != 0 } {
167			set tag [lindex $msg 0]
168			set tail [expr [string length $tag] - 2]
169			set tag [string range $tag $tail $tail]
170			if { [lsearch $select $tag] == -1 } {
171				continue
172			}
173		}
174
175		if { [is_queue $method] != 1 } {
176			if { [string first append $cmd] != -1 } {
177				continue
178			}
179			if { [string first consume $cmd] != -1 } {
180				continue
181			}
182		}
183
184#		if { [is_fixed_length $method] == 1 } {
185#			if { [string first partial $cmd] != -1 } {
186#				continue
187#			}
188#		}
189		op_recover abort $testdir $env_cmd $testfile $cmd $msg
190		op_recover commit $testdir $env_cmd $testfile $cmd $msg
191		#
192		# Note that since prepare-discard ultimately aborts
193		# the txn, it must come before prepare-commit.
194		#
195		op_recover prepare-abort $testdir $env_cmd $testfile2 \
196		    $cmd $msg
197		op_recover prepare-discard $testdir $env_cmd $testfile2 \
198		    $cmd $msg
199		op_recover prepare-commit $testdir $env_cmd $testfile2 \
200		    $cmd $msg
201	}
202	set fixed_len $orig_fixed_len
203
204	if { [is_fixed_length $method] == 1 } {
205		puts "Skipping remainder of test for fixed length methods"
206		return
207	}
208
209	#
210	# Check partial extensions.  If we add a key/data to the database
211	# and then expand it using -partial, then recover, recovery was
212	# failing in #3944.  Check that scenario here.
213	#
214	# !!!
215	# We loop here because on each iteration, we need to clean up
216	# the old env (i.e. this test does not depend on earlier runs).
217	# If we run it without cleaning up the env inbetween, we do not
218	# test the scenario of #3944.
219	#
220	set len [string length $data]
221	set len2 256
222	set part_data [replicate "abcdefgh" 32]
223	set p [list 0 $len]
224	set cmd [subst \
225	    {DB put -txn TXNID -partial "$len $len2" $key $part_data}]
226	set msg "Recd001.o: partial put prepopulated/expanding"
227	foreach op {abort commit prepare-abort prepare-discard prepare-commit} {
228		env_cleanup $testdir
229
230		set dbenv [eval $env_cmd]
231		error_check_good dbenv [is_valid_env $dbenv] TRUE
232		set t [$dbenv txn]
233		error_check_good txn_begin [is_valid_txn $t $dbenv] TRUE
234		set oflags "-create $omethod -mode 0644 \
235		    -env $dbenv -txn $t $opts $testfile"
236		set db [eval {berkdb_open} $oflags]
237		error_check_good db_open [is_valid_db $db] TRUE
238		set oflags "-create $omethod -mode 0644 \
239		    -env $dbenv -txn $t $opts $testfile2"
240		set db2 [eval {berkdb_open} $oflags]
241		error_check_good db_open [is_valid_db $db2] TRUE
242
243		set ret [$db put -txn $t -partial $p $key $data]
244		error_check_good dbput $ret 0
245
246		set ret [$db2 put -txn $t -partial $p $key $data]
247		error_check_good dbput $ret 0
248		error_check_good txncommit [$t commit] 0
249		error_check_good dbclose [$db close] 0
250		error_check_good dbclose [$db2 close] 0
251		error_check_good dbenvclose [$dbenv close] 0
252
253		op_recover $op $testdir $env_cmd $testfile $cmd $msg
254	}
255	return
256}
257