Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for replicas in distributed search #390

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Options
| shards | string | null | Shards to use for request |
| shardhandler | string | null | Request handler to use |
| collections | string | null | A list of collections, for use with SolrCloud (available in Solarium 3.1+) |
| replicas | string | null | A list of replicas, for use with SolrCloud (available in Solarium 3.1+) |
||

Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class DistributedSearch extends AbstractComponent
*/
protected $collections = array();

/**
* Requests will be load balanced across replicas in this list.
*
* @var array
*/
protected $replicas = array();

/**
* Get component type.
*
Expand Down Expand Up @@ -318,6 +325,94 @@ public function getCollections()
return $this->collections;
}

/**
* Add a replica.
*
* @param string $key unique string
* @param string $replica The syntax is host:port/base_url
*
* @return self Provides fluent interface
*
* @link https://cwiki.apache.org/confluence/display/solr/Distributed+Requests
*/
public function addReplica($key, $replica)
{
$this->replicas[$key] = $replica;

return $this;
}

/**
* Add multiple replicas.
*
* @param array $replicas
*
* @return self Provides fluent interface
*/
public function addReplicas(array $replicas)
{
foreach ($replicas as $key => $replica) {
$this->addReplica($key, $replica);
}

return $this;
}

/**
* Remove a replica.
*
* @param string $key
*
* @return self Provides fluent interface
*/
public function removeReplica($key)
{
if (isset($this->replicas[$key])) {
unset($this->replicas[$key]);
}

return $this;
}

/**
* Remove all replicas.
*
* @return self Provides fluent interface
*/
public function clearReplicas()
{
$this->replicas = array();

return $this;
}

/**
* Set multiple replicas.
*
* This overwrites any existing replicas
*
* @param array $replicas Associative array of collections
*
* @return self Provides fluent interface
*/
public function setReplicas(array $replicas)
{
$this->clearReplicas();
$this->addReplicas($replicas);

return $this;
}

/**
* Get a list of the replicas.
*
* @return array
*/
public function getReplicas()
{
return $this->replicas;
}

/**
* Initialize options.
*
Expand All @@ -334,6 +429,9 @@ protected function init()
case 'collections':
$this->setCollections($value);
break;
case 'replicas':
$this->setReplicas($value);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function buildComponent($component, $request)
$request->addParam('shards', implode(',', $shards));
}

$replicas = array_values($component->getReplicas());

if (count($replicas)) {
$value = ($request->getParam('shards')) ? $request->getParam('shards').','.implode('|', $replicas) : implode('|', $replicas);

$request->addParam('shards', $value, true);
}

$request->addParam('shards.qt', $component->getShardRequestHandler());

// add collections to request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ public function testConfigModeForCollections()
$this->assertEquals($options['collections'], $this->distributedSearch->getCollections());
}

public function testConfigModeForReplicas()
{
$options = array(
'replicas' => array(
'replica1' => 'localhost:8983/solr/collection1',
'replica2' => 'localhost:8983/solr/collection2',
),
);

$this->distributedSearch->setOptions($options);
$this->assertEquals($options['replicas'], $this->distributedSearch->getReplicas());
}

public function testGetType()
{
$this->assertEquals(
Expand Down Expand Up @@ -242,4 +255,73 @@ public function testSetCollections()
$collections
);
}

public function testAddReplica()
{
$this->distributedSearch->addReplica('replica1', 'localhost:8983/solr/replica1');
$replicas = $this->distributedSearch->getReplicas();
$this->assertEquals(
'localhost:8983/solr/replica1',
$replicas['replica1']
);
}

public function testRemoveReplica()
{
$this->distributedSearch->addReplica('replica1', 'localhost:8983/solr/replica1');
$this->distributedSearch->removeReplica('replica1');
$replicas = $this->distributedSearch->getReplicas();
$this->assertFalse(isset($replicas['replica1']));
}

public function testClearReplicas()
{
$this->distributedSearch->addReplicas(
array(
'replica1' => 'localhost:8983/solr/replica1',
'replica2' => 'localhost:8983/solr/replica2',
)
);
$this->distributedSearch->clearReplicas();
$replicas = $this->distributedSearch->getReplicas();
$this->assertTrue(is_array($replicas));
$this->assertEquals(0, count($replicas));
}

public function testAddReplicas()
{
$replicas = array(
'replica1' => 'localhost:8983/solr/replica1',
'replica2' => 'localhost:8983/solr/replica2',
);
$this->distributedSearch->addReplicas($replicas);
$this->assertEquals($replicas, $this->distributedSearch->getReplicas());
}

public function testSetReplicas()
{
$this->distributedSearch->addReplicas(
array(
'replica1' => 'localhost:8983/solr/replica1',
'replica2' => 'localhost:8983/solr/replica2',
)
);
$this->distributedSearch->setReplicas(
array(
'replica3' => 'localhost:8983/solr/replica3',
'replica4' => 'localhost:8983/solr/replica4',
'replica5' => 'localhost:8983/solr/replica5',
)
);
$replicas = $this->distributedSearch->getReplicas();
$this->assertEquals(3, count($replicas));
$this->assertEquals(
array(
'replica3' => 'localhost:8983/solr/replica3',
'replica4' => 'localhost:8983/solr/replica4',
'replica5' => 'localhost:8983/solr/replica5',
),
$replicas
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/

namespace Solarium\Tests\QueryType\Select\Query\Component\Facet;

use Solarium\QueryType\Select\Query\Component\Facet\Interval;
use Solarium\QueryType\Select\Query\Component\FacetSet;

class IntervalTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Query
*/
protected $facet;

