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 8.x #2187

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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))
ruflin marked this conversation as resolved.
Show resolved Hide resolved
### 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
Loading