We are using DistributedAtomicLong to use job sequenceID in ZK.
We noticed that getZKId in multithread env fails. value.preValue() and
value.postValue() value = 0 and succeeded = false.
If we synchronized the function it works fine, but I don't think it's a right
approach.
Other approach is to retry multiple time, but how many times. We need to make
sure that getZKId return sequence.
What is the best approach?
DistributedAtomicLong atomicIdGenerator;
PromotedToLock.Builder lockBuilder = PromotedToLock.builder()
.lockPath(getPromotedLock()).retryPolicy(ZKUtils.getRetryPloicy())
.timeout(Service.lockTimeout, TimeUnit.MILLISECONDS);
atomicIdGenerator = new DistributedAtomicLong(zk.getClient(),
ZK_SEQUENCE_PATH, ZKUtils.getRetryPloicy(),
lockBuilder.build());
private long getZKId( ) {
if (atomicIdGenerator == null) {
throw new RuntimeException("Sequence generator can't be null. Path
: " + ZK_SEQUENCE_PATH);
}
AtomicValue<Long> value = null;
try {
value = atomicIdGenerator.increment();
}
catch (Exception e) {
throw new RuntimeException("Exception incrementing UID for session
", e);
}
finally {
if (value != null && value.succeeded()) {
return value.preValue();
}
else {
throw new RuntimeException("Exception incrementing UID for
session ");
}
}
}