You are here

protected function Apache_Solr_Service_Balancer::_selectReadService in Apache Solr Search 5

Iterate through available read services and select the first with a ping that satisfies configured timeout restrictions (or the default)

Return value

Apache_Solr_Service

Throws

Exception If there are no read services that meet requirements

2 calls to Apache_Solr_Service_Balancer::_selectReadService()
Apache_Solr_Service_Balancer::search in SolrPhpClient/Apache/Solr/Service/Balancer.php
Simple Search interface
Apache_Solr_Service_Balancer::setCreateDocuments in SolrPhpClient/Apache/Solr/Service/Balancer.php

File

SolrPhpClient/Apache/Solr/Service/Balancer.php, line 278

Class

Apache_Solr_Service_Balancer
Reference Implementation for using multiple Solr services in a distribution. Functionality includes: routing of read / write operations failover (on selection) for multiple read servers

Code

protected function _selectReadService($forceSelect = false) {
  if (!$this->_currentReadService || !isset($this->_readableServices[$this->_currentReadService]) || $forceSelect) {
    if ($this->_currentReadService && isset($this->_readableServices[$this->_currentReadService]) && $forceSelect) {

      // we probably had a communication error, ping the current read service, remove it if it times out
      if ($this->_readableServices[$this->_currentReadService]
        ->ping($this->_readPingTimeout) === false) {
        $this
          ->removeReadService($this->_currentReadService);
      }
    }
    if (count($this->_readableServices)) {

      // select one of the read services at random
      $ids = array_keys($this->_readableServices);
      $id = $ids[rand(0, count($ids) - 1)];
      $service = $this->_readableServices[$id];
      if (is_array($service)) {

        //convert the array definition to a client object
        $service = new Apache_Solr_Service($service['host'], $service['port'], $service['path']);
        $this->_readableServices[$id] = $service;
      }
      $service
        ->setCreateDocuments($this->_createDocuments);
      $this->_currentReadService = $id;
    }
    else {
      throw new Exception('No read services were available');
    }
  }
  return $this->_readableServices[$this->_currentReadService];
}