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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client connection in branch 2.5.x (#763 and #738). #769

Merged
merged 1 commit into from
Feb 20, 2018
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 @@ -71,4 +71,11 @@ public function getHttpAuthUser();
* @return string
*/
public function getHttpAuthPassword();

/**
* Client config options.
*
* @return array
*/
public function getOptions();
}
80 changes: 6 additions & 74 deletions src/module-elasticsuite-core/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

namespace Smile\ElasticsuiteCore\Client;

use Smile\ElasticsuiteCore\Api\Client\ClientInterface;
use Smile\ElasticsuiteCore\Api\Client\ClientConfigurationInterface;
use Elasticsearch\ClientBuilder;
use Psr\Log\LoggerInterface;
use Smile\ElasticsuiteCore\Api\Client\ClientInterface;

/**
* ElasticSearch client implementation.
Expand All @@ -38,19 +36,12 @@ class Client implements ClientInterface
/**
* Constructor.
*
* @param ClientConfigurationInterfaceFactory $clientConfigurationFactory Client configuration factory.
* @param ClientBuilder $clientBuilder ES client builder.
* @param LoggerInterface $logger Logger.
* @param array $options Client options.
* @param ClientConfigurationInterface $clientConfiguration Client configuration factory.
* @param ClientBuilder $clientBuilder ES client builder.
*/
public function __construct(
\Smile\ElasticsuiteCore\Client\ClientConfigurationFactory $clientConfigurationFactory,
ClientBuilder $clientBuilder,
LoggerInterface $logger,
$options = []
) {
$clientConfiguration = $clientConfigurationFactory->create(['options' => $options]);
$this->esClient = $this->createClient($clientConfiguration, $clientBuilder, $logger);
public function __construct(ClientConfigurationInterface $clientConfiguration, ClientBuilder $clientBuilder)
{
$this->esClient = $clientBuilder->build($clientConfiguration->getOptions());
}

/**
Expand Down Expand Up @@ -187,63 +178,4 @@ public function termvectors($params)
{
return $this->esClient->termvectors($params);
}

/**
* Create an ES Client form configuration.
*
* @param ClientConfigurationInterface $clientConfiguration Client configuration.
* @param ClientBuilder $clientBuilder ES client builder.
* @param LoggerInterface $logger Logger
*
* @return \Elasticsearch\Client
*/
private function createClient(
ClientConfigurationInterface $clientConfiguration,
ClientBuilder $clientBuilder,
LoggerInterface $logger
) {
$hosts = $this->getHosts($clientConfiguration);

if (!empty($hosts)) {
$clientBuilder->setHosts($hosts);
}

if ($clientConfiguration->isDebugModeEnabled()) {
$clientBuilder->setLogger($logger);
}

return $clientBuilder->build();
}

/**
* Return hosts config used to connect to the cluster.
*
* @param ClientConfigurationInterface $clientConfiguration Client configuration.
*
* @return array
*/
private function getHosts(ClientConfigurationInterface $clientConfiguration)
{
$hosts = [];

foreach ($clientConfiguration->getServerList() as $host) {
if (!empty($host)) {
list($hostname, $port) = array_pad(explode(':', $host, 2), 2, 9200);
$currentHostConfig = [
'host' => $hostname,
'port' => $port,
'scheme' => $clientConfiguration->getScheme(),
];

if ($clientConfiguration->isHttpAuthEnabled()) {
$currentHostConfig['user'] = $clientConfiguration->getHttpAuthUser();
$currentHostConfig['pass'] = $clientConfiguration->getHttpAuthPassword();
}

$hosts[] = $currentHostConfig;
}
}

return $hosts;
}
}
120 changes: 120 additions & 0 deletions src/module-elasticsuite-core/Client/ClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Client;

