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

Apply RetryOnConflict on Bulk documents #2184

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put this under "fixed"? sounds more like a bug fix but also happy to keep it under Added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First I added to that section. Then I moved to Added. TBH, I would leave it up to you.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep it, I can always clean it up later.

### 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() && ($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() && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) {
$script->setRetryOnConflict($retry);
}

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

return $this->addAction($action);
Expand Down
24 changes: 24 additions & 0 deletions tests/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,30 @@ 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
Loading