Class LockManager
- java.lang.Object
-
- org.apache.manifoldcf.core.lockmanager.LockManager
-
- All Implemented Interfaces:
ILockManager
public class LockManager extends java.lang.Object implements ILockManager
The lock manager manages locks across all threads and JVMs and cluster members. There should be no more than ONE instance of this class per thread!!! The factory should enforce this.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String_rcsidprotected ILockManagerlockManagerBacking lock manager
-
Constructor Summary
Constructors Constructor Description LockManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancheckGlobalFlag(java.lang.String flagName)Check the condition of a specified flag.booleancheckServiceActive(java.lang.String serviceType, java.lang.String serviceName)Check whether a service is active or not.booleancleanupInactiveService(java.lang.String serviceType, IServiceCleanup cleanup)Clean up any inactive services found.voidclearGlobalFlag(java.lang.String flagName)Clear a flag.voidclearLocks()Clear all outstanding locks in the system.intcountActiveServices(java.lang.String serviceType)Count all active services of a given type.voidendServiceActivity(java.lang.String serviceType, java.lang.String serviceName)End service activity.voidenterCriticalSections(java.lang.String[] readSectionKeys, java.lang.String[] nonExSectionKeys, java.lang.String[] writeSectionKeys)Enter multiple critical sections simultaneously.voidenterLocks(java.lang.String[] readLocks, java.lang.String[] nonExWriteLocks, java.lang.String[] writeLocks)Enter multiple locksvoidenterLocksNoWait(java.lang.String[] readLocks, java.lang.String[] nonExWriteLocks, java.lang.String[] writeLocks)Enter multiple locks simultaneously.voidenterNonExWriteCriticalSection(java.lang.String sectionKey)Enter a named, non-exclusive write critical section (NOT a lock).voidenterNonExWriteLock(java.lang.String lockKey)Enter a non-exclusive write-locked area (blocking out all readers, but letting in other "writers").voidenterNonExWriteLockNoWait(java.lang.String lockKey)Enter a non-exclusive write-locked area (blocking out all readers, but letting in other "writers").voidenterReadCriticalSection(java.lang.String sectionKey)Enter a named, read critical section (NOT a lock).voidenterReadLock(java.lang.String lockKey)Enter a read-only locked area (i.e., block ONLY if there's a writer)voidenterReadLockNoWait(java.lang.String lockKey)Enter a read-only locked area (i.e., block ONLY if there's a writer).voidenterWriteCriticalSection(java.lang.String sectionKey)Enter a named, exclusive critical section (NOT a lock).voidenterWriteLock(java.lang.String lockKey)Enter a write locked area (i.e., block out both readers and other writers) NOTE: Can't enter until all readers have left.voidenterWriteLockNoWait(java.lang.String lockKey)Enter a write locked code area (i.e., block out both readers and other writers), but do not wait if the lock cannot be obtained.ManifoldCFConfigurationgetSharedConfiguration()Get the current shared configuration.voidleaveCriticalSections(java.lang.String[] readSectionKeys, java.lang.String[] nonExSectionKeys, java.lang.String[] writeSectionKeys)Leave multiple critical sections simultaneously.voidleaveLocks(java.lang.String[] readLocks, java.lang.String[] writeNonExLocks, java.lang.String[] writeLocks)Leave multiple locksvoidleaveNonExWriteCriticalSection(java.lang.String sectionKey)Leave a named, non-exclusive write critical section (NOT a lock).voidleaveNonExWriteLock(java.lang.String lockKey)Leave a non-exclusive write lock.voidleaveReadCriticalSection(java.lang.String sectionKey)Leave a named, read critical section (NOT a lock).voidleaveReadLock(java.lang.String lockKey)Leave a read-locked code area.voidleaveWriteCriticalSection(java.lang.String sectionKey)Leave a named, exclusive critical section (NOT a lock).voidleaveWriteLock(java.lang.String lockKey)Leave a write locked code area.byte[]readData(java.lang.String resourceName)Read data from a shared data resource.java.lang.StringregisterServiceBeginServiceActivity(java.lang.String serviceType, java.lang.String serviceName, byte[] initialData, IServiceCleanup cleanup)Register a service and begin service activity.java.lang.StringregisterServiceBeginServiceActivity(java.lang.String serviceType, java.lang.String serviceName, IServiceCleanup cleanup)Register a service and begin service activity.byte[]retrieveServiceData(java.lang.String serviceType, java.lang.String serviceName)Retrieve service data for a service.voidscanServiceData(java.lang.String serviceType, IServiceDataAcceptor dataAcceptor)Scan service data for a service type.voidsetGlobalFlag(java.lang.String flagName)Raise a flag.voidtimedWait(int time)Wait for a time before retrying a lock.voidupdateServiceData(java.lang.String serviceType, java.lang.String serviceName, byte[] serviceData)Set service data for a service.voidwriteData(java.lang.String resourceName, byte[] data)Write data to a shared data resource.
-
-
-
Field Detail
-
_rcsid
public static final java.lang.String _rcsid
- See Also:
- Constant Field Values
-
lockManager
protected final ILockManager lockManager
Backing lock manager
-
-
Constructor Detail
-
LockManager
public LockManager() throws ManifoldCFException- Throws:
ManifoldCFException
-
-
Method Detail
-
registerServiceBeginServiceActivity
public java.lang.String registerServiceBeginServiceActivity(java.lang.String serviceType, java.lang.String serviceName, IServiceCleanup cleanup) throws ManifoldCFExceptionRegister a service and begin service activity. This atomic operation creates a permanent registration entry for a service. If the permanent registration entry already exists, this method will not create it or treat it as an error. This operation also enters the "active" zone for the service. The "active" zone will remain in force until it is canceled, or until the process is interrupted. Ideally, the corresponding endServiceActivity method will be called when the service shuts down. Some ILockManager implementations require that this take place for proper management. If the transient registration already exists, it is treated as an error and an exception will be thrown. If registration will succeed, then this method may call an appropriate IServiceCleanup method to clean up either the current service, or all services on the cluster.- Specified by:
registerServiceBeginServiceActivityin interfaceILockManager- Parameters:
serviceType- is the type of service.serviceName- is the name of the service to register. If null is passed, a transient unique service name will be created, and will be returned to the caller.cleanup- is called to clean up either the current service, or all services of this type, if no other active service exists. May be null. Local service cleanup is never called if the serviceName argument is null.- Returns:
- the actual service name.
- Throws:
ManifoldCFException
-
registerServiceBeginServiceActivity
public java.lang.String registerServiceBeginServiceActivity(java.lang.String serviceType, java.lang.String serviceName, byte[] initialData, IServiceCleanup cleanup) throws ManifoldCFExceptionRegister a service and begin service activity. This atomic operation creates a permanent registration entry for a service. If the permanent registration entry already exists, this method will not create it or treat it as an error. This operation also enters the "active" zone for the service. The "active" zone will remain in force until it is canceled, or until the process is interrupted. Ideally, the corresponding endServiceActivity method will be called when the service shuts down. Some ILockManager implementations require that this take place for proper management. If the transient registration already exists, it is treated as an error and an exception will be thrown. If registration will succeed, then this method may call an appropriate IServiceCleanup method to clean up either the current service, or all services on the cluster.- Specified by:
registerServiceBeginServiceActivityin interfaceILockManager- Parameters:
serviceType- is the type of service.serviceName- is the name of the service to register. If null is passed, a transient unique service name will be created, and will be returned to the caller.initialData- is the initial service data for this service.cleanup- is called to clean up either the current service, or all services of this type, if no other active service exists. May be null. Local service cleanup is never called if the serviceName argument is null.- Returns:
- the actual service name.
- Throws:
ManifoldCFException
-
updateServiceData
public void updateServiceData(java.lang.String serviceType, java.lang.String serviceName, byte[] serviceData) throws ManifoldCFExceptionSet service data for a service.- Specified by:
updateServiceDatain interfaceILockManager- Parameters:
serviceType- is the type of service.serviceName- is the name of the service.serviceData- is the data to update to (may be null). This updates the service's transient data (or deletes it). If the service is not active, an exception is thrown.- Throws:
ManifoldCFException
-
retrieveServiceData
public byte[] retrieveServiceData(java.lang.String serviceType, java.lang.String serviceName) throws ManifoldCFExceptionRetrieve service data for a service.- Specified by:
retrieveServiceDatain interfaceILockManager- Parameters:
serviceType- is the type of service.serviceName- is the name of the service.- Returns:
- the service's transient data.
- Throws:
ManifoldCFException
-
scanServiceData
public void scanServiceData(java.lang.String serviceType, IServiceDataAcceptor dataAcceptor) throws ManifoldCFExceptionScan service data for a service type. Only active service data will be considered.- Specified by:
scanServiceDatain interfaceILockManager- Parameters:
serviceType- is the type of service.dataAcceptor- is the object that will be notified of each item of data for each service name found.- Throws:
ManifoldCFException
-
cleanupInactiveService
public boolean cleanupInactiveService(java.lang.String serviceType, IServiceCleanup cleanup) throws ManifoldCFExceptionClean up any inactive services found. Calling this method will invoke cleanup of one inactive service at a time. If there are no inactive services around, then false will be returned. Note that this method will block whatever service it finds from starting up for the time the cleanup is proceeding. At the end of the cleanup, if successful, the service will be atomically unregistered.- Specified by:
cleanupInactiveServicein interfaceILockManager- Parameters:
serviceType- is the service type.cleanup- is the object to call to clean up an inactive service.- Returns:
- true if there were no cleanup operations necessary.
- Throws:
ManifoldCFException
-
countActiveServices
public int countActiveServices(java.lang.String serviceType) throws ManifoldCFExceptionCount all active services of a given type.- Specified by:
countActiveServicesin interfaceILockManager- Parameters:
serviceType- is the service type.- Returns:
- the count.
- Throws:
ManifoldCFException
-
endServiceActivity
public void endServiceActivity(java.lang.String serviceType, java.lang.String serviceName) throws ManifoldCFExceptionEnd service activity. This operation exits the "active" zone for the service. This must take place using the same ILockManager object that was used to registerServiceBeginServiceActivity() - which implies that it is the same thread.- Specified by:
endServiceActivityin interfaceILockManager- Parameters:
serviceType- is the type of service.serviceName- is the name of the service to exit.- Throws:
ManifoldCFException
-
checkServiceActive
public boolean checkServiceActive(java.lang.String serviceType, java.lang.String serviceName) throws ManifoldCFExceptionCheck whether a service is active or not. This operation returns true if the specified service is considered active at the moment. Once a service is not active anymore, it can only return to activity by calling beginServiceActivity() once more.- Specified by:
checkServiceActivein interfaceILockManager- Parameters:
serviceType- is the type of service.serviceName- is the name of the service to check on.- Returns:
- true if the service is considered active.
- Throws:
ManifoldCFException
-
getSharedConfiguration
public ManifoldCFConfiguration getSharedConfiguration() throws ManifoldCFException
Get the current shared configuration. This configuration is available in common among all nodes, and thus must not be accessed through here for the purpose of finding configuration data that is specific to any one specific node.- Specified by:
getSharedConfigurationin interfaceILockManager- Throws:
ManifoldCFException
-
setGlobalFlag
public void setGlobalFlag(java.lang.String flagName) throws ManifoldCFExceptionRaise a flag. Use this method to assert a condition, or send a global signal. The flag will be reset when the entire system is restarted.- Specified by:
setGlobalFlagin interfaceILockManager- Parameters:
flagName- is the name of the flag to set.- Throws:
ManifoldCFException
-
clearGlobalFlag
public void clearGlobalFlag(java.lang.String flagName) throws ManifoldCFExceptionClear a flag. Use this method to clear a condition, or retract a global signal.- Specified by:
clearGlobalFlagin interfaceILockManager- Parameters:
flagName- is the name of the flag to clear.- Throws:
ManifoldCFException
-
checkGlobalFlag
public boolean checkGlobalFlag(java.lang.String flagName) throws ManifoldCFExceptionCheck the condition of a specified flag.- Specified by:
checkGlobalFlagin interfaceILockManager- Parameters:
flagName- is the name of the flag to check.- Returns:
- true if the flag is set, false otherwise.
- Throws:
ManifoldCFException
-
readData
public byte[] readData(java.lang.String resourceName) throws ManifoldCFExceptionRead data from a shared data resource. Use this method to read any existing data, or get a null back if there is no such resource. Note well that this is not necessarily an atomic operation, and it must thus be protected by a lock.- Specified by:
readDatain interfaceILockManager- Parameters:
resourceName- is the global name of the resource.- Returns:
- a byte array containing the data, or null.
- Throws:
ManifoldCFException
-
writeData
public void writeData(java.lang.String resourceName, byte[] data) throws ManifoldCFExceptionWrite data to a shared data resource. Use this method to write a body of data into a shared resource. Note well that this is not necessarily an atomic operation, and it must thus be protected by a lock.- Specified by:
writeDatain interfaceILockManager- Parameters:
resourceName- is the global name of the resource.data- is the byte array containing the data. Pass null if you want to delete the resource completely.- Throws:
ManifoldCFException
-
timedWait
public void timedWait(int time) throws ManifoldCFExceptionWait for a time before retrying a lock.- Specified by:
timedWaitin interfaceILockManager- Parameters:
time- is the amount of time to wait, in milliseconds. Zero is a legal value, and will wait no time, but will give up the current timeslice to another thread.- Throws:
ManifoldCFException
-
enterNonExWriteLock
public void enterNonExWriteLock(java.lang.String lockKey) throws ManifoldCFExceptionEnter a non-exclusive write-locked area (blocking out all readers, but letting in other "writers"). This kind of lock is designed to be used in conjunction with read locks. It is used typically in a situation where the read lock represents a query and the non-exclusive write lock represents a modification to an individual item that might affect the query, but where multiple modifications do not individually interfere with one another (use of another, standard, write lock per item can guarantee this).- Specified by:
enterNonExWriteLockin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFException
-
enterNonExWriteLockNoWait
public void enterNonExWriteLockNoWait(java.lang.String lockKey) throws ManifoldCFException, LockExceptionDescription copied from interface:ILockManagerEnter a non-exclusive write-locked area (blocking out all readers, but letting in other "writers"). This kind of lock is designed to be used in conjunction with read locks. It is used typically in a situation where the read lock represents a query and the non-exclusive write lock represents a modification to an individual item that might affect the query, but where multiple modifications do not individually interfere with one another (use of another, standard, write lock per item can guarantee this). This method works across JVMs, and will throw LockException if the lock condition cannot be immediately met.- Specified by:
enterNonExWriteLockNoWaitin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFExceptionLockException
-
leaveNonExWriteLock
public void leaveNonExWriteLock(java.lang.String lockKey) throws ManifoldCFExceptionLeave a non-exclusive write lock.- Specified by:
leaveNonExWriteLockin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFException
-
enterWriteLock
public void enterWriteLock(java.lang.String lockKey) throws ManifoldCFExceptionEnter a write locked area (i.e., block out both readers and other writers) NOTE: Can't enter until all readers have left.- Specified by:
enterWriteLockin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFException
-
enterWriteLockNoWait
public void enterWriteLockNoWait(java.lang.String lockKey) throws ManifoldCFException, LockExceptionDescription copied from interface:ILockManagerEnter a write locked code area (i.e., block out both readers and other writers), but do not wait if the lock cannot be obtained. Write locks permit only ONE thread to be in the named section, across JVM's as well. In order to guarantee this, an exception (LockException) will be thrown if the lock condition cannot be met immediately.- Specified by:
enterWriteLockNoWaitin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFExceptionLockException
-
leaveWriteLock
public void leaveWriteLock(java.lang.String lockKey) throws ManifoldCFExceptionDescription copied from interface:ILockManagerLeave a write locked code area. Use this method to exit a write-locked section. The lockKey parameter must correspond to the key used for the enter method.- Specified by:
leaveWriteLockin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFException
-
enterReadLock
public void enterReadLock(java.lang.String lockKey) throws ManifoldCFExceptionEnter a read-only locked area (i.e., block ONLY if there's a writer)- Specified by:
enterReadLockin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFException
-
enterReadLockNoWait
public void enterReadLockNoWait(java.lang.String lockKey) throws ManifoldCFException, LockExceptionDescription copied from interface:ILockManagerEnter a read-only locked area (i.e., block ONLY if there's a writer). This kind of lock permits multiple threads inside the same code area, but only if there is no "writer" in the same section at the same time. This method works across JVMs, and will throw LockException if the required lock cannot be immediately met.- Specified by:
enterReadLockNoWaitin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFExceptionLockException
-
leaveReadLock
public void leaveReadLock(java.lang.String lockKey) throws ManifoldCFExceptionDescription copied from interface:ILockManagerLeave a read-locked code area. Use this method to exit a read-locked section. The lockKey parameter must correspond to the key used for the enter method.- Specified by:
leaveReadLockin interfaceILockManager- Parameters:
lockKey- is the name of the lock.- Throws:
ManifoldCFException
-
clearLocks
public void clearLocks() throws ManifoldCFExceptionDescription copied from interface:ILockManagerClear all outstanding locks in the system. This is a very dangerous method to use (obviously)...- Specified by:
clearLocksin interfaceILockManager- Throws:
ManifoldCFException
-
enterLocks
public void enterLocks(java.lang.String[] readLocks, java.lang.String[] nonExWriteLocks, java.lang.String[] writeLocks) throws ManifoldCFExceptionEnter multiple locks- Specified by:
enterLocksin interfaceILockManager- Parameters:
readLocks- is an array of read lock names, or null if there are no read locks desired.nonExWriteLocks- is an array of non-ex write lock names, or null if none desired.writeLocks- is an array of write lock names, or null if there are none desired.- Throws:
ManifoldCFException
-
enterLocksNoWait
public void enterLocksNoWait(java.lang.String[] readLocks, java.lang.String[] nonExWriteLocks, java.lang.String[] writeLocks) throws ManifoldCFException, LockExceptionDescription copied from interface:ILockManagerEnter multiple locks simultaneously. Use this method if a series or set of locks needs to be thrown for an operation to take place. This operation will avoid deadlock if all the locks are thrown at the start of the area using this method. This method works cross-JVM, and will throw LockException if the required locks are not available.- Specified by:
enterLocksNoWaitin interfaceILockManager- Parameters:
readLocks- is an array of read lock names, or null if there are no read locks desired.nonExWriteLocks- is an array of non-ex write lock names, or null if none desired.writeLocks- is an array of write lock names, or null if there are none desired.- Throws:
ManifoldCFExceptionLockException
-
leaveLocks
public void leaveLocks(java.lang.String[] readLocks, java.lang.String[] writeNonExLocks, java.lang.String[] writeLocks) throws ManifoldCFExceptionLeave multiple locks- Specified by:
leaveLocksin interfaceILockManager- Parameters:
readLocks- is an array of read lock names, or null if there are no read locks desired.writeNonExLocks- is an array of non-ex write lock names, or null if none desired.writeLocks- is an array of write lock names, or null if there are none desired.- Throws:
ManifoldCFException
-
enterReadCriticalSection
public void enterReadCriticalSection(java.lang.String sectionKey) throws ManifoldCFExceptionEnter a named, read critical section (NOT a lock). Critical sections never cross JVM boundaries. Critical section names do not collide with lock names; they have a distinct namespace.- Specified by:
enterReadCriticalSectionin interfaceILockManager- Parameters:
sectionKey- is the name of the section to enter. Only one thread can be in any given named section at a time.- Throws:
ManifoldCFException
-
leaveReadCriticalSection
public void leaveReadCriticalSection(java.lang.String sectionKey) throws ManifoldCFExceptionLeave a named, read critical section (NOT a lock). Critical sections never cross JVM boundaries. Critical section names do not collide with lock names; they have a distinct namespace.- Specified by:
leaveReadCriticalSectionin interfaceILockManager- Parameters:
sectionKey- is the name of the section to leave. Only one thread can be in any given named section at a time.- Throws:
ManifoldCFException
-
enterNonExWriteCriticalSection
public void enterNonExWriteCriticalSection(java.lang.String sectionKey) throws ManifoldCFExceptionEnter a named, non-exclusive write critical section (NOT a lock). Critical sections never cross JVM boundaries. Critical section names do not collide with lock names; they have a distinct namespace.- Specified by:
enterNonExWriteCriticalSectionin interfaceILockManager- Parameters:
sectionKey- is the name of the section to enter. Only one thread can be in any given named section at a time.- Throws:
ManifoldCFException
-
leaveNonExWriteCriticalSection
public void leaveNonExWriteCriticalSection(java.lang.String sectionKey) throws ManifoldCFExceptionLeave a named, non-exclusive write critical section (NOT a lock). Critical sections never cross JVM boundaries. Critical section names do not collide with lock names; they have a distinct namespace.- Specified by:
leaveNonExWriteCriticalSectionin interfaceILockManager- Parameters:
sectionKey- is the name of the section to leave. Only one thread can be in any given named section at a time.- Throws:
ManifoldCFException
-
enterWriteCriticalSection
public void enterWriteCriticalSection(java.lang.String sectionKey) throws ManifoldCFExceptionEnter a named, exclusive critical section (NOT a lock). Critical sections never cross JVM boundaries. Critical section names should be distinct from all lock names.- Specified by:
enterWriteCriticalSectionin interfaceILockManager- Parameters:
sectionKey- is the name of the section to enter. Only one thread can be in any given named section at a time.- Throws:
ManifoldCFException
-
leaveWriteCriticalSection
public void leaveWriteCriticalSection(java.lang.String sectionKey) throws ManifoldCFExceptionLeave a named, exclusive critical section (NOT a lock). Critical sections never cross JVM boundaries. Critical section names should be distinct from all lock names.- Specified by:
leaveWriteCriticalSectionin interfaceILockManager- Parameters:
sectionKey- is the name of the section to leave. Only one thread can be in any given named section at a time.- Throws:
ManifoldCFException
-
enterCriticalSections
public void enterCriticalSections(java.lang.String[] readSectionKeys, java.lang.String[] nonExSectionKeys, java.lang.String[] writeSectionKeys) throws ManifoldCFExceptionEnter multiple critical sections simultaneously.- Specified by:
enterCriticalSectionsin interfaceILockManager- Parameters:
readSectionKeys- is an array of read section descriptors, or null if there are no read sections desired.nonExSectionKeys- is an array of non-ex write section descriptors, or null if none desired.writeSectionKeys- is an array of write section descriptors, or null if there are none desired.- Throws:
ManifoldCFException
-
leaveCriticalSections
public void leaveCriticalSections(java.lang.String[] readSectionKeys, java.lang.String[] nonExSectionKeys, java.lang.String[] writeSectionKeys) throws ManifoldCFExceptionLeave multiple critical sections simultaneously.- Specified by:
leaveCriticalSectionsin interfaceILockManager- Parameters:
readSectionKeys- is an array of read section descriptors, or null if there are no read sections desired.nonExSectionKeys- is an array of non-ex write section descriptors, or null if none desired.writeSectionKeys- is an array of write section descriptors, or null if there are none desired.- Throws:
ManifoldCFException
-
-