summaryrefslogtreecommitdiff
path: root/storage/bdb/test/sysscript.tcl
blob: 810b0df6cef5e5c60e046e5e861c474b9ae49316 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996-2002
#	Sleepycat Software.  All rights reserved.
#
# $Id: sysscript.tcl,v 11.17 2002/07/29 17:05:24 sue Exp $
#
# System integration test script.
# This script runs a single process that tests the full functionality of
# the system.  The database under test contains nfiles files.  Each process
# randomly generates a key and some data.  Both keys and data are bimodally
# distributed between small keys (1-10 characters) and large keys (the avg
# length is indicated via the command line parameter.
# The process then decides on a replication factor between 1 and nfiles.
# It writes the key and data to that many files and tacks on the file ids
# of the files it writes to the data string.  For example, let's say that
# I randomly generate the key dog and data cat.  Then I pick a replication
# factor of 3.  I pick 3 files from the set of n (say 1, 3, and 5).  I then
# rewrite the data as 1:3:5:cat.  I begin a transaction, add the key/data
# pair to each file and then commit.  Notice that I may generate replication
# of the form 1:3:3:cat in which case I simply add a duplicate to file 3.
#
# Usage: sysscript dir nfiles key_avg data_avg
#
# dir: DB_HOME directory
# nfiles: number of files in the set
# key_avg: average big key size
# data_avg: average big data size

source ./include.tcl
source $test_path/test.tcl
source $test_path/testutils.tcl

set mypid [pid]

set usage "sysscript dir nfiles key_avg data_avg method"

# Verify usage
if { $argc != 5 } {
	puts stderr "FAIL:[timestamp] Usage: $usage"
	exit
}

puts [concat "Argc: " $argc " Argv: " $argv]

# Initialize arguments
set dir [lindex $argv 0]
set nfiles [ lindex $argv 1 ]
set key_avg [ lindex $argv 2 ]
set data_avg [ lindex $argv 3 ]
set method [ lindex $argv 4 ]

# Initialize seed
global rand_init
berkdb srand $rand_init

puts "Beginning execution for $mypid"
puts "$dir DB_HOME"
puts "$nfiles files"
puts "$key_avg average key length"
puts "$data_avg average data length"

flush stdout

# Create local environment
set dbenv [berkdb_env -txn -home $dir]
set err [catch {error_check_good $mypid:dbenv [is_substr $dbenv env] 1} ret]
if {$err != 0} {
	puts $ret
	return
}

# Now open the files
for { set i 0 } { $i < $nfiles } { incr i } {
	set file test044.$i.db
	set db($i) [berkdb open -auto_commit -env $dbenv $method $file]
	set err [catch {error_check_bad $mypid:dbopen $db($i) NULL} ret]
	if {$err != 0} {
		puts $ret
		return
	}
	set err [catch {error_check_bad $mypid:dbopen [is_substr $db($i) \
	    error] 1} ret]
	if {$err != 0} {
		puts $ret
		return
	}
}

