cassandra-client-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Godás <dgo...@gmail.com>
Subject Implementing a locking mechanism - RFC
Date Thu, 31 Jan 2013 11:35:42 GMT
Hi all,

I need a locking mechanism on top of cassandra so that multiple
clients can protect a critical section. I've seen some attempts,
including Dominic Williams' wait chain algorithm but I think it can be
simplified. This is the procedure I wrote to implement a simple mutex.
Note that it hasn't been thoroughly tested and I have been using
cassandra for a very short time so I'd appreciate any comments on
obvious errors or things I'm doing plain wrong and will never work.

The assumptions and requirements for the algorithm are the same as
Dominic Williams'
(http://media.fightmymonster.com/Shared/docs/Wait%20Chain%20Algorithm.pdf).

We will create a column family for the locks referred to as "locks"
throughout this procedure. The column family contains two columns; an
identifier for the lock  which will also be the column key ("id") and
a counter ("c"). Throughout the procedure "my_lock_id" will be used as
the lock identifier. An arbitrary time-to-live value is required by
the algorithm. This value will be referred to as "t". Choosing an
appropriate value for "t" will be postponed until the algorithm is
deemed good.

=== begin procedure ===

(A) When a client needs to access the critical section the following
steps are taken:

--- begin ---

1) SELECT c FROM locks WHERE id = my_lock_id
2) if c = 0 try to acquire the lock (B), else don't try (C)

--- end ---

(B) Try to acquire the lock:

--- begin ---

1) UPDATE locks USING TTL t SET c = c + 1 WHERE id = my_lock_id
2) SELECT c FROM locks WHERE id = my_lock_id
3) if c = 1 we acquired the lock (D), else we didn't (C)

--- end ---

(C) Wait before re-trying:

--- begin ---

1) sleep for a random time higher than t and start at (A) again

--- end ---

(D) Execute the critical section and release the lock:

--- begin ---

1) start background thread that increments c with TTL = t every t / 2
interval (UPDATE locks USING TTL t SET c = c + 1 WHERE id =
my_lock_id)
2) execute the critical section
3) kill background thread
4) DELETE * FROM locks WHERE id = my_lock_id

--- end ---

=== end procedure ===

Looking forward to read your comments,
Dan

Mime
View raw message