Skip to content

Commit

Permalink
Redo sniffing connection pool tests (#174)
Browse files Browse the repository at this point in the history
* chore: remove unnecessary implements entry

Signed-off-by: imdhemy <[email protected]>

* chore: remove redundant initializer

Signed-off-by: imdhemy <[email protected]>

* chore: declare return data type

Signed-off-by: imdhemy <[email protected]>

* fix(connection): fix sniffing interval

Signed-off-by: imdhemy <[email protected]>

* chore: code refactoring

Signed-off-by: imdhemy <[email protected]>

* chore(tests): unmark sniffing tests as skipped

Signed-off-by: imdhemy <[email protected]>

* chore: declare return data types

Signed-off-by: imdhemy <[email protected]>

* wip

Signed-off-by: imdhemy <[email protected]>

* fix: remove breaking change exception

Signed-off-by: imdhemy <[email protected]>

* chore: delete unused fixture

Signed-off-by: imdhemy <[email protected]>

* fix: redo sniffing connection pool tests

Signed-off-by: imdhemy <[email protected]>

* chore: add more tests

Signed-off-by: imdhemy <[email protected]>

---------

Signed-off-by: imdhemy <[email protected]>
  • Loading branch information
imdhemy authored Feb 29, 2024
1 parent c1fc911 commit bf9b981
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 354 deletions.
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
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

0 comments on commit bf9b981

Please sign in to comment.