Skip to content

Commit

Permalink
Apply RetryOnConflict on Bulk documents (#2184)
Browse files Browse the repository at this point in the history
This PR is created to solve the issue on the missing RetryOnConflict (retry_on_conflict) metadata attribute when adding documents in bulk (detailed at #2182)
  • Loading branch information
csabavirag authored Jan 18, 2024
1 parent df80484 commit d1b973f
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 @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Backward Compatibility Breaks
### Added
* If not expicitly set, use `retry_on_conflict` from Client configuration in Bulk updates [#2184](https://github.com/ruflin/Elastica/pull/2184)
### Changed
### Deprecated
### Removed
Expand Down
8 changes: 8 additions & 0 deletions src/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,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 @@ -155,6 +159,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 @@ -702,6 +702,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 d1b973f

Please sign in to comment.