Class: Etcd::Cluster

Inherits:
Object
  • Object
show all
Extended by:
Constants, Loggable, Requestable
Defined in:
lib/etcd/cluster.rb

Constant Summary

Constants included from Constants

Etcd::Constants::S_ACTION, Etcd::Constants::S_AND, Etcd::Constants::S_DIR, Etcd::Constants::S_EXPIRATION, Etcd::Constants::S_INDEX, Etcd::Constants::S_KEY, Etcd::Constants::S_KEYS, Etcd::Constants::S_LOCATION, Etcd::Constants::S_NEW_KEY, Etcd::Constants::S_PREV_VALUE, Etcd::Constants::S_SLASH, Etcd::Constants::S_TTL, Etcd::Constants::S_VALUE, Etcd::Constants::S_WATCH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

logger, reset_logger!

Methods included from Requestable

http_client, request, request_data, reset_http_client!

Constructor Details

#initialize(uri) ⇒ Cluster


76
77
78
# File 'lib/etcd/cluster.rb', line 76

def initialize(uri)
  @seed_uri = uri
end

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes


3
4
5
# File 'lib/etcd/cluster.rb', line 3

def nodes
  @nodes
end

#seed_uriObject

Returns the value of attribute seed_uri


4
5
6
# File 'lib/etcd/cluster.rb', line 4

def seed_uri
  @seed_uri
end

Class Method Details

.cluster_status(uri) ⇒ Array

Returns of node attributes as [Hash]

Examples:

Etcd::Cluster.cluster_status("http://127.0.0.1:4001")

15
16
17
18
19
20
21
22
23
24
# File 'lib/etcd/cluster.rb', line 15

def cluster_status(uri)
  begin
    logger.debug("cluster_status - from #{uri}")
    data = request_data(:get, status_uri(uri))
    parse_cluster_status(data)
  rescue Errno::ECONNREFUSED => e
    logger.debug("cluster_status - error!")
    nil
  end
end

.init_from_uris(*uris) ⇒ Cluster

creates new cluster with updated status is preferred way to create a cluster instance

Examples:

Etcd::Cluster.init_from_uris("http://127.0.0.1:4001", "http://127.0.0.1:4002", "http://127.0.0.1:4003")

Raises:


64
65
66
67
68
69
70
71
72
73
# File 'lib/etcd/cluster.rb', line 64

def init_from_uris(*uris)
  Array(uris).each do |uri|
    if Etcd::Cluster.cluster_status(uri)
      instance = Etcd::Cluster.new(uri)
      instance.update_status
      return instance
    end
  end
  raise AllNodesDownError, "could not initialize a cluster from #{uris.join(", ")}"
end

.nodes_from_attributes(node_attributes) ⇒ Object


52
53
54
55
56
# File 'lib/etcd/cluster.rb', line 52

def nodes_from_attributes(node_attributes)
  res = node_attributes.map do |attr|
    Etcd::Node.new(attr)
  end
end

.nodes_from_uri(uri) ⇒ Array

Returns of [Node] instances, representing cluster status

Examples:

Etcd::Cluster.nodes_from_uri("http://127.0.0.1:4001")

See Also:

  • #nodes_from_attributes

47
48
49
50
# File 'lib/etcd/cluster.rb', line 47

def nodes_from_uri(uri)
  node_attributes = cluster_status(uri)
  nodes_from_attributes(node_attributes)
end

.parse_cluster_status(cluster_status_response) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'lib/etcd/cluster.rb', line 30

def parse_cluster_status(cluster_status_response)
  cluster_status_response.map do |attrs|
    node_name = attrs[S_KEY].split(S_SLASH).last
    urls      = attrs[S_VALUE].split(S_AND)
    etcd      = urls.grep(/etcd/).first.split("=").last
    raft      = urls.grep(/raft/).first.split("=").last
    {:name => node_name, :raft => raft, :etcd => etcd}
  end
end

.status_uri(uri) ⇒ Object


26
27
28
# File 'lib/etcd/cluster.rb', line 26

def status_uri(uri)
  "#{uri}/v1/keys/_etcd/machines/"
end

Instance Method Details

#leaderNode

leader instance in cluster


94
95
96
# File 'lib/etcd/cluster.rb', line 94

def leader
  nodes.select{|x| x.is_leader}.first
end

#update_statusObject


84
85
86
87
88
89
90
# File 'lib/etcd/cluster.rb', line 84

def update_status
  @nodes = begin
    nodes = Etcd::Cluster.nodes_from_uri(seed_uri)
    nodes.map{|x| x.update_status}
    nodes
  end
end