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

Transport.php fix backported into 6.8.x #1109

Merged
merged 5 commits into from
Mar 22, 2021
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
11 changes: 11 additions & 0 deletions src/Elasticsearch/Namespaces/IndicesNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ public function getAlias(array $params = [])

return $this->performRequest($endpoint);
}

/**
* Proxy to getAlias()
*
* @see https://github.com/elastic/elasticsearch-php/issues/1112
*/
public function getAliases(array $params = [])
{
return $this->getAlias($params);
}

/**
* $params['index'] = (list) A comma-separated list of index names
* $params['type'] = (list) A comma-separated list of document types
Expand Down
3 changes: 2 additions & 1 deletion src/Elasticsearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ function ($response) {
},
//onFailure
function ($response) {
$code = $response->getCode();
// Ignore 400 level errors, as that means the server responded just fine
if (!(isset($response['code']) && $response['code'] >=400 && $response['code'] < 500)) {
if ($code < 400 || $code >= 500) {
// Otherwise schedule a check
$this->connectionPool->scheduleCheck();
}
Expand Down
81 changes: 42 additions & 39 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,50 @@
* @see https://github.com/elastic/elasticsearch-php/issues/967
*/

$classesToAlias = [
'\Elasticsearch\Endpoints\Nodes\HotThreads' => '\Elasticsearch\Endpoints\Cluster\Nodes\HotThreads',
'\Elasticsearch\Endpoints\Nodes\Info' => '\Elasticsearch\Endpoints\Cluster\Nodes\Info',
'\Elasticsearch\Endpoints\Nodes\ReloadSecureSettings' => '\Elasticsearch\Endpoints\Cluster\Nodes\ReloadSecureSettings',
'\Elasticsearch\Endpoints\Nodes\Stats' => '\Elasticsearch\Endpoints\Cluster\Nodes\Stats',
'\Elasticsearch\Endpoints\Cluster\GetSettings' => '\Elasticsearch\Endpoints\Cluster\Settings\Get',
'\Elasticsearch\Endpoints\Cluster\PutSettings' => '\Elasticsearch\Endpoints\Cluster\Settings\Put',
'\Elasticsearch\Endpoints\Indices\DeleteAlias' => '\Elasticsearch\Endpoints\Indices\Alias\Delete',
'\Elasticsearch\Endpoints\Indices\ExistsAlias' => '\Elasticsearch\Endpoints\Indices\Alias\Exists',
'\Elasticsearch\Endpoints\Indices\GetAlias' => '\Elasticsearch\Endpoints\Indices\Alias\Get',
'\Elasticsearch\Endpoints\Indices\PutAlias' => '\Elasticsearch\Endpoints\Indices\Alias\Put',
'\Elasticsearch\Endpoints\Indices\ClearCache' => '\Elasticsearch\Endpoints\Indices\Cache\Clear',
'\Elasticsearch\Endpoints\Indices\GetMapping' => '\Elasticsearch\Endpoints\Indices\Mapping\Get',
'\Elasticsearch\Endpoints\Indices\GetFieldMapping' => '\Elasticsearch\Endpoints\Indices\Mapping\GetField',
'\Elasticsearch\Endpoints\Indices\PutMapping' => '\Elasticsearch\Endpoints\Indices\Mapping\Put',
'\Elasticsearch\Endpoints\Indices\GetSettings' => '\Elasticsearch\Endpoints\Indices\Settings\Get',
'\Elasticsearch\Endpoints\Indices\PutSettings' => '\Elasticsearch\Endpoints\Indices\Settings\Put',
'\Elasticsearch\Endpoints\Indices\GetTemplate' => '\Elasticsearch\Endpoints\Indices\Template\Get',
'\Elasticsearch\Endpoints\Indices\PutTemplate' => '\Elasticsearch\Endpoints\Indices\Template\Put',
'\Elasticsearch\Endpoints\Indices\ExistsTemplate' => '\Elasticsearch\Endpoints\Indices\Template\Exists',
'\Elasticsearch\Endpoints\Indices\DeleteTemplate' => '\Elasticsearch\Endpoints\Indices\Template\Delete',
'\Elasticsearch\Endpoints\Indices\ExistsType' => '\Elasticsearch\Endpoints\Indices\Type\Exists',
'\Elasticsearch\Endpoints\Indices\GetUpgrade' => '\Elasticsearch\Endpoints\Indices\Upgrade\Get',
'\Elasticsearch\Endpoints\Indices\Upgrade' => '\Elasticsearch\Endpoints\Indices\Upgrade\Post',
'\Elasticsearch\Endpoints\Indices\ValidateQuery' => '\Elasticsearch\Endpoints\Indices\Validate\Query',
'\Elasticsearch\Endpoints\Ingest\DeletePipeline' => '\Elasticsearch\Endpoints\Ingest\Pipeline\Delete',
'\Elasticsearch\Endpoints\Ingest\GetPipeline' => '\Elasticsearch\Endpoints\Ingest\Pipeline\Get',
'\Elasticsearch\Endpoints\Ingest\PutPipeline' => '\Elasticsearch\Endpoints\Ingest\Pipeline\Put',
'\Elasticsearch\Endpoints\Ingest\ProcessorGrok' => '\Elasticsearch\Endpoints\Ingest\Pipeline\ProcessorGrok',
'\Elasticsearch\Endpoints\GetScript' => '\Elasticsearch\Endpoints\Script\Get',
'\Elasticsearch\Endpoints\PutScript' => '\Elasticsearch\Endpoints\Script\Put',
'\Elasticsearch\Endpoints\DeleteScript' => '\Elasticsearch\Endpoints\Script\Delete',
'\Elasticsearch\Endpoints\Snapshot\CreateRepository' => '\Elasticsearch\Endpoints\Snapshot\Repository\Create',
'\Elasticsearch\Endpoints\Snapshot\DeleteRepository' => '\Elasticsearch\Endpoints\Snapshot\Repository\Delete',
'\Elasticsearch\Endpoints\Snapshot\GetRepository' => '\Elasticsearch\Endpoints\Snapshot\Repository\Get',
'\Elasticsearch\Endpoints\Snapshot\VerifyRepository' => '\Elasticsearch\Endpoints\Snapshot\Repository\Verify',
'\Elasticsearch\Endpoints\GetSource' => '\Elasticsearch\Endpoints\Source\Get',
'\Elasticsearch\Endpoints\Tasks\ListTasks' => '\Elasticsearch\Endpoints\Tasks\TasksList'
$aliasToClass = [
'\Elasticsearch\Endpoints\Cluster\Nodes\HotThreads' => '\Elasticsearch\Endpoints\Nodes\HotThreads',
'\Elasticsearch\Endpoints\Cluster\Nodes\Info' => '\Elasticsearch\Endpoints\Nodes\Info' ,
'\Elasticsearch\Endpoints\Cluster\Nodes\ReloadSecureSettings' => '\Elasticsearch\Endpoints\Nodes\ReloadSecureSettings',
'\Elasticsearch\Endpoints\Cluster\Nodes\Stats' => '\Elasticsearch\Endpoints\Nodes\Stats',
'\Elasticsearch\Endpoints\Cluster\Settings\Get' => '\Elasticsearch\Endpoints\Cluster\GetSettings',
'\Elasticsearch\Endpoints\Cluster\Settings\Put' => '\Elasticsearch\Endpoints\Cluster\PutSettings',
'\Elasticsearch\Endpoints\Indices\Alias\Delete' => '\Elasticsearch\Endpoints\Indices\DeleteAlias',
'\Elasticsearch\Endpoints\Indices\Alias\Exists' => '\Elasticsearch\Endpoints\Indices\ExistsAlias',
'\Elasticsearch\Endpoints\Indices\Alias\Get' => '\Elasticsearch\Endpoints\Indices\GetAlias',
'\Elasticsearch\Endpoints\Indices\Alias\Put' => '\Elasticsearch\Endpoints\Indices\PutAlias',
'\Elasticsearch\Endpoints\Indices\Aliases\Update' => '\Elasticsearch\Endpoints\Indices\UpdateAliases',
'\Elasticsearch\Endpoints\Indices\Cache\Clear' => '\Elasticsearch\Endpoints\Indices\ClearCache',
'\Elasticsearch\Endpoints\Indices\Exists\Types' => '\Elasticsearch\Endpoints\Indices\ExistsType',
'\Elasticsearch\Endpoints\Indices\Type\Exists' => '\Elasticsearch\Endpoints\Indices\ExistsType',
'\Elasticsearch\Endpoints\Indices\Field\Get' => '\Elasticsearch\Endpoints\Indices\GetFieldMapping',
'\Elasticsearch\Endpoints\Indices\Mapping\GetField' => '\Elasticsearch\Endpoints\Indices\GetFieldMapping',
'\Elasticsearch\Endpoints\Indices\Mapping\Get' => '\Elasticsearch\Endpoints\Indices\GetMapping',
'\Elasticsearch\Endpoints\Indices\Mapping\Put' => '\Elasticsearch\Endpoints\Indices\PutMapping',
'\Elasticsearch\Endpoints\Indices\Settings\Get' => '\Elasticsearch\Endpoints\Indices\GetSettings',
'\Elasticsearch\Endpoints\Indices\Settings\Put' => '\Elasticsearch\Endpoints\Indices\PutSettings',
'\Elasticsearch\Endpoints\Indices\Template\Get' => '\Elasticsearch\Endpoints\Indices\GetTemplate',
'\Elasticsearch\Endpoints\Indices\Template\Put' => '\Elasticsearch\Endpoints\Indices\PutTemplate',
'\Elasticsearch\Endpoints\Indices\Template\Exists' => '\Elasticsearch\Endpoints\Indices\ExistsTemplate',
'\Elasticsearch\Endpoints\Indices\Template\Delete' => '\Elasticsearch\Endpoints\Indices\DeleteTemplate',
'\Elasticsearch\Endpoints\Indices\Upgrade\Get' => '\Elasticsearch\Endpoints\Indices\GetUpgrade',
'\Elasticsearch\Endpoints\Indices\Upgrade\Post' => '\Elasticsearch\Endpoints\Indices\Upgrade',
'\Elasticsearch\Endpoints\Indices\Validate\Query' => '\Elasticsearch\Endpoints\Indices\ValidateQuery',
'\Elasticsearch\Endpoints\Ingest\Pipeline\Delete' => '\Elasticsearch\Endpoints\Ingest\DeletePipeline',
'\Elasticsearch\Endpoints\Ingest\Pipeline\Get' => '\Elasticsearch\Endpoints\Ingest\GetPipeline',
'\Elasticsearch\Endpoints\Ingest\Pipeline\Put' => '\Elasticsearch\Endpoints\Ingest\PutPipeline',
'\Elasticsearch\Endpoints\Ingest\Pipeline\ProcessorGrok' => '\Elasticsearch\Endpoints\Ingest\ProcessorGrok',
'\Elasticsearch\Endpoints\Script\Get' => '\Elasticsearch\Endpoints\GetScript',
'\Elasticsearch\Endpoints\Script\Put' => '\Elasticsearch\Endpoints\PutScript',
'\Elasticsearch\Endpoints\Script\Delete' => '\Elasticsearch\Endpoints\DeleteScript',
'\Elasticsearch\Endpoints\Snapshot\Repository\Create' => '\Elasticsearch\Endpoints\Snapshot\CreateRepository',
'\Elasticsearch\Endpoints\Snapshot\Repository\Delete' => '\Elasticsearch\Endpoints\Snapshot\DeleteRepository',
'\Elasticsearch\Endpoints\Snapshot\Repository\Get' => '\Elasticsearch\Endpoints\Snapshot\GetRepository',
'\Elasticsearch\Endpoints\Snapshot\Repository\Verify' => '\Elasticsearch\Endpoints\Snapshot\VerifyRepository',
'\Elasticsearch\Endpoints\Source\Get' => '\Elasticsearch\Endpoints\GetSource',
'\Elasticsearch\Endpoints\Tasks\TasksList' => '\Elasticsearch\Endpoints\Tasks\ListTasks'
];

foreach ($classesToAlias as $original => $alias) {
foreach ($aliasToClass as $alias => $original) {
if (!class_exists($alias, false)) {
class_alias($original, $alias);
}
Expand Down
15 changes: 13 additions & 2 deletions tests/Elasticsearch/Tests/BackwardCompatibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Elasticsearch\Tests;

use Elasticsearch\Namespaces\IndicesNamespace;

/**
* Class BackwardCompatibleTest
*
Expand All @@ -30,14 +32,12 @@ class BackwardCompatibleTest extends \PHPUnit\Framework\TestCase
public function getClasses()
{
return [
//['Elasticsearch\Endpoints\TermVectors'],
['Elasticsearch\Endpoints\Delete'],
['Elasticsearch\Endpoints\ScriptsPainlessExecute'],
['Elasticsearch\Endpoints\Create'],
['Elasticsearch\Endpoints\Exists'],
['Elasticsearch\Endpoints\Get'],
['Elasticsearch\Endpoints\Explain'],
//['Elasticsearch\Endpoints\MTermVectors'],
['Elasticsearch\Endpoints\Search'],
['Elasticsearch\Endpoints\FieldCaps'],
['Elasticsearch\Endpoints\Msearch'],
Expand Down Expand Up @@ -90,6 +90,9 @@ public function getClasses()
['Elasticsearch\Endpoints\Indices\Alias\Exists'],
['Elasticsearch\Endpoints\Indices\Alias\Get'],
['Elasticsearch\Endpoints\Indices\Alias\Put'],
['Elasticsearch\Endpoints\Indices\Aliases\Update'],
['Elasticsearch\Endpoints\Indices\Exists\Types'],
['Elasticsearch\Endpoints\Indices\Field\Get'],
['Elasticsearch\Endpoints\Indices\Settings\Get'],
['Elasticsearch\Endpoints\Indices\Settings\Put'],
['Elasticsearch\Endpoints\Indices\Upgrade\Get'],
Expand Down Expand Up @@ -155,4 +158,12 @@ public function testOldClassNamespacesPreviousTo67($class)
{
$this->assertTrue(class_exists($class, true), sprintf("Class %s does not exist", $class));
}

/**
* @see https://github.com/elastic/elasticsearch-php/issues/1112
*/
public function testGetAliasesExistsInIndicesNamespace()
{
$this->assertTrue(method_exists(IndicesNamespace::class, 'getAliases'));
}
}
84 changes: 84 additions & 0 deletions tests/Elasticsearch/Tests/TransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Elasticsearch PHP client
*
* @link https://github.com/elastic/elasticsearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/


declare(strict_types = 1);

namespace Elasticsearch\Tests;

use Elasticsearch\Common\Exceptions\ServerErrorResponseException;
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
use Elasticsearch\Connections\Connection;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\Transport;
use GuzzleHttp\Ring\Future\FutureArray;
use GuzzleHttp\Ring\Future\FutureArrayInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use React\Promise\Deferred;

class TransportTest extends TestCase
{
public function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->trace = $this->createMock(LoggerInterface::class);
$this->serializer = $this->createMock(SerializerInterface::class);
$this->connectionPool = $this->createMock(AbstractConnectionPool::class);
$this->connection = $this->createMock(Connection::class);
}

public function testPerformRequestWithServerErrorResponseException404Result()
{
$deferred = new Deferred();
$deferred->reject(new ServerErrorResponseException('foo', 404));
$future = new FutureArray($deferred->promise());

$this->connection->method('performRequest')
->willReturn($future);

$this->connectionPool->method('nextConnection')
->willReturn($this->connection);

$this->connectionPool->expects($this->never())
->method('scheduleCheck');

$transport = new Transport(1, $this->connectionPool, $this->logger);

$result = $transport->performRequest('GET', '/');
$this->assertInstanceOf(FutureArrayInterface::class, $result);
}

public function testPerformRequestWithServerErrorResponseException500Result()
{
$deferred = new Deferred();
$deferred->reject(new ServerErrorResponseException('foo', 500));
$future = new FutureArray($deferred->promise());

$this->connection->method('performRequest')
->willReturn($future);

$this->connectionPool->method('nextConnection')
->willReturn($this->connection);

$this->connectionPool->expects($this->once())
->method('scheduleCheck');

$transport = new Transport(1, $this->connectionPool, $this->logger);

$result = $transport->performRequest('GET', '/');
$this->assertInstanceOf(FutureArrayInterface::class, $result);
}
}