We are currently evaluating apache-curator for distributed locking use case. Below is our test case:
public class Test {
private static CuratorFramework client = CuratorFrameworkFactory.newClient("zook.company.com", new ExponentialBackoffRetry(10,5));
public static void main(String[] args) {
client.start();
int numLocks = 50000;
int numThreads = 200;
String[] keyPool = new String[numLocks];
for (int i = 1; i <= numLocks; i++) {
keyPool[i - 1] = String.valueOf(100 + i);
}
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread(new Job(numLocks, keyPool));
t.setName("T" + (i + 1));
t.start();
}
}
private static class Job implements Runnable {
private int numLocks;
private String[] keyPool;
public Job(int numLocks, String[] keyPool) {
this.numLocks = numLocks;
this.keyPool = keyPool;
}
@Override
public void run() {
while (true) {
int l = 0;
int h = numLocks;
String lockKey = keyPool[(new Random().nextInt(h - l) + l)];
InterProcessMutex lock = new InterProcessMutex(client, "/"+lockKey);
boolean acquired = false;
String threadName = Thread.currentThread().getName();
try {
long start = System.currentTimeMillis();
acquired = lock.acquire(0, TimeUnit.NANOSECONDS);
if (acquired) {
long end = System.currentTimeMillis();
System.out.println("lock acquired in "+ (end - start) + " ms");
} else {
System.out.println("failed to get lock");
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (acquired) {
long start = System.currentTimeMillis();
lock.release();
long end = System.currentTimeMillis();
System.out.println("lock released in "+(end - start)+" ms");
}
}
}
}
}
}
The test runs on a 2 core/7.5G RAM machine with 2G Xmx. While the zookeeper instance (zook.company.com
) is running on 4 core/15G RAM server with Xmx as 12G, maxClientCnxns=5000, tickTime=2000, initLimit=10 and syncLimit=5.
Both servers reside in the same AWS VPC.
On running the tests for 10 mins, we are getting lock acquisition time as 80 ms
for more than 95% lock attempts. While max time taken for lock acquisition was 340 ms
. Have been trying different combinations of number of threads and number of locks but the times are always on the higher side.
Not able to find if anything wrong anywhere? Because the times seem to be too high. Any clues??