Skip to content

Commit

Permalink
Apply RetryOnConflict on Bulk documents 8.x (#2187)
Browse files Browse the repository at this point in the history
This is a follow up PR to #2184 and replicates changes on 8.x branch.
  • Loading branch information
csabavirag authored Jan 22, 2024
1 parent 32a3010 commit b713330
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added missing `@throws` annotations to Client::request and related methods [#2152](https://github.com/ruflin/Elastica/pull/2152)
* Added ElasticSearch 8.x to CI [#2123](https://github.com/ruflin/Elastica/pull/2123)
* Added more versions of ElasticSearch to CI [#2155](https://github.com/ruflin/Elastica/pull/2155)
* If not expicitly set, use `retry_on_conflict` from Client configuration in Bulk updates (ported from 7.x [#2184](https://github.com/ruflin/Elastica/pull/2184))
### Changed
* Updated `php-cs-fixer` to `3.13.2` [#2143](https://github.com/ruflin/Elastica/pull/2143)
* Modernize code using `match` expression [#2141](https://github.com/ruflin/Elastica/pull/2141)
Expand Down
8 changes: 8 additions & 0 deletions src/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ 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);
}

$action = AbstractDocumentAction::create($document, $opType);

return $this->addAction($action);
Expand All @@ -157,6 +161,10 @@ 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);
}

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

return $this->addAction($action);
Expand Down
23 changes: 23 additions & 0 deletions tests/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,29 @@ public function testRetry(): void

$metadata = $actions[0]->getMetadata();
$this->assertEquals(5, $metadata['retry_on_conflict']);

// Test retry via client
$client->getConnection()->setParam('retryOnConflict', 5);
$doc2 = new Document('2', ['name' => 'Invisible Woman']);
$doc2->setOpType(Action::OP_TYPE_UPDATE);

$bulk = new Bulk($client);
$bulk->addDocument($doc2);

$actions = $bulk->getActions();

$metadata = $actions[0]->getMetadata();
$this->assertEquals(5, $metadata['retry_on_conflict']);

$script = new Script('');

$bulk = new Bulk($client);
$bulk->addScript($script);

$actions = $bulk->getActions();

$metadata = $actions[0]->getMetadata();
$this->assertEquals(5, $metadata['retry_on_conflict']);
}

/**
Expand Down

0 comments on commit b713330

Please sign in to comment.