public function setUp()
{
$this->facet = new Interval();
}

public function testConfigMode()
{
$options = array(
'key' => 'myKey',
'exclude' => array('e1', 'e2'),
'set' => array('i1', 'i2'),
);

$this->facet->setOptions($options);

$this->assertEquals($options['key'], $this->facet->getKey());
$this->assertEquals($options['exclude'], $this->facet->getExcludes());
$this->assertEquals($options['set'], $this->facet->getSet());
}

public function testGetType()
{
$this->assertEquals(
FacetSet::FACET_INTERVAL,
$this->facet->getType()
);
}

public function testSetAndGetSet()
{
$this->facet->setSet('interval1,interval2');
$this->assertEquals(array('interval1', 'interval2'), $this->facet->getSet());
}

public function testEmptySet()
{
$this->assertEquals(array(), $this->facet->getSet());
}

public function testSetAndGetField()
{
$this->facet->setField('field1');
$this->assertEquals('field1', $this->facet->getField());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,47 @@ public function testBuildComponentWithCollections()

$this->assertEquals(array('collection' => $url.'1,'.$url.'2,'.$url.'3'), $request->getParams());
}

public function testBuildComponentWithReplicas()
{
$builder = new RequestBuilder();
$request = new Request();

$url = 'localhost:8983/solr/replica';
$component = new Component();
$component->addReplica('replica1', $url.'1');
$component->addReplicas(
array(
'replica2' => $url.'2',
'replica3' => $url.'3',
)
);

$request = $builder->buildComponent($component, $request);

$this->assertEquals(array('shards' => $url.'1|'.$url.'2|'.$url.'3'), $request->getParams());
}



public function testBuildComponentWithReplicasAndShard()
{
$builder = new RequestBuilder();
$request = new Request();

$url = 'localhost:8983/solr/replica';
$component = new Component();
$component->addShard('shard1', 'localhost:8983/solr/shard1');

$component->addReplicas(
array(
'replica2' => $url.'2',
'replica3' => $url.'3',
)
);

$request = $builder->buildComponent($component, $request);

$this->assertEquals(array('shards' => 'localhost:8983/solr/shard1,'.$url.'2|'.$url.'3'), $request->getParams());
}
}
Loading