Class: MixedGauge::ClusterConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/mixed_gauge/cluster_config.rb

Overview

Mapping of slot -> connection_name.

Defined Under Namespace

Classes: Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ClusterConfig

Returns a new instance of ClusterConfig.

Parameters:

  • name (Symbol)


7
8
9
10
# File 'lib/mixed_gauge/cluster_config.rb', line 7

def initialize(name)
  @name = name
  @connection_registry = {}
end

Instance Attribute Details

#connection_registryObject (readonly)

Returns the value of attribute connection_registry.



4
5
6
# File 'lib/mixed_gauge/cluster_config.rb', line 4

def connection_registry
  @connection_registry
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mixed_gauge/cluster_config.rb', line 4

def name
  @name
end

Instance Method Details

#connectionsArray<Symbol>

Returns An array of connection name.

Returns:

  • (Array<Symbol>)

    An array of connection name



41
42
43
# File 'lib/mixed_gauge/cluster_config.rb', line 41

def connections
  @connection_registry.values
end

#define_slot_size(n) ⇒ Object

Parameters:

  • size (Integer)

    The slot size of this cluster.



13
14
15
# File 'lib/mixed_gauge/cluster_config.rb', line 13

def define_slot_size(n)
  @slots = 0..(n - 1)
end

#fetch(slot) ⇒ Symbol

Returns registered connection name.

Parameters:

  • slot (Integer)

Returns:

  • (Symbol)

    registered connection name



36
37
38
# File 'lib/mixed_gauge/cluster_config.rb', line 36

def fetch(slot)
  @connection_registry.find { |slot_range, _name| slot_range.cover?(slot) }[1]
end

#register(assigned_slots, connection) ⇒ Object

Parameters:

  • assigned_slots (Range)

    The assigned range of slots of given connection (shard).

  • connection (Symbol)

    connection name



20
21
22
# File 'lib/mixed_gauge/cluster_config.rb', line 20

def register(assigned_slots, connection)
  @connection_registry[assigned_slots] = connection
end

#slot_sizeInteger

Returns:

  • (Integer)


30
31
32
# File 'lib/mixed_gauge/cluster_config.rb', line 30

def slot_size
  defined?(@slot_size) ? @slot_size : @slot_size = @slots.size
end

#validate_config!Object

Raises:

  • (RuntimeError)


25
26
27
# File 'lib/mixed_gauge/cluster_config.rb', line 25

def validate_config!
  Validator.new(slot_size, @connection_registry).validate!
end