0

I'm developing an application on GAE/J and looking into how to implement distributed lock on it.

My requirement is quite same with this question. But this question is about 7 years old, so I can't find whether the answers are still valid. Also I can't find these answers works on GAE/J.

How can I implement distributed lock on GAE/J?

Community
  • 1
  • 1
N.F.
  • 3,844
  • 3
  • 22
  • 53

1 Answers1

0

You can make transaction over the datastore, with that you can implement a distributed lock on it. https://cloud.google.com/appengine/docs/java/datastore/transactions

Mr_Thorynque
  • 1,749
  • 1
  • 20
  • 31
  • Thanks for reply. One of my requirement is "if lock holder dies, it will automatically be freed after X seconds". Does datastore meets this requirement? – N.F. Jun 28 '16 at 08:59
  • You put a value in the datastore to acquire the lock using transaction, after the transaction you have set the lock. You have to use finally or what else to ensure the the lock is released. You can have a process who check the lock and release it evaluating time (value can be a timestamp). But nothing work out of the box. – Mr_Thorynque Jun 28 '16 at 09:12
  • 1
    When process dies, finally is not executed. And timestamp method can not be used because we can not guarantee how long our transaction takes. – N.F. Jun 28 '16 at 09:29
  • Why your process dies ? Because of exception or because it's kill by external event ? Transaction is used to acquire lock and read it, to have "atomic" operation, not to do the process job within. – Mr_Thorynque Jun 28 '16 at 09:50
  • op is right. comments here are not considering recovery from leaving the lock locked. without that any solution is useless. – Zig Mandel Jun 28 '16 at 13:32
  • that said, memcached is the closest you will find as it handles distributed value increment. with that you can build a lock that guarantees only one gets the lock, but you would need to detect stale lock (with a timestamp maybe to detect stale lock from dead process) – Zig Mandel Jun 28 '16 at 13:35
  • 1
    memcache in GAE is not safe : "Memcache can be useful for other temporary values. However, when considering whether to store a value solely in the memcache and not backed by other persistent storage, be sure that your application behaves acceptably when the value is suddenly not available" – Mr_Thorynque Jun 28 '16 at 14:35