From aca8ca462238fbbf1a6192fe0c5b78db6a9e2a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20R?= Date: Tue, 6 Mar 2018 15:40:17 +0100 Subject: [PATCH] EZP-28814: Make single core Solr work on platform.sh (#117) Closes #110 --- lib/Gateway/EndpointRegistry.php | 14 ++++++++ lib/Gateway/EndpointResolver.php | 2 -- .../NativeEndpointResolver.php | 34 ++++++++++++++++++- lib/Gateway/Native.php | 12 +++++++ lib/Gateway/SingleEndpointResolver.php | 22 ++++++++++++ lib/Resources/config/container/solr.yml | 1 + lib/ResultExtractor.php | 19 +++++++---- .../LoadingResultExtractor.php | 6 ++-- .../NativeEndpointResolverTest.php | 16 ++++++++- 9 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 lib/Gateway/SingleEndpointResolver.php diff --git a/lib/Gateway/EndpointRegistry.php b/lib/Gateway/EndpointRegistry.php index a6147e85d..b9d7f6026 100644 --- a/lib/Gateway/EndpointRegistry.php +++ b/lib/Gateway/EndpointRegistry.php @@ -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); + } } diff --git a/lib/Gateway/EndpointResolver.php b/lib/Gateway/EndpointResolver.php index 82ab4922b..b9c8b4ded 100644 --- a/lib/Gateway/EndpointResolver.php +++ b/lib/Gateway/EndpointResolver.php @@ -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; diff --git a/lib/Gateway/EndpointResolver/NativeEndpointResolver.php b/lib/Gateway/EndpointResolver/NativeEndpointResolver.php index ae88210f1..09c681401 100644 --- a/lib/Gateway/EndpointResolver/NativeEndpointResolver.php +++ b/lib/Gateway/EndpointResolver/NativeEndpointResolver.php @@ -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. @@ -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. * @@ -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; + } } diff --git a/lib/Gateway/Native.php b/lib/Gateway/Native.php index 2fba8fae5..da97166cd 100644 --- a/lib/Gateway/Native.php +++ b/lib/Gateway/Native.php @@ -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); @@ -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)) { diff --git a/lib/Gateway/SingleEndpointResolver.php b/lib/Gateway/SingleEndpointResolver.php new file mode 100644 index 000000000..d4a93794c --- /dev/null +++ b/lib/Gateway/SingleEndpointResolver.php @@ -0,0 +1,22 @@ +facetBuilderVisitor = $facetBuilderVisitor; + $this->endpointRegistry = $endpointRegistry; } /** @@ -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]'}; } diff --git a/lib/ResultExtractor/LoadingResultExtractor.php b/lib/ResultExtractor/LoadingResultExtractor.php index 607185966..09255387f 100644 --- a/lib/ResultExtractor/LoadingResultExtractor.php +++ b/lib/ResultExtractor/LoadingResultExtractor.php @@ -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; @@ -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); } /** diff --git a/tests/lib/Search/Gateway/EndpointResolver/NativeEndpointResolverTest.php b/tests/lib/Search/Gateway/EndpointResolver/NativeEndpointResolverTest.php index f7f3c91f3..cce585390 100644 --- a/tests/lib/Search/Gateway/EndpointResolver/NativeEndpointResolverTest.php +++ b/tests/lib/Search/Gateway/EndpointResolver/NativeEndpointResolverTest.php @@ -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; @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -789,6 +796,7 @@ public function providerForTestGetSearchTargets() array( 'main_languages_endpoint', ), + false, ), ); } @@ -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(), @@ -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()