Class: RedisRing::ZookeeperConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_ring/zookeeper_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster_name, host_name, base_port, zookeeper_address) ⇒ ZookeeperConnection

Returns a new instance of ZookeeperConnection.



7
8
9
10
11
12
13
14
# File 'lib/redis_ring/zookeeper_connection.rb', line 7

def initialize(cluster_name, host_name, base_port, zookeeper_address)
  @host_name = host_name
  @base_port = base_port
  @zookeeper_address = zookeeper_address
  @connected = false
  @base_path = "/#{cluster_name}"
  @mutex = Mutex.new
end

Instance Attribute Details

#current_nodeObject (readonly)

Returns the value of attribute current_node.



5
6
7
# File 'lib/redis_ring/zookeeper_connection.rb', line 5

def current_node
  @current_node
end

Instance Method Details

#connectObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redis_ring/zookeeper_connection.rb', line 46

def connect
  @mutex.synchronize do
    break if connected?

    @zookeeper = Zookeeper.new(zookeeper_address)

    if @zookeeper.state != Zookeeper::ZOO_CONNECTED_STATE
      raise "Zookeeper not connected!"
    end

    resp = @zookeeper.create(:path => base_path)
    #raise "Could not create base path" unless resp[:rc] == Zookeeper::ZOK

    resp = @zookeeper.create(:path => "#{base_path}/node-", :ephemeral => true, :sequence => true, :data => current_node_data.to_json)
    #raise "Could not create node" unless resp[:rc] == Zookeeper::ZOK

    @current_node = resp[:path].gsub("#{base_path}/", '')

    @connected = true
  end
end

#connected?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/redis_ring/zookeeper_connection.rb', line 42

def connected?
  @connected
end

#node_data(node) ⇒ Object



27
28
29
30
31
# File 'lib/redis_ring/zookeeper_connection.rb', line 27

def node_data(node)
  resp = zookeeper.get(:path => "#{base_path}/#{node}")
  data = resp[:data]
  return data ? JSON.parse(data) : nil
end

#nodesObject



21
22
23
24
25
# File 'lib/redis_ring/zookeeper_connection.rb', line 21

def nodes
  @nodes_watcher = Zookeeper::WatcherCallback.new
  resp = zookeeper.get_children(:path => base_path, :watcher => nodes_watcher, :watcher_context => base_path)
  return resp[:children].sort
end

#nodes_changed?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/redis_ring/zookeeper_connection.rb', line 16

def nodes_changed?
  return true unless nodes_watcher
  return nodes_watcher.completed?
end

#update_status(status) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/redis_ring/zookeeper_connection.rb', line 33

def update_status(status)
  status_path = "#{base_path}_cluster_status"
  if zookeeper.stat(:path => status_path)[:stat].exists
    zookeeper.set(:path => status_path, :data => status.to_json)
  else
    zookeeper.create(:path => status_path, :data => status.to_json)
  end
end