com.netflix.astyanax.recipes.locks
Class ColumnPrefixDistributedRowLock<K>

java.lang.Object
  extended by com.netflix.astyanax.recipes.locks.ColumnPrefixDistributedRowLock<K>
Type Parameters:
K -
All Implemented Interfaces:
DistributedRowLock

public class ColumnPrefixDistributedRowLock<K>
extends Object
implements DistributedRowLock

Takes a distributed row lock for a single row. Algorithm 1. Write a column with name _. Value is a TTL. 2. Read back all columns with a) count==1 Got the lock b) otherwise Look for stale locks a) count==1 Got the lock b) otherwise No lock

Author:
elandau

Field Summary
static String DEFAULT_LOCK_PREFIX
           
static TimeUnit DEFAULT_OPERATION_TIMEOUT_UNITS
           
static int LOCK_TIMEOUT
           
 
Constructor Summary
ColumnPrefixDistributedRowLock(Keyspace keyspace, ColumnFamily<K,String> columnFamily, K key)
           
 
Method Summary
 void acquire()
          Write a lock column using the current time
 ColumnPrefixDistributedRowLock<K> expireLockAfter(long timeout, TimeUnit unit)
          Time for failed locks.
 ColumnPrefixDistributedRowLock<K> failOnStaleLock(boolean failOnStaleLock)
          When set to true the operation will fail if a stale lock is detected
 void fillLockMutation(MutationBatch m, Long time, Integer ttl)
          Fill a mutation with the lock column.
 void fillReleaseMutation(MutationBatch m)
          Fill a mutation that will release the locks.
 ConsistencyLevel getConsistencyLevel()
           
 Keyspace getKeyspace()
           
 String getLockColumn()
           
 Map<String,Long> readLockColumns()
          Return a mapping of existing lock columns and their expiration time
 void release()
          Release the lock by releasing this and any other stale lock columns
 Map<String,Long> releaseAllLocks()
          Release all locks.
 Map<String,Long> releaseExpiredLocks()
          Release all expired locks for this key.
 Map<String,Long> releaseLocks(boolean force)
          Delete locks columns.
 void verifyLock(long curTimeMicros)
          Verify that the lock was acquired
 ColumnPrefixDistributedRowLock<K> withColumnPrefix(String prefix)
          Specify the prefix that uniquely distinguishes the lock columns from data column
 ColumnPrefixDistributedRowLock<K> withConsistencyLevel(ConsistencyLevel consistencyLevel)
          Modify the consistency level being used.
 ColumnPrefixDistributedRowLock<K> withLockColumn(String column)
          Override the autogenerated lock column.
 ColumnPrefixDistributedRowLock<K> withTtl(Integer ttl)
           
protected  String writeLockColumn(long time)
          Write a lock for the specified time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCK_TIMEOUT

public static final int LOCK_TIMEOUT
See Also:
Constant Field Values

DEFAULT_OPERATION_TIMEOUT_UNITS

public static final TimeUnit DEFAULT_OPERATION_TIMEOUT_UNITS

DEFAULT_LOCK_PREFIX

public static final String DEFAULT_LOCK_PREFIX
See Also:
Constant Field Values
Constructor Detail

ColumnPrefixDistributedRowLock

public ColumnPrefixDistributedRowLock(Keyspace keyspace,
                                      ColumnFamily<K,String> columnFamily,
                                      K key)
Method Detail

withConsistencyLevel

public ColumnPrefixDistributedRowLock<K> withConsistencyLevel(ConsistencyLevel consistencyLevel)
Modify the consistency level being used. Consistency should always be a variant of quorum. The default is CL_QUORUM, which is OK for single region. For multi region the consistency level should be CL_LOCAL_QUORUM. CL_EACH_QUORUM can be used but will Incur substantial latency.

Parameters:
consistencyLevel -
Returns:

withColumnPrefix

public ColumnPrefixDistributedRowLock<K> withColumnPrefix(String prefix)
Specify the prefix that uniquely distinguishes the lock columns from data column

Parameters:
prefix -
Returns:

withLockColumn

public ColumnPrefixDistributedRowLock<K> withLockColumn(String column)
Override the autogenerated lock column.

Parameters:
column -
Returns:

failOnStaleLock

public ColumnPrefixDistributedRowLock<K> failOnStaleLock(boolean failOnStaleLock)
When set to true the operation will fail if a stale lock is detected

Parameters:
failOnStaleLock -
Returns:

expireLockAfter

public ColumnPrefixDistributedRowLock<K> expireLockAfter(long timeout,
                                                         TimeUnit unit)
Time for failed locks. Under normal circumstances the lock column will be deleted. If not then this lock column will remain and the row will remain locked. The lock will expire after this timeout.

Parameters:
timeout -
unit -
Returns:

withTtl

public ColumnPrefixDistributedRowLock<K> withTtl(Integer ttl)

acquire

public void acquire()
             throws Exception
Write a lock column using the current time

Specified by:
acquire in interface DistributedRowLock
Throws:
Exception

verifyLock

public void verifyLock(long curTimeMicros)
                throws Exception,
                       BusyLockException,
                       StaleLockException
Verify that the lock was acquired

Parameters:
curTimeMicros -
Throws:
Exception
BusyLockException
StaleLockException

release

public void release()
             throws Exception
Release the lock by releasing this and any other stale lock columns

Specified by:
release in interface DistributedRowLock
Throws:
Exception

fillReleaseMutation

public void fillReleaseMutation(MutationBatch m)
Fill a mutation that will release the locks. This may be used from a separate recipe to release multiple locks.

Parameters:
m -

readLockColumns

public Map<String,Long> readLockColumns()
                                 throws Exception
Return a mapping of existing lock columns and their expiration time

Returns:
Throws:
Exception

releaseAllLocks

public Map<String,Long> releaseAllLocks()
                                 throws Exception
Release all locks. Use this carefully as it could release a lock for a running operation

Returns:
Throws:
Exception

releaseExpiredLocks

public Map<String,Long> releaseExpiredLocks()
                                     throws Exception
Release all expired locks for this key.

Returns:
Throws:
Exception

releaseLocks

public Map<String,Long> releaseLocks(boolean force)
                              throws Exception
Delete locks columns. Set force=true to remove locks that haven't been expired yet.

Parameters:
force -
Returns:
Throws:
Exception

writeLockColumn

protected String writeLockColumn(long time)
                          throws Exception
Write a lock for the specified time.

Parameters:
time -
Returns:
The column name for the lock
Throws:
Exception

fillLockMutation

public void fillLockMutation(MutationBatch m,
                             Long time,
                             Integer ttl)
Fill a mutation with the lock column. This may be used when the mutation is executed externally but should be used with extreme caution to ensure the lock is properly release

Parameters:
m -
time -
ttl -

getKeyspace

public Keyspace getKeyspace()

getConsistencyLevel

public ConsistencyLevel getConsistencyLevel()

getLockColumn

public String getLockColumn()


Copyright © 2012. All Rights Reserved.