Class: RedisFailover::ManualFailover

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_failover/manual_failover.rb

Overview

Provides manual failover support to a new master.

Constant Summary collapse

ZNODE_PATH =

Path for manual failover communication.

'manual_failover'.freeze
ANY_SLAVE =

Denotes that any slave can be used as a candidate for promotion.

"ANY_SLAVE".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zk, root_znode, options = {}) ⇒ ManualFailover

Note:

If options is empty, a random slave will be used as a failover candidate.

Creates a new instance.

Parameters:

  • zk (ZK)

    the ZooKeeper client

  • root_znode (ZNode)

    the root ZK node

  • options (Hash) (defaults to: {})

    the options used for manual failover

Options Hash (options):

  • :host (String)

    the host of the failover candidate

  • :port (String)

    the port of the failover candidate



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/redis_failover/manual_failover.rb', line 24

def initialize(zk, root_znode, options = {})
  @zk = zk
  @root_znode = root_znode
  @options = options

  unless @options.empty?
    port = Integer(@options[:port]) rescue nil
    raise ArgumentError, ':host not properly specified' if @options[:host].to_s.empty?
    raise ArgumentError, ':port not properly specified' if port.nil?
  end
end

Class Method Details

.path(root_znode) ⇒ Object



10
11
12
# File 'lib/redis_failover/manual_failover.rb', line 10

def self.path(root_znode)
  "#{root_znode}/#{ZNODE_PATH}"
end

Instance Method Details

#performObject

Performs a manual failover.



37
38
39
40
41
# File 'lib/redis_failover/manual_failover.rb', line 37

def perform
  create_path
  node = @options.empty? ? ANY_SLAVE : "#{@options[:host]}:#{@options[:port]}"
  @zk.set(self.class.path(@root_znode), node)
end