Class: Solr::Cloud::CollectionsStateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/solr/cloud/collections_state_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zookeeper:, collections:) ⇒ CollectionsStateManager

Returns a new instance of CollectionsStateManager.



6
7
8
9
10
11
# File 'lib/solr/cloud/collections_state_manager.rb', line 6

def initialize(zookeeper:, collections:)
  @zookeeper = zookeeper
  @collections = collections
  @collections_state = {}
  watch_solr_collections_state
end

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



4
5
6
# File 'lib/solr/cloud/collections_state_manager.rb', line 4

def collections
  @collections
end

#collections_stateObject (readonly)

Returns the value of attribute collections_state.



4
5
6
# File 'lib/solr/cloud/collections_state_manager.rb', line 4

def collections_state
  @collections_state
end

#zookeeperObject (readonly)

Returns the value of attribute zookeeper.



4
5
6
# File 'lib/solr/cloud/collections_state_manager.rb', line 4

def zookeeper
  @zookeeper
end

Instance Method Details

#active_nodes_for(collection:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/solr/cloud/collections_state_manager.rb', line 17

def active_nodes_for(collection:)
  shards = collections_state.dig(collection.to_s, 'shards')
  return unless shards
  shards.flat_map do |_, shard|
    shard['replicas'].select do |_, replica|
      replica['state'] == 'active'
    end.flat_map do |_, replica|
      replica['base_url']
    end
  end.uniq
end

#leader_replica_node_for(collection:, shard:) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/solr/cloud/collections_state_manager.rb', line 29

def leader_replica_node_for(collection:, shard:)
  shards = collections_state.dig(collection.to_s, 'shards')
  return unless shards
  shard_replicas = shards[shard.to_s]
  leader_replica = shard_replicas['replicas'].detect do |_, replica|
    replica['state'] == 'active' && replica['leader'] == 'true'
  end
  leader_replica.last['base_url'] if leader_replica
end

#shards_for(collection:) ⇒ Object



13
14
15
# File 'lib/solr/cloud/collections_state_manager.rb', line 13

def shards_for(collection:)
  collections_state.dig(collection.to_s, 'shards').keys
end

#watch_solr_collections_stateObject



39
40
41
42
43
44
45
# File 'lib/solr/cloud/collections_state_manager.rb', line 39

def watch_solr_collections_state
  collections.each do |collection_name|
    zookeeper.watch_collection_state(collection_name) do |state|
      @collections_state[collection_name.to_s] = state
    end
  end
end