Skip to content

Commit

Permalink
Re-work connections and connection pool in favor of Node Pool (#2188)
Browse files Browse the repository at this point in the history
This PR re-works Connection Pool, Connections and Strategies in favor of [Node Pool](https://github.com/elastic/elastic-transport-php#node-pool) from the official client.

As this package is built on top of official client we have to use Node Pool instead of our custom connections mechanism. What makes the whole package a bit simplier to maintain. In the future this could be re-worked again if needed. We would liek to release new version of this package as fast as possible.


### ConnectionPool is replaced with Node Pool

More details can be found via link: https://www.elastic.co/guide/en/elasticsearch/client/php-api/8.12/node_pool.html.

By default Official elasticsearch client builds Simple Node Pool with `RoundRobin` selector and `NoResurrect` resurrect. 

To override default Node Pool behavior do the following:
```php
use Elastic\Transport\NodePool\Resurrect\ElasticsearchResurrect;
use Elastic\Transport\NodePool\Selector\RoundRobin;
use Elastic\Transport\NodePool\SimpleNodePool;

$nodePool = new SimpleNodePool(
    new RoundRobin(),
    new ElasticsearchResurrect()
);

new Client([
    'hosts' => [
        'https://node1.com:9200',
    ],
    'transport_config' => [
        'node_pool' => $nodePool,
    ],
]);
```


### ClientConfiguration changes

`ClientConfiguration` class is a bit reworked and the following parameters have been removed:
- `port`
- `path`
- `url`
- `connections` in favor of `hosts`
- `servers` in favor of `hosts`
- `roundRobin`

`host` parameter has been renamed to `hosts` and it's should be an array of strings.

`elastic/transport` is updated to 8.8 as since that version each host could be configured with its own username/password credentials.

How to configure authentication per node.
```php
new Client([
    'hosts' => [
        'https://username1:[email protected]:9200',
        'https://username2:[email protected]:9200',
    ],
]);
```

### No longer needed dependecies

- `symfony/deprecation-contracts`
- `guzzlehttp/psr7`
- `nyholm/dsn`
  • Loading branch information
sidz authored Mar 18, 2024
1 parent bb817e0 commit b04f280
Show file tree
Hide file tree
Showing 26 changed files with 238 additions and 2,175 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `Elastica\Search::setOptionsAndQuery()`
* `Elastica\Index::search()`
* `Elastica\Index::createSearch()`
* Removed classes [#2188](https://github.com/ruflin/Elastica/pull/2188)
* `Elastica\Connection`
* `Elastica\Connection\ConnectionPool`
* `Elastica\Connection\Strategy\CallbackStrategy`
* `Elastica\Connection\Strategy\RoundRobin`
* `Elastica\Connection\Strategy\Simple`
* `Elastica\Connection\Strategy\StrategyFactory`
* `Elastica\Connection\Strategy\StrategyInterface`
* Removed `Elastica\Client::setLogger()` method [#2148](https://github.com/ruflin/Elastica/pull/2148)



Expand All @@ -84,6 +93,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
* Removed the JSONParseException class, which is replaced by \JsonException
* Removed the JSONParseException test class
* Removed `nyholm/dsn` as no longer needed.
* Removed `symfony/deprecation-contracts` as no longer needed.
### Fixed
* Fix types order in `Elastica\Query` to work with psalm & expand the `aggs` type to include raw arrays

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0",
"ext-json": "*",
"elastic/transport": "^8.4",
"elastic/transport": "^8.8",
"elasticsearch/elasticsearch": "^8.4.1",
"guzzlehttp/psr7": "^2.0",
"nyholm/dsn": "^2.0.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"symfony/deprecation-contracts": "^3.0"
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.2",
Expand All @@ -31,9 +28,11 @@
"phpunit/phpunit": "^9.5",
"symfony/phpunit-bridge": "^6.0"
},
"conflict": {
"guzzlehttp/psr7": "<2.0.0"
},
"suggest": {
"aws/aws-sdk-php": "Allow using IAM authentication with Amazon ElasticSearch Service",
"guzzlehttp/guzzle": "Allow using guzzle as transport",
"monolog/monolog": "Logging request"
},
"autoload": {
Expand All @@ -49,11 +48,12 @@
"config": {
"allow-plugins": {
"php-http/discovery": true
}
},
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "7.0.x-dev"
"dev-master": "8.0.x-dev"
}
}
}
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ parameters:
count: 1
path: tests/ClientFunctionalTest.php

-
message: "#^Parameter \\#1 \\$params of static method Elastica\\\\Connection\\:\\:create\\(\\) expects array\\|Elastica\\\\Connection, string given\\.$#"
count: 1
path: tests/ConnectionTest.php

-
message: "#^Access to an undefined property Elastica\\\\Document\\:\\:\\$field1\\.$#"
count: 1
Expand Down
16 changes: 12 additions & 4 deletions src/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ public function getActions(): array
*/
public function addDocument(Document $document, ?string $opType = null): self
{
if (!$document->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) {
$document->setRetryOnConflict($retry);
if (!$document->hasRetryOnConflict()) {
$retry = $this->_client->getConfigValue('retryOnConflict', 0);

if ($retry > 0) {
$document->setRetryOnConflict($retry);
}
}

$action = AbstractDocumentAction::create($document, $opType);
Expand All @@ -161,8 +165,12 @@ public function addDocuments(array $documents, ?string $opType = null): self
*/
public function addScript(AbstractScript $script, ?string $opType = null): self
{
if (!$script->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) {
$script->setRetryOnConflict($retry);
if (!$script->hasRetryOnConflict()) {
$retry = $this->_client->getConfigValue('retryOnConflict', 0);

if ($retry > 0) {
$script->setRetryOnConflict($retry);
}
}

$action = AbstractDocumentAction::create($script, $opType);
Expand Down
Loading

0 comments on commit b04f280

Please sign in to comment.