Skip to content

Commit

Permalink
EZP-28814: Make single core Solr work on platform.sh (#117)
Browse files Browse the repository at this point in the history
Closes #110
  • Loading branch information
andrerom authored Mar 6, 2018
1 parent 775a2e3 commit aca8ca4
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 12 deletions.
14 changes: 14 additions & 0 deletions lib/Gateway/EndpointRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ public function getEndpoint($name)

return $this->endpoint[$name];
}

/**
* Get first Endpoint, for usecases where there is only one.
*
* @return \EzSystems\EzPlatformSolrSearchEngine\Gateway\Endpoint
*/
public function getFirstEndpoint()
{
if (empty($this->endpoint)) {
throw new OutOfBoundsException("No Endpoint registered at all'.");
}

return reset($this->endpoint);
}
}
2 changes: 0 additions & 2 deletions lib/Gateway/EndpointResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace EzSystems\EzPlatformSolrSearchEngine\Gateway;

Expand Down
34 changes: 33 additions & 1 deletion lib/Gateway/EndpointResolver/NativeEndpointResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
namespace EzSystems\EzPlatformSolrSearchEngine\Gateway\EndpointResolver;

use EzSystems\EzPlatformSolrSearchEngine\Gateway\EndpointResolver;
use EzSystems\EzPlatformSolrSearchEngine\Gateway\SingleEndpointResolver;
use RuntimeException;

