Solutions for resque failover redis

。_饼干妹妹 提交于 2019-12-12 09:06:15

问题


Since clustered Redis is still in the works, are there mechanisms in Resque that automatically will failover to a Redis slave should the master ever go down?


回答1:


I don't think so. However, you can implement the master election mechanism yourself quite easily using Apache Zookeeper:

require "rubygems"
require "zookeeper"

def log(msg)
  puts "[#{Process.pid}] #{msg}"
end

def debug(obj)
  log(obj.inspect)
end

def on_master_changed(&block)
  loop do
    wcb = Zookeeper::WatcherCallback.new
    resp = @zookeeper.get_children(:path => @base_path, :watcher => wcb, :watcher_context => @base_path)
    children = resp[:children].map{|name| "#{@base_path}/#{name}"}
    new_master = children.sort.first

    block.call(new_master)

    while !wcb.completed?
      sleep(0.1)
    end
  end
end

@zookeeper = Zookeeper.new("localhost:2181")

if @zookeeper.state != Zookeeper::ZOO_CONNECTED_STATE
  log 'Unable to connect to Zookeeper!'
  exit(1)
end

@base_path = "/nodes"

@zookeeper.create(:path => @base_path)
resp = @zookeeper.create(:path => "#{@base_path}/node-", :ephemeral => true, :sequence => true, :data => Process.pid.to_s)
my_node = resp[:path]
is_master = false

log "My node is: #{my_node}"

on_master_changed do |new_master|
  if new_master == my_node
    if is_master
      log "I am still the master. Bow before me or die!"
    else
      log "I am the new master. Behold!"
    end
    is_master = true
  else
    pid = @zookeeper.get(:path => new_master)[:data]
    log "New master is process #{pid}"
  end
end

You could modify the script above to:

  1. Use IP/port of the redis server instead of PID of the process
  2. Use redis-cli along with the SLAVEOF command to handle "became master", "master changed" and "no longer master" scenarions.



回答2:


I've released a redis_failover gem that provides Redis failover for Ruby with ZooKeeper:

https://github.com/ryanlecompte/redis_failover



来源:https://stackoverflow.com/questions/7759572/solutions-for-resque-failover-redis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!