Search in sources :

Example 11 with InterProcessSemaphoreMutex

use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project Mycat-Server by MyCATApache.

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

Example 12 with InterProcessSemaphoreMutex

use of org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex in project tutorials by eugenp.

the class RecipesManualTest method givenRunningZookeeper_whenUsingSharedLock_thenNoErrors.

@Test
public void givenRunningZookeeper_whenUsingSharedLock_thenNoErrors() throws Exception {
    try (CuratorFramework client = newClient()) {
        client.start();
        InterProcessSemaphoreMutex sharedLock = new InterProcessSemaphoreMutex(client, "/mutex/process/A");
        sharedLock.acquire();
        // Do process A
        sharedLock.release();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) BaseTest(com.baeldung.apache.curator.BaseTest) Test(org.junit.Test)

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