/**
* NativeEndpointResolver provides Solr endpoints for a Content translations.
*/
class NativeEndpointResolver implements EndpointResolver
class NativeEndpointResolver implements EndpointResolver, SingleEndpointResolver
{
/**
* Holds an array of Solr entry endpoint names.
Expand Down Expand Up @@ -54,6 +55,13 @@ class NativeEndpointResolver implements EndpointResolver
*/
private $mainLanguagesEndpoint;

/**
* Result of hasMultipleEndpoints() once called the first time.
*
* @var bool|null
*/
protected $hasMultiple = null;

/**
* Create from Endpoint names.
*
Expand Down Expand Up @@ -162,4 +170,28 @@ public function getEndpoints()

return array_keys($endpointSet);
}

/**
* Returns true if current configurations has several endpoints.
*
* @return bool
*/
public function hasMultipleEndpoints()
{
if ($this->hasMultiple !== null) {
return $this->hasMultiple;
}

$endpointSet = array_flip($this->endpointMap);

if (isset($this->defaultEndpoint)) {
$endpointSet[$this->defaultEndpoint] = true;
}

if (isset($this->mainLanguagesEndpoint)) {
$endpointSet[$this->mainLanguagesEndpoint] = true;
}

return $this->hasMultiple = count($endpointSet) > 1;
}
}
12 changes: 12 additions & 0 deletions lib/Gateway/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ protected function generateQueryString(array $parameters)
/**
* Returns search targets for given language settings.
*
* Only return endpoints if there are more then one configured, as this is meant for use on shard parameter.
*
* @param array $languageSettings
*
* @return string
*/
protected function getSearchTargets($languageSettings)
{
if ($this->endpointResolver instanceof SingleEndpointResolver && !$this->endpointResolver->hasMultipleEndpoints()) {
return '';
}

$shards = array();
$endpoints = $this->endpointResolver->getSearchTargets($languageSettings);

Expand All @@ -196,10 +202,16 @@ protected function getSearchTargets($languageSettings)
/**
* Returns all search targets without language constraint.
*
* Only return endpoints if there are more then one configured, as this is meant for use on shard parameter.
*
* @return string
*/
protected function getAllSearchTargets()
{
if ($this->endpointResolver instanceof SingleEndpointResolver && !$this->endpointResolver->hasMultipleEndpoints()) {
return '';
}

$shards = [];
$searchTargets = $this->endpointResolver->getEndpoints();
if (!empty($searchTargets)) {
Expand Down
22 changes: 22 additions & 0 deletions lib/Gateway/SingleEndpointResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* This file is part of the eZ Platform Solr Search Engine package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformSolrSearchEngine\Gateway;

/**
* Additional interface for Endpoint resolvers which resolves Solr backend endpoints.
*/
interface SingleEndpointResolver
{
/**
* Returns true if current configurations has several endpoints.
*
* @return bool
*/
public function hasMultipleEndpoints();
}
1 change: 1 addition & 0 deletions lib/Resources/config/container/solr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ services:
class: "%ezpublish.search.solr.result_extractor.native.class%"
arguments:
- "@ezpublish.search.solr.query.content.facet_builder_visitor.aggregate"
- "@ezpublish.search.solr.gateway.endpoint_registry"

ezpublish.search.solr.result_extractor:
alias: ezpublish.search.solr.result_extractor.native
Expand Down
19 changes: 13 additions & 6 deletions lib/ResultExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace EzSystems\EzPlatformSolrSearchEngine;

use EzSystems\EzPlatformSolrSearchEngine\Gateway\EndpointRegistry;
use EzSystems\EzPlatformSolrSearchEngine\Query\FacetFieldVisitor;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
Expand All @@ -20,16 +21,16 @@
*/
abstract class ResultExtractor
{
/**
* Facet builder visitor.
*
* @var \EzSystems\EzPlatformSolrSearchEngine\Query\FacetFieldVisitor
*/
/** @var \EzSystems\EzPlatformSolrSearchEngine\Query\FacetFieldVisitor */
protected $facetBuilderVisitor;

public function __construct(FacetFieldVisitor $facetBuilderVisitor)
/** @var \EzSystems\EzPlatformSolrSearchEngine\Gateway\EndpointRegistry */
protected $endpointRegistry;

public function __construct(FacetFieldVisitor $facetBuilderVisitor, EndpointRegistry $endpointRegistry)
{
$this->facetBuilderVisitor = $facetBuilderVisitor;
$this->endpointRegistry = $endpointRegistry;
}

/**
Expand Down Expand Up @@ -114,6 +115,12 @@ protected function getMatchedLanguageCode($hit)
*/
protected function getIndexIdentifier($hit)
{
// In single core setup, shard parameter is not set on request to avoid issues in environments that does not
// know about own dns, which means it's not set here either
if ($hit->{'[shard]'} === '[not a shard request]') {
return $this->endpointRegistry->getFirstEndpoint()->getIdentifier();
}

return $hit->{'[shard]'};
}

Expand Down
6 changes: 4 additions & 2 deletions lib/ResultExtractor/LoadingResultExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace EzSystems\EzPlatformSolrSearchEngine\ResultExtractor;

use EzSystems\EzPlatformSolrSearchEngine\Gateway\EndpointRegistry;
use EzSystems\EzPlatformSolrSearchEngine\ResultExtractor;
use eZ\Publish\SPI\Persistence\Content\Handler as ContentHandler;
use eZ\Publish\SPI\Persistence\Content\Location\Handler as LocationHandler;
Expand Down Expand Up @@ -39,12 +40,13 @@ class LoadingResultExtractor extends ResultExtractor
public function __construct(
ContentHandler $contentHandler,
LocationHandler $locationHandler,
FacetBuilderVisitor $facetBuilderVisitor
FacetBuilderVisitor $facetBuilderVisitor,
EndpointRegistry $endpointRegistry
) {
$this->contentHandler = $contentHandler;
$this->locationHandler = $locationHandler;

parent::__construct($facetBuilderVisitor);
parent::__construct($facetBuilderVisitor, $endpointRegistry);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EzSystems\EzPlatformSolrSearchEngine\Tests\Search\Gateway\EndpointResolver;

use EzSystems\EzPlatformSolrSearchEngine\Gateway\EndpointResolver\NativeEndpointResolver;
use EzSystems\EzPlatformSolrSearchEngine\Gateway\SingleEndpointResolver;
use EzSystems\EzPlatformSolrSearchEngine\Tests\Search\TestCase;
use RuntimeException;

Expand Down Expand Up @@ -158,6 +159,7 @@ public function providerForTestGetSearchTargets()
array(
'endpoint_en_GB',
),
false,
),
// Will return all endpoints (for always available fallback without main languages endpoint)
3 => array(
Expand Down Expand Up @@ -689,6 +691,7 @@ public function providerForTestGetSearchTargets()
array(
'default_endpoint',
),
false,
),
// Will return main languages endpoint (search on main languages with main languages endpoint)
34 => array(
Expand All @@ -699,6 +702,7 @@ public function providerForTestGetSearchTargets()
array(
'main_languages_endpoint',
),
false,
),
// Will return main languages endpoint (search on main languages with main languages endpoint)
35 => array(
Expand All @@ -722,6 +726,7 @@ public function providerForTestGetSearchTargets()
array(
'default_endpoint',
),
false,
),
// Will return main languages endpoint (search on main languages with main languages endpoint)
37 => array(
Expand All @@ -748,6 +753,7 @@ public function providerForTestGetSearchTargets()
array(
'main_languages_endpoint',
),
false,
),
// Will return all endpoints (search on main languages without main languages endpoint)
39 => array(
Expand All @@ -763,6 +769,7 @@ public function providerForTestGetSearchTargets()
array(
'default_endpoint',
),
false,
),
// Will return main languages endpoint (search on main languages with main languages endpoint)
40 => array(
Expand All @@ -789,6 +796,7 @@ public function providerForTestGetSearchTargets()
array(
'main_languages_endpoint',
),
false,
),
);
}
Expand All @@ -801,13 +809,15 @@ public function providerForTestGetSearchTargets()
* @param null|string $mainLanguagesEndpoint
* @param array $languageSettings
* @param string[] $expected
* @param bool $expectedIsMultiple
*/
public function testGetSearchTargets(
$endpointMap,
$defaultEndpoint,
$mainLanguagesEndpoint,
$languageSettings,
$expected
$expected,
$expectedIsMultiple = true
) {
$endpointResolver = $this->getEndpointResolver(
array(),
Expand All @@ -819,6 +829,10 @@ public function testGetSearchTargets(
$actual = $endpointResolver->getSearchTargets($languageSettings);

$this->assertEquals($expected, $actual);

if ($endpointResolver instanceof SingleEndpointResolver) {
$this->assertEquals($expectedIsMultiple, $endpointResolver->hasMultipleEndpoints());
}
}

public function providerForTestGetSearchTargetsThrowsRuntimeException()
Expand Down

0 comments on commit aca8ca4

Please sign in to comment.