Ability to set connectionStrategy for elastica clients

This commit is contained in:
Tim Nagel 2014-11-01 19:33:05 +11:00
parent 030b194c7b
commit 906e2e0749
5 changed files with 70 additions and 0 deletions

View file

@ -15,6 +15,7 @@ To generate a changelog summary since the last version, run
* 3.0.8 (Unreleased)
* Fixed handling of empty indexes #760
* Added support for `connectionStrategy` Elastica configuration #732
* 3.0.7 (2015-01-21)

View file

@ -84,6 +84,16 @@ class Configuration implements ConfigurationInterface
return $v;
})
->end()
// Elastica names its properties with camel case, support both
->beforeNormalization()
->ifTrue(function ($v) { return isset($v['connection_strategy']); })
->then(function ($v) {
$v['connectionStrategy'] = $v['connection_strategy'];
unset($v['connection_strategy']);
return $v;
})
->end()
// If there is no connections array key defined, assume a single connection.
->beforeNormalization()
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); })
@ -124,6 +134,7 @@ class Configuration implements ConfigurationInterface
->end()
->scalarNode('timeout')->end()
->scalarNode('headers')->end()
->scalarNode('connectionStrategy')->defaultValue('Simple')->end()
->end()
->end()
->end()

View file

@ -11,6 +11,11 @@ fos_elastica:
connections:
- url: http://es1.example.net:9200
- url: http://es2.example.net:9200
connection_strategy: RoundRobin
```
Elastica allows for definition of different connection strategies and by default
supports `RoundRobin` and `Simple`. You can see definitions for these strategies
in the `Elastica\Connection\Strategy` namespace.
For more information on Elastica clustering see http://elastica.io/getting-started/installation.html#section-connect-cluster

View file

@ -0,0 +1,48 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) Tim Nagel <tim@nagel.com.au>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace FOS\ElasticaBundle\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Client;
/**
* @group functional
*/
class ClientTest extends WebTestCase
{
public function testContainerSource()
{
$client = $this->createClient(array('test_case' => 'Basic'));
$es = $client->getContainer()->get('fos_elastica.client.default');
$this->assertInstanceOf('Elastica\\Connection\\Strategy\\RoundRobin', $es->getConnectionStrategy());
$es = $client->getContainer()->get('fos_elastica.client.second_server');
$this->assertInstanceOf('Elastica\\Connection\\Strategy\\RoundRobin', $es->getConnectionStrategy());
$es = $client->getContainer()->get('fos_elastica.client.third');
$this->assertInstanceOf('Elastica\\Connection\\Strategy\\Simple', $es->getConnectionStrategy());
}
protected function setUp()
{
parent::setUp();
$this->deleteTmpDir('Basic');
}
protected function tearDown()
{
parent::tearDown();
$this->deleteTmpDir('Basic');
}
}

View file

@ -15,7 +15,12 @@ fos_elastica:
- url: http://localhost:9200
- host: localhost
port: 9200
connectionStrategy: RoundRobin
second_server:
connections:
- url: http://localhost:9200
connection_strategy: RoundRobin
third:
url: http://localhost:9200
indexes:
index: