Class: ZK::Server::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/zk-server/cluster.rb

Overview

Cluster simplifies the case when you want to test a 3+ node cluster You give the base_dir, and number of nodes, and it will construct a cluster for you. It's less configurable than the SubProcess class, but that's the price of convenience!

Constant Summary collapse

FOLLOWER_PORT_OFFSET =
100
LEADER_PORT_OFFSET =
200
DEFAULT_BASE_PORT =
21811

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_members, opts = {}) ⇒ Cluster

Returns a new instance of Cluster.



24
25
26
27
28
29
30
31
# File 'lib/zk-server/cluster.rb', line 24

def initialize(num_members, opts={})
  @num_members  = num_members
  @base_dir     = Config.default_base_dir
  @base_port    = DEFAULT_BASE_PORT
  @processes    = nil
  @running      = false
  opts.each { |k,v| __send__(:"#{k}=", v) }
end

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



12
13
14
# File 'lib/zk-server/cluster.rb', line 12

def base_dir
  @base_dir
end

#base_portObject

defaults to 21811, used as the lowest port number for the cluster, all others will be offsets of this number



16
17
18
# File 'lib/zk-server/cluster.rb', line 16

def base_port
  @base_port
end

#num_membersObject (readonly)

how many nodes in the cluster



22
23
24
# File 'lib/zk-server/cluster.rb', line 22

def num_members
  @num_members
end

#processesObject (readonly)

access to the SubProcess instances that make up this cluster



19
20
21
# File 'lib/zk-server/cluster.rb', line 19

def processes
  @processes
end

Instance Method Details

#all_running?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/zk-server/cluster.rb', line 61

def all_running?
  processes.all?(&:running?)
end

#clobber!Object



57
58
59
# File 'lib/zk-server/cluster.rb', line 57

def clobber!
  processes.each(&:clobber!)
end

#ping_all?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/zk-server/cluster.rb', line 65

def ping_all?
  processes.all?(&:ping?)
end

#runObject



37
38
39
40
41
42
43
44
45
# File 'lib/zk-server/cluster.rb', line 37

def run
  return if running?
  @running = true

  processes.each { |p| p.run }
rescue Exception
  processes.each { |p| p.shutdown }
  raise
end

#running?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/zk-server/cluster.rb', line 33

def running?
  !!@running
end

#server_hashObject

terrible name, the list of 'servers' lines in the config



86
87
88
89
90
91
92
# File 'lib/zk-server/cluster.rb', line 86

def server_hash
  @server_hash ||= {}.tap do |h|
    num_members.times do |idx|
      h["server.#{idx}"] = "127.0.0.1:#{base_port + FOLLOWER_PORT_OFFSET + idx}:#{base_port + LEADER_PORT_OFFSET + idx}"
    end
  end
end

#shutdownObject



47
48
49
50
51
52
53
54
55
# File 'lib/zk-server/cluster.rb', line 47

def shutdown
  return unless running?
  @running = false

  pary, @processes = @processes, nil

  pary.each(&:shutdown)
  pary
end