2
0

util.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. # count current log lines in server's stdout
  95. proc count_log_lines {srv_idx} {
  96. set _ [exec wc -l < [srv $srv_idx stdout]]
  97. }
  98. # verify pattern exists in server's sdtout after a certain line number
  99. proc verify_log_message {srv_idx pattern from_line} {
  100. set lines_after [count_log_lines]
  101. set lines [expr $lines_after - $from_line]
  102. set result [exec tail -$lines < [srv $srv_idx stdout]]
  103. if {![string match $pattern $result]} {
  104. error "assertion:expected message not found in log file: $pattern"
  105. }
  106. }
  107. # wait for pattern to be found in server's stdout after certain line number
  108. proc wait_for_log_message {srv_idx pattern from_line maxtries delay} {
  109. set retry $maxtries
  110. set stdout [srv $srv_idx stdout]
  111. while {$retry} {
  112. set result [exec tail -n +$from_line < $stdout]
  113. set result [split $result "\n"]
  114. foreach line $result {
  115. if {[string match $pattern $line]} {
  116. return $line
  117. }
  118. }
  119. incr retry -1
  120. after $delay
  121. }
  122. if {$retry == 0} {
  123. fail "log message of '$pattern' not found in $stdout after line: $from_line"
  124. }
  125. }
  126. # Random integer between 0 and max (excluded).
  127. proc randomInt {max} {
  128. expr {int(rand()*$max)}
  129. }
  130. # Random signed integer between -max and max (both extremes excluded).
  131. proc randomSignedInt {max} {
  132. set i [randomInt $max]
  133. if {rand() > 0.5} {
  134. set i -$i
  135. }
  136. return $i
  137. }
  138. proc randpath args {
  139. set path [expr {int(rand()*[llength $args])}]
  140. uplevel 1 [lindex $args $path]
  141. }
  142. proc randomValue {} {
  143. randpath {
  144. # Small enough to likely collide
  145. randomSignedInt 1000
  146. } {
  147. # 32 bit compressible signed/unsigned
  148. randpath {randomSignedInt 2000000000} {randomSignedInt 4000000000}
  149. } {
  150. # 64 bit
  151. randpath {randomSignedInt 1000000000000}
  152. } {
  153. # Random string
  154. randpath {randstring 0 256 alpha} \
  155. {randstring 0 256 compr} \
  156. {randstring 0 256 binary}
  157. }
  158. }
  159. proc randomKey {} {
  160. randpath {
  161. # Small enough to likely collide
  162. randomInt 1000
  163. } {
  164. # 32 bit compressible signed/unsigned
  165. randpath {randomInt 2000000000} {randomInt 4000000000}
  166. } {
  167. # 64 bit
  168. randpath {randomInt 1000000000000}
  169. } {
  170. # Random string
  171. randpath {randstring 1 256 alpha} \
  172. {randstring 1 256 compr}
  173. }
  174. }
  175. proc findKeyWithType {r type} {
  176. for {set j 0} {$j < 20} {incr j} {
  177. set k [{*}$r randomkey]
  178. if {$k eq {}} {
  179. return {}
  180. }
  181. if {[{*}$r type $k] eq $type} {
  182. return $k
  183. }
  184. }
  185. return {}
  186. }
  187. proc createComplexDataset {r ops {opt {}}} {
  188. for {set j 0} {$j < $ops} {incr j} {
  189. set k [randomKey]
  190. set k2 [randomKey]
  191. set f [randomValue]
  192. set v [randomValue]
  193. if {[lsearch -exact $opt useexpire] != -1} {
  194. if {rand() < 0.1} {
  195. {*}$r expire [randomKey] [randomInt 2]
  196. }
  197. }
  198. randpath {
  199. set d [expr {rand()}]
  200. } {
  201. set d [expr {rand()}]
  202. } {
  203. set d [expr {rand()}]
  204. } {
  205. set d [expr {rand()}]
  206. } {
  207. set d [expr {rand()}]
  208. } {
  209. randpath {set d +inf} {set d -inf}
  210. }
  211. set t [{*}$r type $k]
  212. if {$t eq {none}} {
  213. randpath {
  214. {*}$r set $k $v
  215. } {
  216. {*}$r lpush $k $v
  217. } {
  218. {*}$r sadd $k $v
  219. } {
  220. {*}$r zadd $k $d $v
  221. } {
  222. {*}$r hset $k $f $v
  223. } {
  224. {*}$r del $k
  225. }
  226. set t [{*}$r type $k]
  227. }
  228. switch $t {
  229. {string} {
  230. # Nothing to do
  231. }
  232. {list} {
  233. randpath {{*}$r lpush $k $v} \
  234. {{*}$r rpush $k $v} \
  235. {{*}$r lrem $k 0 $v} \
  236. {{*}$r rpop $k} \
  237. {{*}$r lpop $k}
  238. }
  239. {set} {
  240. randpath {{*}$r sadd $k $v} \
  241. {{*}$r srem $k $v} \
  242. {
  243. set otherset [findKeyWithType {*}$r set]
  244. if {$otherset ne {}} {
  245. randpath {
  246. {*}$r sunionstore $k2 $k $otherset
  247. } {
  248. {*}$r sinterstore $k2 $k $otherset
  249. } {
  250. {*}$r sdiffstore $k2 $k $otherset
  251. }
  252. }
  253. }
  254. }
  255. {zset} {
  256. randpath {{*}$r zadd $k $d $v} \
  257. {{*}$r zrem $k $v} \
  258. {
  259. set otherzset [findKeyWithType {*}$r zset]
  260. if {$otherzset ne {}} {
  261. randpath {
  262. {*}$r zunionstore $k2 2 $k $otherzset
  263. } {
  264. {*}$r zinterstore $k2 2 $k $otherzset
  265. }
  266. }
  267. }
  268. }
  269. {hash} {
  270. randpath {{*}$r hset $k $f $v} \
  271. {{*}$r hdel $k $f}
  272. }
  273. }
  274. }
  275. }
  276. proc formatCommand {args} {
  277. set cmd "*[llength $args]\r\n"
  278. foreach a $args {
  279. append cmd "$[string length $a]\r\n$a\r\n"
  280. }
  281. set _ $cmd
  282. }
  283. proc csvdump r {
  284. set o {}
  285. for {set db 0} {$db < 16} {incr db} {
  286. {*}$r select $db
  287. foreach k [lsort [{*}$r keys *]] {
  288. set type [{*}$r type $k]
  289. append o [csvstring $db] , [csvstring $k] , [csvstring $type] ,
  290. switch $type {
  291. string {
  292. append o [csvstring [{*}$r get $k]] "\n"
  293. }
  294. list {
  295. foreach e [{*}$r lrange $k 0 -1] {
  296. append o [csvstring $e] ,
  297. }
  298. append o "\n"
  299. }
  300. set {
  301. foreach e [lsort [{*}$r smembers $k]] {
  302. append o [csvstring $e] ,
  303. }
  304. append o "\n"
  305. }
  306. zset {
  307. foreach e [{*}$r zrange $k 0 -1 withscores] {
  308. append o [csvstring $e] ,
  309. }
  310. append o "\n"
  311. }
  312. hash {
  313. set fields [{*}$r hgetall $k]
  314. set newfields {}
  315. foreach {k v} $fields {
  316. lappend newfields [list $k $v]
  317. }
  318. set fields [lsort -index 0 $newfields]
  319. foreach kv $fields {
  320. append o [csvstring [lindex $kv 0]] ,
  321. append o [csvstring [lindex $kv 1]] ,
  322. }
  323. append o "\n"
  324. }
  325. }
  326. }
  327. }
  328. {*}$r select 9
  329. return $o
  330. }
  331. proc csvstring s {
  332. return "\"$s\""
  333. }
  334. proc roundFloat f {
  335. format "%.10g" $f
  336. }
  337. set ::last_port_attempted 0
  338. proc find_available_port {start count} {
  339. set port [expr $::last_port_attempted + 1]
  340. for {set attempts 0} {$attempts < $count} {incr attempts} {
  341. if {$port < $start || $port >= $start+$count} {
  342. set port $start
  343. }
  344. if {[catch {set fd1 [socket 127.0.0.1 $port]}] &&
  345. [catch {set fd2 [socket 127.0.0.1 [expr $port+10000]]}]} {
  346. set ::last_port_attempted $port
  347. return $port
  348. } else {
  349. catch {
  350. close $fd1
  351. close $fd2
  352. }
  353. }
  354. incr port
  355. }
  356. error "Can't find a non busy port in the $start-[expr {$start+$count-1}] range."
  357. }
  358. # Test if TERM looks like to support colors
  359. proc color_term {} {
  360. expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
  361. }
  362. proc colorstr {color str} {
  363. if {[color_term]} {
  364. set b 0
  365. if {[string range $color 0 4] eq {bold-}} {
  366. set b 1
  367. set color [string range $color 5 end]
  368. }
  369. switch $color {
  370. red {set colorcode {31}}
  371. green {set colorcode {32}}
  372. yellow {set colorcode {33}}
  373. blue {set colorcode {34}}
  374. magenta {set colorcode {35}}
  375. cyan {set colorcode {36}}
  376. white {set colorcode {37}}
  377. default {set colorcode {37}}
  378. }
  379. if {$colorcode ne {}} {
  380. return "\033\[$b;${colorcode};49m$str\033\[0m"
  381. }
  382. } else {
  383. return $str
  384. }
  385. }
  386. # Execute a background process writing random data for the specified number
  387. # of seconds to the specified Redis instance.
  388. proc start_write_load {host port seconds} {
  389. set tclsh [info nameofexecutable]
  390. exec $tclsh tests/helpers/gen_write_load.tcl $host $port $seconds $::tls &
  391. }
  392. # Stop a process generating write load executed with start_write_load.
  393. proc stop_write_load {handle} {
  394. catch {exec /bin/kill -9 $handle}
  395. }
  396. proc K { x y } { set x }
  397. # Shuffle a list. From Tcl wiki. Originally from Steve Cohen that improved
  398. # other versions. Code should be under public domain.
  399. proc lshuffle {list} {
  400. set n [llength $list]
  401. while {$n>0} {
  402. set j [expr {int(rand()*$n)}]
  403. lappend slist [lindex $list $j]
  404. incr n -1
  405. set temp [lindex $list $n]
  406. set list [lreplace [K $list [set list {}]] $j $j $temp]
  407. }
  408. return $slist
  409. }
  410. # Execute a background process writing complex data for the specified number
  411. # of ops to the specified Redis instance.
  412. proc start_bg_complex_data {host port db ops} {
  413. set tclsh [info nameofexecutable]
  414. exec $tclsh tests/helpers/bg_complex_data.tcl $host $port $db $ops $::tls &
  415. }
  416. # Stop a process generating write load executed with start_bg_complex_data.
  417. proc stop_bg_complex_data {handle} {
  418. catch {exec /bin/kill -9 $handle}
  419. }