You are here

protected function Apache_Solr_Service_Balancer::_selectWriteServiceSafe in Apache Solr Search 5

Iterate through available write services and select the first with a ping that satisfies configured timeout restrictions (or the default). The timeout period will increase until a connection is made or the limit is reached. This will allow for increased reliability with heavily loaded server(s).

Return value

Apache_Solr_Service

Throws

Exception If there are no write services that meet requirements

1 call to Apache_Solr_Service_Balancer::_selectWriteServiceSafe()
Apache_Solr_Service_Balancer::_selectWriteService in SolrPhpClient/Apache/Solr/Service/Balancer.php
Iterate through available write services and select the first with a ping that satisfies configured timeout restrictions (or the default)

File

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

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 _selectWriteServiceSafe($forceSelect = false) {
  if (!$this->_currentWriteService || !isset($this->_writeableServices[$this->_currentWriteService]) || $forceSelect) {
    if (count($this->_writeableServices)) {
      $backoff = $this->_defaultBackoff;
      do {

        // select one of the read services at random
        $ids = array_keys($this->_writeableServices);
        $id = $ids[rand(0, count($ids) - 1)];
        $service = $this->_writeableServices[$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->_writeableServices[$id] = $service;
        }
        $this->_currentWriteService = $id;
        $backoff *= $this->_backoffEscalation;
        if ($backoff > $this->_backoffLimit) {
          throw new Exception('No write services were available.  All timeouts exceeded.');
        }
      } while ($this->_writeableServices[$this->_currentWriteService]
        ->ping($backoff) === false);
    }
    else {
      throw new Exception('No write services were available');
    }
  }
  return $this->_writeableServices[$this->_currentWriteService];
}