Back
Close

Computing with Data

elgeish
33.3K views
Previous: Distributed Locks Next: White-Box Testing

Linearizable Counters

We can create a znode for our counter and use Curator to commit a dummy transaction:

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class Main {
private static final String HOST = "127.0.0.1:2181";
public static void main(String args[]) throws Exception {
final RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
try (final CuratorFramework client = CuratorFrameworkFactory.newClient(HOST, retryPolicy)) {
client.start();
client.createContainers·("/path/to/counter");
final int version = client.inTransaction().
setData().forPath("/path/to/counter").and().
commit().iterator().next().
getResultStat().getVersion();
System.out.println("Version: " + version);
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants