use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.
the class TestConsecutiveZkSessionExpiry method testDistributedController.
@Test
public void testDistributedController() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 2;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
4, // number of nodes
n, // replicas
2, "MasterSlave", // do rebalance
true);
ClusterDistributedController[] distributedControllers = new ClusterDistributedController[n];
CountDownLatch startCountdown = new CountDownLatch(1);
CountDownLatch endCountdown = new CountDownLatch(1);
for (int i = 0; i < n; i++) {
String contrllerName = "localhost_" + (12918 + i);
distributedControllers[i] = new ClusterDistributedController(ZK_ADDR, clusterName, contrllerName);
distributedControllers[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
if (i == 0) {
distributedControllers[i].addPreConnectCallback(new PreConnectTestCallback(contrllerName, startCountdown, endCountdown));
}
distributedControllers[i].connect();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// expire the session of distributedController
LOG.info("1st Expiring distributedController session...");
String oldSessionId = distributedControllers[0].getSessionId();
ZkTestHelper.asyncExpireSession(distributedControllers[0].getZkClient());
String newSessionId = distributedControllers[0].getSessionId();
LOG.info("Expried distributedController session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
// expire zk session again during HelixManager#handleNewSession()
startCountdown.await();
LOG.info("2nd Expiring distributedController session...");
oldSessionId = distributedControllers[0].getSessionId();
ZkTestHelper.asyncExpireSession(distributedControllers[0].getZkClient());
newSessionId = distributedControllers[0].getSessionId();
LOG.info("Expried distributedController session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
endCountdown.countDown();
result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// verify leader changes to localhost_12919
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNotNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12918"), true));
LiveInstance leader = pollForProperty(LiveInstance.class, accessor, keyBuilder.controllerLeader(), true);
Assert.assertNotNull(leader);
Assert.assertEquals(leader.getId(), "localhost_12919");
// check localhost_12918 has 2 handlers: message and data-accessor
LOG.debug("handlers: " + TestHelper.printHandlers(distributedControllers[0]));
List<CallbackHandler> handlers = distributedControllers[0].getHandlers();
Assert.assertEquals(handlers.size(), 2, "Distributed controller should have 2 handler (message) after lose leadership, but was " + handlers.size());
// clean up
distributedControllers[0].disconnect();
distributedControllers[1].disconnect();
Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12918"), false));
Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12919"), false));
Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.controllerLeader(), false));
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.
the class TestHelper method printHandlers.
// debug code
public static String printHandlers(ZkTestManager manager) {
StringBuilder sb = new StringBuilder();
List<CallbackHandler> handlers = manager.getHandlers();
sb.append(manager.getInstanceName() + " has " + handlers.size() + " cb-handlers. [");
for (int i = 0; i < handlers.size(); i++) {
CallbackHandler handler = handlers.get(i);
String path = handler.getPath();
sb.append(path.substring(manager.getClusterName().length() + 1) + ": " + handler.getListener());
if (i < (handlers.size() - 1)) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.
the class TestHelper method printZkListeners.
public static void printZkListeners(ZkClient client) throws Exception {
Map<String, Set<IZkDataListener>> datalisteners = ZkTestHelper.getZkDataListener(client);
Map<String, Set<IZkChildListener>> childListeners = ZkTestHelper.getZkChildListener(client);
System.out.println("dataListeners {");
for (String path : datalisteners.keySet()) {
System.out.println("\t" + path + ": ");
Set<IZkDataListener> set = datalisteners.get(path);
for (IZkDataListener listener : set) {
CallbackHandler handler = (CallbackHandler) listener;
System.out.println("\t\t" + handler.getListener());
}
}
System.out.println("}");
System.out.println("childListeners {");
for (String path : childListeners.keySet()) {
System.out.println("\t" + path + ": ");
Set<IZkChildListener> set = childListeners.get(path);
for (IZkChildListener listener : set) {
CallbackHandler handler = (CallbackHandler) listener;
System.out.println("\t\t" + handler.getListener());
}
}
System.out.println("}");
}
use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.
the class TestZkCallbackHandlerLeak method printHandlers.
// debug code
static String printHandlers(ZkTestManager manager) {
StringBuilder sb = new StringBuilder();
List<CallbackHandler> handlers = manager.getHandlers();
sb.append(manager.getInstanceName() + " has " + handlers.size() + " cb-handlers. [");
for (int i = 0; i < handlers.size(); i++) {
CallbackHandler handler = handlers.get(i);
String path = handler.getPath();
sb.append(path.substring(manager.getClusterName().length() + 1) + ": " + handler.getListener());
if (i < (handlers.size() - 1)) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.
the class TestZkCallbackHandlerLeak method printZkListeners.
void printZkListeners(ZkClient client) throws Exception {
Map<String, Set<IZkDataListener>> datalisteners = ZkTestHelper.getZkDataListener(client);
Map<String, Set<IZkChildListener>> childListeners = ZkTestHelper.getZkChildListener(client);
System.out.println("dataListeners {");
for (String path : datalisteners.keySet()) {
System.out.println("\t" + path + ": ");
Set<IZkDataListener> set = datalisteners.get(path);
for (IZkDataListener listener : set) {
CallbackHandler handler = (CallbackHandler) listener;
System.out.println("\t\t" + handler.getListener());
}
}
System.out.println("}");
System.out.println("childListeners {");
for (String path : childListeners.keySet()) {
System.out.println("\t" + path + ": ");
Set<IZkChildListener> set = childListeners.get(path);
for (IZkChildListener listener : set) {
CallbackHandler handler = (CallbackHandler) listener;
System.out.println("\t\t" + handler.getListener());
}
}
System.out.println("}");
}
Aggregations