Class: RedisRing::Slave

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, master_rpc, process_manager) ⇒ Slave

Returns a new instance of Slave.



9
10
11
12
13
14
15
# File 'lib/redis_ring/slave.rb', line 9

def initialize(configuration, master_rpc, process_manager)
  @configuration = configuration
  @master_rpc = master_rpc
  @process_manager = process_manager
  @joined = false
  @running_shards = {}
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/redis_ring/slave.rb', line 6

def configuration
  @configuration
end

#current_master_hostObject

Returns the value of attribute current_master_host.



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

def current_master_host
  @current_master_host
end

#current_master_portObject

Returns the value of attribute current_master_port.



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

def current_master_port
  @current_master_port
end

#master_rpcObject (readonly)

Returns the value of attribute master_rpc.



6
7
8
# File 'lib/redis_ring/slave.rb', line 6

def master_rpc
  @master_rpc
end

#node_idObject

Returns the value of attribute node_id.



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

def node_id
  @node_id
end

#process_managerObject (readonly)

Returns the value of attribute process_manager.



6
7
8
# File 'lib/redis_ring/slave.rb', line 6

def process_manager
  @process_manager
end

#running_shardsObject (readonly)

Returns the value of attribute running_shards.



7
8
9
# File 'lib/redis_ring/slave.rb', line 7

def running_shards
  @running_shards
end

Instance Method Details

#available_shardsObject



21
22
23
24
25
26
27
28
29
# File 'lib/redis_ring/slave.rb', line 21

def available_shards
  available_shards = {}
  configuration.ring_size.times do |shard_no|
    shard_conf = ShardConfig.new(shard_no, configuration)
    timestamp = [shard_conf.db_mtime, shard_conf.aof_mtime].compact.max
    available_shards[shard_no] = timestamp if timestamp
  end
  return available_shards
end

#joinObject



35
36
37
38
39
# File 'lib/redis_ring/slave.rb', line 35

def join
  puts "JOINING CLUSTER"
  @joined = true
  master_rpc.connection(current_master_host, current_master_port).node_loaded(node_id)
end

#joined?Boolean

Returns:

  • (Boolean)


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

def joined?
  @joined
end

#start_shard(shard_number) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/redis_ring/slave.rb', line 41

def start_shard(shard_number)
  puts "STARTING SHARD #{shard_number}"
  return if running_shards.include?(shard_number)
  shard_conf = ShardConfig.new(shard_number, configuration)
  shard = running_shards[shard_number] = Shard.new(shard_conf)
  process_manager.start_shard(shard)
end

#statusObject



31
32
33
# File 'lib/redis_ring/slave.rb', line 31

def status
  { :joined => joined?, :running_shards => running_shards.keys, :available_shards => available_shards }
end

#stop_shard(shard_number) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/redis_ring/slave.rb', line 49

def stop_shard(shard_number)
  puts "STOPPING SHARD #{shard_number}"
  shard = running_shards[shard_number]
  return unless shard
  process_manager.stop_shard(shard)
  running_shards.delete(shard_number)
end

#sync_shard_with(shard_number, host, port) ⇒ Object



57
58
# File 'lib/redis_ring/slave.rb', line 57

def sync_shard_with(shard_number, host, port)
end