2
0

util.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. set ::last_port_attempted 0
  324. proc find_available_port {start count} {
  325. set port [expr $::last_port_attempted + 1]
  326. for {set attempts 0} {$attempts < $count} {incr attempts} {
  327. if {$port < $start || $port >= $start+$count} {
  328. set port $start
  329. }
  330. if {[catch {set fd1 [socket 127.0.0.1 $port]}] &&
  331. [catch {set fd2 [socket 127.0.0.1 [expr $port+10000]]}]} {
  332. set ::last_port_attempted $port
  333. return $port
  334. } else {
  335. catch {
  336. close $fd1
  337. close $fd2
  338. }
  339. }
  340. incr port
  341. }
  342. error "Can't find a non busy port in the $start-[expr {$start+$count-1}] range."
  343. }
  344. # Test if TERM looks like to support colors
  345. proc color_term {} {
  346. expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
  347. }
  348. proc colorstr {color str} {
  349. if {[color_term]} {
  350. set b 0
  351. if {[string range $color 0 4] eq {bold-}} {
  352. set b 1
  353. set color [string range $color 5 end]
  354. }
  355. switch $color {
  356. red {set colorcode {31}}
  357. green {set colorcode {32}}
  358. yellow {set colorcode {33}}
  359. blue {set colorcode {34}}
  360. magenta {set colorcode {35}}
  361. cyan {set colorcode {36}}
  362. white {set colorcode {37}}
  363. default {set colorcode {37}}
  364. }
  365. if {$colorcode ne {}} {
  366. return "\033\[$b;${colorcode};49m$str\033\[0m"
  367. }
  368. } else {
  369. return $str
  370. }
  371. }
  372. # Execute a background process writing random data for the specified number
  373. # of seconds to the specified Redis instance.
  374. proc start_write_load {host port seconds} {
  375. set tclsh [info nameofexecutable]
  376. exec $tclsh tests/helpers/gen_write_load.tcl $host $port $seconds $::tls &
  377. }
  378. # Stop a process generating write load executed with start_write_load.
  379. proc stop_write_load {handle} {
  380. catch {exec /bin/kill -9 $handle}
  381. }
  382. proc K { x y } { set x }
  383. # Shuffle a list. From Tcl wiki. Originally from Steve Cohen that improved
  384. # other versions. Code should be under public domain.
  385. proc lshuffle {list} {
  386. set n [llength $list]
  387. while {$n>0} {
  388. set j [expr {int(rand()*$n)}]
  389. lappend slist [lindex $list $j]
  390. incr n -1
  391. set temp [lindex $list $n]
  392. set list [lreplace [K $list [set list {}]] $j $j $temp]
  393. }
  394. return $slist
  395. }
  396. # Execute a background process writing complex data for the specified number
  397. # of ops to the specified Redis instance.
  398. proc start_bg_complex_data {host port db ops} {
  399. set tclsh [info nameofexecutable]
  400. exec $tclsh tests/helpers/bg_complex_data.tcl $host $port $db $ops $::tls &
  401. }
  402. # Stop a process generating write load executed with start_bg_complex_data.
  403. proc stop_bg_complex_data {handle} {
  404. catch {exec /bin/kill -9 $handle}
  405. }