2
0

cluster.tcl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Cluster-specific test functions.
  2. #
  3. # Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com
  4. # This softare is released under the BSD License. See the COPYING file for
  5. # more information.
  6. # Returns a parsed CLUSTER NODES output as a list of dictionaries.
  7. proc get_cluster_nodes id {
  8. set lines [split [R $id cluster nodes] "\r\n"]
  9. set nodes {}
  10. foreach l $lines {
  11. set l [string trim $l]
  12. if {$l eq {}} continue
  13. set args [split $l]
  14. set node [dict create \
  15. id [lindex $args 0] \
  16. addr [lindex $args 1] \
  17. flags [split [lindex $args 2] ,] \
  18. slaveof [lindex $args 3] \
  19. ping_sent [lindex $args 4] \
  20. pong_recv [lindex $args 5] \
  21. config_epoch [lindex $args 6] \
  22. linkstate [lindex $args 7] \
  23. slots [lrange $args 8 -1] \
  24. ]
  25. lappend nodes $node
  26. }
  27. return $nodes
  28. }
  29. # Test node for flag.
  30. proc has_flag {node flag} {
  31. expr {[lsearch -exact [dict get $node flags] $flag] != -1}
  32. }
  33. # Returns the parsed myself node entry as a dictionary.
  34. proc get_myself id {
  35. set nodes [get_cluster_nodes $id]
  36. foreach n $nodes {
  37. if {[has_flag $n myself]} {return $n}
  38. }
  39. return {}
  40. }
  41. # Return the value of the specified CLUSTER INFO field.
  42. proc CI {n field} {
  43. get_info_field [R $n cluster info] $field
  44. }
  45. # Assuming nodes are reest, this function performs slots allocation.
  46. # Only the first 'n' nodes are used.
  47. proc cluster_allocate_slots {n} {
  48. set slot 16383
  49. while {$slot >= 0} {
  50. # Allocate successive slots to random nodes.
  51. set node [randomInt $n]
  52. lappend slots_$node $slot
  53. incr slot -1
  54. }
  55. for {set j 0} {$j < $n} {incr j} {
  56. R $j cluster addslots {*}[set slots_${j}]
  57. }
  58. }