set record_based [is_record_based $method]
while { 1 } {
	# Decide if we're going to create a big key or a small key
	# We give small keys a 70% chance.
	if { [berkdb random_int 1 10] < 8 } {
		set k [random_data 5 0 0 $record_based]
	} else {
		set k [random_data $key_avg 0 0 $record_based]
	}
	set data [chop_data $method [random_data $data_avg 0 0]]

	set txn [$dbenv txn]
	set err [catch {error_check_good $mypid:txn_begin [is_substr $txn \
	    $dbenv.txn] 1} ret]
	if {$err != 0} {
		puts $ret
		return
	}

	# Open cursors
	for { set f 0 } {$f < $nfiles} {incr f} {
		set cursors($f) [$db($f) cursor -txn $txn]
		set err [catch {error_check_good $mypid:cursor_open \
		    [is_substr $cursors($f) $db($f)] 1} ret]
		if {$err != 0} {
			puts $ret
			return
		}
	}
	set aborted 0

	# Check to see if key is already in database
	set found 0
	for { set i 0 } { $i < $nfiles } { incr i } {
		set r [$db($i) get -txn $txn $k]
		set r [$db($i) get -txn $txn $k]
		if { $r == "-1" } {
			for {set f 0 } {$f < $nfiles} {incr f} {
				set err [catch {error_check_good \
				    $mypid:cursor_close \
				    [$cursors($f) close] 0} ret]
				if {$err != 0} {
					puts $ret
					return
				}
			}
			set err [catch {error_check_good $mypid:txn_abort \
			    [$txn abort] 0} ret]
			if {$err != 0} {
				puts $ret
				return
			}
			set aborted 1
			set found 2
			break
		} elseif { $r != "Key $k not found." } {
			set found 1
			break
		}
	}
	switch $found {
	2 {
		# Transaction aborted, no need to do anything.
	}
	0 {
		# Key was not found, decide how much to replicate
		# and then create a list of that many file IDs.
		set repl [berkdb random_int 1 $nfiles]
		set fset ""
		for { set i 0 } { $i < $repl } {incr i} {
			set f [berkdb random_int 0 [expr $nfiles - 1]]
			lappend fset $f
			set data [chop_data $method $f:$data]
		}

		foreach i $fset {
			set r [$db($i) put -txn $txn $k $data]
			if {$r == "-1"} {
				for {set f 0 } {$f < $nfiles} {incr f} {
					set err [catch {error_check_good \
					    $mypid:cursor_close \
					    [$cursors($f) close] 0} ret]
					if {$err != 0} {
						puts $ret
						return
					}
				}
				set err [catch {error_check_good \
				    $mypid:txn_abort [$txn abort] 0} ret]
				if {$err != 0} {
					puts $ret
					return
				}
				set aborted 1
				break
			}
		}
	}
	1 {
		# Key was found.  Make sure that all the data values
		# look good.
		set f [zero_list $nfiles]
		set data $r
		while { [set ndx [string first : $r]] != -1 } {
			set fnum [string range $r 0 [expr $ndx - 1]]
			if { [lindex $f $fnum] == 0 } {
				#set flag -set
				set full [record $cursors($fnum) get -set $k]
			} else {
				#set flag -next
				set full [record $cursors($fnum) get -next]
			}
			if {[llength $full] == 0} {
				for {set f 0 } {$f < $nfiles} {incr f} {
					set err [catch {error_check_good \
					    $mypid:cursor_close \
					    [$cursors($f) close] 0} ret]
					if {$err != 0} {
						puts $ret
						return
					}
				}
				set err [catch {error_check_good \
				    $mypid:txn_abort [$txn abort] 0} ret]
				if {$err != 0} {
					puts $ret
					return
				}
				set aborted 1
				break
			}
			set err [catch {error_check_bad \
			    $mypid:curs_get($k,$data,$fnum,$flag) \
			    [string length $full] 0} ret]
			if {$err != 0} {
				puts $ret
				return
			}
			set key [lindex [lindex $full 0] 0]
			set rec [pad_data $method [lindex [lindex $full 0] 1]]
			set err [catch {error_check_good \
			    $mypid:dbget_$fnum:key $key $k} ret]
			if {$err != 0} {
				puts $ret
				return
			}
			set err [catch {error_check_good \
			    $mypid:dbget_$fnum:data($k) $rec $data} ret]
			if {$err != 0} {
				puts $ret
				return
			}
			set f [lreplace $f $fnum $fnum 1]
			incr ndx
			set r [string range $r $ndx end]
		}
	}
	}
	if { $aborted == 0 } {
		for {set f 0 } {$f < $nfiles} {incr f} {
			set err [catch {error_check_good $mypid:cursor_close \
			    [$cursors($f) close] 0} ret]
			if {$err != 0} {
				puts $ret
				return
			}
		}
		set err [catch {error_check_good $mypid:commit [$txn commit] \
		    0} ret]
		if {$err != 0} {
			puts $ret
			return
		}
	}
}

# Close files
for { set i 0 } { $i < $nfiles} { incr i } {
	set r [$db($i) close]
	set err [catch {error_check_good $mypid:db_close:$i $r 0} ret]
	if {$err != 0} {
		puts $ret
		return
	}
}

# Close tm and environment
$dbenv close

puts "[timestamp] [pid] Complete"
flush stdout

filecheck $file 0