Version conflict when using the delete method of e

2019-07-18 11:26发布

问题:

So, we're using elasticsearch in our Django project, and we're using the elasticsearch-dsl python library.

We got the following error in production:

ConflictError(409, '{"took":7,"timed_out":false,"total":1,"deleted":0,"batches":1,"version_conflicts":1,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1.0,"throttled_until_millis":0,"failures":[{"index":"events","type":"_doc","id":"KJ7SpWsBZnen1jNBRWWM","cause":{"type":"version_conflict_engine_exception","reason":"[KJ7SpWsBZnen1jNBRWWM]: version conflict, required seqNo [1418], primary term [1]. current document has seqNo [1419] and primary term [1]","index_uuid":"2-fSZILVQzuJE8KVmpLFXQ","shard":"0","index":"events"},"status":409}]}')

And with better formatting:

{
    "took": 7,
    "timed_out": false,
    "total": 1,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 1,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": [
        {
            "index": "events",
            "type": "_doc",
            "id": "KJ7SpWsBZnen1jNBRWWM",
            "cause": {
                "type": "version_conflict_engine_exception",
                "reason": "[KJ7SpWsBZnen1jNBRWWM]: version conflict, required seqNo [1418], primary term [1]. current document has seqNo [1419] and primary term [1]",
                "index_uuid": "2-fSZILVQzuJE8KVmpLFXQ",
                "shard": "0",
                "index": "events"
            },
            "status": 409
        }
    ]
}

The code that produced the error was this call to the dsl delete method:

connections.create_connection(
    hosts=[settings.ELASTICSEARCH_HOST],
    timeout=20,
)
search = EventDocument.search()
# The query is made by the django model's id
search.query('match', id=self.id).delete()

And here's the definition of EventDocument:

from elasticsearch_dsl import (
    Document,
    Integer,
)


class EventDocument(Document):
    id = Integer()
    # other fields

Our biggest issue right now is that we don't have access to the server, we got the error through an automated email we configured for errors. So I don't even know how to reproduce it.

Hope you can help, thanks.

回答1:

This error is happening due to the version conflict in your documents. From ES official docs

Elasticsearch is distributed. When documents are created, updated, or deleted, the new version of the document has to be replicated to other nodes in the cluster. Elasticsearch is also asynchronous and concurrent, meaning that these replication requests are sent in parallel, and may arrive at their destination out of sequence. Elasticsearch needs a way of ensuring that an older version of a document never overwrites a newer version.

Read more about how to handle the version conflict http 409 exception in ES in this official doc, which also explains why you get the exception and various ways on how to handle it and explains the concept in details.