Class: ConsistentCluster::SyncClient

Inherits:
Object
  • Object
show all
Defined in:
lib/consistent-cluster/sync-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SyncClient

Returns a new instance of SyncClient.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/consistent-cluster/sync-client.rb', line 14

def initialize(options)
  @zk = ZK.new(options[:zookeeper_service])
  @path = options[:zookeeper_path]

  @data = {}
  @cluster = {}

  replicas = options[:consistent_hashing_replicas] || 3
  @ring = ConsistentHashing::Ring.new([],replicas)

  @create_proc = options[:create_proc]
  @destroy_proc = options[:destroy_proc]
  @after_sync_proc = options[:after_sync_proc] 

  @to_sync,@syncing = false,false

  @zk.register(@path) do |event|
    sync_services
  end

  sync_services

  @shard_num = 0
end

Instance Method Details

#shard(key = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/consistent-cluster/sync-client.rb', line 39

def shard(key=nil)
  cluster_sum = @cluster.length
  raise "no service available" if cluster_sum < 1
  if key
    point = @ring.point_for(key)
    server = @cluster[point.node]
  else
    @shard_num += 1
    @shard_num = @shard_num%cluster_sum
    server = @cluster.values[@shard_num]
  end
  server
end