How can I trap for the 409 error on too much contention when updating an entity in Google Datastore

156 Views Asked by At

Every few days, on Google Datastore, when I update an entity then I receive the error "Aborted: Too much contention when updating entity". It is certainly possible that other streams could be accessing the entity at the same time. Related questions have been asked over the past 10 years and I can't find an answer, also maybe there are some newer options available. For an early example, see

"Too much contention" when creating new entity in dataStore

Consider a very basic update:

    average_data = codecs.encode(json.dumps(json_data).encode('utf-8'), encoding='zlib_codec')
    with client.transaction():
        entity = client.get(key)
        entity["average_data"] = average_data
        client.put(entity)

How can I best trap for a 409 exception? The client.put() method doesnt seem to actually return anything. I could try something like this, although it a bit seems dumb:

average_data = codecs.encode(json.dumps(json_data).encode('utf-8'), encoding='zlib_codec')
with client.transaction():
    entity = client.get(key)
    entity["average_data"] = average_data
    try:
       client.put(entity)
    except:
       try:
          sleep(0.1)
          client.put(entity)
       except:
          sleep(0.5)
          client.put(entity)

But maybe that is indeed the best option?

Thanks,

0

There are 0 best solutions below