org.egothor.lock
Class LockManager

java.lang.Object
  extended by org.egothor.lock.LockManager
Direct Known Subclasses:
LockFileManager, LockServerManager

public abstract class LockManager
extends java.lang.Object

Entry point for all lock processing requests. It provides methods to acquire or release locks plus other methods involved in multithreaded usage of some location, like modifier state activation, index constancy processing. There are 3 ways of getting a lock:

- simple get[Write|Read]Lock - single request

- getSleep[Write|Read]Lock - repetative requests in case of failure until succes (or timeout). Following attempts are processed after a period of sleeping.

- getSpin[Write|Read]Lock - repetative requests in case of failure until succes (or timeout). Following attempts are proccesed imediatly after failure.

Author:
Jakub Podhorny

Constructor Summary
LockManager()
          Constructor
 
Method Summary
 java.io.File createUniqueFile(java.lang.String location, java.lang.String prefix)
          Creates a unique file with the input prefix in the input location.
 void ensureModifierActiveState(java.lang.String globalTankerLocation, java.lang.String localTankerLocation)
          Ensures that a caller has an active modifier state.
 LockResult getReadLock(java.lang.String location, long indexConstPeriodOfTime, long threadGroupId, int timeOut)
          Gets a READ lock in the location using only single request.
 boolean getSleepReadLock(java.lang.String location, long indexConstPeriodOfTime, long threadGroupId, int timeOut)
          Gets a READ lock in the location, using sleeping - repetative requests until succes with short sleeping in between.
 boolean getSleepRecoveryLock(java.lang.String location, int timeOut)
          Gets a RECOVERY lock in the location, using sleeping - repetative requests until succes with short sleeping in between.
 boolean getSleepWriteLock(java.lang.String location, int timeOut)
          Gets a WRITE lock in the location, using sleeping - repetative requests until succes with short sleeping in between.
 boolean getSpinReadLock(java.lang.String location, long indexConstPeriodOfTime, long threadGroupId, int timeOut)
          Gets a READ lock in the location, using spinning - repetative requests until succes with no delay in between.
 boolean getSpinRecoveryLock(java.lang.String location, int timeOut)
          Gets a RECOVERY lock in the location, using spinning - repetative requests until succes with no delay in between.
 boolean getSpinWriteLock(java.lang.String location, int timeOut)
          Gets a WRITE lock in the location, using spinning - repetative requests until succes with no delay in between.
 LockResult getWriteLock(java.lang.String location, int timeOut)
          Gets a WRITE lock in the location using only single request.
 boolean isIndexConstant(java.lang.String location, long threadGroupId)
          Checks whether index in the location is constant for the caller.
protected abstract  LockResult processLock(LockRequest request)
          Method will try to achieve read lock in the appropriate implementation.
 boolean releaseLock(java.lang.String location)
          Releases lock in the location.
 void removeActiveModifierState(java.lang.String globalTankerLocation, java.lang.String localTankerLocation)
          Removes active modifier state from the location
 void removeIndexConstState(java.lang.String location, long threadGroupId)
          Discards index constancy in the location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LockManager

public LockManager()
Constructor

Method Detail

getSpinReadLock

public boolean getSpinReadLock(java.lang.String location,
                               long indexConstPeriodOfTime,
                               long threadGroupId,
                               int timeOut)
Gets a READ lock in the location, using spinning - repetative requests until succes with no delay in between.

Parameters:
location - Location to lock
indexConstPeriodOfTime - Period of time when index will remain constant for the caller
threadGroupId - Id of a group of thread that share single instance of a tanker
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getSpinWriteLock

public boolean getSpinWriteLock(java.lang.String location,
                                int timeOut)
Gets a WRITE lock in the location, using spinning - repetative requests until succes with no delay in between.

Parameters:
location - Location to lock
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getSleepReadLock

public boolean getSleepReadLock(java.lang.String location,
                                long indexConstPeriodOfTime,
                                long threadGroupId,
                                int timeOut)
Gets a READ lock in the location, using sleeping - repetative requests until succes with short sleeping in between.

Parameters:
location - Location to lock
indexConstPeriodOfTime - Period of time when index will remain constant for the caller
threadGroupId - Id of a group of thread that share single instance of a tanker
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getSleepWriteLock

public boolean getSleepWriteLock(java.lang.String location,
                                 int timeOut)
Gets a WRITE lock in the location, using sleeping - repetative requests until succes with short sleeping in between.

Parameters:
location - Location to lock
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getSpinRecoveryLock

public boolean getSpinRecoveryLock(java.lang.String location,
                                   int timeOut)
Gets a RECOVERY lock in the location, using spinning - repetative requests until succes with no delay in between. This lock is only for recovery purposes, should not be used otherwise.

Parameters:
location - Location to lock
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getSleepRecoveryLock

public boolean getSleepRecoveryLock(java.lang.String location,
                                    int timeOut)
Gets a RECOVERY lock in the location, using sleeping - repetative requests until succes with short sleeping in between. This lock is only for recovery purposes, should not be used otherwise.

Parameters:
location - Location to lock
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getReadLock

public LockResult getReadLock(java.lang.String location,
                              long indexConstPeriodOfTime,
                              long threadGroupId,
                              int timeOut)
Gets a READ lock in the location using only single request.

Parameters:
location - Location to lock
indexConstPeriodOfTime - Period of time when index will remain constant for the caller
threadGroupId - Id of a group of thread that share single instance of a tanker
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

getWriteLock

public LockResult getWriteLock(java.lang.String location,
                               int timeOut)
Gets a WRITE lock in the location using only single request.

Parameters:
location - Location to lock
timeOut - Period of time in which this request must be processed
Returns:
true if lock granted, false otherwise

releaseLock

public boolean releaseLock(java.lang.String location)
Releases lock in the location.

Parameters:
location - Location where the lock should be released
Returns:
true if lock released, false otherwise

isIndexConstant

public boolean isIndexConstant(java.lang.String location,
                               long threadGroupId)
Checks whether index in the location is constant for the caller. Returns true if constancy is valid, false otherwise

Parameters:
location - Location where to check a constancy state
threadGroupId - Identifier of a group of threads that share one instance of the tanker.
Returns:
true if constancy is valid, false otherwise

removeIndexConstState

public void removeIndexConstState(java.lang.String location,
                                  long threadGroupId)
Discards index constancy in the location.

Parameters:
location - Location where to cancel a constancy for the caller
threadGroupId - Identifier of a group of threads that share one instance of the tanker.

ensureModifierActiveState

public void ensureModifierActiveState(java.lang.String globalTankerLocation,
                                      java.lang.String localTankerLocation)
Ensures that a caller has an active modifier state. It writes localTankerLocation into the active modif state file for recovery reasons.

Parameters:
globalTankerLocation - Location where a active modifier state should be created
localTankerLocation - Location of tanker where modifications are stored before commit

removeActiveModifierState

public void removeActiveModifierState(java.lang.String globalTankerLocation,
                                      java.lang.String localTankerLocation)
Removes active modifier state from the location

Parameters:
globalTankerLocation - Location where an active modifier state should be canceled
localTankerLocation - TODO

createUniqueFile

public java.io.File createUniqueFile(java.lang.String location,
                                     java.lang.String prefix)
Creates a unique file with the input prefix in the input location.

Parameters:
location - Location where to create a unique file
prefix - Prefix of the unique file
Returns:
java.io.File object of the created file

processLock

protected abstract LockResult processLock(LockRequest request)
                                   throws java.io.IOException
Method will try to achieve read lock in the appropriate implementation.

Parameters:
request -
Returns:
Throws:
java.io.IOException