Skip to content

Commit 5def762

Browse files
authored
Fix for StickyRoundRobinSelector starts with second host (#1253)
* Improved the StickyRoundRobinSelector test * Fixed StickyRoundRobinSelector starts with second host
1 parent e1ed999 commit 5def762

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Connection implements ConnectionInterface
9999
/**
100100
* @var bool
101101
*/
102-
protected $isAlive = false;
102+
protected $isAlive = true;
103103

104104
/**
105105
* @var float

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818

1919
namespace Elasticsearch\Tests;
2020

21+
use CurlHandle;
2122
use Elasticsearch\Client;
2223
use Elasticsearch\ClientBuilder;
2324
use Elasticsearch\Common\Exceptions\ElasticsearchException;
2425
use Elasticsearch\Common\Exceptions\RuntimeException;
26+
use Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector;
27+
use Elasticsearch\ConnectionPool\StaticNoPingConnectionPool;
2528
use Elasticsearch\Tests\ClientBuilder\DummyLogger;
29+
use GuzzleHttp\Ring\Client\CurlHandler;
30+
use GuzzleHttp\Ring\Client\MockHandler;
2631
use PHPUnit\Framework\TestCase;
2732

2833
class ClientBuilderTest extends TestCase
@@ -376,4 +381,36 @@ public function testFromConfigWithIncludePortInHostHeader()
376381
$this->assertEquals($url, $request['request']['headers']['Host'][0]);
377382
}
378383
}
384+
385+
/**
386+
* @see https://github.com/elastic/elasticsearch-php/issues/1242
387+
*/
388+
public function testRandomizedHostsDisableWithStickRoundRobinSelectorSelectFirstNode()
389+
{
390+
$hosts = ['one', 'two', 'three'];
391+
$data = '{"foo":"bar}';
392+
393+
$handler = new MockHandler([
394+
'status' => 200,
395+
'transfer_stats' => [
396+
'total_time' => 100
397+
],
398+
'body' => fopen('data:text/plain,'.urlencode($data), 'rb'),
399+
'effective_url' => 'one'
400+
]);
401+
402+
$client = ClientBuilder::create()
403+
->setHosts($hosts)
404+
->setHandler($handler)
405+
->setSelector(StickyRoundRobinSelector::class)
406+
->setConnectionPool(StaticNoPingConnectionPool::class, ['randomizeHosts' => false])
407+
->build();
408+
409+
try {
410+
$result = $client->info();
411+
} catch (ElasticsearchException $e) {
412+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
413+
$this->assertEquals('one', $request['request']['headers']['Host'][0]);
414+
}
415+
}
379416
}

tests/Elasticsearch/Tests/ConnectionPool/Selectors/StickyRoundRobinSelectorTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@
2929
*/
3030
class StickyRoundRobinSelectorTest extends \PHPUnit\Framework\TestCase
3131
{
32+
public function setUp(): void
33+
{
34+
$this->roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
35+
}
36+
3237
public function tearDown(): void
3338
{
3439
m::close();
3540
}
3641

3742
public function testTenConnections()
3843
{
39-
$roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
40-
4144
$mockConnections = [];
4245
$mockConnections[] = m::mock(ConnectionInterface::class)
4346
->shouldReceive('isAlive')->times(16)->andReturn(true)->getMock();
@@ -47,16 +50,14 @@ public function testTenConnections()
4750
}
4851

4952
foreach (range(0, 15) as $index) {
50-
$retConnection = $roundRobin->select($mockConnections);
53+
$retConnection = $this->roundRobin->select($mockConnections);
5154

5255
$this->assertSame($mockConnections[0], $retConnection);
5356
}
5457
}
5558

5659
public function testTenConnectionsFirstDies()
5760
{
58-
$roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
59-
6061
$mockConnections = [];
6162
$mockConnections[] = m::mock(ConnectionInterface::class)
6263
->shouldReceive('isAlive')->once()->andReturn(false)->getMock();
@@ -69,7 +70,7 @@ public function testTenConnectionsFirstDies()
6970
}
7071

7172
foreach (range(0, 15) as $index) {
72-
$retConnection = $roundRobin->select($mockConnections);
73+
$retConnection = $this->roundRobin->select($mockConnections);
7374

7475
$this->assertSame($mockConnections[1], $retConnection);
7576
}

0 commit comments

Comments
 (0)