use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class StorageDriverService method getStorageDriverOperationLock.
protected InterProcessLock getStorageDriverOperationLock() {
// Try to acquire lock, succeed or throw Exception
InterProcessLock lock = coordinator.getSiteLocalLock(STORAGE_DRIVER_OPERATION_lOCK);
boolean acquired;
try {
acquired = lock.acquire(LOCK_WAIT_TIME_SEC, TimeUnit.SECONDS);
} catch (Exception e) {
try {
lock.release();
} catch (Exception ex) {
log.error("Fail to release storage driver operation lock", ex);
}
throw APIException.internalServerErrors.installDriverPrecheckFailed("Acquiring lock failed, there's another trigger operation holding lock");
}
if (!acquired) {
throw APIException.internalServerErrors.installDriverPrecheckFailed("Acquiring lock failed, there's another trigger operation holding lock");
}
// Check if there's ongoing storage operation, if there is, release lock
// and throw exception
StorageSystemType opOngoingStorageType = null;
List<StorageSystemType> types = listStorageSystemTypes();
for (StorageSystemType type : types) {
String statusStr = type.getDriverStatus();
if (statusStr == null) {
log.info("Bypass type {} as it has no status field value", type.getStorageTypeName());
continue;
}
StorageSystemType.STATUS status = Enum.valueOf(StorageSystemType.STATUS.class, type.getDriverStatus());
if (status.isStorageOperationOngoing()) {
opOngoingStorageType = type;
break;
}
}
if (opOngoingStorageType != null) {
try {
lock.release();
} catch (Exception e) {
log.error("Fail to release storage driver operation lock", e);
}
throw APIException.internalServerErrors.installDriverPrecheckFailed(String.format("Driver %s is in % state", opOngoingStorageType.getDriverName(), opOngoingStorageType.getDriverStatus()));
}
return lock;
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class InterVDCTokenCacheHelper method saveTokenKeysBundle.
/**
* Stores the token key bundle in cache (zookeeper path based on vdcid)
* Locks on vdcid for the write.
*
* @param vdcID
* @param bundle
*/
private synchronized void saveTokenKeysBundle(String vdcID, TokenKeysBundle bundle) {
InterProcessLock tokenBundleLock = null;
try {
tokenBundleLock = coordinator.getLock(FOREIGN_TOKEN_BUNDLE_CONFIG_LOCK);
if (tokenBundleLock == null) {
log.error("Could not acquire lock for tokenkeys bundle caching");
throw SecurityException.fatals.couldNotAcquireLockTokenCaching();
}
tokenBundleLock.acquire();
Configuration config = coordinator.queryConfiguration(FOREIGN_TOKEN_KEYS_BUNDLE_CONFIG, FOREIGN_TOKEN_KEYS_BUNDLE_KEYID);
ConfigurationImpl configImpl = null;
if (config == null) {
configImpl = new ConfigurationImpl();
configImpl.setId(FOREIGN_TOKEN_KEYS_BUNDLE_KEYID);
configImpl.setKind(FOREIGN_TOKEN_KEYS_BUNDLE_CONFIG);
log.debug("Creating new foreign tokens config");
} else {
configImpl = (ConfigurationImpl) config;
log.debug("Updating existing foreign token config");
}
configImpl.setConfig(vdcID, SerializerUtils.serializeAsBase64EncodedString(bundle));
coordinator.persistServiceConfiguration(configImpl);
foreignTokenKeysMap.put(vdcID, bundle);
} catch (Exception ex) {
log.error("Could not acquire lock while trying to cache tokenkeys bundle.", ex);
} finally {
try {
if (tokenBundleLock != null) {
tokenBundleLock.release();
}
} catch (Exception ex) {
log.error("Unable to release token keys bundle caching lock", ex);
}
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class InterVDCTokenCacheHelper method cacheForeignTokenArtifacts.
/**
* Saves the token and user dao records to the db. Set the cache expiration time
* to 10 minutes or time left on the token, whichever is sooner.
* Note: this method assumes validity of the token (expiration) has been checked
*
* @param t
* @param user
* @param now current time in minutes
*/
private synchronized void cacheForeignTokenArtifacts(final Token token, final StorageOSUserDAO user) {
long now = System.currentTimeMillis() / (MIN_TO_MSECS);
InterProcessLock tokenLock = null;
try {
tokenLock = coordinator.getLock(token.getId().toString());
if (tokenLock == null) {
log.error("Could not acquire lock for token caching");
throw SecurityException.fatals.couldNotAcquireLockTokenCaching();
}
tokenLock.acquire();
StorageOSUserDAO userToPersist = dbClient.queryObject(StorageOSUserDAO.class, user.getId());
userToPersist = (userToPersist == null) ? new StorageOSUserDAO() : userToPersist;
userToPersist.setAttributes(user.getAttributes());
userToPersist.setCreationTime(user.getCreationTime());
userToPersist.setDistinguishedName(user.getDistinguishedName());
userToPersist.setGroups(user.getGroups());
userToPersist.setId(user.getId());
userToPersist.setIsLocal(user.getIsLocal());
userToPersist.setTenantId(user.getTenantId());
userToPersist.setUserName(user.getUserName());
dbClient.persistObject(userToPersist);
Token tokenToPersist = dbClient.queryObject(Token.class, token.getId());
tokenToPersist = (tokenToPersist == null) ? new Token() : tokenToPersist;
if ((token.getExpirationTime() - now) > maxLifeValuesHolder.getForeignTokenCacheExpirationInMins()) {
tokenToPersist.setCacheExpirationTime(now + maxLifeValuesHolder.getForeignTokenCacheExpirationInMins());
} else {
tokenToPersist.setCacheExpirationTime(token.getExpirationTime());
}
tokenToPersist.setId(token.getId());
// relative index, Id of the userDAO record
tokenToPersist.setUserId(user.getId());
tokenToPersist.setIssuedTime(token.getIssuedTime());
tokenToPersist.setLastAccessTime(now);
tokenToPersist.setExpirationTime(token.getExpirationTime());
tokenToPersist.setIndexed(true);
tokenToPersist.setZoneId(token.getZoneId());
dbClient.persistObject(tokenToPersist);
log.info("Cached user {} and token", user.getUserName());
} catch (Exception ex) {
log.error("Could not acquire lock while trying to get a proxy token.", ex);
} finally {
try {
if (tokenLock != null) {
tokenLock.release();
}
} catch (Exception ex) {
log.error("Unable to release token caching lock", ex);
}
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class CoordinatorConfigStoringHelper method removeConfig.
/**
* Removes the specified config from coordinator. If siteId is not null, config
* is in global area. Otherwise it is in site specific area
*
* @param lockName
* the name of the lock to use while removing this object
* @param configKInd
* @param configId
* @throws Exception
*/
public void removeConfig(String lockName, String siteId, String configKInd, String configId) throws Exception {
InterProcessLock lock = acquireLock(lockName);
try {
Configuration config = coordinator.queryConfiguration(siteId, configKInd, configId);
if (config != null) {
coordinator.removeServiceConfiguration(siteId, config);
log.debug("removed config successfully");
} else {
log.debug("config " + configId + " of kind " + configKInd + " was not removed since it could not be found");
}
} finally {
releaseLock(lock);
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class CoordinatorConfigStoringHelper method acquireLock.
/**
* Acquires an interprocess lock
*
* @param lockName
* the lock to acquire
* @return the acquired lock
* @throws Exception
* if failed to acquire the lock
*/
public synchronized InterProcessLock acquireLock(String lockName) throws Exception {
InterProcessLock lock = nameLockMap.get(lockName);
if (lock == null) {
lock = coordinator.getSiteLocalLock(lockName);
nameLockMap.put(lockName, lock);
}
lock.acquire();
log.info("Acquired the lock {}", lockName);
return lock;
}
Aggregations