Class: Riak::Cluster

Inherits:
Object show all
Includes:
Util::Translation
Defined in:
lib/riak/cluster.rb

Overview

Generates and controls a cluster of Node instances for use in development or testing on a single machine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(config = {}) ⇒ Cluster

Creates a Riak::Cluster of Nodes.

Parameters:

  • config (Hash) (defaults to: {})

    the configuration for the cluster

Options Hash (config):

  • :count (Fixnum)

    the number of nodes to create

  • :source (String)

    path to the Riak bin/ directory. See Node#source.

  • :root (String)

    path to where the nodes will be generated.

  • :min_port (Fixnum)

    the base port number from which nodes will claim IP ports for HTTP, PB, handoff.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/riak/cluster.rb', line 28

def initialize(config={})
  raise ArgumentError, t('source_and_root_required') unless config[:source] && config[:root]
  @configuration = config
  @count = config.delete(:count) || 4
  @min_port = config.delete(:min_port) || 9000
  @root = Pathname.new(config.delete(:root))
  @nodes = []
  cookie = "#{rand(100000).to_s}_#{rand(1000000).to_s}"
  @count.times do |i|
    nodes << Riak::Node.new(config.merge(:min_port => @min_port + (i * 3),
                                         :root => @root + (i+1).to_s,
                                         :cookie => cookie))
  end
end

Instance Attribute Details

#configurationHash (readonly)

Returns the cluster configuration.

Returns:

  • (Hash)

    the cluster configuration



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

def configuration
  @configuration
end

#nodesArray<Node> (readonly)

Returns the member Nodes of this cluster.

Returns:

  • (Array<Node>)

    the member Nodes of this cluster



11
12
13
# File 'lib/riak/cluster.rb', line 11

def nodes
  @nodes
end

#rootPathname (readonly)

Returns the root directory of the cluster.

Returns:

  • (Pathname)

    the root directory of the cluster



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

def root
  @root
end

Instance Method Details

#attachArray<Riak::Node::Console>

Attaches to the console on all nodes, returning a list of Node::Console objects.

Returns:

  • (Array<Riak::Node::Console>)

    consoles for all running nodes, with nil for nodes that aren’t running or otherwise fail to connect



108
109
110
111
112
113
114
115
116
# File 'lib/riak/cluster.rb', line 108

def attach
  nodes.map do |n|
    begin
      n.attach
    rescue ArgumentError, SystemCallError
      nil
    end
  end
end

#createObject

Generates all nodes in the cluster.



49
50
51
52
53
54
# File 'lib/riak/cluster.rb', line 49

def create
  unless exist?
    root.mkpath unless root.exist?
    nodes.each {|n| n.create }
  end
end

#destroyObject

Removes all nodes in the cluster and the root, and freezes the object.



58
59
60
61
62
# File 'lib/riak/cluster.rb', line 58

def destroy
  nodes.each {|n| n.destroy }
  root.rmtree if root.exist?
  freeze
end

#dropObject

Drops all data from the cluster without destroying the nodes.



72
73
74
# File 'lib/riak/cluster.rb', line 72

def drop
  nodes.each {|n| n.drop }
end

#exist?true, false

Returns whether the cluster has been created.

Returns:

  • (true, false)

    whether the cluster has been created



44
45
46
# File 'lib/riak/cluster.rb', line 44

def exist?
  root.directory? && nodes.all? {|n| n.exist? }
end

#joinObject

Note:

This method relies on cluster membership changes present in the 1.0 series of Riak, and is NOT safe on 0.14 and earlier.

Joins the nodes together into a cluster.



145
146
147
148
149
# File 'lib/riak/cluster.rb', line 145

def join
  claimant = nodes.first.name # Not really the claimant, just a
                              # node to join to
  nodes[1..-1].each {|n| n.join(claimant) unless n.peers.include?(claimant) }
end

#js_reloadObject

Forces the cluster nodes to restart/reload their JavaScript VMs, effectively reloading any user-provided code.



99
100
101
# File 'lib/riak/cluster.rb', line 99

def js_reload
  nodes.each {|n| n.js_reload }
end

#rebootObject

Reboots all nodes in the cluster



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

def reboot
  nodes.each {|n| n.reboot }
end

#recreateObject

Removes and recreates the cluster.



65
66
67
68
69
# File 'lib/riak/cluster.rb', line 65

def recreate
  stop unless stopped?
  root.rmtree if root.exist?
  create
end

#restartObject

Restarts all nodes in the cluster (without exiting the Erlang runtime)



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

def restart
  nodes.each {|n| n.restart }
end

#startObject

Starts all nodes in the cluster.



77
78
79
# File 'lib/riak/cluster.rb', line 77

def start
  nodes.each {|n| n.start }
end

#started?Boolean

Is the cluster started?

Returns:

  • (Boolean)


132
133
134
# File 'lib/riak/cluster.rb', line 132

def started?
  nodes.all? {|n| n.started? }
end

#stopObject

Stops all nodes in the cluster.



82
83
84
# File 'lib/riak/cluster.rb', line 82

def stop
  nodes.each {|n| n.stop }
end

#stopped?Boolean

Is the cluster stopped?

Returns:

  • (Boolean)


137
138
139
# File 'lib/riak/cluster.rb', line 137

def stopped?
  nodes.all? {|n| n.stopped? }
end

#with_console {|console| ... } ⇒ Object

Executes the given block on each node against the node’s console. You could use this to send Erlang commands to all nodes in the cluster.

Yields:

  • (console)

    A block of commands to be run against the console

Yield Parameters:

  • console (Riak::Node::Console)

    A console manager for sending commands to the current node in the iteration



125
126
127
128
129
# File 'lib/riak/cluster.rb', line 125

def with_console(&block)
  nodes.each do |n|
    n.with_console(&block)
  end
end