class RSolr::Cloud::Connection

RSolr connection adapter for SolrCloud

Constants

ZNODE_CLUSTER_PROPS
ZNODE_LIVE_NODES

Public Class Methods

new(zk, options = {}) click to toggle source
Calls superclass method
# File lib/rsolr/cloud/connection.rb, line 10
def initialize(zk, options = {})
  super()
  @zk = zk
  @options = options
  init_url_scheme
  init_live_node_watcher
end

Public Instance Methods

execute(client, request_context) click to toggle source
Calls superclass method
# File lib/rsolr/cloud/connection.rb, line 18
def execute(client, request_context)
  collection_name = request_context[:collection]
  raise 'The :collection option must be specified.' unless collection_name
  path  = request_context[:path].to_s
  query = request_context[:query]
  query = query ? "?#{query}" : ''
  url   = select_node(collection_name)
  raise RSolr::Cloud::Error::NotEnoughNodes unless url
  request_context[:uri] = RSolr::Uri.create(url).merge(path + query)
  super(client, request_context)
end

Private Instance Methods

init_live_node_watcher() click to toggle source
# File lib/rsolr/cloud/connection.rb, line 45
def init_live_node_watcher
  @zk.register(ZNODE_LIVE_NODES) do
    update_live_nodes
  end
  update_live_nodes
end
init_url_scheme() click to toggle source
# File lib/rsolr/cloud/connection.rb, line 32
def init_url_scheme
  @url_scheme = 'http'
  if @zk.exists?(ZNODE_CLUSTER_PROPS)
    json, _stat = @zk.get(ZNODE_CLUSTER_PROPS)
    props = JSON.parse(json)
    @url_scheme = props['urlScheme'] || 'http'
  end
end
node_url(node) click to toggle source
# File lib/rsolr/cloud/connection.rb, line 61
def node_url(node)
  # "/" between host_and_port part of url and context is replaced with "_" in ZK
  u = node.tr('_', '/')
  # add basic auth credentials in the form of "http://user:pass@host"
  if @options[:user] && @options[:password]
    u = @options[:user] + ':' + @options[:password] + '@' + u
  end
  # add url scheme
  u = @url_scheme + '://' + u
  u
end
select_node(collection) click to toggle source
# File lib/rsolr/cloud/connection.rb, line 41
def select_node(collection)
  synchronize { @live_nodes.sample + '/' + collection }
end
update_live_nodes() click to toggle source
# File lib/rsolr/cloud/connection.rb, line 52
def update_live_nodes
  synchronize do
    @live_nodes = []
    @zk.children(ZNODE_LIVE_NODES, watch: true).each do |node|
      @live_nodes << node_url(node)
    end
  end
end