Search in sources :

Example 1 with InterProcessSemaphoreMutex

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);
    }
}
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 2 with InterProcessSemaphoreMutex

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);
    }
}
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)

Example 3 with InterProcessSemaphoreMutex

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

Example 4 with InterProcessSemaphoreMutex

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);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 5 with InterProcessSemaphoreMutex

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

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