@Override public long getAndIncrement() throws Exception { while (true) { ConnectionState connState = connStateListener.getLastState(); if (connState != null) { throw new IllegalStateException("Connection state: " + connState); } VersionedValue<Integer> current = sharedCount.getVersionedValue(); int newCount = current.getValue() + 1; if (newCount < 0) { // overflow and wrap around throw new Exception("Checkpoint counter overflow. ZooKeeper checkpoint counter only supports " + "checkpoints Ids up to " + Integer.MAX_VALUE); } if (sharedCount.trySetCount(current, newCount)) { return current.getValue(); } } }
private void incrSharedCount(SharedCount sharedCount) throws Exception { while (true) { // Loop until we successfully increment the counter VersionedValue<Integer> versionedValue = sharedCount.getVersionedValue(); if (sharedCount.trySetCount(versionedValue, versionedValue.getValue() + 1)) { break; } } }
public ZooKeeperVersionedValue(VersionedValue<T> versionedValue) { this.versionedValue = Preconditions.checkNotNull(versionedValue); }
VersionedValue<T> getVersionedValue() { return versionedValue; }