class Mcrain::Riak

Attributes

automatic_clustering[RW]
backend[W]
cluster_size[W]
strong_consistency[W]

Public Instance Methods

backend() click to toggle source
# File lib/mcrain/riak.rb, line 16
def backend
  @backend ||= "bitcask" # "leveldb"
end
client_class() click to toggle source
# File lib/mcrain/riak.rb, line 99
def client_class
  ::Riak::Client
end
client_init_args() click to toggle source
# File lib/mcrain/riak.rb, line 103
def client_init_args
  return [
   {
     nodes: nodes.map{|node| {host: node.host, pb_port: node.port} }
   }
  ]
end
client_require() click to toggle source
# File lib/mcrain/riak.rb, line 111
def client_require
  'riak'
end
cluster_size() click to toggle source
# File lib/mcrain/riak.rb, line 13
def cluster_size
  @cluster_size ||= 1
end
nodes() click to toggle source
# File lib/mcrain/riak.rb, line 84
def nodes
  unless @nodes
    @nodes = (cluster_size || 1).times.map{ Node.new(self) }
    array = @nodes.dup
    primary_node = array.shift
    array.each{|node| node.primary_node = primary_node}
  end
  @nodes
end
reset() click to toggle source
Calls superclass method Mcrain::Base#reset
# File lib/mcrain/riak.rb, line 94
def reset
  (@nodes || []).each(&:reset)
  super
end
setup() click to toggle source
# File lib/mcrain/riak.rb, line 147
def setup
  return false if Mcrain.before_setup && !Mcrain.before_setup.call(self)
  DockerMachine.setup_docker_options
  # setup_nodes(nodes[0, 1]) # primary node
  # setup_nodes(nodes[1..-1])
  nodes.each do |node|
    setup_nodes([node])
  end
end
setup_nodes(nodes) click to toggle source
# File lib/mcrain/riak.rb, line 157
def setup_nodes(nodes)
  return if nodes.empty?
  containers = nodes.map(&:container)
  containers.each(&:start!)

  # http://basho.co.jp/riak-quick-start-with-docker/
  #
  # "Please wait approximately 30 seconds for the cluster to stabilize"
  #   from https://gist.github.com/agutow/11133143#file-docker3-sh-L12
  sleep(30)
  nodes.each do |node|
    success = false
    20.times do
      success = node.ping
      break if success
      sleep(3)
    end
    unless success
      msg = "failed to run a riak server"
      timeout(10) do
        logs = node.container.logs(stdout: 1, stderr: 1)
        logger.error("#{msg}\nthe container logs...\n#{logs}")
      end
      raise msg
    end
  end
  logger.info("container started: " << containers.map{|c| c.json}.join("\n"))
end
strong_consistency() click to toggle source
# File lib/mcrain/riak.rb, line 19
def strong_consistency
  @strong_consistency ||= "off"
end
teardown() click to toggle source
# File lib/mcrain/riak.rb, line 186
def teardown
  nodes.map(&:container).each do |c|
    begin
      c.stop!
    rescue => e
      c.kill!
    end
    c.remove
  end
  reset unless skip_reset_after_teardown
end
wait_for_ready() click to toggle source
# File lib/mcrain/riak.rb, line 115
def wait_for_ready
  c = client
  logger.debug("sending a ping from client")
  begin
    r = c.ping
    raise "Ping failure with #{c.inspect}" unless r
  rescue => e
    logger.debug("[#{e.class.name}] #{e.message} by #{c.inspect}")
    raise e
  end
  20.times do |i|
    begin
      logger.debug("get and store ##{i}")
      o1 = c.bucket("test").get_or_new("foo")
      o1.data = {"bar" => 100}
      o1.store
      o2 = c.bucket("test").get_or_new("foo")
      raise "Something wrong!" unless o2.data == o1.data
      break
    rescue => e
      if e.message =~ /Expected success from Riak but received 0/
        sleep(0.5)
        logger.debug("retrying [#{e.class}] #{e.message}")
        retry
      else
        logger.warn(e)
        raise
      end
    end
  end
end
wait_port() click to toggle source

ポートがLISTENされるまで待つ

# File lib/mcrain/riak.rb, line 199
def wait_port
  nodes.each do |node|
    Mcrain.wait_port_opened(node.host, node.port, interval: 0.5, timeout: 30)
  end
end