Повторная запись с txnNumber 4 запрещена на сессии | Django и MongoDB

Как сделать атомарное обновление в приведенном ниже коде

следующая строка кода выдает ошибку:

self._large_results.replace(json_result, encoding='utf-8', content_type='application/json')

Код приложения:

class MyWrapper(EmbeddedDocument):
    # Set the maximum size to 12 MB
    MAXIMUM_MONGO_DOCUMENT_SIZE = 12582912

    # Lock object to lock while updating the _large_result
    lock = threading.Lock()

    # The default location to save results
    results = DictField()

    # When the results are very large (> 16M mongo will not save it in
    # a single document. We will use a file field to store these)
    _large_results = FileField(required=True)

    def large_results(self):
        try:
            self._large_results.seek(0)
            return json.load(self._large_results)
        except:
            return {}

    # Whether we are using the _large_results field
    using_large_results = BooleanField(default=False)

    def __get_true_result(self):
        if self.using_large_results:
            self._large_results.seek(0)
            try:
                return json.loads(self._large_results.read() or '{}')
            except:
                logger.exception("Error while json converting from _large_result")
                raise InvalidResultError
        else:
            return self.results

    def __set_true_result(self, result, result_class, update=False):
        class_name = result_class.__name__
        valid_result = self.__get_true_result()

        with self.lock:
            try:
                current = valid_result[class_name] if update else {}
            except:
                current = {}

            if update:
                current.update(result)
            else:
                current = result

            valid_result.update({class_name: current})

            json_result = json.dumps(valid_result)
            self.using_large_results = len(json_result) >= self.MAXIMUM_MONGO_DOCUMENT_SIZE

            if self.using_large_results:
                self._large_results.replace(json_result, encoding='utf-8', content_type='application/json')
                self.results = {}
                self._large_results.seek(0)
            else:
                self.results = valid_result
                self._large_results.replace('{}', encoding='utf-8', content_type='application/json')

Используем Django с Mongo, развернутым в кластере, и Celery для выполнения этих утверждений.

получаем следующую ошибку -

mongoengine.errors.OperationError: Could not save document (Retryable write with txnNumber 4 is prohibited on session e6d643cc-8f77-4589-b754-3fddb332b1b9 - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU= - - because a newer retryable write with txnNumber 6 has already started on this session.).

Любая информация/помощь приветствуется

Вернуться на верх