Class ZooKeeperLockManager
- java.lang.Object
-
- org.apache.manifoldcf.core.lockmanager.BaseLockManager
-
- org.apache.manifoldcf.core.lockmanager.ZooKeeperLockManager
-
- All Implemented Interfaces:
ILockManager
public class ZooKeeperLockManager extends BaseLockManager implements ILockManager
The lock manager manages locks across all threads and JVMs and cluster members, using Zookeeper. There should be no more than ONE instance of this class per thread!!! The factory should enforce this.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classZooKeeperLockManager.ZooKeeperShutdown-
Nested classes/interfaces inherited from class org.apache.manifoldcf.core.lockmanager.BaseLockManager
BaseLockManager.LockDescription
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String_rcsidprotected static java.lang.StringanonymousServiceNamePrefixAnonymous service name prefix, to be followed by an integerprotected ManifoldCFConfigurationcachedConfigurationprotected static java.lang.IntegerconnectionPoolLockprotected static java.lang.IntegerephemeralPoolLockerprotected static ZooKeeperEphemeralNodePoolmyEphemeralNodesprotected static LockPoolmyZooKeeperLocksprotected static ZooKeeperConnectionPoolpoolprotected static java.lang.StringzookeeperConnectStringParameterprotected static java.lang.IntegerzookeeperPoolLockerprotected static java.lang.StringzookeeperSessionTimeoutParameter-
Fields inherited from class org.apache.manifoldcf.core.lockmanager.BaseLockManager
activePrefix, anonymousServiceTypeCounter, globalData, globalFlags, localLocks, localSections, myLocks, mySections, serviceDataPrefix, serviceListPrefix, servicePrefix, serviceTypeLockPrefix, threadID, TYPE_READ, TYPE_WRITE, TYPE_WRITENONEX
-
-
Constructor Summary
Constructors Constructor Description ZooKeeperLockManager()Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static java.lang.StringbuildServiceTypeActivePath(java.lang.String serviceType, java.lang.String encodedServiceName)Build a zk path for the active node for a specific service of a specific type.protected static java.lang.StringbuildServiceTypeLockPath(java.lang.String serviceType)Build a zk path for the lock for a specific service type.protected static java.lang.StringbuildServiceTypeRegistrationPath(java.lang.String serviceType)Build a zk path for the registration node for a specific service type.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.protected java.lang.StringconstructUniqueServiceName(ZooKeeperConnection connection, java.lang.String serviceType)Construct a unique service name given the service type.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.protected voidenterServiceRegistryReadLock(ZooKeeperConnection connection, java.lang.String serviceType)Enter service registry read lockprotected voidenterServiceRegistryWriteLock(ZooKeeperConnection connection, java.lang.String serviceType)Enter service registry write lockprotected LockPoolgetGlobalLockPool()Override this method to change the nature of global locks.ManifoldCFConfigurationgetSharedConfiguration()Get the current shared configuration.protected voidleaveServiceRegistryLock(ZooKeeperConnection connection)Leave service registry lockstatic voidmain(java.lang.String[] argv)protected static java.lang.StringmakeServiceCounterName(java.lang.String serviceType)Make the service counter name for a service type.byte[]readData(java.lang.String resourceName)Read data from a shared data resource.protected intreadServiceCounter(ZooKeeperConnection connection, java.lang.String serviceCounterName)Read service counter.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.voidsetSharedConfiguration(java.io.InputStream configurationInputStream)Write shared configuration.protected static voidshutdownPool()Shutdown the connection pool.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.protected voidwriteServiceCounter(ZooKeeperConnection connection, java.lang.String serviceCounterName, int counter)Write service counter.-
Methods inherited from class org.apache.manifoldcf.core.lockmanager.BaseLockManager
buildServiceListEntry, buildServiceTypeLockName, clear, clearLocks, constructUniqueServiceName, deleteServiceData, enter, enterCriticalSections, enterLocks, enterLocksNoWait, enterNonExWrite, enterNonExWriteCriticalSection, enterNonExWriteLock, enterNonExWriteLockNoWait, enterNonExWriteNoWait, enterNoWait, enterRead, enterReadCriticalSection, enterReadLock, enterReadLockNoWait, enterReadNoWait, enterWrite, enterWriteCriticalSection, enterWriteLock, enterWriteLockNoWait, enterWriteNoWait, getSortedUniqueLocks, leave, leaveCriticalSections, leaveLocks, leaveNonExWrite, leaveNonExWriteCriticalSection, leaveNonExWriteLock, leaveRead, leaveReadCriticalSection, leaveReadLock, leaveWrite, leaveWriteCriticalSection, leaveWriteLock, makeActiveServiceFlagName, makeRegisteredServiceFlagName, makeServiceDataName, readServiceCounter, readServiceData, readServiceName, timedWait, writeServiceCounter, writeServiceData, writeServiceName
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.manifoldcf.core.interfaces.ILockManager
clearLocks, enterCriticalSections, enterLocks, enterLocksNoWait, enterNonExWriteCriticalSection, enterNonExWriteLock, enterNonExWriteLockNoWait, enterReadCriticalSection, enterReadLock, enterReadLockNoWait, enterWriteCriticalSection, enterWriteLock, enterWriteLockNoWait, leaveCriticalSections, leaveLocks, leaveNonExWriteCriticalSection, leaveNonExWriteLock, leaveReadCriticalSection, leaveReadLock, leaveWriteCriticalSection, leaveWriteLock, timedWait
-
-
-
-
Field Detail
-
_rcsid
public static final java.lang.String _rcsid
- See Also:
- Constant Field Values
-
zookeeperConnectStringParameter
protected static final java.lang.String zookeeperConnectStringParameter
- See Also:
- Constant Field Values
-
zookeeperSessionTimeoutParameter
protected static final java.lang.String zookeeperSessionTimeoutParameter
- See Also:
- Constant Field Values
-
anonymousServiceNamePrefix
protected static final java.lang.String anonymousServiceNamePrefix
Anonymous service name prefix, to be followed by an integer- See Also:
- Constant Field Values
-
connectionPoolLock
protected static java.lang.Integer connectionPoolLock
-
pool
protected static ZooKeeperConnectionPool pool
-
zookeeperPoolLocker
protected static java.lang.Integer zookeeperPoolLocker
-
myZooKeeperLocks
protected static LockPool myZooKeeperLocks
-
ephemeralPoolLocker
protected static java.lang.Integer ephemeralPoolLocker
-
myEphemeralNodes
protected static ZooKeeperEphemeralNodePool myEphemeralNodes
-
cachedConfiguration
protected ManifoldCFConfiguration cachedConfiguration
-
-
Constructor Detail
-
ZooKeeperLockManager
public ZooKeeperLockManager() throws ManifoldCFExceptionConstructor- 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- Overrides:
registerServiceBeginServiceActivityin classBaseLockManager- 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- Overrides:
registerServiceBeginServiceActivityin classBaseLockManager- 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- Overrides:
updateServiceDatain classBaseLockManager- 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- Overrides:
retrieveServiceDatain classBaseLockManager- 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- Overrides:
scanServiceDatain classBaseLockManager- 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
-
countActiveServices
public int countActiveServices(java.lang.String serviceType) throws ManifoldCFExceptionCount all active services of a given type.- Specified by:
countActiveServicesin interfaceILockManager- Overrides:
countActiveServicesin classBaseLockManager- Parameters:
serviceType- is the service type.- Returns:
- the count.
- 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- Overrides:
cleanupInactiveServicein classBaseLockManager- 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
-
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- Overrides:
endServiceActivityin classBaseLockManager- 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- Overrides:
checkServiceActivein classBaseLockManager- 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
-
enterServiceRegistryReadLock
protected void enterServiceRegistryReadLock(ZooKeeperConnection connection, java.lang.String serviceType) throws ManifoldCFException, java.lang.InterruptedException
Enter service registry read lock- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
enterServiceRegistryWriteLock
protected void enterServiceRegistryWriteLock(ZooKeeperConnection connection, java.lang.String serviceType) throws ManifoldCFException, java.lang.InterruptedException
Enter service registry write lock- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
leaveServiceRegistryLock
protected void leaveServiceRegistryLock(ZooKeeperConnection connection) throws ManifoldCFException, java.lang.InterruptedException
Leave service registry lock- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
constructUniqueServiceName
protected java.lang.String constructUniqueServiceName(ZooKeeperConnection connection, java.lang.String serviceType) throws ManifoldCFException, java.lang.InterruptedException
Construct a unique service name given the service type.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
makeServiceCounterName
protected static java.lang.String makeServiceCounterName(java.lang.String serviceType)
Make the service counter name for a service type.
-
readServiceCounter
protected int readServiceCounter(ZooKeeperConnection connection, java.lang.String serviceCounterName) throws ManifoldCFException, java.lang.InterruptedException
Read service counter.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
writeServiceCounter
protected void writeServiceCounter(ZooKeeperConnection connection, java.lang.String serviceCounterName, int counter) throws ManifoldCFException, java.lang.InterruptedException
Write service counter.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
buildServiceTypeLockPath
protected static java.lang.String buildServiceTypeLockPath(java.lang.String serviceType)
Build a zk path for the lock for a specific service type.
-
buildServiceTypeActivePath
protected static java.lang.String buildServiceTypeActivePath(java.lang.String serviceType, java.lang.String encodedServiceName)Build a zk path for the active node for a specific service of a specific type.
-
buildServiceTypeRegistrationPath
protected static java.lang.String buildServiceTypeRegistrationPath(java.lang.String serviceType)
Build a zk path for the registration node for a specific service type.
-
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- Overrides:
getSharedConfigurationin classBaseLockManager- Throws:
ManifoldCFException
-
setSharedConfiguration
public void setSharedConfiguration(java.io.InputStream configurationInputStream) throws ManifoldCFExceptionWrite shared configuration. Caller closes the input stream.- 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- Overrides:
setGlobalFlagin classBaseLockManager- 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- Overrides:
clearGlobalFlagin classBaseLockManager- 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- Overrides:
checkGlobalFlagin classBaseLockManager- 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- Overrides:
readDatain classBaseLockManager- 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- Overrides:
writeDatain classBaseLockManager- 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
-
main
public static void main(java.lang.String[] argv)
-
getGlobalLockPool
protected LockPool getGlobalLockPool()
Override this method to change the nature of global locks.- Overrides:
getGlobalLockPoolin classBaseLockManager
-
shutdownPool
protected static void shutdownPool() throws ManifoldCFExceptionShutdown the connection pool.- Throws:
ManifoldCFException
-
-