@Test public void testBounceAll() throws Exception { // pick numbers that don't divide evenly final int NUM_PARTICIPANTS = 5; final int NUM_PARTITIONS = 123; final int NUM_REPLICAS = 1; final String RESOURCE_PREFIX = "TestDB"; final String RESOURCE_NAME = RESOURCE_PREFIX + "0"; // create a cluster name based on this test name String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String clusterName = className + "_" + methodName; System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); // Set up cluster TestHelper.setupCluster( clusterName, ZK_ADDR, 12918, // participant port "localhost", // participant name prefix "TestDB", // resource name prefix 1, // resources NUM_PARTITIONS, // partitions per resource NUM_PARTICIPANTS, // number of nodes NUM_REPLICAS, // replicas "OnlineOffline", RebalanceMode.FULL_AUTO, // use FULL_AUTO mode to test node tagging true); // do rebalance // Start the participants HelixManager[] participants = new HelixManager[NUM_PARTICIPANTS]; for (int i = 0; i < NUM_PARTICIPANTS; i++) { final String instanceName = "localhost_" + (12918 + i); participants[i] = createParticipant(clusterName, instanceName); participants[i].connect(); } // Start the controller ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller"); controller.syncStart(); // get an admin and accessor HelixAdmin helixAdmin = new ZKHelixAdmin(_gZkClient); BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor); PropertyKey.Builder keyBuilder = accessor.keyBuilder(); // do the test try { Thread.sleep(1000); // ensure that the external view coalesces boolean result = ClusterStateVerifier.verifyByZkCallback( new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName)); Assert.assertTrue(result); ExternalView stableExternalView = accessor.getProperty(keyBuilder.externalView(RESOURCE_NAME)); for (HelixManager participant : participants) { // disable the controller, bounce the node, re-enable the controller, verify assignments // remained the same helixAdmin.enableCluster(clusterName, false); participant.disconnect(); Thread.sleep(1000); participant = createParticipant(clusterName, participant.getInstanceName()); participant.connect(); Thread.sleep(1000); helixAdmin.enableCluster(clusterName, true); Thread.sleep(1000); result = ClusterStateVerifier.verifyByZkCallback( new MatchingExternalViewVerifier(stableExternalView, clusterName)); Assert.assertTrue(result); } } finally { // clean up controller.syncStop(); for (HelixManager participant : participants) { participant.disconnect(); } System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); } }
@Test public void testRemoveUserCbHandlerOnPathRemoval() throws Exception { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String clusterName = className + "_" + methodName; final int n = 3; final String zkAddr = ZK_ADDR; System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); TestHelper.setupCluster( clusterName, zkAddr, 12918, "localhost", "TestDB", 1, // resource 32, // partitions n, // nodes 2, // replicas "MasterSlave", true); final ClusterControllerManager controller = new ClusterControllerManager(zkAddr, clusterName, "controller_0"); controller.syncStart(); MockParticipantManager[] participants = new MockParticipantManager[n]; for (int i = 0; i < n; i++) { String instanceName = "localhost_" + (12918 + i); participants[i] = new MockParticipantManager(zkAddr, clusterName, instanceName); participants[i].syncStart(); // register a controller listener on participant_0 if (i == 0) { // ZkHelixTestManager manager = participants[0].getManager(); MockParticipantManager manager = participants[0]; manager.addCurrentStateChangeListener( new CurrentStateChangeListener() { @Override public void onStateChange( String instanceName, List<CurrentState> statesInfo, NotificationContext changeContext) { // To change body of implemented methods use File | Settings | File Templates. // System.out.println(instanceName + " on current-state change, type: " + // changeContext.getType()); } }, manager.getInstanceName(), manager.getSessionId()); } } Boolean result = ClusterStateVerifier.verifyByZkCallback( new ClusterStateVerifier.BestPossAndExtViewZkVerifier(zkAddr, clusterName)); Assert.assertTrue(result); MockParticipantManager participantToExpire = participants[0]; String oldSessionId = participantToExpire.getSessionId(); PropertyKeyBuilder keyBuilder = new PropertyKeyBuilder(clusterName); // check manager#hanlders Assert.assertEquals( participantToExpire.getHandlers().size(), 3, "Should have 3 handlers: CURRENTSTATE/{sessionId}, CONTROLLER, and MESSAGES"); // check zkclient#listeners Map<String, Set<IZkDataListener>> dataListeners = ZkTestHelper.getZkDataListener(participantToExpire.getZkClient()); Map<String, Set<IZkChildListener>> childListeners = ZkTestHelper.getZkChildListener(participantToExpire.getZkClient()); // printZkListeners(participantToExpire.getZkClient()); Assert.assertEquals( dataListeners.size(), 1, "Should have 1 path (CURRENTSTATE/{sessionId}/TestDB0) which has 1 data-listeners"); String path = keyBuilder .currentState(participantToExpire.getInstanceName(), oldSessionId, "TestDB0") .getPath(); Assert.assertEquals( dataListeners.get(path).size(), 1, "Should have 1 data-listeners on path: " + path); Assert.assertEquals( childListeners.size(), 3, "Should have 3 paths (CURRENTSTATE/{sessionId}, CONTROLLER, and MESSAGES) each of which has 1 child-listener"); path = keyBuilder.currentStates(participantToExpire.getInstanceName(), oldSessionId).getPath(); Assert.assertEquals( childListeners.get(path).size(), 1, "Should have 1 child-listener on path: " + path); path = keyBuilder.messages(participantToExpire.getInstanceName()).getPath(); Assert.assertEquals( childListeners.get(path).size(), 1, "Should have 1 child-listener on path: " + path); path = keyBuilder.controller().getPath(); Assert.assertEquals( childListeners.get(path).size(), 1, "Should have 1 child-listener on path: " + path); // check zookeeper#watches on client side Map<String, List<String>> watchPaths = ZkTestHelper.getZkWatch(participantToExpire.getZkClient()); // System.out.println("localhost_12918 zk-client side watchPaths: " + watchPaths + "\n"); Assert.assertEquals( watchPaths.get("dataWatches").size(), 4, "Should have 4 data-watches: CURRENTSTATE/{sessionId}, CURRENTSTATE/{sessionId}/TestDB, CONTROLLER, MESSAGES"); Assert.assertEquals( watchPaths.get("childWatches").size(), 3, "Should have 3 child-watches: CONTROLLER, MESSAGES, and CURRENTSTATE/{sessionId}"); // expire localhost_12918 System.out.println( "Expire participant: " + participantToExpire.getInstanceName() + ", session: " + participantToExpire.getSessionId()); ZkTestHelper.expireSession(participantToExpire.getZkClient()); String newSessionId = participantToExpire.getSessionId(); System.out.println( participantToExpire.getInstanceName() + " oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId); result = ClusterStateVerifier.verifyByZkCallback( new ClusterStateVerifier.BestPossAndExtViewZkVerifier(zkAddr, clusterName)); Assert.assertTrue(result); // check manager#hanlders Assert.assertEquals( participantToExpire.getHandlers().size(), 2, "Should have 2 handlers: CONTROLLER and MESSAGES. CURRENTSTATE/{sessionId} handler should be removed by CallbackHandler#handleChildChange()"); // check zkclient#listeners dataListeners = ZkTestHelper.getZkDataListener(participantToExpire.getZkClient()); childListeners = ZkTestHelper.getZkChildListener(participantToExpire.getZkClient()); // printZkListeners(participantToExpire.getZkClient()); Assert.assertTrue(dataListeners.isEmpty(), "Should have no data-listeners"); Assert.assertEquals( childListeners.size(), 3, "Should have 3 paths (CURRENTSTATE/{oldSessionId}, CONTROLLER, and MESSAGES). " + "CONTROLLER and MESSAGE has 1 child-listener each. CURRENTSTATE/{oldSessionId} doesn't have listener (ZkClient doesn't remove empty childListener set. probably a ZkClient bug. see ZkClient#unsubscribeChildChange())"); path = keyBuilder.currentStates(participantToExpire.getInstanceName(), oldSessionId).getPath(); Assert.assertEquals( childListeners.get(path).size(), 0, "Should have no child-listener on path: " + path); path = keyBuilder.messages(participantToExpire.getInstanceName()).getPath(); Assert.assertEquals( childListeners.get(path).size(), 1, "Should have 1 child-listener on path: " + path); path = keyBuilder.controller().getPath(); Assert.assertEquals( childListeners.get(path).size(), 1, "Should have 1 child-listener on path: " + path); // check zookeeper#watches on client side watchPaths = ZkTestHelper.getZkWatch(participantToExpire.getZkClient()); // System.out.println("localhost_12918 zk-client side watchPaths: " + watchPaths + "\n"); Assert.assertEquals( watchPaths.get("dataWatches").size(), 2, "Should have 2 data-watches: CONTROLLER and MESSAGES"); Assert.assertEquals( watchPaths.get("childWatches").size(), 2, "Should have 2 child-watches: CONTROLLER and MESSAGES"); Assert.assertEquals( watchPaths.get("existWatches").size(), 2, "Should have 2 exist-watches: CURRENTSTATE/{oldSessionId} and CURRENTSTATE/{oldSessionId}/TestDB0"); // another session expiry on localhost_12918 should clear the two exist-watches on // CURRENTSTATE/{oldSessionId} System.out.println( "Expire participant: " + participantToExpire.getInstanceName() + ", session: " + participantToExpire.getSessionId()); ZkTestHelper.expireSession(participantToExpire.getZkClient()); result = ClusterStateVerifier.verifyByZkCallback( new ClusterStateVerifier.BestPossAndExtViewZkVerifier(zkAddr, clusterName)); Assert.assertTrue(result); // check zookeeper#watches on client side watchPaths = ZkTestHelper.getZkWatch(participantToExpire.getZkClient()); // System.out.println("localhost_12918 zk-client side watchPaths: " + watchPaths + "\n"); Assert.assertEquals( watchPaths.get("dataWatches").size(), 2, "Should have 2 data-watches: CONTROLLER and MESSAGES"); Assert.assertEquals( watchPaths.get("childWatches").size(), 2, "Should have 2 child-watches: CONTROLLER and MESSAGES"); Assert.assertEquals( watchPaths.get("existWatches").size(), 0, "Should have no exist-watches. exist-watches on CURRENTSTATE/{oldSessionId} and CURRENTSTATE/{oldSessionId}/TestDB0 should be cleared during handleNewSession"); // Thread.sleep(1000); // clean up controller.syncStop(); for (int i = 0; i < n; i++) { participants[i].syncStop(); } System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testCbHandlerLeakOnParticipantSessionExpiry() throws Exception { // Logger.getRootLogger().setLevel(Level.INFO); String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String clusterName = className + "_" + methodName; final int n = 2; System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); TestHelper.setupCluster( clusterName, ZK_ADDR, 12918, // participant port "localhost", // participant name prefix "TestDB", // resource name prefix 1, // resources 32, // partitions per resource n, // number of nodes 2, // replicas "MasterSlave", true); // do rebalance final ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0"); controller.syncStart(); // start participants MockParticipantManager[] participants = new MockParticipantManager[n]; for (int i = 0; i < n; i++) { String instanceName = "localhost_" + (12918 + i); participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName); participants[i].syncStart(); } boolean result = ClusterStateVerifier.verifyByZkCallback( new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName)); Assert.assertTrue(result); final MockParticipantManager participantManagerToExpire = participants[1]; // check controller zk-watchers result = TestHelper.verify( new TestHelper.Verifier() { @Override public boolean verify() throws Exception { Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR); // Set<String> watchPaths = watchers.get("0x" + controllerManager.getSessionId()); Set<String> watchPaths = watchers.get("0x" + controller.getSessionId()); // System.out.println("controller watch paths: " + watchPaths); // controller should have 5 + 2n + m + (m+2)n zk-watchers // where n is number of nodes and m is number of resources return watchPaths.size() == (6 + 5 * n); } }, 500); Assert.assertTrue(result, "Controller should have 6 + 5*n zk-watchers."); // check participant zk-watchers result = TestHelper.verify( new TestHelper.Verifier() { @Override public boolean verify() throws Exception { Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR); Set<String> watchPaths = watchers.get("0x" + participantManagerToExpire.getSessionId()); // System.out.println("participant watch paths: " + watchPaths); // participant should have 2 zk-watchers: 1 for MESSAGE and 1 for CONTROLLER return watchPaths.size() == 2; } }, 500); Assert.assertTrue(result, "Participant should have 2 zk-watchers."); // check HelixManager#_handlers // printHandlers(controllerManager); // printHandlers(participantManagerToExpire); int controllerHandlerNb = controller.getHandlers().size(); int particHandlerNb = participantManagerToExpire.getHandlers().size(); Assert.assertEquals( controllerHandlerNb, 9, "HelixController should have 9 (5+2n) callback handlers for 2 (n) participant"); Assert.assertEquals( particHandlerNb, 2, "HelixParticipant should have 2 (msg+cur-state) callback handlers"); // expire the session of participant System.out.println("Expiring participant session..."); String oldSessionId = participantManagerToExpire.getSessionId(); ZkTestHelper.expireSession(participantManagerToExpire.getZkClient()); String newSessionId = participantManagerToExpire.getSessionId(); System.out.println( "Expried participant session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId); result = ClusterStateVerifier.verifyByPolling( new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName)); Assert.assertTrue(result); // check controller zk-watchers result = TestHelper.verify( new TestHelper.Verifier() { @Override public boolean verify() throws Exception { Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR); Set<String> watchPaths = watchers.get("0x" + controller.getSessionId()); // System.out.println("controller watch paths after session expiry: " + watchPaths); // controller should have 5 + 2n + m + (m+2)n zk-watchers // where n is number of nodes and m is number of resources return watchPaths.size() == (6 + 5 * n); } }, 500); Assert.assertTrue(result, "Controller should have 6 + 5*n zk-watchers after session expiry."); // check participant zk-watchers result = TestHelper.verify( new TestHelper.Verifier() { @Override public boolean verify() throws Exception { Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR); Set<String> watchPaths = watchers.get("0x" + participantManagerToExpire.getSessionId()); // System.out.println("participant watch paths after session expiry: " + // watchPaths); // participant should have 2 zk-watchers: 1 for MESSAGE and 1 for CONTROLLER return watchPaths.size() == 2; } }, 500); Assert.assertTrue(result, "Participant should have 2 zk-watchers after session expiry."); // check handlers // printHandlers(controllerManager); // printHandlers(participantManagerToExpire); int handlerNb = controller.getHandlers().size(); Assert.assertEquals( handlerNb, controllerHandlerNb, "controller callback handlers should not increase after participant session expiry"); handlerNb = participantManagerToExpire.getHandlers().size(); Assert.assertEquals( handlerNb, particHandlerNb, "participant callback handlers should not increase after participant session expiry"); // clean up controller.syncStop(); for (int i = 0; i < n; i++) { participants[i].syncStop(); } System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testBasic() throws Exception { // Logger.getRootLogger().setLevel(Level.INFO); String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String clusterName = className + "_" + methodName; final int n = 5; System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); MockParticipantManager[] participants = new MockParticipantManager[n]; TestHelper.setupCluster( clusterName, ZK_ADDR, 12918, // participant port "localhost", // participant name prefix "TestDB", // resource name prefix 1, // resources 10, // partitions per resource n, // number of nodes 3, // replicas "MasterSlave", true); // do rebalance ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0"); controller.syncStart(); // start participants for (int i = 0; i < n; i++) { String instanceName = "localhost_" + (12918 + i); participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName); participants[i].syncStart(); } boolean result = ClusterStateVerifier.verifyByZkCallback( new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName)); Assert.assertTrue(result); // add a new idealState without registering message handling factory ClusterSetup setupTool = new ClusterSetup(ZK_ADDR); setupTool.addResourceToCluster(clusterName, "TestDB1", 16, "MasterSlave"); ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor); Builder keyBuilder = accessor.keyBuilder(); IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB1")); idealState.setStateModelFactoryName("TestDB1_Factory"); accessor.setProperty(keyBuilder.idealStates("TestDB1"), idealState); setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3); // assert that we have received OFFLINE->SLAVE messages for all partitions int totalMsgs = 0; for (int retry = 0; retry < 5; retry++) { Thread.sleep(100); totalMsgs = 0; for (int i = 0; i < n; i++) { List<Message> msgs = accessor.getChildValues(keyBuilder.messages(participants[i].getInstanceName())); totalMsgs += msgs.size(); } if (totalMsgs == 48) // partition# x replicas break; } Assert.assertEquals( totalMsgs, 48, "Should accumulated 48 unprocessed messages (1 O->S per partition per replica) because TestDB1 is added without state-model-factory but was " + totalMsgs); // register "TestDB1_Factory" state model factory // Logger.getRootLogger().setLevel(Level.INFO); for (int i = 0; i < n; i++) { participants[i] .getStateMachineEngine() .registerStateModelFactory("MasterSlave", new MockMSModelFactory(), "TestDB1_Factory"); } result = ClusterStateVerifier.verifyByZkCallback( new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName)); Assert.assertTrue(result); // clean up // wait for all zk callbacks done controller.syncStop(); for (int i = 0; i < 5; i++) { participants[i].syncStop(); } System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); }