Retryable write with txnNumber 4 is prohibited on session | Django and MongoDB
How to do atomic update in below code
following line of code throws an error:
self._large_results.replace(json_result, encoding='utf-8', content_type='application/json')
Application Code:
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')
Using Django with Mongo deployed in cluster and Celery to run these statements.
getting following error -
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.).
Any leads/help appreciated