Skip to content

Commit

Permalink
IBX-3863: Parametrized Solr HTTP Client timeout and max retries (#241)
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-3863 or #241

* Parametrized Solr HTTP Client timeout and max retries

* Provided semantic config for HTTP Client timeout and no. of retries

* Changed Monolog channel from solr to ibexa.solr

* Fixed superfluous leading slash in FQCN class DI param
  • Loading branch information
alongosz committed Oct 5, 2022
1 parent 32a3491 commit 5c6878b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
25 changes: 25 additions & 0 deletions bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

class Configuration implements ConfigurationInterface
{
public const SOLR_HTTP_CLIENT_DEFAULT_TIMEOUT = 10;
public const SOLR_HTTP_CLIENT_DEFAULT_MAX_RETRIES = 3;

protected $rootNodeName;

/**
Expand Down Expand Up @@ -48,6 +51,7 @@ public function getConfigTreeBuilder()

$this->addEndpointsSection($rootNode);
$this->addConnectionsSection($rootNode);
$this->addHttpClientConfigurationSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -394,4 +398,25 @@ function (array $v) {
->end()
->end();
}

private function addHttpClientConfigurationSection(ArrayNodeDefinition $node): void
{
$node->children()
->arrayNode('http_client')
->info('Configuration settings for HTTP Client used to communicate with Solr instance')
->children()
->integerNode('timeout')
->info('HTTP Client timeout')
->min(0)
->defaultValue(self::SOLR_HTTP_CLIENT_DEFAULT_TIMEOUT)
->end()
->integerNode('max_retries')
->info('HTTP Client max retries after failure')
->min(0)
->defaultValue(self::SOLR_HTTP_CLIENT_DEFAULT_MAX_RETRIES)
->end()
->end()
->end()
->end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

/**
* @phpstan-type SolrHttpClientConfigArray = array{timeout: int, max_retries: int}
*/
class EzSystemsEzPlatformSolrSearchEngineExtension extends Extension
{
/**
Expand Down Expand Up @@ -169,6 +172,10 @@ protected function processConnectionConfiguration(ContainerBuilder $container, a
// Factory for BoostFactorProvider uses mapping configured for the connection in use
$boostFactorProviderDef = $container->findDefinition(self::BOOST_FACTOR_PROVIDER_ID);
$boostFactorProviderDef->setFactory([new Reference('ezpublish.solr.boost_factor_provider_factory'), 'buildService']);

if (isset($config['http_client'])) {
$this->configureHttpClient($container, $config['http_client']);
}
}

/**
Expand Down Expand Up @@ -315,4 +322,16 @@ protected function buildBoostFactorMap(array $config)

return $boostFactorMap;
}

/**
* @phpstan-param SolrHttpClientConfigArray $httpClientConfig
*/
private function configureHttpClient(ContainerBuilder $container, array $httpClientConfig): void
{
$container->setParameter('ibexa.solr.http_client.timeout', $httpClientConfig['timeout']);
$container->setParameter(
'ibexa.solr.http_client.max_retries',
$httpClientConfig['max_retries']
);
}
}
11 changes: 7 additions & 4 deletions lib/Resources/config/container/solr/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ parameters:
ezpublish.search.solr.field_mapper.location.class: EzSystems\EzPlatformSolrSearchEngine\FieldMapper\LocationFieldMapper\Aggregate
ezpublish.search.solr.field_mapper.boost_factor_provider.class: EzSystems\EzPlatformSolrSearchEngine\FieldMapper\BoostFactorProvider
ezpublish.search.solr.field_mapper.boost_factor_provider.map: []
ezpublish.search.solr.field_mapper.indexing_depth_provider.class: \EzSystems\EzPlatformSolrSearchEngine\FieldMapper\IndexingDepthProvider
ezpublish.search.solr.field_mapper.indexing_depth_provider.class: EzSystems\EzPlatformSolrSearchEngine\FieldMapper\IndexingDepthProvider
ezpublish.search.solr.field_mapper.indexing_depth_provider.map: []
ezpublish.search.solr.field_mapper.indexing_depth_provider.default: 0
ibexa.solr.http_client.timeout: !php/const \EzSystems\EzPlatformSolrSearchEngineBundle\DependencyInjection\Configuration::SOLR_HTTP_CLIENT_DEFAULT_TIMEOUT
ibexa.solr.http_client.max_retries: !php/const \EzSystems\EzPlatformSolrSearchEngineBundle\DependencyInjection\Configuration::SOLR_HTTP_CLIENT_DEFAULT_MAX_RETRIES

services:
ibexa.solr.http_client.retryable:
Expand All @@ -21,24 +23,25 @@ services:
arguments:
$client: '@.inner'
$strategy: null
$maxRetries: 5
$maxRetries: '%ibexa.solr.http_client.max_retries%'
$logger: '@?logger'
tags:
- { name: monolog.logger, channel: solr }
- { name: monolog.logger, channel: ibexa.solr }

ibexa.solr.http_client:
class: Symfony\Contracts\HttpClient\HttpClientInterface
factory: [\Symfony\Component\HttpClient\HttpClient, 'create']
calls:
- setLogger: ['@logger']
tags:
- { name: monolog.logger, channel: solr }
- { name: monolog.logger, channel: ibexa.solr }

ezpublish.search.solr.gateway.client.http.stream:
class: '%ezpublish.search.solr.gateway.client.http.stream.class%'
autoconfigure: true
arguments:
$client: '@ibexa.solr.http_client'
$timeout: '%ibexa.solr.http_client.timeout%'

# Note: services tagged with 'ezpublish.search.solr.query.content.criterion_visitor'
# are registered to this one using compilation pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
*/
namespace EzSystems\EzPlatformSolrSearchEngineBundle\Tests\DependencyInjection;

use EzSystems\EzPlatformSolrSearchEngineBundle\DependencyInjection\Configuration;
use EzSystems\EzPlatformSolrSearchEngineBundle\DependencyInjection\EzSystemsEzPlatformSolrSearchEngineExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* @phpstan-import-type SolrHttpClientConfigArray from EzSystemsEzPlatformSolrSearchEngineExtension
*/
class EzPublishEzPlatformSolrSearchEngineExtensionTest extends AbstractExtensionTestCase
{
/**
Expand Down Expand Up @@ -650,4 +654,45 @@ public function testBoostFactorMap(array $configuration, array $map)
$map
);
}

/**
* @dataProvider getDataForTestHttpClientConfiguration
*
* @phpstan-param SolrHttpClientConfigArray $httpClientConfig
*/
public function testHttpClientConfiguration(array $config): void
{
$this->load(
[
'http_client' => $config,
]
);

$this->assertContainerBuilderHasParameter(
'ibexa.solr.http_client.timeout',
$config['timeout'],
);

$this->assertContainerBuilderHasParameter(
'ibexa.solr.http_client.max_retries',
$config['max_retries'],
);
}

public function getDataForTestHttpClientConfiguration(): iterable
{
yield 'default values' => [
[
'timeout' => Configuration::SOLR_HTTP_CLIENT_DEFAULT_TIMEOUT,
'max_retries' => Configuration::SOLR_HTTP_CLIENT_DEFAULT_MAX_RETRIES,
],
];

yield 'custom values' => [
[
'timeout' => 16,
'max_retries' => 2,
],
];
}
}

0 comments on commit 5c6878b

Please sign in to comment.