Skip to content

Commit c5d9861

Browse files
armando1793sethmlarson
authored andcommitted
[7.x] Remove optimistic concurrency metadata when retry_on_conflict is used with update()
1 parent e4801e1 commit c5d9861

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

elasticsearch_dsl/document.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,11 @@ def update(
406406
doc_meta["retry_on_conflict"] = retry_on_conflict
407407

408408
# Optimistic concurrency control
409-
if "seq_no" in self.meta and "primary_term" in self.meta:
409+
if (
410+
retry_on_conflict in (None, 0)
411+
and "seq_no" in self.meta
412+
and "primary_term" in self.meta
413+
):
410414
doc_meta["if_seq_no"] = self.meta["seq_no"]
411415
doc_meta["if_primary_term"] = self.meta["primary_term"]
412416

tests/test_integration/test_document.py

+33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from datetime import datetime
1919
from ipaddress import ip_address
2020

21+
import pytest
2122
from elasticsearch import ConflictError, NotFoundError
2223
from pytest import raises
2324
from pytz import timezone
@@ -234,6 +235,38 @@ def test_update_script(write_client):
234235
assert w.views == 47
235236

236237

238+
def test_update_retry_on_conflict(write_client):
239+
Wiki.init()
240+
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
241+
w.save()
242+
243+
w1 = Wiki.get(id="elasticsearch-py")
244+
w2 = Wiki.get(id="elasticsearch-py")
245+
w1.update(script="ctx._source.views += params.inc", inc=5, retry_on_conflict=1)
246+
w2.update(script="ctx._source.views += params.inc", inc=5, retry_on_conflict=1)
247+
248+
w = Wiki.get(id="elasticsearch-py")
249+
assert w.views == 52
250+
251+
252+
@pytest.mark.parametrize("retry_on_conflict", [None, 0])
253+
def test_update_conflicting_version(write_client, retry_on_conflict):
254+
Wiki.init()
255+
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
256+
w.save()
257+
258+
w1 = Wiki.get(id="elasticsearch-py")
259+
w2 = Wiki.get(id="elasticsearch-py")
260+
w1.update(script="ctx._source.views += params.inc", inc=5)
261+
262+
with raises(ConflictError):
263+
w2.update(
264+
script="ctx._source.views += params.inc",
265+
inc=5,
266+
retry_on_conflict=retry_on_conflict,
267+
)
268+
269+
237270
def test_save_and_update_return_doc_meta(write_client):
238271
Wiki.init()
239272
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)

0 commit comments

Comments
 (0)