Class: RedisFailover::NodeSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_failover/node_snapshot.rb

Overview

Represents a snapshot of a particular redis node as seen by all currently running redis node managers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ NodeSnapshot

Creates a new instance.

Parameters:

  • the (String)

    redis node

See Also:



12
13
14
15
16
# File 'lib/redis_failover/node_snapshot.rb', line 12

def initialize(node)
  @node = node
  @available = {}
  @unavailable = []
end

Instance Attribute Details

#nodeString (readonly)

Returns the redis node

Returns:

  • (String)

    the redis node



6
7
8
# File 'lib/redis_failover/node_snapshot.rb', line 6

def node
  @node
end

Instance Method Details

#all_available?Boolean

node was viewable

Returns:

  • (Boolean)

    true if all node managers indicated that this



65
66
67
# File 'lib/redis_failover/node_snapshot.rb', line 65

def all_available?
  available_count > 0 && unavailable_count == 0
end

#all_unavailable?Boolean

node was unviewable

Returns:

  • (Boolean)

    true if all node managers indicated that this



71
72
73
# File 'lib/redis_failover/node_snapshot.rb', line 71

def all_unavailable?
  unavailable_count > 0 && available_count == 0
end

#available_countInteger

this node is available

Returns:

  • (Integer)

    the number of node managers saying



42
43
44
# File 'lib/redis_failover/node_snapshot.rb', line 42

def available_count
  @available.size
end

#avg_latencyInteger

Returns the average available latency

Returns:

  • (Integer)

    the average available latency



53
54
55
56
# File 'lib/redis_failover/node_snapshot.rb', line 53

def avg_latency
  return if @available.empty?
  @available.values.inject(0) { |sum, n| sum + n } / @available.size
end

#node_managersArray<String>

Returns all node managers involved in this snapshot

Returns:

  • (Array<String>)

    all node managers involved in this snapshot



59
60
61
# File 'lib/redis_failover/node_snapshot.rb', line 59

def node_managers
  (@available.keys + @unavailable).uniq
end

#to_sString

Returns a friendly representation of this node snapshot

Returns:

  • (String)

    a friendly representation of this node snapshot



76
77
78
79
# File 'lib/redis_failover/node_snapshot.rb', line 76

def to_s
  'Node %s available by %p, unavailable by %p (%d up, %d down)' %
    [node, @available, @unavailable, available_count, unavailable_count]
end

#unavailable_countInteger

this node is unavailable

Returns:

  • (Integer)

    the number of node managers saying



48
49
50
# File 'lib/redis_failover/node_snapshot.rb', line 48

def unavailable_count
  @unavailable.size
end

#unviewable_by(node_manager) ⇒ Object

Declares this node unavailable by the specified node manager.

Parameters:

  • node_manager (String)

    the node manager id



36
37
38
# File 'lib/redis_failover/node_snapshot.rb', line 36

def unviewable_by(node_manager)
  @unavailable << node_manager
end

#viewable_by(node_manager, latency) ⇒ Object

Declares this node available by the specified node manager.

Parameters:

  • node_manager (String)

    the node manager id

  • latency (Integer)

    the latency



22
23
24
# File 'lib/redis_failover/node_snapshot.rb', line 22

def viewable_by(node_manager, latency)
  @available[node_manager] = latency
end

#viewable_by?(node_manager) ⇒ Boolean

Determines if this node is viewable by a node manager.

Parameters:

  • node_manager (String)

    the node manager id

Returns:

  • (Boolean)


29
30
31
# File 'lib/redis_failover/node_snapshot.rb', line 29

def viewable_by?(node_manager)
  @available.key?(node_manager)
end