instances.tcl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. # Multi-instance test framework.
  2. # This is used in order to test Sentinel and Redis Cluster, and provides
  3. # basic capabilities for spawning and handling N parallel Redis / Sentinel
  4. # instances.
  5. #
  6. # Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com
  7. # This software is released under the BSD License. See the COPYING file for
  8. # more information.
  9. package require Tcl 8.5
  10. set tcl_precision 17
  11. source ../support/redis.tcl
  12. source ../support/util.tcl
  13. source ../support/server.tcl
  14. source ../support/test.tcl
  15. set ::verbose 0
  16. set ::valgrind 0
  17. set ::pause_on_error 0
  18. set ::simulate_error 0
  19. set ::failed 0
  20. set ::sentinel_instances {}
  21. set ::redis_instances {}
  22. set ::sentinel_base_port 20000
  23. set ::redis_base_port 30000
  24. set ::pids {} ; # We kill everything at exit
  25. set ::dirs {} ; # We remove all the temp dirs at exit
  26. set ::run_matching {} ; # If non empty, only tests matching pattern are run.
  27. if {[catch {cd tmp}]} {
  28. puts "tmp directory not found."
  29. puts "Please run this test from the Redis source root."
  30. exit 1
  31. }
  32. # Execute the specified instance of the server specified by 'type', using
  33. # the provided configuration file. Returns the PID of the process.
  34. proc exec_instance {type cfgfile} {
  35. if {$type eq "redis"} {
  36. set prgname redis-server
  37. } elseif {$type eq "sentinel"} {
  38. set prgname redis-sentinel
  39. } else {
  40. error "Unknown instance type."
  41. }
  42. if {$::valgrind} {
  43. set pid [exec valgrind --track-origins=yes --suppressions=../../../src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full ../../../src/${prgname} $cfgfile &]
  44. } else {
  45. set pid [exec ../../../src/${prgname} $cfgfile &]
  46. }
  47. return $pid
  48. }
  49. # Spawn a redis or sentinel instance, depending on 'type'.
  50. proc spawn_instance {type base_port count {conf {}}} {
  51. for {set j 0} {$j < $count} {incr j} {
  52. set port [find_available_port $base_port]
  53. incr base_port
  54. puts "Starting $type #$j at port $port"
  55. # Create a directory for this instance.
  56. set dirname "${type}_${j}"
  57. lappend ::dirs $dirname
  58. catch {exec rm -rf $dirname}
  59. file mkdir $dirname
  60. # Write the instance config file.
  61. set cfgfile [file join $dirname $type.conf]
  62. set cfg [open $cfgfile w]
  63. puts $cfg "port $port"
  64. puts $cfg "dir ./$dirname"
  65. puts $cfg "logfile log.txt"
  66. # Add additional config files
  67. foreach directive $conf {
  68. puts $cfg $directive
  69. }
  70. close $cfg
  71. # Finally exec it and remember the pid for later cleanup.
  72. set pid [exec_instance $type $cfgfile]
  73. lappend ::pids $pid
  74. # Check availability
  75. if {[server_is_up 127.0.0.1 $port 100] == 0} {
  76. abort_sentinel_test "Problems starting $type #$j: ping timeout"
  77. }
  78. # Push the instance into the right list
  79. set link [redis 127.0.0.1 $port]
  80. $link reconnect 1
  81. lappend ::${type}_instances [list \
  82. pid $pid \
  83. host 127.0.0.1 \
  84. port $port \
  85. link $link \
  86. ]
  87. }
  88. }
  89. proc log_crashes {} {
  90. set start_pattern {*REDIS BUG REPORT START*}
  91. set logs [glob */log.txt]
  92. foreach log $logs {
  93. set fd [open $log]
  94. set found 0
  95. while {[gets $fd line] >= 0} {
  96. if {[string match $start_pattern $line]} {
  97. puts "\n*** Crash report found in $log ***"
  98. set found 1
  99. }
  100. if {$found} {puts $line}
  101. }
  102. }
  103. }
  104. proc cleanup {} {
  105. puts "Cleaning up..."
  106. log_crashes
  107. foreach pid $::pids {
  108. catch {exec kill -9 $pid}
  109. }
  110. foreach dir $::dirs {
  111. catch {exec rm -rf $dir}
  112. }
  113. }
  114. proc abort_sentinel_test msg {
  115. incr ::failed
  116. puts "WARNING: Aborting the test."
  117. puts ">>>>>>>> $msg"
  118. if {$::pause_on_error} pause_on_error
  119. cleanup
  120. exit 1
  121. }
  122. proc parse_options {} {
  123. for {set j 0} {$j < [llength $::argv]} {incr j} {
  124. set opt [lindex $::argv $j]
  125. set val [lindex $::argv [expr $j+1]]
  126. if {$opt eq "--single"} {
  127. incr j
  128. set ::run_matching "*${val}*"
  129. } elseif {$opt eq "--pause-on-error"} {
  130. set ::pause_on_error 1
  131. } elseif {$opt eq "--fail"} {
  132. set ::simulate_error 1
  133. } elseif {$opt eq {--valgrind}} {
  134. set ::valgrind 1
  135. } elseif {$opt eq "--help"} {
  136. puts "Hello, I'm sentinel.tcl and I run Sentinel unit tests."
  137. puts "\nOptions:"
  138. puts "--single <pattern> Only runs tests specified by pattern."
  139. puts "--pause-on-error Pause for manual inspection on error."
  140. puts "--fail Simulate a test failure."
  141. puts "--valgrind Run with valgrind."
  142. puts "--help Shows this help."
  143. exit 0
  144. } else {
  145. puts "Unknown option $opt"
  146. exit 1
  147. }
  148. }
  149. }
  150. # If --pause-on-error option was passed at startup this function is called
  151. # on error in order to give the developer a chance to understand more about
  152. # the error condition while the instances are still running.
  153. proc pause_on_error {} {
  154. puts ""
  155. puts [colorstr yellow "*** Please inspect the error now ***"]
  156. puts "\nType \"continue\" to resume the test, \"help\" for help screen.\n"
  157. while 1 {
  158. puts -nonewline "> "
  159. flush stdout
  160. set line [gets stdin]
  161. set argv [split $line " "]
  162. set cmd [lindex $argv 0]
  163. if {$cmd eq {continue}} {
  164. break
  165. } elseif {$cmd eq {show-redis-logs}} {
  166. set count 10
  167. if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
  168. foreach_redis_id id {
  169. puts "=== REDIS $id ===="
  170. puts [exec tail -$count redis_$id/log.txt]
  171. puts "---------------------\n"
  172. }
  173. } elseif {$cmd eq {show-sentinel-logs}} {
  174. set count 10
  175. if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
  176. foreach_sentinel_id id {
  177. puts "=== SENTINEL $id ===="
  178. puts [exec tail -$count sentinel_$id/log.txt]
  179. puts "---------------------\n"
  180. }
  181. } elseif {$cmd eq {ls}} {
  182. foreach_redis_id id {
  183. puts -nonewline "Redis $id"
  184. set errcode [catch {
  185. set str {}
  186. append str "@[RI $id tcp_port]: "
  187. append str "[RI $id role] "
  188. if {[RI $id role] eq {slave}} {
  189. append str "[RI $id master_host]:[RI $id master_port]"
  190. }
  191. set str
  192. } retval]
  193. if {$errcode} {
  194. puts " -- $retval"
  195. } else {
  196. puts $retval
  197. }
  198. }
  199. foreach_sentinel_id id {
  200. puts -nonewline "Sentinel $id"
  201. set errcode [catch {
  202. set str {}
  203. append str "@[SI $id tcp_port]: "
  204. append str "[join [S $id sentinel get-master-addr-by-name mymaster]]"
  205. set str
  206. } retval]
  207. if {$errcode} {
  208. puts " -- $retval"
  209. } else {
  210. puts $retval
  211. }
  212. }
  213. } elseif {$cmd eq {help}} {
  214. puts "ls List Sentinel and Redis instances."
  215. puts "show-sentinel-logs \[N\] Show latest N lines of logs."
  216. puts "show-redis-logs \[N\] Show latest N lines of logs."
  217. puts "S <id> cmd ... arg Call command in Sentinel <id>."
  218. puts "R <id> cmd ... arg Call command in Redis <id>."
  219. puts "SI <id> <field> Show Sentinel <id> INFO <field>."
  220. puts "RI <id> <field> Show Sentinel <id> INFO <field>."
  221. puts "continue Resume test."
  222. } else {
  223. set errcode [catch {eval $line} retval]
  224. if {$retval ne {}} {puts "$retval"}
  225. }
  226. }
  227. }
  228. # We redefine 'test' as for Sentinel we don't use the server-client
  229. # architecture for the test, everything is sequential.
  230. proc test {descr code} {
  231. set ts [clock format [clock seconds] -format %H:%M:%S]
  232. puts -nonewline "$ts> $descr: "
  233. flush stdout
  234. if {[catch {set retval [uplevel 1 $code]} error]} {
  235. incr ::failed
  236. if {[string match "assertion:*" $error]} {
  237. set msg [string range $error 10 end]
  238. puts [colorstr red $msg]
  239. if {$::pause_on_error} pause_on_error
  240. puts "(Jumping to next unit after error)"
  241. return -code continue
  242. } else {
  243. # Re-raise, let handler up the stack take care of this.
  244. error $error $::errorInfo
  245. }
  246. } else {
  247. puts [colorstr green OK]
  248. }
  249. }
  250. # Check memory leaks when running on OSX using the "leaks" utility.
  251. proc check_leaks instance_types {
  252. if {[string match {*Darwin*} [exec uname -a]]} {
  253. puts -nonewline "Testing for memory leaks..."; flush stdout
  254. foreach type $instance_types {
  255. foreach_instance_id [set ::${type}_instances] id {
  256. if {[instance_is_killed $type $id]} continue
  257. set pid [get_instance_attrib $type $id pid]
  258. set output {0 leaks}
  259. catch {exec leaks $pid} output
  260. if {[string match {*process does not exist*} $output] ||
  261. [string match {*cannot examine*} $output]} {
  262. # In a few tests we kill the server process.
  263. set output "0 leaks"
  264. } else {
  265. puts -nonewline "$type/$pid "
  266. flush stdout
  267. }
  268. if {![string match {*0 leaks*} $output]} {
  269. puts [colorstr red "=== MEMORY LEAK DETECTED ==="]
  270. puts "Instance type $type, ID $id:"
  271. puts $output
  272. puts "==="
  273. incr ::failed
  274. }
  275. }
  276. }
  277. puts ""
  278. }
  279. }
  280. # Execute all the units inside the 'tests' directory.
  281. proc run_tests {} {
  282. set tests [lsort [glob ../tests/*]]
  283. foreach test $tests {
  284. if {$::run_matching ne {} && [string match $::run_matching $test] == 0} {
  285. continue
  286. }
  287. if {[file isdirectory $test]} continue
  288. puts [colorstr yellow "Testing unit: [lindex [file split $test] end]"]
  289. source $test
  290. check_leaks {redis sentinel}
  291. }
  292. }
  293. # Print a message and exists with 0 / 1 according to zero or more failures.
  294. proc end_tests {} {
  295. if {$::failed == 0} {
  296. puts "GOOD! No errors."
  297. exit 0
  298. } else {
  299. puts "WARNING $::failed tests faield."
  300. exit 1
  301. }
  302. }
  303. # The "S" command is used to interact with the N-th Sentinel.
  304. # The general form is:
  305. #
  306. # S <sentinel-id> command arg arg arg ...
  307. #
  308. # Example to ping the Sentinel 0 (first instance): S 0 PING
  309. proc S {n args} {
  310. set s [lindex $::sentinel_instances $n]
  311. [dict get $s link] {*}$args
  312. }
  313. # Like R but to chat with Redis instances.
  314. proc R {n args} {
  315. set r [lindex $::redis_instances $n]
  316. [dict get $r link] {*}$args
  317. }
  318. proc get_info_field {info field} {
  319. set fl [string length $field]
  320. append field :
  321. foreach line [split $info "\n"] {
  322. set line [string trim $line "\r\n "]
  323. if {[string range $line 0 $fl] eq $field} {
  324. return [string range $line [expr {$fl+1}] end]
  325. }
  326. }
  327. return {}
  328. }
  329. proc SI {n field} {
  330. get_info_field [S $n info] $field
  331. }
  332. proc RI {n field} {
  333. get_info_field [R $n info] $field
  334. }
  335. # Iterate over IDs of sentinel or redis instances.
  336. proc foreach_instance_id {instances idvar code} {
  337. upvar 1 $idvar id
  338. for {set id 0} {$id < [llength $instances]} {incr id} {
  339. set errcode [catch {uplevel 1 $code} result]
  340. if {$errcode == 1} {
  341. error $result $::errorInfo $::errorCode
  342. } elseif {$errcode == 4} {
  343. continue
  344. } elseif {$errcode == 3} {
  345. break
  346. } elseif {$errcode != 0} {
  347. return -code $errcode $result
  348. }
  349. }
  350. }
  351. proc foreach_sentinel_id {idvar code} {
  352. set errcode [catch {uplevel 1 [list foreach_instance_id $::sentinel_instances $idvar $code]} result]
  353. return -code $errcode $result
  354. }
  355. proc foreach_redis_id {idvar code} {
  356. set errcode [catch {uplevel 1 [list foreach_instance_id $::redis_instances $idvar $code]} result]
  357. return -code $errcode $result
  358. }
  359. # Get the specific attribute of the specified instance type, id.
  360. proc get_instance_attrib {type id attrib} {
  361. dict get [lindex [set ::${type}_instances] $id] $attrib
  362. }
  363. # Set the specific attribute of the specified instance type, id.
  364. proc set_instance_attrib {type id attrib newval} {
  365. set d [lindex [set ::${type}_instances] $id]
  366. dict set d $attrib $newval
  367. lset ::${type}_instances $id $d
  368. }
  369. # Create a master-slave cluster of the given number of total instances.
  370. # The first instance "0" is the master, all others are configured as
  371. # slaves.
  372. proc create_redis_master_slave_cluster n {
  373. foreach_redis_id id {
  374. if {$id == 0} {
  375. # Our master.
  376. R $id slaveof no one
  377. R $id flushall
  378. } elseif {$id < $n} {
  379. R $id slaveof [get_instance_attrib redis 0 host] \
  380. [get_instance_attrib redis 0 port]
  381. } else {
  382. # Instances not part of the cluster.
  383. R $id slaveof no one
  384. }
  385. }
  386. # Wait for all the slaves to sync.
  387. wait_for_condition 1000 50 {
  388. [RI 0 connected_slaves] == ($n-1)
  389. } else {
  390. fail "Unable to create a master-slaves cluster."
  391. }
  392. }
  393. proc get_instance_id_by_port {type port} {
  394. foreach_${type}_id id {
  395. if {[get_instance_attrib $type $id port] == $port} {
  396. return $id
  397. }
  398. }
  399. fail "Instance $type port $port not found."
  400. }
  401. # Kill an instance of the specified type/id with SIGKILL.
  402. # This function will mark the instance PID as -1 to remember that this instance
  403. # is no longer running and will remove its PID from the list of pids that
  404. # we kill at cleanup.
  405. #
  406. # The instance can be restarted with restart-instance.
  407. proc kill_instance {type id} {
  408. set pid [get_instance_attrib $type $id pid]
  409. set port [get_instance_attrib $type $id port]
  410. if {$pid == -1} {
  411. error "You tried to kill $type $id twice."
  412. }
  413. exec kill -9 $pid
  414. set_instance_attrib $type $id pid -1
  415. set_instance_attrib $type $id link you_tried_to_talk_with_killed_instance
  416. # Remove the PID from the list of pids to kill at exit.
  417. set ::pids [lsearch -all -inline -not -exact $::pids $pid]
  418. # Wait for the port it was using to be available again, so that's not
  419. # an issue to start a new server ASAP with the same port.
  420. set retry 10
  421. while {[incr retry -1]} {
  422. set port_is_free [catch {set s [socket 127.0.01 $port]}]
  423. if {$port_is_free} break
  424. catch {close $s}
  425. after 1000
  426. }
  427. if {$retry == 0} {
  428. error "Port $port does not return available after killing instance."
  429. }
  430. }
  431. # Return true of the instance of the specified type/id is killed.
  432. proc instance_is_killed {type id} {
  433. set pid [get_instance_attrib $type $id pid]
  434. expr {$pid == -1}
  435. }
  436. # Restart an instance previously killed by kill_instance
  437. proc restart_instance {type id} {
  438. set dirname "${type}_${id}"
  439. set cfgfile [file join $dirname $type.conf]
  440. set port [get_instance_attrib $type $id port]
  441. # Execute the instance with its old setup and append the new pid
  442. # file for cleanup.
  443. set pid [exec_instance $type $cfgfile]
  444. set_instance_attrib $type $id pid $pid
  445. lappend ::pids $pid
  446. # Check that the instance is running
  447. if {[server_is_up 127.0.0.1 $port 100] == 0} {
  448. abort_sentinel_test "Problems starting $type #$id: ping timeout"
  449. }
  450. # Connect with it with a fresh link
  451. set link [redis 127.0.0.1 $port]
  452. $link reconnect 1
  453. set_instance_attrib $type $id link $link
  454. # Make sure the instance is not loading the dataset when this
  455. # function returns.
  456. while 1 {
  457. catch {[$link ping]} retval
  458. if {[string match {*LOADING*} $retval]} {
  459. after 100
  460. continue
  461. } else {
  462. break
  463. }
  464. }
  465. }