use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class DrUtil method getDROperationLock.
/**
* @param checkAllSitesOperations
* @return DR operation lock only when successfully acquired lock and there's no ongoing DR operation, throw Exception otherwise
*/
public InterProcessLock getDROperationLock(boolean checkAllSitesOperations) {
// Try to acquire lock, succeed or throw Exception
InterProcessLock lock = coordinator.getLock(DR_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 DR operation lock", ex);
}
throw APIException.internalServerErrors.failToAcquireDROperationLock();
}
if (!acquired) {
throw APIException.internalServerErrors.failToAcquireDROperationLock();
}
// Has successfully acquired lock
if (!checkAllSitesOperations) {
return lock;
}
// Check if there's ongoing DR operation on any site, if there is, release lock and throw exception
Site ongoingSite = null;
List<Site> sites = listSites();
for (Site site : sites) {
if (site.getState().isDROperationOngoing()) {
ongoingSite = site;
break;
}
}
if (ongoingSite != null) {
try {
lock.release();
} catch (Exception e) {
log.error("Fail to release DR operation lock", e);
}
throw APIException.internalServerErrors.concurrentDROperationNotAllowed(ongoingSite.getName(), ongoingSite.getState().toString());
}
return lock;
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class CoordinatorClientImpl method loadInetAddressFromCoordinator.
/**
* Try to look up the node in zk configuration - this is called ONLY if map does not have it.
*
* @param nodeId
* the node in the lookup, any node
* @return DualInetAddress of the node
*/
public DualInetAddress loadInetAddressFromCoordinator(String nodeId) {
DualInetAddress dualAddress = null;
// grab the lock
InterProcessLock lock = null;
try {
lock = getLock(NODE_DUALINETADDR_CONFIG + nodeId);
lock.acquire();
Configuration config = queryConfiguration(Constants.NODE_DUALINETADDR_CONFIG, nodeId);
if (config != null) {
dualAddress = parseInetAddressConfig(config);
}
} catch (Exception e) {
log.warn("Unexpected exception during loadInetAddressFromCoordinator()", e);
} finally {
if (lock != null) {
try {
lock.release();
} catch (Exception e) {
log.warn("Unexpected exception unlocking loadInetAddressFromCoordinator()", e);
}
}
}
// Add it to the map
if (dualAddress != null) {
inetAddressLookupMap.put(nodeId, dualAddress);
}
return dualAddress;
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class EncryptionProviderImpl method restoreKey.
public void restoreKey(SecretKey key) throws Exception {
InterProcessLock lock = null;
try {
lock = _coordinator.getLock(CONFIG_KIND);
lock.acquire();
persistKey(key);
} finally {
if (lock != null) {
lock.release();
}
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class EncryptionProviderImpl method cacheKey.
/**
* Reads existing encryption key from coordinator and caches it
*
* @throws Exception
*/
private synchronized void cacheKey() throws Exception {
Configuration config = _coordinator.queryConfiguration(CONFIG_KIND, _encryptId);
if (config != null) {
readKey(config);
} else {
// key has not been generated
InterProcessLock lock = null;
try {
lock = _coordinator.getLock(CONFIG_KIND);
lock.acquire();
config = _coordinator.queryConfiguration(CONFIG_KIND, _encryptId);
if (config == null) {
_logger.warn("Encryption key not found, initializing it");
generateKey();
} else {
readKey(config);
}
} finally {
if (lock != null) {
lock.release();
}
}
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessLock in project coprhd-controller by CoprHD.
the class BackupOps method updateRestoreStatus.
public void updateRestoreStatus(String backupName, boolean isLocal, BackupRestoreStatus.Status newStatus, String details, Map<String, Long> downloadSize, long increasedSize, boolean increaseCompletedNodeNumber, List<String> backupfileNames, boolean doLog, boolean doLock) {
InterProcessLock lock = null;
try {
if (doLock) {
lock = getLock(BackupConstants.RESTORE_STATUS_UPDATE_LOCK, -1, // -1= no timeout
TimeUnit.MILLISECONDS);
if (doLog) {
log.info("get lock {}", BackupConstants.RESTORE_STATUS_UPDATE_LOCK);
}
}
BackupRestoreStatus currentStatus = queryBackupRestoreStatus(backupName, isLocal);
if (!canBeUpdated(newStatus, currentStatus)) {
log.info("Can't update the status from {} to {}", currentStatus, newStatus);
return;
}
currentStatus.setBackupName(backupName);
if (newStatus != null) {
currentStatus.setStatusWithDetails(newStatus, details);
}
if (downloadSize != null) {
currentStatus.setSizeToDownload(downloadSize);
}
if (increasedSize > 0) {
String localHostID = getLocalHostID();
currentStatus.increaseDownloadedSize(localHostID, increasedSize);
}
if (increaseCompletedNodeNumber) {
currentStatus.increaseNodeCompleted();
}
if (backupfileNames != null) {
currentStatus.setBackupFileNames(backupfileNames);
}
updateBackupRestoreStatus(currentStatus, backupName);
persistBackupRestoreStatus(currentStatus, isLocal, doLog);
if (doLog) {
log.info("Persist backup restore newStatus {} to zk successfully", currentStatus);
}
} finally {
if (doLock) {
if (doLog) {
log.info("To release lock {}", BackupConstants.RESTORE_STATUS_UPDATE_LOCK);
}
releaseLock(lock);
}
}
}
Aggregations