From f8a20e1fbb4a977118b7b4aae0325b732e1389f0 Mon Sep 17 00:00:00 2001 From: csabavirag Date: Tue, 16 Jan 2024 16:13:25 +0100 Subject: [PATCH 1/3] Apply RetryOnConflict on Bulk documents --- src/Bulk.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Bulk.php b/src/Bulk.php index 5cff33a07..d793fcdf2 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -131,6 +131,10 @@ public function getActions(): array */ public function addDocument(Document $document, ?string $opType = null): self { + if ($this->_client->getConnection()->hasParam('retryOnConflict')) { + $document->setRetryOnConflict($this->_client->getConnection()->getParam('retryOnConflict')); + } + $action = AbstractDocumentAction::create($document, $opType); return $this->addAction($action); From b8963c73beb78670a43f1c59c0781fd387474f4e Mon Sep 17 00:00:00 2001 From: csabavirag Date: Wed, 17 Jan 2024 11:27:45 +0100 Subject: [PATCH 2/3] Do not reset Retry if already set + Update BulkTest --- CHANGELOG.md | 1 + src/Bulk.php | 8 ++++++-- tests/BulkTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4216e5f7..113d740fa 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Bulk.php b/src/Bulk.php index d793fcdf2..739bf129c 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -131,8 +131,8 @@ public function getActions(): array */ public function addDocument(Document $document, ?string $opType = null): self { - if ($this->_client->getConnection()->hasParam('retryOnConflict')) { - $document->setRetryOnConflict($this->_client->getConnection()->getParam('retryOnConflict')); + if (!$document->hasRetryOnConflict() && $this->_client->hasConnection() && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) { + $document->setRetryOnConflict($retry); } $action = AbstractDocumentAction::create($document, $opType); @@ -159,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); diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 07dcdf52d..3c74c642d 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -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']); + } /** From 42adf6448f99f8c8c3f6489543bc15340e0ccae7 Mon Sep 17 00:00:00 2001 From: csabavirag Date: Wed, 17 Jan 2024 17:45:33 +0100 Subject: [PATCH 3/3] Fix CI errors --- src/Bulk.php | 4 ++-- tests/BulkTest.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Bulk.php b/src/Bulk.php index 739bf129c..cd4e883f0 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -131,7 +131,7 @@ 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) { + if (!$document->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) { $document->setRetryOnConflict($retry); } @@ -159,7 +159,7 @@ 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) { + if (!$script->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) { $script->setRetryOnConflict($retry); } diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 3c74c642d..34d9c55d5 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -725,7 +725,6 @@ public function testRetry(): void $metadata = $actions[0]->getMetadata(); $this->assertEquals(5, $metadata['retry_on_conflict']); - } /**