public static boolean verifyByZkCallback(ZkVerifier verifier, long timeout)
{
long startTime = System.currentTimeMillis();
CountDownLatch countDown = new CountDownLatch(1);
ZkClient zkClient = verifier.getZkClient();
String clusterName = verifier.getClusterName();
// add an ephemeral node to /{clusterName}/CONFIGS/CLUSTER/verify
// so when analyze zk log, we know when a test ends
zkClient.createEphemeral("/" + clusterName + "/CONFIGS/CLUSTER/verify");
ExtViewVeriferZkListener listener =
new ExtViewVeriferZkListener(countDown, zkClient, verifier);
String extViewPath =
PropertyPathConfig.getPath(PropertyType.EXTERNALVIEW, clusterName);
zkClient.subscribeChildChanges(extViewPath, listener);
for (String child : zkClient.getChildren(extViewPath))
{
String childPath =
extViewPath.equals("/") ? extViewPath + child : extViewPath + "/" + child;
zkClient.subscribeDataChanges(childPath, listener);
}
// do initial verify
boolean result = verifier.verify();
if (result == false)
{
try
{
result = countDown.await(timeout, TimeUnit.MILLISECONDS);
if (result == false)
{
// make a final try if timeout
result = verifier.verify();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// clean up
zkClient.unsubscribeChildChanges(extViewPath, listener);
for (String child : zkClient.getChildren(extViewPath))
{
String childPath =
extViewPath.equals("/") ? extViewPath + child : extViewPath + "/" + child;
zkClient.unsubscribeDataChanges(childPath, listener);
}
long endTime = System.currentTimeMillis();
zkClient.delete("/" + clusterName + "/CONFIGS/CLUSTER/verify");
// debug
System.err.println(result + ": wait " + (endTime - startTime) + "ms, " + verifier);
return result;
}