Search in sources :

Example 6 with InterProcessSemaphoreMutex

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;
}
Also used : InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with InterProcessSemaphoreMutex

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;
}
Also used : InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with InterProcessSemaphoreMutex

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);
}
Also used : EnsurePath(org.apache.curator.utils.EnsurePath) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) RetryableCoordinatorException(com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException) PropertyInfoMapper.decodeFromString(com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException) RetryableCoordinatorException(com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 9 with InterProcessSemaphoreMutex

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;
}
Also used : InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with InterProcessSemaphoreMutex

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();
}
Also used : InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

InterProcessSemaphoreMutex (org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex)12 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 KeeperException (org.apache.zookeeper.KeeperException)3 Stat (org.apache.zookeeper.data.Stat)3 IOException (java.io.IOException)2 TimeoutException (java.util.concurrent.TimeoutException)2 BaseTest (com.baeldung.apache.curator.BaseTest)1 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)1 RetryableCoordinatorException (com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException)1 PropertyInfoMapper.decodeFromString (com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 UnknownHostException (java.net.UnknownHostException)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 InterProcessLock (org.apache.curator.framework.recipes.locks.InterProcessLock)1 EnsurePath (org.apache.curator.utils.EnsurePath)1 Test (org.junit.Test)1 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)1