1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: log006.tcl,v 12.9 2008/04/02 18:22:28 carol Exp $
6#
7# TEST	log006
8# TEST	Test log file auto-remove.
9# TEST		Test normal operation.
10# TEST		Test a long-lived txn.
11# TEST		Test log_archive flags.
12# TEST		Test db_archive flags.
13# TEST		Test turning on later.
14# TEST		Test setting via DB_CONFIG.
15proc log006 { } {
16	source ./include.tcl
17
18	puts "Log006: Check auto-remove of log files."
19	env_cleanup $testdir
20
21	# Open the environment, set auto-remove flag.  Use smaller log
22	# files to make more of them.
23	puts "\tLog006.a: open environment, populate database."
24	set lbuf 16384
25	set lmax 65536
26	set env [berkdb_env_noerr -log_remove \
27	    -create -home $testdir -log_buffer $lbuf -log_max $lmax -txn]
28	error_check_good envopen [is_valid_env $env] TRUE
29
30	log006_put $testdir $env
31
32	#
33	# Check log files.  Using the small log file size, we should have
34	# have made a lot of log files.  Check that we have only a few left.
35	# Dividing by 5 tests that at least 80% of the files are gone.
36	#
37	set log_number [stat_field $env log_stat "Current log file number"]
38	set log_expect [expr $log_number / 5]
39
40	puts "\tLog006.b: Check log files removed."
41	set lfiles [glob -nocomplain $testdir/log.*]
42	set remlen [llength $lfiles]
43	error_check_good lfiles_len [expr $remlen < $log_expect] 1
44	error_check_good lfiles [lsearch $lfiles $testdir/log.0000000001] -1
45	# Save last log file for later check.
46	# Files may not be sorted, sort them and then save the last filename.
47	set oldfile [lindex [lsort -ascii $lfiles] end]
48
49	# Rerun log006_put with a long lived txn.
50	#
51	puts "\tLog006.c: Rerun put loop with long-lived transaction."
52	cleanup $testdir $env
53	set txn [$env txn]
54	error_check_good txn [is_valid_txn $txn $env] TRUE
55
56	# Give the txn something to do so no files can be removed.
57	set testfile temp.db
58	set db [eval {berkdb_open_noerr -create -mode 0644} \
59	    -env $env -txn $txn -pagesize 8192 -btree $testfile]
60	error_check_good dbopen [is_valid_db $db] TRUE
61
62	log006_put $testdir $env
63
64	puts "\tLog006.d: Check log files not removed."
65	set lfiles [glob -nocomplain $testdir/log.*]
66	error_check_good lfiles2_len [expr [llength $lfiles] > $remlen] 1
67	set lfiles [lsort -ascii $lfiles]
68	error_check_good lfiles_chk [lsearch $lfiles $oldfile] 0
69	error_check_good txn_commit [$txn commit] 0
70	error_check_good db_close [$db close] 0
71	error_check_good ckp1 [$env txn_checkpoint] 0
72	error_check_good ckp2 [$env txn_checkpoint] 0
73
74	puts "\tLog006.e: Run log_archive with -auto_remove flag."
75	# When we're done, only the last log file should remain.
76	set lfiles [glob -nocomplain $testdir/log.*]
77	set oldfile [lindex [lsort -ascii $lfiles] end]
78
79	# First, though, verify mutual-exclusiveness of flag.
80	foreach f {-arch_abs -arch_data -arch_log} {
81		set stat [catch {eval $env log_archive -arch_remove $f} ret]
82		error_check_good stat $stat 1
83		error_check_good la:$f:fail [is_substr $ret "illegal flag"] 1
84	}
85	# Now run it for real.
86	set stat [catch {$env log_archive -arch_remove} ret]
87	error_check_good stat $stat 0
88
89	puts "\tLog006.f: Check only $oldfile remains."
90	set lfiles [glob -nocomplain $testdir/log.*]
91	error_check_good 1log [llength $lfiles] 1
92	error_check_good lfiles_chk [lsearch $lfiles $oldfile] 0
93
94	puts "\tLog006.g: Rerun put loop with long-lived transaction."
95	set txn [$env txn]
96	error_check_good txn [is_valid_txn $txn $env] TRUE
97	log006_put $testdir $env
98	error_check_good txn_commit [$txn commit] 0
99	error_check_good ckp1 [$env txn_checkpoint] 0
100	error_check_good ckp2 [$env txn_checkpoint] 0
101	error_check_good env_close [$env close] 0
102
103	#
104	# Test db_archive's auto-remove flag.
105	# After we are done, only the last log file should be there.
106	# First check that the delete flag cannot be used with any
107	# of the other flags.
108	#
109	puts "\tLog006.h: Run db_archive with delete flag."
110	set lfiles [glob -nocomplain $testdir/log.*]
111	set oldfile [lindex [lsort -ascii $lfiles] end]
112	#
113	# Again, first check illegal flag combinations with db_archive.
114	#
115	foreach f {-a -l -s} {
116		set stat [catch {exec $util_path/db_archive $f -d -h $testdir} \
117		    ret]
118		error_check_good stat $stat 1
119		error_check_good la:fail [is_substr $ret "illegal flag"] 1
120	}
121	set stat [catch {exec $util_path/db_archive -d -h $testdir} ret]
122	error_check_good stat $stat 0
123
124	puts "\tLog006.i: Check only $oldfile remains."
125	set lfiles [glob -nocomplain $testdir/log.*]
126	error_check_good 1log [llength $lfiles] 1
127	error_check_good lfiles_chk [lsearch $lfiles $oldfile] 0
128
129	#
130	# Now rerun some parts with other env settings tested.
131	#
132	env_cleanup $testdir
133
134	# First test that the option can be turned on later.
135	# 1. Open env w/o auto-remove.
136	# 2. Run log006_put.
137	# 3. Verify log files all there.
138	# 4. Call env set_flags to turn it on.
139	# 5. Run log006_put.
140	# 6. Verify log files removed.
141	puts "\tLog006.j: open environment w/o auto remove, populate database."
142	set env [berkdb_env -recover \
143	    -create -home $testdir -log_buffer $lbuf -log_max $lmax -txn]
144	error_check_good envopen [is_valid_env $env] TRUE
145
146	log006_put $testdir $env
147
148	puts "\tLog006.k: Check log files not removed."
149	set lfiles [glob -nocomplain $testdir/log.*]
150	error_check_good lfiles2_len [expr [llength $lfiles] > $remlen] 1
151	set lfiles [lsort -ascii $lfiles]
152	error_check_good lfiles [lsearch $lfiles $testdir/log.0000000001] 0
153
154	puts "\tLog006.l: turn on auto remove and repopulate database."
155	error_check_good sf [$env log_config "autoremove on"] 0
156
157	log006_put $testdir $env
158
159	puts "\tLog006.m: Check log files removed."
160	set lfiles [glob -nocomplain $testdir/log.*]
161	error_check_good lfiles_len [expr [llength $lfiles] < $log_expect] 1
162	error_check_good lfiles [lsearch $lfiles $testdir/log.0000000001] -1
163	error_check_good env_close [$env close] 0
164
165	#
166	# Configure via DB_CONFIG.
167	#
168	env_cleanup $testdir
169
170	puts "\tLog006.n: Test setting via DB_CONFIG."
171	# Open the environment, w/o remove flag, but DB_CONFIG.
172	set cid [open $testdir/DB_CONFIG w]
173	puts $cid "set_log_config db_log_auto_remove"
174	close $cid
175	set env [berkdb_env -recover \
176	    -create -home $testdir -log_buffer $lbuf -log_max $lmax -txn]
177	error_check_good envopen [is_valid_env $env] TRUE
178
179	log006_put $testdir $env
180
181	puts "\tLog006.o: Check log files removed."
182	set lfiles [glob -nocomplain $testdir/log.*]
183	error_check_good lfiles_len [expr [llength $lfiles] < $log_expect] 1
184	error_check_good lfiles [lsearch $lfiles $testdir/log.0000000001] -1
185	error_check_good env_close [$env close] 0
186
187}
188
189#
190# Modified from test003.
191#
192proc log006_put { testdir env } {
193	set testfile log006.db
194	#
195	# Specify a pagesize so we can control how many log files
196	# are created and left over.
197	#
198	set db [eval {berkdb_open_noerr -create -mode 0644} \
199	    -env $env -auto_commit -pagesize 8192 -btree $testfile]
200	error_check_good dbopen [is_valid_db $db] TRUE
201
202	set lmax [$env get_lg_max]
203	set file_list [get_file_list]
204	set count 0
205	foreach f $file_list {
206		if { [string compare [file type $f] "file"] != 0 } {
207			continue
208		}
209		set key $f
210		# Should really catch errors
211		set fid [open $f r]
212		fconfigure $fid -translation binary
213		# Read in less than the maximum log size.
214		set data [read $fid [expr $lmax - [expr $lmax / 8]]]
215		close $fid
216
217		set t [$env txn]
218		error_check_good txn [is_valid_txn $t $env] TRUE
219		set txn "-txn $t"
220		set ret [eval {$db put} $txn {$key $data}]
221		error_check_good put $ret 0
222		error_check_good txn [$t commit] 0
223		if { $count % 10 == 0 } {
224			error_check_good ckp($count) [$env txn_checkpoint] 0
225		}
226
227		incr count
228	}
229	error_check_good db_close [$db close] 0
230}
231