Skip to content

Commit

Permalink
Add support for multiple servers in Elastica_Client (issue #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruflin committed Jun 18, 2011
1 parent 39934d9 commit fa7117e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Elastica_Client
'transport' => self::DEFAULT_TRANSPORT,
'timeout' => self::TIMEOUT,
'headers' => array(),
'servers' => array()
'servers' => array(),
'roundRobin' => false,
);

/**
Expand Down
32 changes: 30 additions & 2 deletions lib/Elastica/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Elastica_Request {
protected $_method;
protected $_data;

/**
* Internal id of last used server. This is used for round robin
*
* @var int Last server id
*/
protected static $_serverId = null;

/**
* @param string $path Request path
* @param string $method Request method (use const's)
Expand Down Expand Up @@ -113,10 +120,31 @@ public function getTransport() {
return new $className($this);
}

/**
* Sends request to server
*
* @return Elastica_Response Response object
*/
public function send() {
$transport = $this->getTransport();

// TODO Implement multiple servers
return $transport->exec($this->getClient()->getHost(), $this->getClient()->getPort());
$servers = $this->getClient()->getConfig('servers');

if (empty($servers)) {
$response = $transport->exec($this->getClient()->getHost(), $this->getClient()->getPort());
} else {
// Set server id for first request (round robin by default)
if (is_null(self::$_serverId)) {
self::$_serverId = rand(0, count($servers) - 1);
} else {
self::$_serverId = (self::$_serverId + 1) % count($servers);
}

$server = $servers[self::$_serverId];

$response = $transport->exec($server['host'], $server['port']);
}

return $response;
}
}
7 changes: 7 additions & 0 deletions lib/Elastica/Transport/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ public function getRequest() {
return $this->_request;
}

/**
* Executes the transport request
*
* @param string $host Hostname
* @param int $port Port number
* @return Elastica_Response Response object
*/
abstract public function exec($host, $port);
}
30 changes: 30 additions & 0 deletions test/lib/Elastica/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ public function testDefaults() {
$this->assertEquals(Elastica_Client::DEFAULT_TRANSPORT, $client->getTransport());
}

public function testServersArray() {
// Creates a new index 'xodoa' and a type 'user' inside this index
$client = new Elastica_Client(array('servers' => array(array('host' => 'localhost', 'port' => 9200))));
$index = $client->getIndex('test1');
$index->create(array(), true);

$type = $index->getType('user');

// Adds 1 document to the index
$doc1 = new Elastica_Document(1,
array('username' => 'hans', 'test' => array('2', '3', '5'))
);
$type->addDocument($doc1);

// Adds a list of documents with _bulk upload to the index
$docs = array();
$docs[] = new Elastica_Document(2,
array('username' => 'john', 'test' => array('1', '3', '6'))
);
$docs[] = new Elastica_Document(3,
array('username' => 'rolf', 'test' => array('2', '3', '7'))
);
$type->addDocuments($docs);

// Refresh index
$index->refresh();

$resultSet = $type->search('rolf');
}

public function testBulk() {
$client = new Elastica_Client();

Expand Down
1 change: 0 additions & 1 deletion test/lib/Elastica/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function testExample() {

$type = $index->getType('user');


// Adds 1 document to the index
$doc1 = new Elastica_Document(1,
array('username' => 'hans', 'test' => array('2', '3', '5'))
Expand Down

0 comments on commit fa7117e

Please sign in to comment.