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

Redo sniffing connection pool tests #174

Merged
48 changes: 25 additions & 23 deletions src/OpenSearch/ConnectionPool/SniffingConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,30 @@
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
use OpenSearch\ConnectionPool\Selectors\SelectorInterface;
use OpenSearch\Connections\Connection;
use OpenSearch\Connections\ConnectionInterface;
use OpenSearch\Connections\ConnectionFactoryInterface;
use OpenSearch\Connections\ConnectionInterface;

class SniffingConnectionPool extends AbstractConnectionPool implements ConnectionPoolInterface
shyim marked this conversation as resolved.
Show resolved Hide resolved
class SniffingConnectionPool extends AbstractConnectionPool
{
/**
* @var int
*/
private $sniffingInterval = 300;
private $sniffingInterval;

/**
* @var int
*/
private $nextSniff = -1;
private $nextSniff;

/**
* {@inheritdoc}
*/
public function __construct($connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, $connectionPoolParams)
{
public function __construct(
$connections,
SelectorInterface $selector,
ConnectionFactoryInterface $factory,
$connectionPoolParams
) {
parent::__construct($connections, $selector, $factory, $connectionPoolParams);

$this->setConnectionPoolParams($connectionPoolParams);
Expand Down Expand Up @@ -78,9 +82,9 @@ public function scheduleCheck(): void
$this->nextSniff = -1;
}

private function sniff(bool $force = false)
private function sniff(bool $force = false): void
{
if ($force === false && $this->nextSniff >= time()) {
if ($force === false && $this->nextSniff > time()) {
return;
}

Expand Down Expand Up @@ -123,19 +127,19 @@ private function sniffConnection(Connection $connection): bool
return false;
}

$nodes = $this->parseClusterState($connection->getTransportSchema(), $response);
$nodes = $this->parseClusterState($response);

if (count($nodes) === 0) {
return false;
}

$this->connections = array();
$this->connections = [];

foreach ($nodes as $node) {
$nodeDetails = array(
$nodeDetails = [
'host' => $node['host'],
'port' => $node['port']
);
'port' => $node['port'],
];
$this->connections[] = $this->connectionFactory->create($nodeDetails);
}

Expand All @@ -144,29 +148,27 @@ private function sniffConnection(Connection $connection): bool
return true;
}

private function parseClusterState(string $transportSchema, $nodeInfo): array
private function parseClusterState($nodeInfo): array
{
$pattern = '/([^:]*):([0-9]+)/';
$hosts = [];
$pattern = '/([^:]*):(\d+)/';
$hosts = [];

foreach ($nodeInfo['nodes'] as $node) {
if (isset($node['http']) === true && isset($node['http']['publish_address']) === true) {
if (preg_match($pattern, $node['http']['publish_address'], $match) === 1) {
$hosts[] = array(
$hosts[] = [
'host' => $match[1],
'port' => (int) $match[2],
);
'port' => (int)$match[2],
];
}
}
}

return $hosts;
}

private function setConnectionPoolParams(array $connectionPoolParams)
private function setConnectionPoolParams(array $connectionPoolParams): void
{
if (isset($connectionPoolParams['sniffingInterval']) === true) {
$this->sniffingInterval = $connectionPoolParams['sniffingInterval'];
}
$this->sniffingInterval = (int)($connectionPoolParams['sniffingInterval'] ?? 300);
}
}
14 changes: 6 additions & 8 deletions tests/ConnectionPool/SniffingConnectionPoolIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@

use OpenSearch\ClientBuilder;
use OpenSearch\ConnectionPool\SniffingConnectionPool;
use OpenSearch\ConnectionPool\StaticConnectionPool;
use OpenSearch\Tests\Utility;
use PHPUnit\Framework\TestCase;

/**
* Class SniffingConnectionPoolIntegrationTest
*
* @subpackage Tests/SniffingConnectionPoolTest
* @group Integration
*/
class SniffingConnectionPoolIntegrationTest extends \PHPUnit\Framework\TestCase
class SniffingConnectionPoolIntegrationTest extends TestCase
{
protected function setUp(): void
{
static::markTestSkipped("All of Sniffing unit tests use outdated cluster state format, need to redo");
}

public function testSniff()
public function testSniff(): void
{
$client = ClientBuilder::create()
->setHosts([Utility::getHost()])
->setConnectionPool(SniffingConnectionPool::class, ['sniffingInterval' => -10])
->setConnectionPool(SniffingConnectionPool::class, ['sniffingInterval' => 10])
->setSSLVerification(false)
->build();

$pinged = $client->ping();
Expand Down
Loading
Loading