/**
* ElasticSearch client builder.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
*/
class ClientBuilder
{
/**
* @var \Elasticsearch\ClientBuilder
*/
private $clientBuilder;

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* @var array
*/
private $defaultOptions = [
'servers' => 'localhost:9200',
'enable_http_auth' => false,
'http_auth_user' => null,
'http_auth_pwd' => null,
'is_debug_mode_enabled' => false,
];

/**
* Constructor.
*
* @param \Elasticsearch\ClientBuilder $clientBuilder Client builder.
* @param \Psr\Log\LoggerInterface $logger Logger.
*/
public function __construct(\Elasticsearch\ClientBuilder $clientBuilder, \Psr\Log\LoggerInterface $logger)
{
$this->clientBuilder = $clientBuilder;
$this->logger = $logger;
}

/**
* Build an ES client from options.
*
* @param array $options Client options. See self::defaultOptions for available options.
*
* @return \Elasticsearch\Client
*/
public function build($options = [])
{
$options = array_merge($this->defaultOptions, $options);

$clientBuilder = $this->clientBuilder->create();

$hosts = $this->getHosts($options);

if (!empty($hosts)) {
$clientBuilder->setHosts($hosts);
}

if ($options['is_debug_mode_enabled']) {
$clientBuilder->setLogger($this->logger);
}

return $clientBuilder->build();
}

/**
* Return hosts config used to connect to the cluster.
*
* @param array $options Client options. See self::defaultOptions for available options.
*
* @return array
*/
private function getHosts($options)
{
$hosts = [];

if (is_string($options['servers'])) {
$options['servers'] = explode(',', $options['servers']);
}

foreach ($options['servers'] as $host) {
if (!empty($host)) {
list($hostname, $port) = array_pad(explode(':', trim($host), 2), 2, 9200);
$currentHostConfig = [
'host' => $hostname,
'port' => $port,
'scheme' => isset($options['enable_https_mode']) ? 'https' : $options['scheme'] ?? 'http',
];

if ($options['enable_http_auth']) {
$currentHostConfig['user'] = $options['http_auth_user'];
$currentHostConfig['pass'] = $options['http_auth_pwd'];
}

$hosts[] = $currentHostConfig;
}
}

return $hosts;
}
}
32 changes: 20 additions & 12 deletions src/module-elasticsuite-core/Client/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class ClientConfiguration implements ClientConfigurationInterface
*/
const ES_CLIENT_CONFIG_XML_PREFIX = 'smile_elasticsuite_core_base_settings/es_client';

/**
* @var array
*/
private $options;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
Expand All @@ -45,14 +40,10 @@ class ClientConfiguration implements ClientConfigurationInterface
/**
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig Config.
* @param array $options Custom options.
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
$options = []
) {
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
$this->options = $options;
}

/**
Expand Down Expand Up @@ -113,6 +104,23 @@ public function getHttpAuthPassword()
return (string) $this->getElasticsearchClientConfigParam('http_auth_pwd');
}

/**
* {@inheritDoc}
*/
public function getOptions()
{
$options = [
'servers' => $this->getServerList(),
'scheme' => $this->getScheme(),
'enable_http_auth' => $this->isHttpAuthEnabled(),
'http_auth_user' => $this->getHttpAuthUser(),
'http_auth_pwd' => $this->getHttpAuthPassword(),
'is_debug_mode_enabled' => $this->isDebugModeEnabled(),
];

return $options;
}

/**
* Read config under the path smile_elasticsuite_core_base_settings/es_client.
*
Expand All @@ -124,6 +132,6 @@ private function getElasticsearchClientConfigParam($configField)
{
$path = self::ES_CLIENT_CONFIG_XML_PREFIX . '/' . $configField;

return $this->options[$configField] ?? $this->scopeConfig->getValue($path);
return $this->scopeConfig->getValue($path);
}
}
15 changes: 8 additions & 7 deletions src/module-elasticsuite-core/Setup/ConfigOptionsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ class ConfigOptionsList implements ConfigOptionsListInterface
const CONFIG_PATH_ES_PASS = self::CONF_PREFIX . '/http_auth_pwd';

/**
* @var ClientFactoryInterface
* @var \Smile\ElasticsuiteCore\Client\ClientBuilder
*/
private $clientFactory;
private $clientBuilder;

/**
* Constructor.
*
* @param \Smile\ElasticsuiteCore\Client\ClientFactory $clientFactory ES Client factory.
* @param \Smile\ElasticsuiteCore\Client\ClientBuilder $clientBuilder ES client builder.
*/
public function __construct(\Smile\ElasticsuiteCore\Client\ClientFactory $clientFactory)
public function __construct(\Smile\ElasticsuiteCore\Client\ClientBuilder $clientBuilder)
{
$this->clientFactory = $clientFactory;
$this->clientBuilder = $clientBuilder;
}

/**
Expand Down Expand Up @@ -119,8 +119,8 @@ public function validate(array $options, DeploymentConfig $deploymentConfig)
$errors = [];

try {
$clientsOptions = array_filter($this->getClientOptions($options, $deploymentConfig));
$this->clientFactory->create(['options' => $clientsOptions])->info();
$options = array_filter($this->getClientOptions($options, $deploymentConfig));
$this->clientBuilder->build($options)->info();
} catch (\Exception $e) {
$errors[] = "Unable to connect ElasticSearch server : {$e->getMessage()}";
}
Expand All @@ -129,6 +129,7 @@ public function validate(array $options, DeploymentConfig $deploymentConfig)
}

/**
* Read client options from CLI / env file.
*
* @param array $options Input options.
* @param DeploymentConfig $deploymentConfig Deployment config.
Expand Down