Skip to content

Commit

Permalink
Add ability to configure URL Prefix
Browse files Browse the repository at this point in the history
This commit allows a user to configure per-node URL prefixes.  Very
simple change, but touched many places of the code (particularly the
older unit tests).

Connections are now built with a single array containing [host, port,
path].  Port and path are optional.

The user enables prefixing by adding it to the host string parameter
on client creation:

```
$params['hosts'] = [
  'localhost:9200/prefix',
  'localhost:9201',
  'localhost:9202/another/prefix'
];
$client = new Client($params);
```
  • Loading branch information
polyfractal committed Mar 31, 2014
1 parent 146397c commit 00d28df
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 98 deletions.
5 changes: 1 addition & 4 deletions src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1323,10 +1323,7 @@ private function extractURIParts($host)
$parts['port'] = 9200;
}

return array(
'host' => $parts['host'],
'port' => $parts['port'],
);
return $parts;
}


Expand Down
5 changes: 2 additions & 3 deletions src/Elasticsearch/Common/DICBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,9 @@ private function setEndpointDICObjects()
private function setConnectionObj()
{
$this->dic['connection'] = function ($dicParams) {
return function ($host, $port = null) use ($dicParams) {
return function ($hostDetails) use ($dicParams) {
return new $dicParams['connectionClass'](
$host,
$port,
$hostDetails,
$dicParams['connectionParamsShared'],
$dicParams['logObject'],
$dicParams['traceObject']
Expand Down
7 changes: 4 additions & 3 deletions src/Elasticsearch/ConnectionPool/SniffingConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ private function sniffConnection(AbstractConnection $connection)
$this->connections = array();

foreach ($nodes as $node) {
$this->connections[] = $this->connectionFactory->create(
$node['host'],
$node['port']
$nodeDetails = array(
'host' => $node['host'],
'port' => $node['port']
);
$this->connections[] = $this->connectionFactory->create($nodeDetails);
}

$this->nextSniff = time() + $this->sniffingInterval;
Expand Down
20 changes: 16 additions & 4 deletions src/Elasticsearch/Connections/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ abstract public function getTransportSchema();
/**
* Constructor
*
* @param string $host Host string
* @param string $port Host port
* @param array $hostDetails
* @param array $connectionParams Array of connection-specific parameters
* @param \Psr\Log\LoggerInterface $log Logger object
* @param \Psr\Log\LoggerInterface $trace
*/
public function __construct($host, $port, $connectionParams, LoggerInterface $log, LoggerInterface $trace)
public function __construct($hostDetails, $connectionParams, LoggerInterface $log, LoggerInterface $trace)
{
$this->host = $this->transportSchema . '://' . $host . ':' . $port;
$host = $this->transportSchema.'://'.$hostDetails['host'].':'.$hostDetails['port'];
if (isset($hostDetails['path']) === true) {
$host .= $hostDetails['path'];
}
$this->host = $host;
$this->log = $log;
$this->trace = $trace;
$this->connectionParams = $connectionParams;
Expand Down Expand Up @@ -272,6 +275,15 @@ public function getPingFailures()
}


/**
* @return string
*/
public function getHost()
{
return $this->host;
}


/**
* @param $curlErrorNumber
* @param $message
Expand Down
8 changes: 3 additions & 5 deletions src/Elasticsearch/Connections/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
class ConnectionFactory extends AbstractFactory
{
/**
* @param string $host
* @param int $port
* @param $hostDetails
*
* @return AbstractConnection
*/
public function create($host, $port)
public function create($hostDetails)
{
return $this->container['connection'](
$host,
$port,
$hostDetails,
$this->container['connectionParamsShared'],
$this->container['logObject'],
$this->container['traceObject']
Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch/Connections/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
interface ConnectionInterface
{
public function __construct($host, $port, $connectionParams, LoggerInterface $log, LoggerInterface $trace);
public function __construct($hostDetails, $connectionParams, LoggerInterface $log, LoggerInterface $trace);

public function getTransportSchema();

Expand Down
11 changes: 5 additions & 6 deletions src/Elasticsearch/Connections/CurlMultiConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class CurlMultiConnection extends AbstractConnection implements ConnectionInterf
/**
* Constructor
*
* @param string $host Host string
* @param int $port Host port
* @param array $hostDetails
* @param array $connectionParams Array of connection parameters
* @param \Psr\Log\LoggerInterface $log logger object
* @param \Psr\Log\LoggerInterface $trace logger object (for curl traces)
Expand All @@ -58,7 +57,7 @@ class CurlMultiConnection extends AbstractConnection implements ConnectionInterf
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
* @return CurlMultiConnection
*/
public function __construct($host, $port, $connectionParams, LoggerInterface $log, LoggerInterface $trace)
public function __construct($hostDetails, $connectionParams, LoggerInterface $log, LoggerInterface $trace)
{
if (extension_loaded('curl') !== true) {
$log->critical('Curl library/extension is required for CurlMultiConnection.');
Expand All @@ -70,15 +69,15 @@ public function __construct($host, $port, $connectionParams, LoggerInterface $lo
throw new InvalidArgumentException('curlMultiHandle must be set in connectionParams');
}

if (isset($port) !== true) {
$port = 9200;
if (isset($hostDetails['port']) !== true) {
$hostDetails['port'] = 9200;
}

$connectionParams = $this->transformAuth($connectionParams);
$this->curlOpts = $this->generateCurlOpts($connectionParams);

$this->multiHandle = $connectionParams['curlMultiHandle'];
return parent::__construct($host, $port, $connectionParams, $log, $trace);
return parent::__construct($hostDetails, $connectionParams, $log, $trace);

}

Expand Down
11 changes: 5 additions & 6 deletions src/Elasticsearch/Connections/GuzzleConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,31 @@ class GuzzleConnection extends AbstractConnection implements ConnectionInterface


/**
* @param string $host Host string
* @param int $port Host port
* @param array $hostDetails
* @param array $connectionParams Array of connection parameters
* @param \Psr\Log\LoggerInterface $log logger object
* @param \Psr\Log\LoggerInterface $trace logger object (for curl traces)
*
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
* @return \Elasticsearch\Connections\GuzzleConnection
*/
public function __construct($host, $port, $connectionParams, LoggerInterface $log, LoggerInterface $trace)
public function __construct($hostDetails, $connectionParams, LoggerInterface $log, LoggerInterface $trace)
{
if (isset($connectionParams['guzzleClient']) !== true) {
$log->critical('guzzleClient must be set in connectionParams');
throw new InvalidArgumentException('guzzleClient must be set in connectionParams');
}

if (isset($port) !== true) {
$port = 9200;
if (isset($hostDetails['port']) !== true) {
$hostDetails['port'] = 9200;
}
$this->guzzle = $connectionParams['guzzleClient'];

if (isset($connectionParams['connectionParams'])) {
$this->connectionOpts = $connectionParams['connectionParams'];
}

return parent::__construct($host, $port, $connectionParams, $log, $trace);
return parent::__construct($hostDetails, $connectionParams, $log, $trace);

}

Expand Down
11 changes: 3 additions & 8 deletions src/Elasticsearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,9 @@ private function hostsToConnections($hosts)
{
$connections = array();
foreach ($hosts as $host) {
if (isset($host['port']) === true) {
$connections[] = $this->params['connection'](
$host['host'],
$host['port']
);
} else {
$connections[] = $this->params['connection']($host['host']);
}
$connections[] = $this->params['connection'](
$host
);
}

return $connections;
Expand Down
10 changes: 5 additions & 5 deletions tests/Elasticsearch/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testOneGoodOneBadHostNoException()
public function testOneGoodOneBadHostNoRetryException()
{
$params = array('hosts' => array (
'127.0.0.1:80',
'127.0.0.1:1',
$_SERVER['ES_TEST_HOST'],
));
$params['retries'] = 0;
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testConstructorEmptyPort()
'hosts' => array('localhost:'),
'dic' => function ($hosts, $params) use ($mockDIC, $that) {

$expected = array(array('host' => 'localhost', 'port' => 9200));
$expected = array(array('scheme' => 'http', 'host' => 'localhost', 'port' => 9200));
$that->assertEquals($expected, $hosts);
return $mockDIC;
}
Expand All @@ -152,7 +152,7 @@ public function testConstructorNoPort()
'hosts' => array('localhost'),
'dic' => function ($hosts, $params) use ($mockDIC, $that) {

$expected = array(array('host' => 'localhost', 'port' => 9200));
$expected = array(array('scheme' => 'http', 'host' => 'localhost', 'port' => 9200));
$that->assertEquals($expected, $hosts);
return $mockDIC;
}
Expand All @@ -172,7 +172,7 @@ public function testConstructorWithPort()
'hosts' => array('localhost:9200'),
'dic' => function ($hosts, $params) use ($mockDIC, $that) {

$expected = array(array('host' => 'localhost', 'port' => 9200));
$expected = array(array('scheme' => 'http', 'host' => 'localhost', 'port' => 9200));
$that->assertEquals($expected, $hosts);
return $mockDIC;
}
Expand All @@ -192,7 +192,7 @@ public function testConstructorWithSchemeAndPort()
'hosts' => array('http://localhost:9200'),
'dic' => function ($hosts, $params) use ($mockDIC, $that) {

$expected = array(array('host' => 'localhost', 'port' => 9200));
$expected = array(array('scheme' => 'http', 'host' => 'localhost', 'port' => 9200));
$that->assertEquals($expected, $hosts);
return $mockDIC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testAddOneHostAndTriggerSniff()
->getMock();

$connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
->shouldReceive('create')->with('192.168.1.119', 9200)->andReturn($mockNewConnection)->getMock();
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($mockNewConnection)->getMock();

$connectionPoolParams = array(
'randomizeHosts' => false,
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testAddOneHostAndForceNext()
->shouldReceive('select')->once()->andReturn($mockNewConnection)->getMock();

$connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
->shouldReceive('create')->with('192.168.1.119', 9200)->andReturn($mockNewConnection)->getMock();
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($mockNewConnection)->getMock();

$connectionPoolParams = array(
'randomizeHosts' => false
Expand Down Expand Up @@ -258,8 +258,8 @@ public function testAddOneHostSniffTwo()
->getMock();

$connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
->shouldReceive('create')->with('192.168.1.119', 9200)->andReturn($newConnections[0])->getMock()
->shouldReceive('create')->with('192.168.1.119', 9201)->andReturn($newConnections[1])->getMock();
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[0])->getMock()
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[1])->getMock();

$connectionPoolParams = array(
'randomizeHosts' => false,
Expand Down Expand Up @@ -308,8 +308,8 @@ public function testAddSeed_SniffTwo_TimeoutTwo()
->getMock();

$connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
->shouldReceive('create')->with('192.168.1.119', 9200)->andReturn($newConnections[0])->getMock()
->shouldReceive('create')->with('192.168.1.119', 9201)->andReturn($newConnections[1])->getMock();
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[0])->getMock()
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[1])->getMock();

$connectionPoolParams = array(
'randomizeHosts' => false,
Expand Down Expand Up @@ -361,8 +361,8 @@ public function testTen_TimeoutNine_SniffTenth_AddTwoAlive()
->getMock();

$connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
->shouldReceive('create')->with('192.168.1.119', 9200)->andReturn($newConnections[10])->getMock()
->shouldReceive('create')->with('192.168.1.119', 9201)->andReturn($newConnections[11])->getMock();
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[10])->getMock()
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[11])->getMock();

$connectionPoolParams = array(
'randomizeHosts' => false,
Expand Down Expand Up @@ -424,8 +424,8 @@ public function testTen_TimeoutNine_SniffTenth_AddTwoDead_TimeoutEveryone()
$RRConnections = $newConnections;
//array_push($connections);
$connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
->shouldReceive('create')->with('192.168.1.119', 9200)->andReturn($newConnections[10])->getMock()
->shouldReceive('create')->with('192.168.1.119', 9201)->andReturn($newConnections[11])->getMock();
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[10])->getMock()
->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[11])->getMock();

$connectionPoolParams = array(
'randomizeHosts' => false,
Expand Down
27 changes: 21 additions & 6 deletions tests/Elasticsearch/Tests/Connections/AbstractConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public function testPing()
$stub = $this->getMockForAbstractClass(
'Elasticsearch\Connections\AbstractConnection',
array(
'localhost',
9200,
array('host' => 'localhost', 'port' => 9200),
array(),
$logger,
$logger
Expand Down Expand Up @@ -65,8 +64,7 @@ public function testPingBadCode()
$stub = $this->getMockForAbstractClass(
'Elasticsearch\Connections\AbstractConnection',
array(
'localhost',
9200,
array('host' => 'localhost', 'port' => 9200),
array(),
$logger,
$logger
Expand Down Expand Up @@ -94,8 +92,7 @@ public function testPingTimeout()
$stub = $this->getMockForAbstractClass(
'Elasticsearch\Connections\AbstractConnection',
array(
'localhost',
9200,
array('host' => 'localhost', 'port' => 9200),
array(),
$logger,
$logger
Expand All @@ -116,5 +113,23 @@ public function testPingTimeout()
$this->assertFalse($ret);
}

public function testURLPrefix()
{
$logger = new Elasticsearch\Common\EmptyLogger();

$stub = $this->getMockForAbstractClass(
'Elasticsearch\Connections\AbstractConnection',
array(
array('host' => 'localhost', 'port' => 9200, 'path' => '/prefix'),
array(),
$logger,
$logger
)
);

/** @var AbstractConnection $stub */
$this->assertEquals('http://localhost:9200/prefix', $stub->getHost());
}

}

10 changes: 6 additions & 4 deletions tests/Elasticsearch/Tests/Connections/ConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function tearDown() {
public function testCreate()
{
$mockConnection = m::mock('\Elasticsearch\Connections\AbstractConnection');
$mockFunction = function($host, $port, $params, $log, $trace) use ($mockConnection) {
$mockFunction = function($hostDetails, $params, $log, $trace) use ($mockConnection) {
return $mockConnection;
};

Expand All @@ -35,11 +35,13 @@ public function testCreate()
->shouldReceive('offsetGet')->with('logObject')->andReturn(array())->getMock()
->shouldReceive('offsetGet')->with('traceObject')->andReturn(array())->getMock();

$host = 'localhost';
$port = 9200;
$hostDetails = array(
'host' => 'localhost',
'port' => 9200
);

$factory = new ConnectionFactory($mockPimple);
$connection = $factory->create($host, $port);
$connection = $factory->create($hostDetails);

$this->assertEquals($mockConnection, $connection);
}
Expand Down
Loading

0 comments on commit 00d28df

Please sign in to comment.