org.apache.curator.framework.recipes.atomic.AtomicValue Java Examples
The following examples show how to use
org.apache.curator.framework.recipes.atomic.AtomicValue.
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: ZookeeperQueryLock.java From datawave with Apache License 2.0 | 6 votes |
public void stopQuery() throws Exception { // decrement the zookeeper query file and delete if 0 clientLock.lock(); try { DistributedAtomicLong atomicLong = getLong(); AtomicValue<Long> longValue = atomicLong.decrement(); if (!longValue.succeeded()) { throw new ZookeeperLockException("Unable to stop query"); } long postValue = longValue.postValue(); if (log.isTraceEnabled()) { long preValue = longValue.preValue(); log.trace("Updated value for " + getQueryFile() + " from " + preValue + " to " + postValue); } if (postValue == 0) { client.delete().forPath(getQueryFile()); } } finally { clientLock.unlock(); } }
Example #2
Source File: ZookeeperQueryLock.java From datawave with Apache License 2.0 | 6 votes |
@Override public boolean isQueryRunning() { // check existence of the query file clientLock.lock(); try { DistributedAtomicLong atomicLong = getLong(); AtomicValue<Long> longValue = atomicLong.get(); if (!longValue.succeeded()) { throw new ZookeeperLockException("Unable to get query lock count"); } long postValue = longValue.postValue(); if (log.isTraceEnabled()) { log.trace("Got value for " + getQueryFile() + " to be " + postValue); } return postValue > 0; } catch (Exception e) { log.error("Unable to determine if zookeeper lock exists", e); // assume the query is still running for now return true; } finally { clientLock.unlock(); } }
Example #3
Source File: DistAtomicIntTest.java From BigData-In-Practice with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { for (int i = 0; i < clientNums; i++) { String name = "client#" + i; new Thread(new Runnable() { @Override public void run() { try { CuratorFramework client = ZKUtils.getClient(); client.start(); DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger(client, distAtomicPath, new RetryNTimes(3, 1000)); for (int j = 0; j < 10; j++) { AtomicValue<Integer> rc = atomicInteger.add(1); System.out.println(name + " Result: " + rc.succeeded() + ", postValue: " + rc.postValue()); Thread.sleep(100); } } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } } }).start(); } countDownLatch.await(); }
Example #4
Source File: ZookeeperQueryLock.java From datawave with Apache License 2.0 | 6 votes |
@Override public void startQuery() throws Exception { // increment the zookeeper query file clientLock.lock(); try { DistributedAtomicLong atomicLong = getLong(); AtomicValue<Long> longValue = atomicLong.increment(); if (!longValue.succeeded()) { throw new ZookeeperLockException("Unable to start query"); } if (log.isTraceEnabled()) { long preValue = longValue.preValue(); long postValue = longValue.postValue(); log.trace("Updated value for " + getQueryFile() + " from " + preValue + " to " + postValue); } } finally { clientLock.unlock(); } }
Example #5
Source File: ZKTimestampStorage.java From phoenix-omid with Apache License 2.0 | 6 votes |
@Override public void updateMaxTimestamp(long previousMaxTimestamp, long newMaxTimestamp) throws IOException { if (newMaxTimestamp < 0) { LOG.error("Negative value received for maxTimestamp: {}", newMaxTimestamp); throw new IllegalArgumentException(); } if (newMaxTimestamp <= previousMaxTimestamp) { LOG.error("maxTimestamp {} <= previousMaxTimesamp: {}", newMaxTimestamp, previousMaxTimestamp); throw new IllegalArgumentException(); } AtomicValue<Long> compareAndSet; try { compareAndSet = timestamp.compareAndSet(previousMaxTimestamp, newMaxTimestamp); } catch (Exception e) { throw new IOException("Problem setting timestamp in ZK", e); } if (!compareAndSet.succeeded()) { // We have to explicitly check for success (See Curator doc) throw new IOException("GetAndSet operation for storing timestamp in ZK did not succeed " + compareAndSet.preValue() + " " + compareAndSet.postValue()); } }
Example #6
Source File: TransactorID.java From fluo with Apache License 2.0 | 5 votes |
private static Long createID(CuratorFramework curator) { try { DistributedAtomicLong counter = new DistributedAtomicLong(curator, ZookeeperPath.TRANSACTOR_COUNT, new ExponentialBackoffRetry(1000, 10)); AtomicValue<Long> nextId = counter.increment(); while (nextId.succeeded() == false) { nextId = counter.increment(); } return nextId.postValue(); } catch (Exception e) { throw new IllegalStateException(e); } }
Example #7
Source File: ExecutorIdGenerator.java From Singularity with Apache License 2.0 | 5 votes |
public String getNextExecutorId() { checkState(distributedGenerator != null, "never started!"); try { AtomicValue<Integer> atomic = distributedGenerator.increment(); Preconditions.checkState(atomic.succeeded(), "Atomic increment did not succeed"); return convertUsingAlphabet(atomic.postValue()); } catch (Throwable t) { throw new RuntimeException(t); } }
Example #8
Source File: DistributedAtomicLongExample.java From ZKRecipesByExample with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException, Exception { try (TestingServer server = new TestingServer()) { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); List<DistributedAtomicLong> examples = Lists.newArrayList(); ExecutorService service = Executors.newFixedThreadPool(QTY); for (int i = 0; i < QTY; ++i) { final DistributedAtomicLong count = new DistributedAtomicLong(client, PATH, new RetryNTimes(10, 10)); examples.add(count); Callable<Void> task = new Callable<Void>() { @Override public Void call() throws Exception { try { //Thread.sleep(rand.nextInt(1000)); AtomicValue<Long> value = count.increment(); //AtomicValue<Long> value = count.decrement(); //AtomicValue<Long> value = count.add((long)rand.nextInt(20)); System.out.println("succeed: " + value.succeeded()); if (value.succeeded()) System.out.println("Increment: from " + value.preValue() + " to " + value.postValue()); } catch (Exception e) { e.printStackTrace(); } return null; } }; service.submit(task); } service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); } }
Example #9
Source File: CuratorCounter.java From vespa with Apache License 2.0 | 5 votes |
public long get() { try { AtomicValue<Long> value = counter.get(); if (!value.succeeded()) { throw new RuntimeException("Get did not succeed"); } return value.postValue(); } catch (Exception e) { throw new RuntimeException("Unable to get value", e); } }
Example #10
Source File: CuratorCounter.java From vespa with Apache License 2.0 | 5 votes |
/** * Atomically add and return resulting value. * * @param delta value to add, may be negative * @return the resulting value * @throws IllegalStateException if addition fails */ public synchronized long add(long delta) { try { AtomicValue<Long> value = counter.add(delta); if (!value.succeeded()) { throw new IllegalStateException("Add did not succeed"); } return value.postValue(); } catch (Exception e) { throw new IllegalStateException("Unable to add value", e); } }
Example #11
Source File: ZKTimestampStorage.java From phoenix-omid with Apache License 2.0 | 5 votes |
@Override public long getMaxTimestamp() throws IOException { AtomicValue<Long> atomicValue; try { atomicValue = timestamp.get(); } catch (Exception e) { throw new IOException("Problem getting data from ZK", e); } if (!atomicValue.succeeded()) { // We have to explicitly check for success (See Curator doc) throw new IOException("Get operation to obtain timestamp from ZK did not succeed"); } return atomicValue.postValue(); }
Example #12
Source File: ZkService.java From DBus with Apache License 2.0 | 5 votes |
/** * 自增并获得,自增后的变量 * * @param path * @return * @throws Exception */ public Long incrementAndGetValue(String path) throws Exception { DistributedAtomicLong atomicId = new DistributedAtomicLong(client, path, new RetryNTimes(32, 1000)); AtomicValue<Long> rc = atomicId.increment(); if (rc.succeeded()) { logger.info("incrementAndGetValue({}) success! before: {}, after: {}.", path, rc.preValue(), rc.postValue()); } else { logger.warn("incrementAndGetValue({}) failed! before: {}, after: {}.", path, rc.preValue(), rc.postValue()); } return rc.postValue(); }
Example #13
Source File: ZkService.java From DBus with Apache License 2.0 | 5 votes |
/** * 获得分布式自增变量 * * @param path * @return * @throws Exception */ public Long getIncrementValue(String path) throws Exception { DistributedAtomicLong atomicId = new DistributedAtomicLong(client, path, new RetryNTimes(32, 1000)); AtomicValue<Long> rc = atomicId.get(); if (rc.succeeded()) { logger.debug("getIncrementValue({}) success! get: {}.", path, rc.postValue()); } else { logger.warn("getIncrementValue({}) failed! get: {}.", path, rc.postValue()); } return rc.postValue(); }
Example #14
Source File: ZkService.java From DBus with Apache License 2.0 | 5 votes |
/** * 获得ZK中的当前值,如果不存在,抛出异常 * * @param path * @return * @throws Exception */ private long currentValueFromZk(String path) throws Exception { if (isExists(path)) { DistributedAtomicLong count = new DistributedAtomicLong(client, path, new RetryNTimes(10, 1000)); AtomicValue<Long> val = count.get(); return val.postValue(); } else { throw new RuntimeException("Path is not existed! Call nextValue firstly!"); } }
Example #15
Source File: ZookeeperRegistryCenter.java From paascloud-master with Apache License 2.0 | 5 votes |
/** * Gets atomic value. * * @param path the path * @param retryNTimes the retry n times * * @return the atomic value */ @Override public AtomicValue<Integer> getAtomicValue(String path, RetryNTimes retryNTimes) { try { distributedAtomicInteger = new DistributedAtomicInteger(client, path, retryNTimes); return distributedAtomicInteger.get(); } catch (Exception e) { log.error("getAtomicValue={}", e.getMessage(), e); } return null; }
Example #16
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
@Override public AtomicValue<Long> get() { if (value == null) return new MockLongValue(0); return value; }
Example #17
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
public AtomicValue<Long> add(Long delta) throws Exception { return trySet(value.postValue() + delta); }
Example #18
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
public AtomicValue<Long> subtract(Long delta) throws Exception { return trySet(value.postValue() - delta); }
Example #19
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
@Override public AtomicValue<Long> increment() { return trySet(value.postValue() + 1); }
Example #20
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
public AtomicValue<Long> decrement() throws Exception { return trySet(value.postValue() - 1); }
Example #21
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
@Override public AtomicValue<Long> trySet(Long longval) { value = new MockLongValue(longval); return value; }
Example #22
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
public AtomicValue<Long> compareAndSet(Long expectedValue, Long newValue) throws Exception { throw new UnsupportedOperationException("Not implemented in MockCurator"); }
Example #23
Source File: RegistryCenter.java From paascloud-master with Apache License 2.0 | 2 votes |
/** * Instantiates a new Get atomic value. * * @param path the path * @param retryNTimes the retry n times * * @return the atomic value */ AtomicValue<Integer> getAtomicValue(String path, RetryNTimes retryNTimes);