use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project Mycat_plus by coderczp.
the class IncrSequenceZKHandler method handle.
private void handle(String key) throws Exception {
String table = key.substring(0, key.indexOf(KEY_MIN_NAME));
InterProcessSemaphoreMutex interProcessSemaphoreMutex = interProcessSemaphoreMutexThreadLocal.get();
if (interProcessSemaphoreMutex == null) {
interProcessSemaphoreMutex = new InterProcessSemaphoreMutex(client, PATH + "/" + table + SEQ + LOCK);
interProcessSemaphoreMutexThreadLocal.set(interProcessSemaphoreMutex);
}
Map<String, Map<String, String>> tableParaValMap = tableParaValMapThreadLocal.get();
if (tableParaValMap == null) {
tableParaValMap = new HashMap<>();
tableParaValMapThreadLocal.set(tableParaValMap);
}
Map<String, String> paraValMap = tableParaValMap.get(table);
if (paraValMap == null) {
paraValMap = new ConcurrentHashMap<>();
tableParaValMap.put(table, paraValMap);
String seqPath = PATH + ZookeeperPath.ZK_SEPARATOR.getKey() + table + SEQ;
Stat stat = this.client.checkExists().forPath(seqPath);
if (stat == null || (stat.getDataLength() == 0)) {
paraValMap.put(table + KEY_MIN_NAME, props.getProperty(key));
paraValMap.put(table + KEY_MAX_NAME, props.getProperty(table + KEY_MAX_NAME));
paraValMap.put(table + KEY_CUR_NAME, props.getProperty(table + KEY_CUR_NAME));
try {
String val = props.getProperty(table + KEY_MIN_NAME);
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(PATH + "/" + table + SEQ, val.getBytes());
} catch (Exception e) {
LOGGER.debug("Node exists! Maybe other instance is initializing!");
}
}
fetchNextPeriod(table);
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project dble by actiontech.
the class IncrSequenceZKHandler method handle.
private void handle(String key) throws Exception {
String table = key.substring(0, key.indexOf(KEY_MIN_NAME));
InterProcessSemaphoreMutex interProcessSemaphoreMutex = interProcessSemaphoreMutexThreadLocal.get();
if (interProcessSemaphoreMutex == null) {
interProcessSemaphoreMutex = new InterProcessSemaphoreMutex(client, PATH + table + SEQ + LOCK);
interProcessSemaphoreMutexThreadLocal.set(interProcessSemaphoreMutex);
}
Map<String, Map<String, String>> tableParaValMap = tableParaValMapThreadLocal.get();
if (tableParaValMap == null) {
tableParaValMap = new HashMap<>();
tableParaValMapThreadLocal.set(tableParaValMap);
}
Map<String, String> paraValMap = tableParaValMap.get(table);
if (paraValMap == null) {
paraValMap = new ConcurrentHashMap<>();
tableParaValMap.put(table, paraValMap);
String seqPath = PATH + table + SEQ;
Stat stat = this.client.checkExists().forPath(seqPath);
if (stat == null || (stat.getDataLength() == 0)) {
paraValMap.put(table + KEY_MIN_NAME, props.getProperty(key));
paraValMap.put(table + KEY_MAX_NAME, props.getProperty(table + KEY_MAX_NAME));
paraValMap.put(table + KEY_CUR_NAME, props.getProperty(table + KEY_CUR_NAME));
try {
String val = props.getProperty(table + KEY_MIN_NAME);
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(PATH + table + SEQ, val.getBytes());
} catch (Exception e) {
LOGGER.debug("Node exists! Maybe other instance is initializing!");
}
}
fetchNextPeriod(table);
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project dble by actiontech.
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();
try {
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_MIN_NAME, (now) + "");
paraValMap.put(prefixName + KEY_MAX_NAME, (now + period) + "");
paraValMap.put(prefixName + KEY_CUR_NAME, (now) - 1 + "");
} catch (Exception e) {
LOGGER.info("Error caught while updating period from ZK:" + e.getCause());
} finally {
interProcessSemaphoreMutex.release();
}
} catch (Exception e) {
LOGGER.error("Error caught while use distributed lock:" + e.getCause());
}
return true;
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project nakadi by zalando.
the class AbstractZkSubscriptionClient method runLocked.
@Override
public final <T> T runLocked(final Callable<T> function) {
try {
Exception releaseException = null;
if (null == lock) {
lock = new InterProcessSemaphoreMutex(curatorFramework, "/nakadi/locks/subscription_" + subscriptionId);
}
final boolean acquired = lock.acquire(SECONDS_TO_WAIT_FOR_LOCK, TimeUnit.SECONDS);
if (!acquired) {
throw new ServiceUnavailableException("Failed to acquire subscription lock within " + SECONDS_TO_WAIT_FOR_LOCK + " seconds");
}
final T result;
try {
result = function.call();
} finally {
try {
lock.release();
} catch (final Exception e) {
log.error("Failed to release lock", e);
releaseException = e;
}
}
if (releaseException != null) {
throw releaseException;
}
return result;
} catch (final NakadiRuntimeException | MyNakadiRuntimeException1 e) {
throw e;
} catch (final Exception e) {
throw new NakadiRuntimeException(e);
}
}
use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project nakadi by zalando.
the class TimelineSyncImpl method runLocked.
private void runLocked(final Runnable action) {
try {
Exception releaseException = null;
final InterProcessLock lock = new InterProcessSemaphoreMutex(zooKeeperHolder.get(), ROOT_PATH + "/lock");
lock.acquire();
try {
action.run();
} finally {
try {
lock.release();
} catch (final Exception ex) {
LOG.error("Failed to release lock", ex);
releaseException = ex;
}
}
if (null != releaseException) {
throw releaseException;
}
} catch (final RuntimeException ex) {
throw ex;
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
}
Aggregations