use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project vespa by vespa-engine.
the class ZookeeperStatusService method acquireMutexOrThrow.
private InterProcessSemaphoreMutex acquireMutexOrThrow(long timeout, TimeUnit timeoutTimeUnit, String lockPath) throws Exception {
InterProcessSemaphoreMutex mutex = new InterProcessSemaphoreMutex(curator.framework(), lockPath);
log.log(LogLevel.DEBUG, "Waiting for lock on " + lockPath);
boolean acquired = mutex.acquire(timeout, timeoutTimeUnit);
if (!acquired) {
log.log(LogLevel.DEBUG, "Timed out waiting for lock on " + lockPath);
throw new TimeoutException("Timed out waiting for lock on " + lockPath);
}
log.log(LogLevel.DEBUG, "Successfully acquired lock on " + lockPath);
return mutex;
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project Mycat-Server by MyCATApache.
the class IncrSequenceZKHandler method fetchNextPeriod.
@Override
public Boolean fetchNextPeriod(String prefixName) {
InterProcessSemaphoreMutex interProcessSemaphoreMutex = interProcessSemaphoreMutexThreadLocal.get();
try {
if (interProcessSemaphoreMutex == null) {
throw new IllegalStateException("IncrSequenceZKHandler should be loaded first!");
}
interProcessSemaphoreMutex.acquire();
Map<String, Map<String, String>> tableParaValMap = tableParaValMapThreadLocal.get();
if (tableParaValMap == null) {
throw new IllegalStateException("IncrSequenceZKHandler should be loaded first!");
}
Map<String, String> paraValMap = tableParaValMap.get(prefixName);
if (paraValMap == null) {
throw new IllegalStateException("IncrSequenceZKHandler should be loaded first!");
}
if (paraValMap.get(prefixName + KEY_MAX_NAME) == null) {
paraValMap.put(prefixName + KEY_MAX_NAME, props.getProperty(prefixName + KEY_MAX_NAME));
}
if (paraValMap.get(prefixName + KEY_MIN_NAME) == null) {
paraValMap.put(prefixName + KEY_MIN_NAME, props.getProperty(prefixName + KEY_MIN_NAME));
}
if (paraValMap.get(prefixName + KEY_CUR_NAME) == null) {
paraValMap.put(prefixName + KEY_CUR_NAME, props.getProperty(prefixName + KEY_CUR_NAME));
}
long period = Long.parseLong(paraValMap.get(prefixName + KEY_MAX_NAME)) - Long.parseLong(paraValMap.get(prefixName + KEY_MIN_NAME));
long now = Long.parseLong(new String(client.getData().forPath(PATH + "/" + prefixName + SEQ)));
client.setData().forPath(PATH + "/" + prefixName + SEQ, ((now + period + 1) + "").getBytes());
paraValMap.put(prefixName + KEY_MAX_NAME, (now + period + 1) + "");
paraValMap.put(prefixName + KEY_MIN_NAME, (now + 1) + "");
paraValMap.put(prefixName + KEY_CUR_NAME, (now) + "");
} catch (Exception e) {
LOGGER.error("Error caught while updating period from ZK:" + e.getCause());
} finally {
try {
interProcessSemaphoreMutex.release();
} catch (Exception e) {
LOGGER.error("Error caught while realeasing distributed lock" + e.getCause());
}
}
return true;
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project coprhd-controller by CoprHD.
the class CoordinatorClientImpl method getSemaphoreLock.
@Override
public InterProcessSemaphoreMutex getSemaphoreLock(String name) throws CoordinatorException {
EnsurePath path = new EnsurePath(ZkPath.MUTEX.toString());
try {
path.ensure(_zkConnection.curator().getZookeeperClient());
} catch (Exception e) {
throw new RetryableCoordinatorException(ServiceCode.COORDINATOR_SVC_NOT_FOUND, e, "Unable to get lock {0}. Caused by: {1}", new Object[] { name, e.getMessage() });
}
String lockPath = ZKPaths.makePath(ZkPath.MUTEX.toString(), name);
return new InterProcessSemaphoreMutex(_zkConnection.curator(), lockPath);
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project Mycat_plus by coderczp.
the class IncrSequenceZKHandler method fetchNextPeriod.
@Override
public Boolean fetchNextPeriod(String prefixName) {
InterProcessSemaphoreMutex interProcessSemaphoreMutex = interProcessSemaphoreMutexThreadLocal.get();
try {
if (interProcessSemaphoreMutex == null) {
throw new IllegalStateException("IncrSequenceZKHandler should be loaded first!");
}
interProcessSemaphoreMutex.acquire();
Map<String, Map<String, String>> tableParaValMap = tableParaValMapThreadLocal.get();
if (tableParaValMap == null) {
throw new IllegalStateException("IncrSequenceZKHandler should be loaded first!");
}
Map<String, String> paraValMap = tableParaValMap.get(prefixName);
if (paraValMap == null) {
throw new IllegalStateException("IncrSequenceZKHandler should be loaded first!");
}
if (paraValMap.get(prefixName + KEY_MAX_NAME) == null) {
paraValMap.put(prefixName + KEY_MAX_NAME, props.getProperty(prefixName + KEY_MAX_NAME));
}
if (paraValMap.get(prefixName + KEY_MIN_NAME) == null) {
paraValMap.put(prefixName + KEY_MIN_NAME, props.getProperty(prefixName + KEY_MIN_NAME));
}
if (paraValMap.get(prefixName + KEY_CUR_NAME) == null) {
paraValMap.put(prefixName + KEY_CUR_NAME, props.getProperty(prefixName + KEY_CUR_NAME));
}
long period = Long.parseLong(paraValMap.get(prefixName + KEY_MAX_NAME)) - Long.parseLong(paraValMap.get(prefixName + KEY_MIN_NAME));
long now = Long.parseLong(new String(client.getData().forPath(PATH + "/" + prefixName + SEQ)));
client.setData().forPath(PATH + "/" + prefixName + SEQ, ((now + period + 1) + "").getBytes());
paraValMap.put(prefixName + KEY_MAX_NAME, (now + period + 1) + "");
paraValMap.put(prefixName + KEY_MIN_NAME, (now + 1) + "");
paraValMap.put(prefixName + KEY_CUR_NAME, (now) + "");
} catch (Exception e) {
LOGGER.error("Error caught while updating period from ZK:" + e.getCause());
} finally {
try {
interProcessSemaphoreMutex.release();
} catch (Exception e) {
LOGGER.error("Error caught while realeasing distributed lock" + e.getCause());
}
}
return true;
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project dcos-commons by mesosphere.
the class CuratorLocker method lockInternal.
/**
* Gets an exclusive lock on service-specific ZK node to ensure two schedulers aren't running simultaneously for the
* same service.
*/
@VisibleForTesting
void lockInternal() {
if (curatorClient != null) {
throw new IllegalStateException("Already locked");
}
curatorClient = CuratorFrameworkFactory.newClient(zookeeperHostPort, CuratorUtils.getDefaultRetry());
curatorClient.start();
final String lockPath = PersisterUtils.join(CuratorUtils.getServiceRootPath(serviceName), LOCK_PATH_NAME);
InterProcessSemaphoreMutex curatorMutex = new InterProcessSemaphoreMutex(curatorClient, lockPath);
LOGGER.info("Acquiring ZK lock on {}...", lockPath);
final String failureLogMsg = String.format("Failed to acquire ZK lock on %s. " + "Duplicate service named '%s', or recently restarted instance of '%s'?", lockPath, serviceName, serviceName);
try {
// Start at 1 for pretty display of "1/3" through "3/3":
for (int attempt = 1; attempt < LOCK_ATTEMPTS + 1; ++attempt) {
if (curatorMutex.acquire(10, getWaitTimeUnit())) {
LOGGER.info("{}/{} Lock acquired.", attempt, LOCK_ATTEMPTS);
this.curatorMutex = curatorMutex;
return;
}
if (attempt < LOCK_ATTEMPTS) {
LOGGER.error("{}/{} {} Retrying lock...", attempt, LOCK_ATTEMPTS, failureLogMsg);
}
}
LOGGER.error(failureLogMsg + " Restarting scheduler process to try again.");
} catch (Exception ex) {
LOGGER.error(String.format("Error acquiring ZK lock on path: %s", lockPath), ex);
}
curatorClient = null;
exit();
}
Aggregations