Class: Cassandra::Tasks::SeedRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra/tasks/seedregistry.rb

Instance Method Summary collapse

Constructor Details

#initialize(cluster_name) ⇒ SeedRegistry

Create a new SeedRegistry task

Parameters:

  • cluster_name (String)

    unique name (in Consul) for the Cassandra cluster

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
# File 'lib/cassandra/tasks/seedregistry.rb', line 13

def initialize cluster_name
  @cluster_name = cluster_name.to_s
  raise ArgumentError.new('cluster_name must not be empty') if @cluster_name.empty?
  @semaphore = nil
  @renew_thread = nil
  @nodetool_info_cache = nil
end

Instance Method Details

#can_seed?Boolean

Return true if the Cassandra node is a valid seed, false otherwise

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cassandra/tasks/seedregistry.rb', line 42

def can_seed?
  return false unless state == :normal

  results = (nodetool_info_cached || '').split("\n")
  results.map! { |line| line.strip }

  filter_results = lambda do |key|
    potential = results.select { |line| line.include? key }
    potential.map! { |line| line.split(':')[1] }
    potential.compact!
    potential.size == 1 && potential.first.strip == 'true'
  end

  return false unless filter_results.call('Gossip active')
  return false unless filter_results.call('Thrift active')
  return false unless filter_results.call('Native Transport active')

  true
end

#data_centerString?

Return the data center the Cassandra node is in

The returned data center is reported by “nodetool info”.

Returns:

  • (String, nil)


84
85
86
87
88
89
90
91
92
# File 'lib/cassandra/tasks/seedregistry.rb', line 84

def data_center
  results = (nodetool_info_cached || '').split("\n")
  results.map! { |line| line.strip }
  results.select! { |line| line.include?('Data Center') }
  results.map! { |line| line.split(':')[1] }
  results.compact!
  return nil if results.size != 1
  results.first.strip
end

#rackString?

Return the rack the Cassandra node is in

The returned rack is reported by “nodetool info”.

Returns:

  • (String, nil)


100
101
102
103
104
105
106
107
108
# File 'lib/cassandra/tasks/seedregistry.rb', line 100

def rack
  results = (nodetool_info_cached || '').split("\n")
  results.map! { |line| line.strip }
  results.select! { |line| line.include?('Rack') }
  results.map! { |line| line.split(':')[1] }
  results.compact!
  return nil if results.size != 1
  results.first.strip
end

#run!Object

Get a lock in Consul registering the Cassandra node as a seed



29
30
31
32
33
34
35
36
# File 'lib/cassandra/tasks/seedregistry.rb', line 29

def run!
  @nodetool_info_cache = nil
  if can_seed?
    try_get_seed_lock
  else
    release_seed_lock
  end
end

#scheduleObject

Schedule the seed registration process to run periodically



23
24
25
# File 'lib/cassandra/tasks/seedregistry.rb', line 23

def schedule
  [:interval, '10s']
end

#statestate?

Return the state of the Cassandra node

The returned state is reported by “nodetool netstats”.

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/cassandra/tasks/seedregistry.rb', line 68

def state
  results = (nodetool_netstats || '').split("\n")
  results.map! { |line| line.strip }
  results.select! { |line| line.include? 'Mode:' }
  results.map! { |line| line.split(':')[1] }
  results.compact!
  return nil if results.size != 1
  results.first.strip.downcase.to_sym
end