我们目前正在评估用于分布式锁定用例的 apache-curator。下面是我们的测试用例:
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");
}
}
}
}
}
}
该测试在 2G Xmx 的 2 核/7.5G RAM 机器上运行。Zookeeper 实例 ( zook.company.com
) 在 4 核/15G RAM 服务器上运行,Xmx 为 12G,maxClientCnxns=5000,tickTime=2000,initLimit=10 和 syncLimit=5。
两台服务器都位于同一个 AWS VPC 中。
在运行 10 分钟的测试时,我们获得80 ms
了超过 95% 的锁定尝试的锁定获取时间。虽然获取锁的最大时间是340 ms
. 一直在尝试线程数和锁数的不同组合,但时间总是偏高。
无法找到任何地方是否有问题?因为时代似乎太高了。有什么线索吗??