From b713330c570a357ec1b95aedfb059c52b97df9ff Mon Sep 17 00:00:00 2001 From: csabavirag Date: Mon, 22 Jan 2024 08:54:35 +0100 Subject: [PATCH] Apply RetryOnConflict on Bulk documents 8.x (#2187) This is a follow up PR to #2184 and replicates changes on 8.x branch. --- CHANGELOG.md | 1 + src/Bulk.php | 8 ++++++++ tests/BulkTest.php | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5059a5c3..96c65266a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Bulk.php b/src/Bulk.php index 313254d30..98aea084c 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -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); @@ -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); diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 17cbab9cf..4e9c06cd4 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -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']); } /**