1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2003,2008 Oracle.  All rights reserved.
4#
5# $Id: txn011.tcl,v 12.8 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	txn011
8# TEST	Test durable and non-durable txns.
9# TEST	Test a mixed env (with both durable and non-durable
10# TEST	dbs), then a purely non-durable env.  Make sure commit
11# TEST	and abort work, and that only the log records we
12# TEST	expect are written.
13# TEST	Test that we can't get a durable handle on a ND
14# TEST	database, or vice versa.  Test that all subdb's
15# TEST	must be of the same type (D or ND).
16proc txn011 { {ntxns 100} } {
17	source ./include.tcl
18	global util_path
19
20	foreach envtype { "" "-private" } {
21		puts "Txn011: Non-durable txns ($envtype)."
22		env_cleanup $testdir
23
24		puts "\tTxn011.a: Persistent env recovery with -log_inmemory"
25		set lbuf [expr 8 * [expr 1024 * 1024]]
26		set env_cmd "berkdb_env -create \
27		    -home $testdir -txn -log_inmemory -log_buffer $lbuf"
28		set ndenv [eval $env_cmd $envtype]
29		set db [berkdb_open -create -auto_commit \
30		    -btree -env $ndenv -notdurable test.db]
31		check_log_records $testdir
32		error_check_good db_close [$db close] 0
33		error_check_good ndenv_close [$ndenv close] 0
34
35		# Run recovery with -e to retain environment.
36		set stat [catch {exec $util_path/db_recover -e -h $testdir} ret]
37		error_check_good db_printlog $stat 0
38
39		# Rejoin env and make sure that the db is still there.
40		set ndenv [berkdb_env -home $testdir]
41		set db [berkdb_open -auto_commit -env $ndenv test.db]
42		error_check_good db_close [$db close] 0
43		error_check_good ndenv_close [$ndenv close] 0
44		env_cleanup $testdir
45
46		# Start with a new env for the next test.
47		set ndenv [eval $env_cmd]
48		error_check_good env_open [is_valid_env $ndenv] TRUE
49
50		# Open/create the database.
51		set testfile notdurable.db
52		set db [eval berkdb_open -create \
53		    -auto_commit -env $ndenv -notdurable -btree $testfile]
54		error_check_good dbopen [is_valid_db $db] TRUE
55
56		puts "\tTxn011.b: Abort txns in in-memory logging env."
57		txn011_runtxns $ntxns $db $ndenv abort
58		# Make sure there is nothing in the db.
59		txn011_check_empty $db $ndenv
60
61		puts "\tTxn011.c: Commit txns in in-memory logging env."
62		txn011_runtxns $ntxns $db $ndenv commit
63
64		# Make sure we haven't written any inappropriate log records
65		check_log_records $testdir
66
67		# Clean up non-durable env tests.
68		error_check_good db_close [$db close] 0
69		error_check_good ndenv_close [$ndenv close] 0
70		env_cleanup $testdir
71
72		puts "\tTxn011.d: Set up mixed durable/non-durable test."
73		# Open/create the mixed environment
74		set mixed_env_cmd "berkdb_env_noerr -create \
75			-home $testdir -txn -log_inmemory -log_buffer $lbuf"
76		set env [eval $mixed_env_cmd]
77		error_check_good env_open [is_valid_env $env] TRUE
78		check_log_records $testdir
79
80		# Open/create the non-durable database
81		set nondurfile nondurable.db
82		set ndb [berkdb_open_noerr -create\
83		    -auto_commit -env $env -btree -notdurable $nondurfile]
84		error_check_good dbopen [is_valid_db $ndb] TRUE
85		check_log_records $testdir
86
87		puts "\tTxn011.e: Abort txns in non-durable db."
88		txn011_runtxns $ntxns $ndb $env abort
89		# Make sure there is nothing in the db.
90		txn011_check_empty $ndb $env
91		check_log_records $testdir
92
93		puts "\tTxn011.f: Commit txns in non-durable db."
94		txn011_runtxns $ntxns $ndb $env commit
95		check_log_records $testdir
96
97		# Open/create the durable database
98		set durfile durable.db
99		set ddb [eval berkdb_open_noerr \
100		   -create -auto_commit -env $env -btree $durfile]
101		error_check_good dbopen [is_valid_db $ddb] TRUE
102
103		# Try to get a not-durable handle on the durable db.
104		puts "\tTxn011.g: Try to get a not-durable handle on\
105		    a durable db."
106		set errormsg "Cannot open DURABLE and NOT DURABLE handles"
107		catch {berkdb_open_noerr \
108		    -auto_commit -env $env -notdurable $durfile} res
109		error_check_good handle_error1 [is_substr $res $errormsg] 1
110		error_check_good ddb_close [$ddb close] 0
111		catch {berkdb_open_noerr \
112		    -auto_commit -env $env -notdurable $durfile} res
113		error_check_good handle_error2 [is_substr $res $errormsg] 1
114
115		# Now reopen as durable for the remainder of the test.
116		set ddb [berkdb_open_noerr \
117		    -auto_commit -env $env -btree $durfile]
118		error_check_good dbopen [is_valid_db $ddb] TRUE
119
120		puts "\tTxn011.h: Abort txns in durable db."
121		# Add items to db in several txns but abort every one.
122		txn011_runtxns $ntxns $ddb $env abort
123		# Make sure there is nothing in the db.
124		txn011_check_empty $ddb $env
125
126		puts "\tTxn011.i: Commit txns in durable db."
127		txn011_runtxns $ntxns $ddb $env commit
128
129		puts "\tTxn011.j: Subdbs must all be durable or all not durable."
130		# Ask for -notdurable on durable db/subdb
131		set sdb1 [eval berkdb_open_noerr -create -auto_commit \
132		    -env $env -btree testfile1.db subdb1]
133		catch {set sdb2 [eval berkdb_open_noerr -create -auto_commit \
134		    -env $env -btree -notdurable testfile1.db subdb2]} res
135		error_check_good same_type_subdb1 [is_substr $res $errormsg] 1
136		error_check_good sdb1_close [$sdb1 close] 0
137
138		# Ask for durable on notdurable db/subdb
139		set sdb3 [eval berkdb_open_noerr -create -auto_commit \
140		    -env $env -btree -notdurable testfile2.db subdb3]
141		catch {set sdb4 [eval berkdb_open_noerr -create -auto_commit \
142		    -env $env -btree testfile2.db subdb4]} res
143		error_check_good same_type_subdb2 [is_substr $res $errormsg] 1
144		error_check_good sdb3_close [$sdb3 close] 0
145
146		puts "\tTxn011.k: Try to get a durable handle on a\
147		    not-durable db."
148		# Try to get a durable handle on a not-durable database,
149		# while open or on a new open.  Both should fail.
150		catch {berkdb_open_noerr -auto_commit -env $env $nondurfile} res
151		error_check_good handle_error [is_substr $res $errormsg] 1
152		error_check_good ndb_close [$ndb close] 0
153		catch {berkdb_open_noerr -auto_commit -env $env $nondurfile} res
154		error_check_good handle_error [is_substr $res $errormsg] 1
155
156		# Clean up mixed env.
157		error_check_good ddb_close [$ddb close] 0
158		error_check_good env_close [$env close] 0
159	}
160}
161
162proc txn011_runtxns { ntxns db env end } {
163	source ./include.tcl
164
165	set did [open $dict]
166	set i 0
167	while { [gets $did str] != -1 && $i < $ntxns } {
168		set txn [$env txn]
169		error_check_good txn_begin [is_valid_txn $txn $env] TRUE
170
171		error_check_good db_put_txn [$db put -txn $txn $i $str] 0
172		error_check_good txn_$end [$txn $end] 0
173		incr i
174	}
175	close $did
176}
177
178# Verify that a database is empty
179proc txn011_check_empty { db env } {
180	# Start a transaction
181	set t [$env txn]
182	error_check_good txn [is_valid_txn $t $env] TRUE
183	set txn "-txn $t"
184
185	# If a cursor get -first returns nothing, the db is empty.
186	set dbc [eval {$db cursor} $txn]
187	error_check_good db_cursor [is_substr $dbc $db] 1
188	set ret [$dbc get -first]
189	error_check_good get_on_empty [string length $ret] 0
190	error_check_good dbc_close [$dbc close] 0
191
192	# End transaction
193	error_check_good txn [$t commit] 0
194}
195
196# Some log records are still produced when we run create in a
197# non-durable db in a regular env.  Just make sure we don't see
198# any unexpected types.
199proc check_log_records { dir } {
200	global util_path
201
202	set tmpfile $dir/printlog.out
203	set stat [catch {exec $util_path/db_printlog -h $dir > $tmpfile} ret]
204	error_check_good db_printlog $stat 0
205
206	set f [open $tmpfile r]
207	while { [gets $f record] >= 0 } {
208		set r [regexp {\[[^\]]*\]\[[^\]]*\]([^\:]*)\:} $record whl name]
209		if { $r == 1 && [string match *_debug $name] != 1 && \
210		    [string match __txn_regop $name] != 1 && \
211		    [string match __txn_child $name] != 1 } {
212			puts "FAIL: unexpected log record $name found"
213		}
214	}
215	close $f
216	fileremove $tmpfile
217}
218