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

#logger, #start, #stop_or_kill_and_remove, #wait

Methods included from ClientProvider

#build_client, #client, #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

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

Instance Method Details

#client_classObject



93
94
95
# File 'lib/mcrain/riak.rb', line 93

def client_class
  ::Riak::Client
end

#client_init_argsObject



97
98
99
100
101
102
103
# File 'lib/mcrain/riak.rb', line 97

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

#client_requireObject



105
106
107
# File 'lib/mcrain/riak.rb', line 105

def client_require
  'riak'
end

#nodesObject



78
79
80
81
82
83
84
85
86
# File 'lib/mcrain/riak.rb', line 78

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



88
89
90
91
# File 'lib/mcrain/riak.rb', line 88

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

#setupObject



141
142
143
144
145
146
147
148
# File 'lib/mcrain/riak.rb', line 141

def setup
  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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/mcrain/riak.rb', line 150

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



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/mcrain/riak.rb', line 179

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mcrain/riak.rb', line 109

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されるまで待つ



192
193
194
195
196
# File 'lib/mcrain/riak.rb', line 192

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