Java Code Examples for org.apache.curator.framework.recipes.locks.InterProcessMutex#acquire()
The following examples show how to use
org.apache.curator.framework.recipes.locks.InterProcessMutex#acquire() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SetupSteps.java From atlas with Apache License 2.0 | 6 votes |
/** * Call each registered {@link SetupStep} one after the other. * @throws SetupException Thrown with any error during running setup, including Zookeeper interactions, and * individual failures in the {@link SetupStep}. */ @PostConstruct public void runSetup() throws SetupException { HAConfiguration.ZookeeperProperties zookeeperProperties = HAConfiguration.getZookeeperProperties(configuration); InterProcessMutex lock = curatorFactory.lockInstance(zookeeperProperties.getZkRoot()); try { LOG.info("Trying to acquire lock for running setup."); lock.acquire(); LOG.info("Acquired lock for running setup."); handleSetupInProgress(configuration, zookeeperProperties); for (SetupStep step : setupSteps) { LOG.info("Running setup step: {}", step); step.run(); } clearSetupInProgress(zookeeperProperties); } catch (SetupException se) { LOG.error("Got setup exception while trying to setup", se); throw se; } catch (Throwable e) { LOG.error("Error running setup steps", e); throw new SetupException("Error running setup steps", e); } finally { releaseLock(lock); curatorFactory.close(); } }
Example 2
Source File: SetupSteps.java From incubator-atlas with Apache License 2.0 | 6 votes |
/** * Call each registered {@link SetupStep} one after the other. * @throws SetupException Thrown with any error during running setup, including Zookeeper interactions, and * individual failures in the {@link SetupStep}. */ @PostConstruct public void runSetup() throws SetupException { HAConfiguration.ZookeeperProperties zookeeperProperties = HAConfiguration.getZookeeperProperties(configuration); InterProcessMutex lock = curatorFactory.lockInstance(zookeeperProperties.getZkRoot()); try { LOG.info("Trying to acquire lock for running setup."); lock.acquire(); LOG.info("Acquired lock for running setup."); handleSetupInProgress(configuration, zookeeperProperties); for (SetupStep step : setupSteps) { LOG.info("Running setup step: {}", step); step.run(); } clearSetupInProgress(zookeeperProperties); } catch (SetupException se) { LOG.error("Got setup exception while trying to setup", se); throw se; } catch (Throwable e) { LOG.error("Error running setup steps", e); throw new SetupException("Error running setup steps", e); } finally { releaseLock(lock); curatorFactory.close(); } }
Example 3
Source File: JobRunningZKStateManager.java From Eagle with Apache License 2.0 | 6 votes |
@Override public void truncateEverything() { String path = zkRoot; InterProcessMutex lock = new InterProcessMutex(_curator, path); try{ lock.acquire(); if(_curator.checkExists().forPath(path) != null){ _curator.delete().deletingChildrenIfNeeded().forPath(path); } }catch(Exception ex){ LOG.error("fail truncating verything", ex); throw new RuntimeException(ex); } finally { try{ lock.release(); }catch(Exception e){ LOG.error("fail releasing lock", e); throw new RuntimeException(e); } } }
Example 4
Source File: DataHostSwitch.java From dble with GNU General Public License v2.0 | 6 votes |
public static boolean switchWithZK(int id, PhysicalDataHost dh, String subHostName, ManagerConnection mc) { CuratorFramework zkConn = ZKUtils.getConnection(); InterProcessMutex distributeLock = new InterProcessMutex(zkConn, KVPathUtil.getHaLockPath(dh.getHostName())); try { try { if (!distributeLock.acquire(100, TimeUnit.MILLISECONDS)) { mc.writeErrMessage(ErrorCode.ER_YES, "Other instance is change the dataHost status"); return false; } String result = dh.switchMaster(subHostName, false); DataHostDisable.setStatusToZK(KVPathUtil.getHaStatusPath(dh.getHostName()), zkConn, result); HaConfigManager.getInstance().haFinish(id, null, result); } finally { distributeLock.release(); LOGGER.info("reload config: release distributeLock " + KVPathUtil.getConfChangeLockPath() + " from zk"); } } catch (Exception e) { LOGGER.info("reload config using ZK failure", e); mc.writeErrMessage(ErrorCode.ER_YES, e.getMessage()); HaConfigManager.getInstance().haFinish(id, e.getMessage(), null); return false; } return true; }
Example 5
Source File: CreateKeyspacesCommand.java From emodb with Apache License 2.0 | 6 votes |
private static void inMutex(CuratorFramework curator, String mutexPath, Runnable work) { final InterProcessMutex mutex = new InterProcessMutex(curator, mutexPath); try { // try to acquire mutex for index within flush period if (mutex.acquire(LOCK_ACQUIRE_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS)) { try { work.run(); } finally { mutex.release(); } } else { _log.warn("could not acquire index lock after {} millis!!", LOCK_ACQUIRE_TIMEOUT.toMillis()); } } catch (Exception e) { throw Throwables.propagate(e); } }
Example 6
Source File: SpringBootDemoZookeeperApplicationTests.java From spring-boot-demo with MIT License | 6 votes |
public void manualBuy() { String lockPath = "/buy"; log.info("try to buy sth."); try { InterProcessMutex lock = new InterProcessMutex(zkClient, lockPath); try { if (lock.acquire(1, TimeUnit.MINUTES)) { doBuy(); log.info("buy successfully!"); } } finally { lock.release(); } } catch (Exception e) { log.error("zk error"); } }
Example 7
Source File: ZooKeeperCommandExecutor.java From centraldogma with Apache License 2.0 | 6 votes |
private SafeLock safeLock(String executionPath) { final InterProcessMutex mtx = mutexMap.computeIfAbsent( executionPath, k -> new InterProcessMutex(curator, absolutePath(LOCK_PATH, executionPath))); try { mtx.acquire(); } catch (Exception e) { logger.error("Failed to acquire a lock for {}; entering read-only mode", executionPath, e); stopLater(); throw new ReplicationException("failed to acquire a lock for " + executionPath, e); } return () -> { try { mtx.release(); } catch (Exception ignored) { // Ignore. } }; }
Example 8
Source File: ZookeeperDistributedLock.java From micro-service with MIT License | 6 votes |
public static void main(String[] args) throws Exception { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy); client.start(); final InterProcessMutex mutex = new InterProcessMutex(client, "/curator/lock"); if (mutex.acquire(1, TimeUnit.MINUTES)) { try { // business logic } finally { mutex.release(); } } client.close(); }
Example 9
Source File: DistributedLockServiceCuratorImpl.java From micro-server with Apache License 2.0 | 6 votes |
@Override public boolean tryLock(String key) { try { InterProcessMutex mutex = locks.computeIfAbsent(key, __ -> new InterProcessMutex(curatorFramework, String.join("/", basePath, key))); boolean owned = mutex.isAcquiredInThisProcess(); if(owned) { return true; } else { mutex.acquire(timeout, TimeUnit.MILLISECONDS); } return mutex.isAcquiredInThisProcess(); } catch (Exception e) { return false; } }
Example 10
Source File: SpringBootDemoZookeeperApplicationTests.java From spring-boot-demo with MIT License | 6 votes |
public void manualBuy() { String lockPath = "/buy"; log.info("try to buy sth."); try { InterProcessMutex lock = new InterProcessMutex(zkClient, lockPath); try { if (lock.acquire(1, TimeUnit.MINUTES)) { doBuy(); log.info("buy successfully!"); } } finally { lock.release(); } } catch (Exception e) { log.error("zk error"); } }
Example 11
Source File: ZooLockAspect.java From spring-boot-demo with MIT License | 6 votes |
/** * 环绕操作 * * @param point 切入点 * @return 原方法返回值 * @throws Throwable 异常信息 */ @Around("doLock()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Object[] args = point.getArgs(); ZooLock zooLock = method.getAnnotation(ZooLock.class); if (StrUtil.isBlank(zooLock.key())) { throw new RuntimeException("分布式锁键不能为空"); } String lockKey = buildLockKey(zooLock, method, args); InterProcessMutex lock = new InterProcessMutex(zkClient, lockKey); try { // 假设上锁成功,以后拿到的都是 false if (lock.acquire(zooLock.timeout(), zooLock.timeUnit())) { return point.proceed(); } else { throw new RuntimeException("请勿重复提交"); } } finally { lock.release(); } }
Example 12
Source File: SpringBootDemoZookeeperApplicationTests.java From spring-boot-demo with MIT License | 6 votes |
public void manualBuy() { String lockPath = "/buy"; log.info("try to buy sth."); try { InterProcessMutex lock = new InterProcessMutex(zkClient, lockPath); try { if (lock.acquire(1, TimeUnit.MINUTES)) { doBuy(); log.info("buy successfully!"); } } finally { lock.release(); } } catch (Exception e) { log.error("zk error"); } }
Example 13
Source File: ZooLockAspect.java From spring-boot-demo with MIT License | 6 votes |
/** * 环绕操作 * * @param point 切入点 * @return 原方法返回值 * @throws Throwable 异常信息 */ @Around("doLock()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Object[] args = point.getArgs(); ZooLock zooLock = method.getAnnotation(ZooLock.class); if (StrUtil.isBlank(zooLock.key())) { throw new RuntimeException("分布式锁键不能为空"); } String lockKey = buildLockKey(zooLock, method, args); InterProcessMutex lock = new InterProcessMutex(zkClient, lockKey); try { // 假设上锁成功,以后拿到的都是 false if (lock.acquire(zooLock.timeout(), zooLock.timeUnit())) { return point.proceed(); } else { throw new RuntimeException("请勿重复提交"); } } finally { lock.release(); } }
Example 14
Source File: CuratorMutex.java From emodb with Apache License 2.0 | 5 votes |
private InterProcessMutex acquire(Duration acquireTimeout) { InterProcessMutex mutex = new InterProcessMutex(_curatorFramework, _path); try { if (!mutex.acquire(acquireTimeout.toMillis(), TimeUnit.MILLISECONDS)) { throw new TimeoutException(); } } catch (Exception e) { throw Throwables.propagate(e); } return mutex; }
Example 15
Source File: EtlLock.java From canal-1.1.3 with Apache License 2.0 | 5 votes |
public boolean tryLock(String key, long timeout, TimeUnit unit) { try { if (mode == Mode.LOCAL) { return getLock(key).tryLock(timeout, unit); } else { InterProcessMutex lock = getRemoteLock(key); return lock.acquire(timeout, unit); } } catch (Exception e) { return false; } }
Example 16
Source File: ZktoXmlMain.java From dble with GNU General Public License v2.0 | 5 votes |
private static void initZKIfNot(CuratorFramework zkConn) throws Exception { String confInited = KVPathUtil.getConfInitedPath(); //init conf if not if (zkConn.checkExists().forPath(confInited) == null) { InterProcessMutex confLock = new InterProcessMutex(zkConn, KVPathUtil.getConfInitLockPath()); //someone acquired the lock if (!confLock.acquire(100, TimeUnit.MILLISECONDS)) { //loop wait for initialized while (true) { if (!confLock.acquire(100, TimeUnit.MILLISECONDS)) { LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1000)); } else { try { if (zkConn.checkExists().forPath(confInited) == null) { XmltoZkMain.initFileToZK(); } break; } finally { confLock.release(); } } } } else { try { XmltoZkMain.initFileToZK(); } finally { confLock.release(); } } } }
Example 17
Source File: ZkAtom.java From Milkomeda with MIT License | 5 votes |
private AtomLockInfo sharedReentrantReadWriteLock(String keyPath, long time, TimeUnit unit, boolean readOnly) throws Exception { String lockPath = ROOT_LOCK_NODE + SEPARATOR + keyPath; InterProcessReadWriteLock rwLock = new InterProcessReadWriteLock(client, lockPath); InterProcessMutex lock = readOnly ? rwLock.readLock() : rwLock.writeLock(); boolean isLocked = lock.acquire(time, unit); return AtomLockInfo.builder().isLocked(isLocked).lock(lock).build(); }
Example 18
Source File: MetadataHelperUpdateHdfsListener.java From datawave with Apache License 2.0 | 4 votes |
private void maybeUpdateTypeMetadataInHdfs(final SharedCacheCoordinator watcher, String triStateName, String metadataTableName) throws Exception { boolean locked = false; InterProcessMutex lock = (InterProcessMutex) watcher.getMutex("lock"); try { locked = lock.acquire(this.lockWaitTime, TimeUnit.MILLISECONDS); if (!locked) log.debug("table:" + metadataTableName + " Unable to acquire lock to update " + metadataTableName + ". Another webserver is updating the typeMetadata."); else log.debug("table:" + metadataTableName + " Obtained lock on updateTypeMetadata for " + metadataTableName); } catch (Exception e) { log.warn("table:" + metadataTableName + " Got Exception trying to acquire lock to update " + metadataTableName + ".", e); } try { if (locked) { try { log.debug("table:" + metadataTableName + " checkTriState(" + triStateName + ", " + SharedTriState.STATE.NEEDS_UPDATE); if (watcher.checkTriState(triStateName, SharedTriState.STATE.NEEDS_UPDATE)) { if (log.isDebugEnabled()) { log.debug("table:" + metadataTableName + " " + this + " STATE is NEEDS_UPDATE. Will write the TypeMetadata map to hdfs"); } watcher.setTriState(triStateName, SharedTriState.STATE.UPDATING); if (log.isDebugEnabled()) { log.debug("table:" + metadataTableName + " " + this + " setTriState to UPDATING"); } // get a connection for my MetadataHelper, and get the TypeMetadata map ZooKeeperInstance instance = new ZooKeeperInstance(ClientConfiguration.loadDefault().withInstance(this.instance) .withZkHosts(this.zookeepers)); Connector connector = instance.getConnector(this.username, new PasswordToken(this.password)); TypeMetadataHelper typeMetadataHelper = this.typeMetadataHelperFactory.createTypeMetadataHelper(connector, metadataTableName, allMetadataAuths, false); typeMetadataWriter.writeTypeMetadataMap(typeMetadataHelper.getTypeMetadataMap(this.allMetadataAuths), metadataTableName); if (log.isDebugEnabled()) { log.debug("table:" + metadataTableName + " " + this + " set the sharedTriState needsUpdate to UPDATED for " + metadataTableName); } watcher.setTriState(triStateName, SharedTriState.STATE.UPDATED); } else { if (log.isDebugEnabled()) { log.debug("table:" + metadataTableName + " " + this + " STATE is not NEEDS_UPDATE! Someone else may be writing or has already written the TypeMetadata map, just release the lock"); } } } catch (Exception ex) { log.warn("table:" + metadataTableName + " Unable to write TypeMetadataMap for " + metadataTableName, ex); watcher.setTriState(triStateName, SharedTriState.STATE.NEEDS_UPDATE); if (log.isDebugEnabled()) { log.debug("After exception, set the SharedTriState STATE to NEEDS_UPDATE"); } } } } finally { if (locked) { lock.release(); if (log.isTraceEnabled()) log.trace("table:" + metadataTableName + " " + this + " released the lock for " + metadataTableName); } } }
Example 19
Source File: DefaultJobService.java From emodb with Apache License 2.0 | 4 votes |
private boolean acquireMutex(InterProcessMutex mutex) throws Exception { return mutex.acquire(200, TimeUnit.MILLISECONDS); }
Example 20
Source File: CollectionPerTimeFrameAssignmentStrategy.java From storm-solr with Apache License 2.0 | 4 votes |
protected void checkCollectionExists(CloudSolrClient cloudSolrClient, String collection) throws Exception { if (!cloudSolrClient.getZkStateReader().getClusterState().hasCollection(collection)) { synchronized (this) { if (curatorClient == null) { curatorClient = CuratorFrameworkFactory.newClient(cloudSolrClient.getZkHost(), new ExponentialBackoffRetry(1000, 3)); curatorClient.start(); } } String lockPath = lockZnodePath+"/"+collection; InterProcessMutex lock = new InterProcessMutex(curatorClient, lockPath); try { if (!lock.acquire(60, TimeUnit.SECONDS)) { // couldn't acquire the lock, but let's check to see if the collection was created before failing cloudSolrClient.getZkStateReader().updateClusterState(); if (!cloudSolrClient.getZkStateReader().getClusterState().hasCollection(collection)) { throw new IllegalStateException("Failed to acquire the create collection lock within 60 seconds! Cannot create "+collection); } } // we have the lock here ... cloudSolrClient.getZkStateReader().updateClusterState(); if (!cloudSolrClient.getZkStateReader().getClusterState().hasCollection(collection)) { log.info("Acquired inter-process lock for creating " + collection); // ok, it doesn't exist ... go ahead and create it long startMs = System.currentTimeMillis(); createCollection(cloudSolrClient, collection); log.info("Collection created, took "+(System.currentTimeMillis()-startMs)+" ms ... updating alias: "+alias); // add the new collection to the collection alias if one is registered if (alias != null) { List<String> aliasList = getAliasList(cloudSolrClient, alias); if (!aliasList.contains(collection)) { aliasList.add(collection); log.info("Added " + collection + " to the " + alias + " alias"); // trim the alias down to the desired size int numColls = aliasList.size(); if (maxCollectionsInAlias > 0 && numColls > maxCollectionsInAlias) { Collections.sort(aliasList); int numToRemove = numColls - maxCollectionsInAlias; aliasList = aliasList.subList(numToRemove, numColls); log.info("Removed "+numToRemove+" collections from alias: "+aliasList); } CollectionAdminRequest.CreateAlias createAliasCmd = new CollectionAdminRequest.CreateAlias(); createAliasCmd.setAliasName(alias); createAliasCmd.setAliasedCollections(StrUtils.join(aliasList, ',')); cloudSolrClient.request(createAliasCmd); } } } else { log.info("Collection "+collection+" was created by another process while we were waiting to acquire the lock ..."); } } finally { lock.release(); } } }