1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2000,2008 Oracle.  All rights reserved.
4#
5# $Id: recd011.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	recd011
8# TEST	Verify that recovery to a specific timestamp works.
9proc recd011 { method {niter 200} {ckpt_freq 15} {sleep_time 1} args } {
10	source ./include.tcl
11	global rand_init
12	berkdb srand $rand_init
13
14	set args [convert_args $method $args]
15	set omethod [convert_method $method]
16	set tnum "011"
17
18	puts "Recd$tnum ($method $args): Test recovery to a specific timestamp."
19
20	set testfile recd$tnum.db
21	env_cleanup $testdir
22
23	set i 0
24	if { [is_record_based $method] == 1 } {
25		set key 1
26		set bigkey 1001
27	} else {
28		set key KEY
29		set bigkey BIGKEY
30	}
31
32	puts "\tRecd$tnum.a: Create environment and database."
33	set bufsize [expr 8 * 1024]
34	set maxsize [expr 8 * $bufsize]
35	set flags "-create -txn -home $testdir -log_buffer $bufsize \
36	    -log_max $maxsize"
37
38	set env_cmd "berkdb_env $flags"
39	set dbenv [eval $env_cmd]
40	error_check_good dbenv [is_valid_env $dbenv] TRUE
41
42	set oflags "-auto_commit -env $dbenv -create -mode 0644 $args $omethod"
43	set db [eval {berkdb_open} $oflags $testfile]
44	error_check_good dbopen [is_valid_db $db] TRUE
45
46	# Main loop:  every second or so, increment the db in a txn.
47	puts "\t\tInitial Checkpoint"
48	error_check_good "Initial Checkpoint" [$dbenv txn_checkpoint] 0
49
50	puts "\tRecd$tnum.b ($niter iterations):\
51	    Transaction-protected increment loop."
52	for { set i 0 } { $i <= $niter } { incr i } {
53		set str [random_data 4096 0 NOTHING]
54		set data $i
55		set bigdata $i$str
56
57		# Put, in a txn.
58		set txn [$dbenv txn]
59		error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
60		error_check_good db_put \
61		    [$db put -txn $txn $key [chop_data $method $data]] 0
62		error_check_good db_put \
63		    [$db put -txn $txn $bigkey [chop_data $method $bigdata]] 0
64		error_check_good txn_commit [$txn commit] 0
65
66		# We need to sleep before taking the timestamp to guarantee
67		# that the timestamp is *after* this transaction commits.
68		# Since the resolution of the system call used by Berkeley DB
69		# is less than a second, rounding to the nearest second can
70		# otherwise cause off-by-one errors in the test.
71		tclsleep $sleep_time
72
73		set timeof($i) [timestamp -r]
74
75		# If an appropriate period has elapsed, checkpoint.
76		if { $i % $ckpt_freq == $ckpt_freq - 1 } {
77			puts "\t\tIteration $i: Checkpointing."
78			error_check_good ckpt($i) [$dbenv txn_checkpoint] 0
79		}
80
81		# Sleep again to ensure that the next operation definitely
82		# occurs after the timestamp.
83		tclsleep $sleep_time
84	}
85	error_check_good db_close [$db close] 0
86	error_check_good env_close [$dbenv close] 0
87
88	# Now, loop through and recover to each timestamp, verifying the
89	# expected increment.
90	puts "\tRecd$tnum.c: Recover to each timestamp and check."
91	for { set i $niter } { $i >= 0 } { incr i -1 } {
92
93		# Run db_recover.
94		set t [clock format $timeof($i) -format "%y%m%d%H%M.%S"]
95		# puts $t
96		berkdb debug_check
97		set ret [catch {exec $util_path/db_recover -h $testdir -t $t} r]
98		error_check_good db_recover($i,$t,$r) $ret 0
99
100		# Now open the db and check the timestamp.
101		set db [eval {berkdb_open} $testdir/$testfile]
102		error_check_good db_open($i) [is_valid_db $db] TRUE
103
104		set dbt [$db get $key]
105		set datum [lindex [lindex $dbt 0] 1]
106		error_check_good timestamp_recover $datum [pad_data $method $i]
107
108		error_check_good db_close [$db close] 0
109	}
110
111	# Finally, recover to a time well before the first timestamp
112	# and well after the last timestamp.  The latter should
113	# be just like the timestamp of the last test performed;
114	# the former should fail.
115	puts "\tRecd$tnum.d: Recover to before the first timestamp."
116	set t [clock format [expr $timeof(0) - 1000] -format "%y%m%d%H%M.%S"]
117	set ret [catch {exec $util_path/db_recover -h $testdir -t $t} r]
118	error_check_bad db_recover(before,$t) $ret 0
119
120	puts "\tRecd$tnum.e: Recover to after the last timestamp."
121	set t [clock format \
122	    [expr $timeof($niter) + 1000] -format "%y%m%d%H%M.%S"]
123	set ret [catch {exec $util_path/db_recover -h $testdir -t $t} r]
124	error_check_good db_recover(after,$t) $ret 0
125
126	# Now open the db and check the timestamp.
127	set db [eval {berkdb_open} $testdir/$testfile]
128	error_check_good db_open(after) [is_valid_db $db] TRUE
129
130	set dbt [$db get $key]
131	set datum2 [lindex [lindex $dbt 0] 1]
132
133	error_check_good timestamp_recover $datum2 $datum
134	error_check_good db_close [$db close] 0
135}
136