Sha256: db579bfd0feb80a730b9f7d406098fd51f712cd1a8c6608d9a1d0b085dd7593c

Contents?: true

Size: 1.59 KB

Versions: 13

Compression:

Stored size: 1.59 KB

Contents

module RedisFailover
  # Provides manual failover support to a new master.
  class ManualFailover
    # Path for manual failover communication.
    ZNODE_PATH = 'manual_failover'.freeze

    # Denotes that any slave can be used as a candidate for promotion.
    ANY_SLAVE = "ANY_SLAVE".freeze

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

    # Creates a new instance.
    #
    # @param [ZK] zk the ZooKeeper client
    # @param [ZNode] root_znode the root ZK node
    # @param [Hash] options the options used for manual failover
    # @option options [String] :host the host of the failover candidate
    # @option options [String] :port the port of the failover candidate
    # @note
    #   If options is empty, a random slave will be used
    #   as a failover candidate.
    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

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

    private

    # Creates the znode path used for coordinating manual failovers.
    def create_path
      @zk.create(self.class.path(@root_znode))
    rescue ZK::Exceptions::NodeExists
      # best effort
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
spbtv_redis_failover-1.0.2.1 lib/redis_failover/manual_failover.rb
nogara-redis_failover-1.0.6 lib/redis_failover/manual_failover.rb
nogara-redis_failover-1.0.5 lib/redis_failover/manual_failover.rb
nogara-redis_failover-1.0.4 lib/redis_failover/manual_failover.rb
redis_failover-1.0.2 lib/redis_failover/manual_failover.rb
nogara-redis_failover-1.0.2 lib/redis_failover/manual_failover.rb
nogara-redis_failover-1.0.1 lib/redis_failover/manual_failover.rb
redis_failover-1.0.1 lib/redis_failover/manual_failover.rb
nogara-redis_failover-1.0.0 lib/redis_failover/manual_failover.rb
redis_failover-1.0.0 lib/redis_failover/manual_failover.rb
nogara-redis_failover-0.9.7.4 lib/redis_failover/manual_failover.rb
nogara-redis_failover-0.9.7.3 lib/redis_failover/manual_failover.rb
nogara-redis_failover-0.9.7.2 lib/redis_failover/manual_failover.rb