2
0

util.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. proc randstring {min max {type binary}} {
  2. set len [expr {$min+int(rand()*($max-$min+1))}]
  3. set output {}
  4. if {$type eq {binary}} {
  5. set minval 0
  6. set maxval 255
  7. } elseif {$type eq {alpha}} {
  8. set minval 48
  9. set maxval 122
  10. } elseif {$type eq {compr}} {
  11. set minval 48
  12. set maxval 52
  13. }
  14. while {$len} {
  15. append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
  16. incr len -1
  17. }
  18. return $output
  19. }
  20. # Useful for some test
  21. proc zlistAlikeSort {a b} {
  22. if {[lindex $a 0] > [lindex $b 0]} {return 1}
  23. if {[lindex $a 0] < [lindex $b 0]} {return -1}
  24. string compare [lindex $a 1] [lindex $b 1]
  25. }
  26. # Return all log lines starting with the first line that contains a warning.
  27. # Generally, this will be an assertion error with a stack trace.
  28. proc warnings_from_file {filename} {
  29. set lines [split [exec cat $filename] "\n"]
  30. set matched 0
  31. set logall 0
  32. set result {}
  33. foreach line $lines {
  34. if {[string match {*REDIS BUG REPORT START*} $line]} {
  35. set logall 1
  36. }
  37. if {[regexp {^\[\d+\]\s+\d+\s+\w+\s+\d{2}:\d{2}:\d{2} \#} $line]} {
  38. set matched 1
  39. }
  40. if {$logall || $matched} {
  41. lappend result $line
  42. }
  43. }
  44. join $result "\n"
  45. }
  46. # Return value for INFO property
  47. proc status {r property} {
  48. if {[regexp "\r\n$property:(.*?)\r\n" [{*}$r info] _ value]} {
  49. set _ $value
  50. }
  51. }
  52. proc waitForBgsave r {
  53. while 1 {
  54. if {[status r rdb_bgsave_in_progress] eq 1} {
  55. if {$::verbose} {
  56. puts -nonewline "\nWaiting for background save to finish... "
  57. flush stdout
  58. }
  59. after 1000
  60. } else {
  61. break
  62. }
  63. }
  64. }
  65. proc waitForBgrewriteaof r {
  66. while 1 {
  67. if {[status r aof_rewrite_in_progress] eq 1} {
  68. if {$::verbose} {
  69. puts -nonewline "\nWaiting for background AOF rewrite to finish... "
  70. flush stdout
  71. }
  72. after 1000
  73. } else {
  74. break
  75. }
  76. }
  77. }
  78. proc wait_for_sync r {
  79. while 1 {
  80. if {[status $r master_link_status] eq "down"} {
  81. after 10
  82. } else {
  83. break
  84. }
  85. }
  86. }
  87. proc wait_for_ofs_sync {r1 r2} {
  88. wait_for_condition 50 100 {
  89. [status $r1 master_repl_offset] eq [status $r2 master_repl_offset]
  90. } else {
  91. fail "replica didn't sync in time"
  92. }
  93. }
  94. proc wait_for_log_message {srv_idx pattern last_lines maxtries delay} {
  95. set retry $maxtries
  96. set stdout [srv $srv_idx stdout]
  97. while {$retry} {
  98. set result [exec tail -$last_lines < $stdout]
  99. set result [split $result "\n"]
  100. foreach line $result {
  101. if {[string match $pattern $line]} {
  102. return $line
  103. }
  104. }
  105. incr retry -1
  106. after $delay
  107. }
  108. if {$retry == 0} {
  109. fail "log message of '$pattern' not found"
  110. }
  111. }
  112. # Random integer between 0 and max (excluded).
  113. proc randomInt {max} {
  114. expr {int(rand()*$max)}
  115. }
  116. # Random signed integer between -max and max (both extremes excluded).
  117. proc randomSignedInt {max} {
  118. set i [randomInt $max]
  119. if {rand() > 0.5} {
  120. set i -$i
  121. }
  122. return $i
  123. }
  124. proc randpath args {
  125. set path [expr {int(rand()*[llength $args])}]
  126. uplevel 1 [lindex $args $path]
  127. }
  128. proc randomValue {} {
  129. randpath {
  130. # Small enough to likely collide
  131. randomSignedInt 1000
  132. } {
  133. # 32 bit compressible signed/unsigned
  134. randpath {randomSignedInt 2000000000} {randomSignedInt 4000000000}
  135. } {
  136. # 64 bit
  137. randpath {randomSignedInt 1000000000000}
  138. } {
  139. # Random string
  140. randpath {randstring 0 256 alpha} \
  141. {randstring 0 256 compr} \
  142. {randstring 0 256 binary}
  143. }
  144. }
  145. proc randomKey {} {
  146. randpath {
  147. # Small enough to likely collide
  148. randomInt 1000
  149. } {
  150. # 32 bit compressible signed/unsigned
  151. randpath {randomInt 2000000000} {randomInt 4000000000}
  152. } {
  153. # 64 bit
  154. randpath {randomInt 1000000000000}
  155. } {
  156. # Random string
  157. randpath {randstring 1 256 alpha} \
  158. {randstring 1 256 compr}
  159. }
  160. }
  161. proc findKeyWithType {r type} {
  162. for {set j 0} {$j < 20} {incr j} {
  163. set k [{*}$r randomkey]
  164. if {$k eq {}} {
  165. return {}
  166. }
  167. if {[{*}$r type $k] eq $type} {
  168. return $k
  169. }
  170. }
  171. return {}
  172. }
  173. proc createComplexDataset {r ops {opt {}}} {
  174. for {set j 0} {$j < $ops} {incr j} {
  175. set k [randomKey]
  176. set k2 [randomKey]
  177. set f [randomValue]
  178. set v [randomValue]
  179. if {[lsearch -exact $opt useexpire] != -1} {
  180. if {rand() < 0.1} {
  181. {*}$r expire [randomKey] [randomInt 2]
  182. }
  183. }
  184. randpath {
  185. set d [expr {rand()}]
  186. } {
  187. set d [expr {rand()}]
  188. } {
  189. set d [expr {rand()}]
  190. } {
  191. set d [expr {rand()}]
  192. } {
  193. set d [expr {rand()}]
  194. } {
  195. randpath {set d +inf} {set d -inf}
  196. }
  197. set t [{*}$r type $k]
  198. if {$t eq {none}} {
  199. randpath {
  200. {*}$r set $k $v
  201. } {
  202. {*}$r lpush $k $v
  203. } {
  204. {*}$r sadd $k $v
  205. } {
  206. {*}$r zadd $k $d $v
  207. } {
  208. {*}$r hset $k $f $v
  209. } {
  210. {*}$r del $k
  211. }
  212. set t [{*}$r type $k]
  213. }
  214. switch $t {
  215. {string} {
  216. # Nothing to do
  217. }
  218. {list} {
  219. randpath {{*}$r lpush $k $v} \
  220. {{*}$r rpush $k $v} \
  221. {{*}$r lrem $k 0 $v} \
  222. {{*}$r rpop $k} \
  223. {{*}$r lpop $k}
  224. }
  225. {set} {
  226. randpath {{*}$r sadd $k $v} \
  227. {{*}$r srem $k $v} \
  228. {
  229. set otherset [findKeyWithType {*}$r set]
  230. if {$otherset ne {}} {
  231. randpath {
  232. {*}$r sunionstore $k2 $k $otherset
  233. } {
  234. {*}$r sinterstore $k2 $k $otherset
  235. } {
  236. {*}$r sdiffstore $k2 $k $otherset
  237. }
  238. }
  239. }
  240. }
  241. {zset} {
  242. randpath {{*}$r zadd $k $d $v} \
  243. {{*}$r zrem $k $v} \
  244. {
  245. set otherzset [findKeyWithType {*}$r zset]
  246. if {$otherzset ne {}} {
  247. randpath {
  248. {*}$r zunionstore $k2 2 $k $otherzset
  249. } {
  250. {*}$r zinterstore $k2 2 $k $otherzset
  251. }
  252. }
  253. }
  254. }
  255. {hash} {
  256. randpath {{*}$r hset $k $f $v} \
  257. {{*}$r hdel $k $f}
  258. }
  259. }
  260. }
  261. }
  262. proc formatCommand {args} {
  263. set cmd "*[llength $args]\r\n"
  264. foreach a $args {
  265. append cmd "$[string length $a]\r\n$a\r\n"
  266. }
  267. set _ $cmd
  268. }
  269. proc csvdump r {
  270. set o {}
  271. for {set db 0} {$db < 16} {incr db} {
  272. {*}$r select $db
  273. foreach k [lsort [{*}$r keys *]] {
  274. set type [{*}$r type $k]
  275. append o [csvstring $db] , [csvstring $k] , [csvstring $type] ,
  276. switch $type {
  277. string {
  278. append o [csvstring [{*}$r get $k]] "\n"
  279. }
  280. list {
  281. foreach e [{*}$r lrange $k 0 -1] {
  282. append o [csvstring $e] ,
  283. }
  284. append o "\n"
  285. }
  286. set {
  287. foreach e [lsort [{*}$r smembers $k]] {
  288. append o [csvstring $e] ,
  289. }
  290. append o "\n"
  291. }
  292. zset {
  293. foreach e [{*}$r zrange $k 0 -1 withscores] {
  294. append o [csvstring $e] ,
  295. }
  296. append o "\n"
  297. }
  298. hash {
  299. set fields [{*}$r hgetall $k]
  300. set newfields {}
  301. foreach {k v} $fields {
  302. lappend newfields [list $k $v]
  303. }
  304. set fields [lsort -index 0 $newfields]
  305. foreach kv $fields {
  306. append o [csvstring [lindex $kv 0]] ,
  307. append o [csvstring [lindex $kv 1]] ,
  308. }
  309. append o "\n"
  310. }
  311. }
  312. }
  313. }
  314. {*}$r select 9
  315. return $o
  316. }
  317. proc csvstring s {
  318. return "\"$s\""
  319. }
  320. proc roundFloat f {
  321. format "%.10g" $f
  322. }
  323. proc find_available_port start {
  324. for {set j $start} {$j < $start+1024} {incr j} {
  325. if {[catch {set fd1 [socket 127.0.0.1 $j]}] &&
  326. [catch {set fd2 [socket 127.0.0.1 [expr $j+10000]]}]} {
  327. return $j
  328. } else {
  329. catch {
  330. close $fd1
  331. close $fd2
  332. }
  333. }
  334. }
  335. if {$j == $start+1024} {
  336. error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
  337. }
  338. }
  339. # Test if TERM looks like to support colors
  340. proc color_term {} {
  341. expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
  342. }
  343. proc colorstr {color str} {
  344. if {[color_term]} {
  345. set b 0
  346. if {[string range $color 0 4] eq {bold-}} {
  347. set b 1
  348. set color [string range $color 5 end]
  349. }
  350. switch $color {
  351. red {set colorcode {31}}
  352. green {set colorcode {32}}
  353. yellow {set colorcode {33}}
  354. blue {set colorcode {34}}
  355. magenta {set colorcode {35}}
  356. cyan {set colorcode {36}}
  357. white {set colorcode {37}}
  358. default {set colorcode {37}}
  359. }
  360. if {$colorcode ne {}} {
  361. return "\033\[$b;${colorcode};49m$str\033\[0m"
  362. }
  363. } else {
  364. return $str
  365. }
  366. }
  367. # Execute a background process writing random data for the specified number
  368. # of seconds to the specified Redis instance.
  369. proc start_write_load {host port seconds} {
  370. set tclsh [info nameofexecutable]
  371. exec $tclsh tests/helpers/gen_write_load.tcl $host $port $seconds $::tls &
  372. }
  373. # Stop a process generating write load executed with start_write_load.
  374. proc stop_write_load {handle} {
  375. catch {exec /bin/kill -9 $handle}
  376. }
  377. proc K { x y } { set x }
  378. # Shuffle a list. From Tcl wiki. Originally from Steve Cohen that improved
  379. # other versions. Code should be under public domain.
  380. proc lshuffle {list} {
  381. set n [llength $list]
  382. while {$n>0} {
  383. set j [expr {int(rand()*$n)}]
  384. lappend slist [lindex $list $j]
  385. incr n -1
  386. set temp [lindex $list $n]
  387. set list [lreplace [K $list [set list {}]] $j $j $temp]
  388. }
  389. return $slist
  390. }
  391. # Execute a background process writing complex data for the specified number
  392. # of ops to the specified Redis instance.
  393. proc start_bg_complex_data {host port db ops} {
  394. set tclsh [info nameofexecutable]
  395. exec $tclsh tests/helpers/bg_complex_data.tcl $host $port $db $ops $::tls &
  396. }
  397. # Stop a process generating write load executed with start_bg_complex_data.
  398. proc stop_bg_complex_data {handle} {
  399. catch {exec /bin/kill -9 $handle}
  400. }