2010-05-14 15:31:11 +00:00
|
|
|
proc randstring {min max {type binary}} {
|
|
|
|
set len [expr {$min+int(rand()*($max-$min+1))}]
|
|
|
|
set output {}
|
|
|
|
if {$type eq {binary}} {
|
|
|
|
set minval 0
|
|
|
|
set maxval 255
|
|
|
|
} elseif {$type eq {alpha}} {
|
|
|
|
set minval 48
|
|
|
|
set maxval 122
|
|
|
|
} elseif {$type eq {compr}} {
|
|
|
|
set minval 48
|
|
|
|
set maxval 52
|
|
|
|
}
|
|
|
|
while {$len} {
|
|
|
|
append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
|
|
|
|
incr len -1
|
|
|
|
}
|
|
|
|
return $output
|
|
|
|
}
|
|
|
|
|
|
|
|
# Useful for some test
|
|
|
|
proc zlistAlikeSort {a b} {
|
|
|
|
if {[lindex $a 0] > [lindex $b 0]} {return 1}
|
|
|
|
if {[lindex $a 0] < [lindex $b 0]} {return -1}
|
|
|
|
string compare [lindex $a 1] [lindex $b 1]
|
|
|
|
}
|
|
|
|
|
2010-05-15 21:48:08 +00:00
|
|
|
# Return all log lines starting with the first line that contains a warning.
|
|
|
|
# Generally, this will be an assertion error with a stack trace.
|
|
|
|
proc warnings_from_file {filename} {
|
|
|
|
set lines [split [exec cat $filename] "\n"]
|
|
|
|
set matched 0
|
2012-05-22 11:13:24 +00:00
|
|
|
set logall 0
|
2010-05-15 21:48:08 +00:00
|
|
|
set result {}
|
|
|
|
foreach line $lines {
|
2012-05-22 11:13:24 +00:00
|
|
|
if {[string match {*REDIS BUG REPORT START*} $line]} {
|
|
|
|
set logall 1
|
|
|
|
}
|
2010-05-15 21:48:08 +00:00
|
|
|
if {[regexp {^\[\d+\]\s+\d+\s+\w+\s+\d{2}:\d{2}:\d{2} \#} $line]} {
|
|
|
|
set matched 1
|
|
|
|
}
|
2012-05-22 11:13:24 +00:00
|
|
|
if {$logall || $matched} {
|
2010-05-15 21:48:08 +00:00
|
|
|
lappend result $line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
join $result "\n"
|
|
|
|
}
|
|
|
|
|
2010-05-14 18:48:57 +00:00
|
|
|
# Return value for INFO property
|
|
|
|
proc status {r property} {
|
2010-07-28 12:08:46 +00:00
|
|
|
if {[regexp "\r\n$property:(.*?)\r\n" [{*}$r info] _ value]} {
|
2010-05-14 18:48:57 +00:00
|
|
|
set _ $value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 15:31:11 +00:00
|
|
|
proc waitForBgsave r {
|
|
|
|
while 1 {
|
2012-05-25 13:20:59 +00:00
|
|
|
if {[status r rdb_bgsave_in_progress] eq 1} {
|
2010-12-10 15:13:21 +00:00
|
|
|
if {$::verbose} {
|
|
|
|
puts -nonewline "\nWaiting for background save to finish... "
|
|
|
|
flush stdout
|
|
|
|
}
|
2010-05-14 15:31:11 +00:00
|
|
|
after 1000
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc waitForBgrewriteaof r {
|
|
|
|
while 1 {
|
2012-05-25 13:20:59 +00:00
|
|
|
if {[status r aof_rewrite_in_progress] eq 1} {
|
2010-12-10 15:13:21 +00:00
|
|
|
if {$::verbose} {
|
|
|
|
puts -nonewline "\nWaiting for background AOF rewrite to finish... "
|
|
|
|
flush stdout
|
|
|
|
}
|
2010-05-14 15:31:11 +00:00
|
|
|
after 1000
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 18:50:58 +00:00
|
|
|
proc wait_for_sync r {
|
|
|
|
while 1 {
|
2012-06-02 21:29:57 +00:00
|
|
|
if {[status $r master_link_status] eq "down"} {
|
2010-05-14 18:50:58 +00:00
|
|
|
after 10
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 13:32:37 +00:00
|
|
|
# Random integer between 0 and max (excluded).
|
2010-05-14 15:31:11 +00:00
|
|
|
proc randomInt {max} {
|
|
|
|
expr {int(rand()*$max)}
|
|
|
|
}
|
|
|
|
|
2013-06-25 13:32:37 +00:00
|
|
|
# Random signed integer between -max and max (both extremes excluded).
|
2012-06-11 13:19:46 +00:00
|
|
|
proc randomSignedInt {max} {
|
|
|
|
set i [randomInt $max]
|
|
|
|
if {rand() > 0.5} {
|
|
|
|
set i -$i
|
|
|
|
}
|
|
|
|
return $i
|
|
|
|
}
|
|
|
|
|
2010-05-14 15:31:11 +00:00
|
|
|
proc randpath args {
|
|
|
|
set path [expr {int(rand()*[llength $args])}]
|
|
|
|
uplevel 1 [lindex $args $path]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc randomValue {} {
|
|
|
|
randpath {
|
|
|
|
# Small enough to likely collide
|
2012-06-11 13:19:46 +00:00
|
|
|
randomSignedInt 1000
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
|
|
|
# 32 bit compressible signed/unsigned
|
2012-06-11 13:19:46 +00:00
|
|
|
randpath {randomSignedInt 2000000000} {randomSignedInt 4000000000}
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
|
|
|
# 64 bit
|
2012-06-11 13:19:46 +00:00
|
|
|
randpath {randomSignedInt 1000000000000}
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
|
|
|
# Random string
|
|
|
|
randpath {randstring 0 256 alpha} \
|
|
|
|
{randstring 0 256 compr} \
|
|
|
|
{randstring 0 256 binary}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc randomKey {} {
|
|
|
|
randpath {
|
|
|
|
# Small enough to likely collide
|
|
|
|
randomInt 1000
|
|
|
|
} {
|
|
|
|
# 32 bit compressible signed/unsigned
|
|
|
|
randpath {randomInt 2000000000} {randomInt 4000000000}
|
|
|
|
} {
|
|
|
|
# 64 bit
|
|
|
|
randpath {randomInt 1000000000000}
|
|
|
|
} {
|
|
|
|
# Random string
|
|
|
|
randpath {randstring 1 256 alpha} \
|
|
|
|
{randstring 1 256 compr}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-06 16:30:38 +00:00
|
|
|
proc findKeyWithType {r type} {
|
|
|
|
for {set j 0} {$j < 20} {incr j} {
|
2010-07-28 12:08:46 +00:00
|
|
|
set k [{*}$r randomkey]
|
2010-07-06 16:30:38 +00:00
|
|
|
if {$k eq {}} {
|
|
|
|
return {}
|
|
|
|
}
|
2010-07-28 12:08:46 +00:00
|
|
|
if {[{*}$r type $k] eq $type} {
|
2010-07-06 16:30:38 +00:00
|
|
|
return $k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
2010-08-03 11:38:39 +00:00
|
|
|
proc createComplexDataset {r ops {opt {}}} {
|
2010-05-14 15:31:11 +00:00
|
|
|
for {set j 0} {$j < $ops} {incr j} {
|
|
|
|
set k [randomKey]
|
2010-07-06 16:30:38 +00:00
|
|
|
set k2 [randomKey]
|
2010-05-14 15:31:11 +00:00
|
|
|
set f [randomValue]
|
|
|
|
set v [randomValue]
|
2010-08-03 11:38:39 +00:00
|
|
|
|
|
|
|
if {[lsearch -exact $opt useexpire] != -1} {
|
|
|
|
if {rand() < 0.1} {
|
|
|
|
{*}$r expire [randomKey] [randomInt 2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 15:31:11 +00:00
|
|
|
randpath {
|
|
|
|
set d [expr {rand()}]
|
|
|
|
} {
|
|
|
|
set d [expr {rand()}]
|
|
|
|
} {
|
|
|
|
set d [expr {rand()}]
|
|
|
|
} {
|
|
|
|
set d [expr {rand()}]
|
|
|
|
} {
|
|
|
|
set d [expr {rand()}]
|
|
|
|
} {
|
|
|
|
randpath {set d +inf} {set d -inf}
|
|
|
|
}
|
2010-07-28 12:08:46 +00:00
|
|
|
set t [{*}$r type $k]
|
2010-05-14 15:31:11 +00:00
|
|
|
|
|
|
|
if {$t eq {none}} {
|
|
|
|
randpath {
|
2010-07-28 12:08:46 +00:00
|
|
|
{*}$r set $k $v
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
2010-07-28 12:08:46 +00:00
|
|
|
{*}$r lpush $k $v
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
2010-07-28 12:08:46 +00:00
|
|
|
{*}$r sadd $k $v
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
2010-07-28 12:08:46 +00:00
|
|
|
{*}$r zadd $k $d $v
|
2010-05-14 15:31:11 +00:00
|
|
|
} {
|
2010-07-28 12:08:46 +00:00
|
|
|
{*}$r hset $k $f $v
|
2010-07-06 16:30:38 +00:00
|
|
|
} {
|
2010-07-28 12:08:46 +00:00
|
|
|
{*}$r del $k
|
2010-05-14 15:31:11 +00:00
|
|
|
}
|
2010-07-28 12:08:46 +00:00
|
|
|
set t [{*}$r type $k]
|
2010-05-14 15:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch $t {
|
|
|
|
{string} {
|
|
|
|
# Nothing to do
|
|
|
|
}
|
|
|
|
{list} {
|
2010-07-28 12:08:46 +00:00
|
|
|
randpath {{*}$r lpush $k $v} \
|
|
|
|
{{*}$r rpush $k $v} \
|
|
|
|
{{*}$r lrem $k 0 $v} \
|
|
|
|
{{*}$r rpop $k} \
|
|
|
|
{{*}$r lpop $k}
|
2010-05-14 15:31:11 +00:00
|
|
|
}
|
|
|
|
{set} {
|
2010-07-28 12:08:46 +00:00
|
|
|
randpath {{*}$r sadd $k $v} \
|
|
|
|
{{*}$r srem $k $v} \
|
2010-07-06 16:30:38 +00:00
|
|
|
{
|
2010-11-04 09:48:49 +00:00
|
|
|
set otherset [findKeyWithType {*}$r set]
|
2010-07-06 16:30:38 +00:00
|
|
|
if {$otherset ne {}} {
|
2010-07-28 12:08:46 +00:00
|
|
|
randpath {
|
|
|
|
{*}$r sunionstore $k2 $k $otherset
|
|
|
|
} {
|
|
|
|
{*}$r sinterstore $k2 $k $otherset
|
|
|
|
} {
|
|
|
|
{*}$r sdiffstore $k2 $k $otherset
|
|
|
|
}
|
2010-07-06 16:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-14 15:31:11 +00:00
|
|
|
}
|
|
|
|
{zset} {
|
2010-07-28 12:08:46 +00:00
|
|
|
randpath {{*}$r zadd $k $d $v} \
|
|
|
|
{{*}$r zrem $k $v} \
|
2010-07-06 16:30:38 +00:00
|
|
|
{
|
2010-11-04 09:48:49 +00:00
|
|
|
set otherzset [findKeyWithType {*}$r zset]
|
2010-07-06 16:30:38 +00:00
|
|
|
if {$otherzset ne {}} {
|
2010-07-28 12:08:46 +00:00
|
|
|
randpath {
|
|
|
|
{*}$r zunionstore $k2 2 $k $otherzset
|
|
|
|
} {
|
|
|
|
{*}$r zinterstore $k2 2 $k $otherzset
|
|
|
|
}
|
2010-07-06 16:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-14 15:31:11 +00:00
|
|
|
}
|
|
|
|
{hash} {
|
2010-07-28 12:08:46 +00:00
|
|
|
randpath {{*}$r hset $k $f $v} \
|
|
|
|
{{*}$r hdel $k $f}
|
2010-05-14 15:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-19 12:33:39 +00:00
|
|
|
|
|
|
|
proc formatCommand {args} {
|
|
|
|
set cmd "*[llength $args]\r\n"
|
|
|
|
foreach a $args {
|
|
|
|
append cmd "$[string length $a]\r\n$a\r\n"
|
|
|
|
}
|
|
|
|
set _ $cmd
|
|
|
|
}
|
2010-07-27 12:42:11 +00:00
|
|
|
|
|
|
|
proc csvdump r {
|
|
|
|
set o {}
|
2010-07-28 12:08:46 +00:00
|
|
|
foreach k [lsort [{*}$r keys *]] {
|
|
|
|
set type [{*}$r type $k]
|
2010-07-27 12:42:11 +00:00
|
|
|
append o [csvstring $k] , [csvstring $type] ,
|
|
|
|
switch $type {
|
|
|
|
string {
|
2010-07-28 12:08:46 +00:00
|
|
|
append o [csvstring [{*}$r get $k]] "\n"
|
2010-07-27 12:42:11 +00:00
|
|
|
}
|
|
|
|
list {
|
2010-07-28 12:08:46 +00:00
|
|
|
foreach e [{*}$r lrange $k 0 -1] {
|
2010-07-27 12:42:11 +00:00
|
|
|
append o [csvstring $e] ,
|
|
|
|
}
|
|
|
|
append o "\n"
|
|
|
|
}
|
|
|
|
set {
|
2010-07-28 12:08:46 +00:00
|
|
|
foreach e [lsort [{*}$r smembers $k]] {
|
2010-07-27 12:42:11 +00:00
|
|
|
append o [csvstring $e] ,
|
|
|
|
}
|
|
|
|
append o "\n"
|
|
|
|
}
|
|
|
|
zset {
|
2010-07-28 12:08:46 +00:00
|
|
|
foreach e [{*}$r zrange $k 0 -1 withscores] {
|
2010-07-27 12:42:11 +00:00
|
|
|
append o [csvstring $e] ,
|
|
|
|
}
|
|
|
|
append o "\n"
|
|
|
|
}
|
|
|
|
hash {
|
2010-07-28 12:08:46 +00:00
|
|
|
set fields [{*}$r hgetall $k]
|
2010-07-27 12:42:11 +00:00
|
|
|
set newfields {}
|
|
|
|
foreach {k v} $fields {
|
|
|
|
lappend newfields [list $k $v]
|
|
|
|
}
|
|
|
|
set fields [lsort -index 0 $newfields]
|
|
|
|
foreach kv $fields {
|
|
|
|
append o [csvstring [lindex $kv 0]] ,
|
|
|
|
append o [csvstring [lindex $kv 1]] ,
|
|
|
|
}
|
|
|
|
append o "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $o
|
|
|
|
}
|
|
|
|
|
|
|
|
proc csvstring s {
|
|
|
|
return "\"$s\""
|
|
|
|
}
|
2011-11-16 13:40:50 +00:00
|
|
|
|
|
|
|
proc roundFloat f {
|
|
|
|
format "%.10g" $f
|
|
|
|
}
|
2014-02-17 11:29:54 +00:00
|
|
|
|
|
|
|
proc find_available_port start {
|
|
|
|
for {set j $start} {$j < $start+1024} {incr j} {
|
|
|
|
if {[catch {
|
|
|
|
set fd [socket 127.0.0.1 $j]
|
|
|
|
}]} {
|
|
|
|
return $j
|
|
|
|
} else {
|
|
|
|
close $fd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {$j == $start+1024} {
|
|
|
|
error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 16:36:50 +00:00
|
|
|
|
|
|
|
# Test if TERM looks like to support colors
|
|
|
|
proc color_term {} {
|
|
|
|
expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc colorstr {color str} {
|
|
|
|
if {[color_term]} {
|
|
|
|
set b 0
|
|
|
|
if {[string range $color 0 4] eq {bold-}} {
|
|
|
|
set b 1
|
|
|
|
set color [string range $color 5 end]
|
|
|
|
}
|
|
|
|
switch $color {
|
|
|
|
red {set colorcode {31}}
|
|
|
|
green {set colorcode {32}}
|
|
|
|
yellow {set colorcode {33}}
|
|
|
|
blue {set colorcode {34}}
|
|
|
|
magenta {set colorcode {35}}
|
|
|
|
cyan {set colorcode {36}}
|
|
|
|
white {set colorcode {37}}
|
|
|
|
default {set colorcode {37}}
|
|
|
|
}
|
|
|
|
if {$colorcode ne {}} {
|
|
|
|
return "\033\[$b;${colorcode};40m$str\033\[0m"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $str
|
|
|
|
}
|
|
|
|
}
|