Class: Mcrain::Riak

Inherits:
Base
  • Object
show all
Defined in:
lib/mcrain/riak.rb

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Attributes inherited from Base

#skip_reset_after_teardown

Instance Method Summary collapse

Methods inherited from Base

#initialize, #logger, #start, #start_callback, #stop_or_kill_and_remove, #wait, work_dir

Methods included from ClientProvider

#build_client, #client, #client_instantiation_script, #client_script

Methods included from ContainerController

#add_volume_options, #build_docker_options, #container, #container_image, #find_portno, #host, included, #info, #ip, #name, #port, #ssh_uri, #url

Constructor Details

This class inherits a constructor from Mcrain::Base

Instance Attribute Details

#automatic_clusteringObject

Returns the value of attribute automatic_clustering.



12
13
14
# File 'lib/mcrain/riak.rb', line 12

def automatic_clustering
  @automatic_clustering
end

#backendObject



17
18
19
# File 'lib/mcrain/riak.rb', line 17

def backend
  @backend ||= "bitcask" # "leveldb"
end

#cluster_sizeObject



14
15
16
# File 'lib/mcrain/riak.rb', line 14

def cluster_size
  @cluster_size ||= 1
end

#strong_consistencyObject



20
21
22
# File 'lib/mcrain/riak.rb', line 20

def strong_consistency
  @strong_consistency ||= "off"
end

Instance Method Details

#client_classObject



100
101
102
# File 'lib/mcrain/riak.rb', line 100

def client_class
  ::Riak::Client
end

#client_init_argsObject



104
105
106
107
108
109
110
# File 'lib/mcrain/riak.rb', line 104

def client_init_args
  return [
   {
     nodes: nodes.map{|node| {host: node.host, pb_port: node.port} }
   }
  ]
end

#client_requireObject



112
113
114
# File 'lib/mcrain/riak.rb', line 112

def client_require
  'riak'
end

#nodesObject



85
86
87
88
89
90
91
92
93
# File 'lib/mcrain/riak.rb', line 85

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

#resetObject



95
96
97
98
# File 'lib/mcrain/riak.rb', line 95

def reset
  (@nodes || []).each(&:reset)
  super
end

#setupObject



148
149
150
151
152
153
154
155
156
# File 'lib/mcrain/riak.rb', line 148

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) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mcrain/riak.rb', line 158

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

#teardownObject



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mcrain/riak.rb', line 187

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_readyObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mcrain/riak.rb', line 116

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_portObject

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



200
201
202
203
204
# File 'lib/mcrain/riak.rb', line 200

def wait_port
  nodes.each do |node|
    Mcrain.wait_port_opened(node.host, node.port, interval: 0.5, timeout: 30)
  end
end