Return-Path: X-Original-To: apmail-helix-commits-archive@minotaur.apache.org Delivered-To: apmail-helix-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1C8A1D310 for ; Wed, 28 Nov 2012 23:44:29 +0000 (UTC) Received: (qmail 23557 invoked by uid 500); 28 Nov 2012 23:44:29 -0000 Delivered-To: apmail-helix-commits-archive@helix.apache.org Received: (qmail 23530 invoked by uid 500); 28 Nov 2012 23:44:29 -0000 Mailing-List: contact commits-help@helix.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@helix.incubator.apache.org Delivered-To: mailing list commits@helix.incubator.apache.org Received: (qmail 23517 invoked by uid 99); 28 Nov 2012 23:44:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Nov 2012 23:44:28 +0000 X-ASF-Spam-Status: No, hits=-2000.4 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 28 Nov 2012 23:44:26 +0000 Received: (qmail 22070 invoked by uid 99); 28 Nov 2012 23:44:05 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Nov 2012 23:44:05 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id A3039813172; Wed, 28 Nov 2012 23:44:05 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: zzhang@apache.org To: commits@helix.incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: git commit: [HELIX-7] fix random test failures Message-Id: <20121128234405.A3039813172@tyr.zones.apache.org> Date: Wed, 28 Nov 2012 23:44:05 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org Updated Branches: refs/heads/master 64f7f2823 -> 7d478437d [HELIX-7] fix random test failures Project: http://git-wip-us.apache.org/repos/asf/incubator-helix/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-helix/commit/7d478437 Tree: http://git-wip-us.apache.org/repos/asf/incubator-helix/tree/7d478437 Diff: http://git-wip-us.apache.org/repos/asf/incubator-helix/diff/7d478437 Branch: refs/heads/master Commit: 7d478437de4dfe510fd09498b4df71be7c51ecd9 Parents: 64f7f28 Author: zzhang Authored: Wed Nov 28 15:43:55 2012 -0800 Committer: zzhang Committed: Wed Nov 28 15:43:55 2012 -0800 ---------------------------------------------------------------------- .../org/apache/helix/tools/TestHelixAdminCli.java | 586 +++++++++++++++ .../helix/tools/TestHelixAdminScenariosCli.java | 452 ----------- 2 files changed, 586 insertions(+), 452 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/7d478437/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java ---------------------------------------------------------------------- diff --git a/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java b/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java new file mode 100644 index 0000000..38be0d4 --- /dev/null +++ b/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java @@ -0,0 +1,586 @@ +package org.apache.helix.tools; + +import java.io.FileWriter; +import java.io.PrintWriter; +import java.util.Date; + +import org.apache.helix.BaseDataAccessor; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.TestHelper; +import org.apache.helix.ZNRecord; +import org.apache.helix.controller.HelixControllerMain; +import org.apache.helix.integration.ZkIntegrationTestBase; +import org.apache.helix.manager.zk.ZKHelixDataAccessor; +import org.apache.helix.manager.zk.ZKUtil; +import org.apache.helix.manager.zk.ZkBaseDataAccessor; +import org.apache.helix.mock.controller.ClusterController; +import org.apache.helix.mock.participant.MockParticipant; +import org.apache.helix.model.IdealState; +import org.apache.helix.model.LiveInstance; +import org.apache.helix.store.ZNRecordJsonSerializer; +import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier; +import org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class TestHelixAdminCli extends ZkIntegrationTestBase { + @Test + public void testAddCluster() throws Exception { + String command = "--zkSvr localhost:2183 -addCluster clusterTest"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // malformed cluster name + command = "--zkSvr localhost:2183 -addCluster /ClusterTest"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("ClusterSetup should fail since /ClusterTest is not a valid name"); + } catch (Exception e) { + // OK + } + + // Add the grand cluster + // " is ignored by zk + command = "--zkSvr localhost:2183 -addCluster \"Klazt3rz"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "--zkSvr localhost:2183 -addCluster \\ClusterTest"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // Add already exist cluster + command = "--zkSvr localhost:2183 -addCluster clusterTest"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("ClusterSetup should fail since clusterTest already exists"); + } catch (Exception e) { + // OK + } + + // make sure clusters are properly setup + Assert.assertTrue(ZKUtil.isClusterSetup("Klazt3rz", _gZkClient)); + Assert.assertTrue(ZKUtil.isClusterSetup("clusterTest", _gZkClient)); + Assert.assertTrue(ZKUtil.isClusterSetup("\\ClusterTest", _gZkClient)); + + // delete cluster without resource and instance + command = "-zkSvr localhost:2183 -dropCluster \\ClusterTest"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -dropCluster clusterTest1"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -dropCluster clusterTest"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + Assert.assertFalse(_gZkClient.exists("/clusterTest")); + Assert.assertFalse(_gZkClient.exists("/\\ClusterTest")); + Assert.assertFalse(_gZkClient.exists("/clusterTest1")); + + // System.out.println("END test"); + } + + @Test + public void testAddResource() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + String command = "-zkSvr localhost:2183 -addCluster " + clusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_22 144 MasterSlave"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_11 44 MasterSlave"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // Add duplicate resource + command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_22 55 OnlineOffline"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("ClusterSetup should fail since resource db_22 already exists"); + } catch (Exception e) { + // OK + } + + // drop resource now + command = "-zkSvr localhost:2183 -dropResource " + clusterName + " db_11 "; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } + + @Test + public void testAddInstance() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + String command = "-zkSvr localhost:2183 -addCluster " + clusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + for (int i = 0; i < 3; i++) { + command = "-zkSvr localhost:2183 -addNode " + clusterName + " localhost:123" + i; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + } + + command = "-zkSvr localhost:2183 -addNode " + clusterName + + " localhost:1233;localhost:1234;localhost:1235;localhost:1236"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // delete enabled node + command = "-zkSvr localhost:2183 -dropNode " + clusterName + " localhost:1236"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("delete node localhost:1236 should fail since it's not disabled"); + } catch (Exception e) { + // OK + } + + // delete non-exist node + command = "-zkSvr localhost:2183 -dropNode " + clusterName + " localhost:12367"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("delete node localhost:1237 should fail since it doesn't exist"); + } catch (Exception e) { + // OK + } + + // disable node + command = "-zkSvr localhost:2183 -enableInstance " + clusterName + " localhost:1236 false"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -dropNode " + clusterName + " localhost:1236"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // add a duplicated host + command = "-zkSvr localhost:2183 -addNode " + clusterName + " localhost:1234"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("add node localhost:1234 should fail since it already exists"); + } catch (Exception e) { + // OK + } + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } + + @Test + public void testRebalanceResource() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + String command = "-zkSvr localhost:2183 -addCluster " + clusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_11 12 MasterSlave"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + for (int i = 0; i < 6; i++) { + command = "-zkSvr localhost:2183 -addNode " + clusterName + " localhost:123" + i; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + } + + command = "-zkSvr localhost:2183 -rebalance " + clusterName + " db_11 3"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -dropResource " + clusterName + " db_11 "; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // re-add and rebalance + command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_11 48 MasterSlave"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -rebalance " + clusterName + " db_11 3"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // rebalance with key prefix + command = "-zkSvr localhost:2183 -rebalance " + clusterName + " db_11 2 -key alias"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } + + @Test + public void testStartCluster() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + String grandClusterName = clusterName + "_grand"; + final int n = 6; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + MockParticipant[] participants = new MockParticipant[n]; + ClusterController[] controllers = new ClusterController[2]; + setupCluster(clusterName, grandClusterName, n, participants, controllers); + + // activate clusters + // wrong grand clusterName + String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + + " nonExistGrandCluster true"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("add " + clusterName + + " to grandCluster should fail since grandCluster doesn't exists"); + } catch (Exception e) { + // OK + } + + // wrong cluster name + command = "-zkSvr localhost:2183 -activateCluster nonExistCluster " + grandClusterName + + " true"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("add nonExistCluster to " + grandClusterName + + " should fail since nonExistCluster doesn't exists"); + } catch (Exception e) { + // OK + } + + command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + grandClusterName + + " true"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Thread.sleep(500); + + // drop a running cluster + command = "-zkSvr localhost:2183 -dropCluster " + clusterName; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("drop " + clusterName + " should fail since it's still running"); + } catch (Exception e) { + // OK + } + + // verify leader node + BaseDataAccessor baseAccessor = new ZkBaseDataAccessor(_gZkClient); + HelixDataAccessor accessor = new ZKHelixDataAccessor(grandClusterName, baseAccessor); + LiveInstance controllerLeader = accessor.getProperty(accessor.keyBuilder() + .controllerLeader()); + Assert.assertNotNull(controllerLeader, + "controllerLeader should be either controller_9000 or controller_9001"); + Assert.assertTrue(controllerLeader.getInstanceName().startsWith("controller_900")); + + accessor = new ZKHelixDataAccessor(clusterName, baseAccessor); + LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader()); + for (int i = 0; i < 20; i++) { + if (leader != null) { + break; + } + Thread.sleep(200); + leader = accessor.getProperty(accessor.keyBuilder().controllerLeader()); + } + Assert.assertTrue(leader.getInstanceName().startsWith("controller_900")); + + boolean verifyResult = ClusterStateVerifier + .verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, clusterName)); + Assert.assertTrue(verifyResult); + + verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier( + ZK_ADDR, clusterName)); + Assert.assertTrue(verifyResult); + + // clean up + // for (int i = 0; i < 2; i++) { + // controllers[i].syncStop(); + // Thread.sleep(1000); // wait for all zk callbacks done + // } + // Thread.sleep(5000); + // for (int i = 0; i < n; i++) { + // participants[i].syncStop(); + // } + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } + + @Test + public void testDropAddResource() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + String grandClusterName = clusterName + "_grand"; + final int n = 6; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + MockParticipant[] participants = new MockParticipant[n]; + ClusterController[] controllers = new ClusterController[2]; + setupCluster(clusterName, grandClusterName, n, participants, controllers); + String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + + grandClusterName + " true"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Thread.sleep(500); + + // save ideal state + BaseDataAccessor baseAccessor = new ZkBaseDataAccessor(_gZkClient); + HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor); + IdealState idealState = accessor.getProperty(accessor.keyBuilder().idealStates("db_11")); + ZNRecordJsonSerializer serializer = new ZNRecordJsonSerializer(); + + String tmpDir = System.getProperty("java.io.tmpdir"); + if (tmpDir == null) { + tmpDir = "/tmp"; + } + final String tmpIdealStateFile = tmpDir + "/" + clusterName + "_idealState.log"; + FileWriter fos = new FileWriter(tmpIdealStateFile); + PrintWriter pw = new PrintWriter(fos); + pw.write(new String(serializer.serialize(idealState.getRecord()))); + pw.close(); + + command = "-zkSvr localhost:2183 -dropResource " + clusterName + " db_11 "; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + boolean verifyResult = ClusterStateVerifier + .verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName)); + Assert.assertTrue(verifyResult); + + command = "-zkSvr localhost:2183 -addIdealState " + clusterName + " db_11 " + + tmpIdealStateFile; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier( + ZK_ADDR, clusterName)); + Assert.assertTrue(verifyResult); + + IdealState idealState2 = accessor.getProperty(accessor.keyBuilder().idealStates("db_11")); + Assert.assertTrue(idealState2.getRecord().equals(idealState.getRecord())); + + // clean up + // for (int i = 0; i < 2; i++) { + // controllers[i].syncStop(); + // Thread.sleep(1000); // wait for all zk callbacks done + // } + // Thread.sleep(5000); + // for (int i = 0; i < n; i++) { + // participants[i].syncStop(); + // } + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + + } + + private void setupCluster(String clusterName, String grandClusterName, final int n, + MockParticipant[] participants, ClusterController[] controllers) throws Exception, + InterruptedException { + // add cluster + String command = "-zkSvr localhost:2183 -addCluster " + clusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // add grand cluster + command = "-zkSvr localhost:2183 -addCluster " + grandClusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // add nodes + for (int i = 0; i < n; i++) { + command = "-zkSvr localhost:2183 -addNode " + clusterName + " localhost:123" + i; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + } + + // add resource + command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_11 48 MasterSlave"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // rebalance with key prefix + command = "-zkSvr localhost:2183 -rebalance " + clusterName + " db_11 2 -key alias"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // add nodes to grand cluster + command = "-zkSvr localhost:2183 -addNode " + grandClusterName + + " controller:9000;controller:9001"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // start mock nodes + for (int i = 0; i < n; i++) { + String instanceName = "localhost_123" + i; + participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR); + participants[i].syncStart(); + } + + // start controller nodes + for (int i = 0; i < 2; i++) { + controllers[i] = new ClusterController(grandClusterName, "controller_900" + i, ZK_ADDR, + HelixControllerMain.DISTRIBUTED); + controllers[i].syncStart(); + } + + Thread.sleep(100); + } + + @Test + public void testInstanceOperations() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + String grandClusterName = clusterName + "_grand"; + final int n = 6; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + MockParticipant[] participants = new MockParticipant[n]; + ClusterController[] controllers = new ClusterController[2]; + setupCluster(clusterName, grandClusterName, n, participants, controllers); + String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + + grandClusterName + " true"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Thread.sleep(500); + + // drop node should fail if the node is not disabled + command = "-zkSvr localhost:2183 -dropNode " + clusterName + " localhost:1232"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("dropNode should fail since the node is not disabled"); + } catch (Exception e) { + // OK + } + + // disabled node + command = "-zkSvr localhost:2183 -enableInstance " + clusterName + " localhost:1232 false"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // Cannot dropNode if the node is not disconnected + command = "-zkSvr localhost:2183 -dropNode " + clusterName + " localhost:1232"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("dropNode should fail since the node is not disconnected"); + } catch (Exception e) { + // OK + } + + // Cannot swapNode if the node is not disconnected + command = "-zkSvr localhost:2183 -swapInstance " + clusterName + + " localhost_1232 localhost_12320"; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("swapInstance should fail since the node is not disconnected"); + } catch (Exception e) { + // OK + } + + // disconnect localhost_1232 + participants[2].syncStop(); + + // add new node then swap instance + command = "-zkSvr localhost:2183 -addNode " + clusterName + " localhost:12320"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + // swap instance. The instance get swapped out should not exist anymore + command = "-zkSvr localhost:2183 -swapInstance " + clusterName + + " localhost_1232 localhost_12320"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + BaseDataAccessor baseAccessor = new ZkBaseDataAccessor(_gZkClient); + HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor); + String path = accessor.keyBuilder().instanceConfig("localhost_1232").getPath(); + Assert.assertFalse(_gZkClient.exists(path), path + + " should not exist since localhost_1232 has been swapped by localhost_12320"); + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } + + @Test + public void testExpandCluster() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + String grandClusterName = clusterName + "_grand"; + final int n = 6; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + MockParticipant[] participants = new MockParticipant[n]; + ClusterController[] controllers = new ClusterController[2]; + setupCluster(clusterName, grandClusterName, n, participants, controllers); + String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + + grandClusterName + " true"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Thread.sleep(500); + + command = "-zkSvr localhost:2183 -addNode " + clusterName + + " localhost:12331;localhost:12341;localhost:12351;localhost:12361"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + command = "-zkSvr localhost:2183 -expandCluster " + clusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + MockParticipant[] newParticipants = new MockParticipant[4]; + for (int i = 3; i <= 6; i++) { + String instanceName = "localhost_123" + i + "1"; + newParticipants[i - 3] = new MockParticipant(clusterName, instanceName, ZK_ADDR); + newParticipants[i - 3].syncStart(); + } + + boolean verifyResult = ClusterStateVerifier + .verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, clusterName)); + Assert.assertTrue(verifyResult); + + verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier( + ZK_ADDR, clusterName)); + Assert.assertTrue(verifyResult); + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } + + @Test + public void testDeactivateCluster() throws Exception { + String className = TestHelper.getTestClassName(); + String methodName = TestHelper.getTestMethodName(); + String clusterName = className + "_" + methodName; + String grandClusterName = clusterName + "_grand"; + final int n = 6; + + System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); + + MockParticipant[] participants = new MockParticipant[n]; + ClusterController[] controllers = new ClusterController[2]; + setupCluster(clusterName, grandClusterName, n, participants, controllers); + String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + + grandClusterName + " true"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Thread.sleep(500); + + // deactivate cluster + command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + grandClusterName + + " false"; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + BaseDataAccessor baseAccessor = new ZkBaseDataAccessor(_gZkClient); + HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor); + String path = accessor.keyBuilder().controllerLeader().getPath(); + for (int i = 0; i < 10; i++) { + Thread.sleep(1000); + if (!_gZkClient.exists(path)) { + break; + } + } + Assert.assertFalse(_gZkClient.exists(path), + "leader should be gone after deactivate the cluster"); + + command = "-zkSvr localhost:2183 -dropCluster " + clusterName; + try { + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + Assert.fail("dropCluster should fail since there are still instances running"); + } catch (Exception e) { + // OK + } + + for (int i = 0; i < n; i++) { + participants[i].syncStop(); + } + + command = "-zkSvr localhost:2183 -dropCluster " + clusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s")); + + for (int i = 0; i < 2; i++) { + controllers[i].syncStop(); + Thread.sleep(1000); + } + command = "-zkSvr localhost:2183 -dropCluster " + grandClusterName; + ClusterSetup.processCommandLineArgs(command.split("\\s+")); + + System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); + } +} http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/7d478437/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminScenariosCli.java ---------------------------------------------------------------------- diff --git a/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminScenariosCli.java b/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminScenariosCli.java deleted file mode 100644 index 9dd41a3..0000000 --- a/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminScenariosCli.java +++ /dev/null @@ -1,452 +0,0 @@ -package org.apache.helix.tools; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Simulate all the admin tasks needed by using command line tool - * - * */ -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.HashMap; -import java.util.Map; - -import org.apache.helix.HelixDataAccessor; -import org.apache.helix.TestHelper; -import org.apache.helix.ZNRecord; -import org.apache.helix.TestHelper.StartCMResult; -import org.apache.helix.controller.HelixControllerMain; -import org.apache.helix.controller.restlet.ZKPropertyTransferServer; -import org.apache.helix.controller.restlet.ZkPropertyTransferClient; -import org.apache.helix.integration.ZkIntegrationTestBase; -import org.apache.helix.manager.zk.ZKUtil; -import org.apache.helix.model.LiveInstance; -import org.apache.helix.tools.ClusterSetup; -import org.apache.helix.tools.ClusterStateVerifier; -import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier; -import org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier; -import org.codehaus.jackson.JsonGenerationException; -import org.codehaus.jackson.map.JsonMappingException; -import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.map.SerializationConfig; -import org.testng.Assert; -import org.testng.annotations.Test; - - -public class TestHelixAdminScenariosCli extends ZkIntegrationTestBase -{ - Map _startCMResultMap = - new HashMap(); - - public static String ObjectToJson(Object object) throws JsonGenerationException, - JsonMappingException, - IOException - { - ObjectMapper mapper = new ObjectMapper(); - SerializationConfig serializationConfig = mapper.getSerializationConfig(); - serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); - - StringWriter sw = new StringWriter(); - mapper.writeValue(sw, object); - - return sw.toString(); - } - - - @Test - public void testAddDeleteClusterAndInstanceAndResource() throws Exception - { - // Helix bug helix-102 - //ZKPropertyTransferServer.PERIOD = 500; - //ZkPropertyTransferClient.SEND_PERIOD = 500; - //ZKPropertyTransferServer.getInstance().init(19999, ZK_ADDR); - - /**======================= Add clusters ==============================*/ - - testAddCluster(); - - /**================= Add / drop some resources ===========================*/ - - testAddResource(); - - /**====================== Add / delete instances ===========================*/ - - testAddInstance(); - - /**===================== Rebalance resource ===========================*/ - - testRebalanceResource(); - - /**==================== start the clusters =============================*/ - - testStartCluster(); - - /**==================== drop add resource in live clusters ===================*/ - testDropAddResource(); - /**======================Operations with live node ============================*/ - - testInstanceOperations(); - - /**============================ expand cluster ===========================*/ - - testExpandCluster(); - - /**============================ deactivate cluster ===========================*/ - testDeactivateCluster(); - - } - - void assertClusterSetupException(String command) - { - boolean exceptionThrown = false; - try - { - ClusterSetup.processCommandLineArgs(command.split(" ")); - } - catch(Exception e) - { - exceptionThrown = true; - } - Assert.assertTrue(exceptionThrown); - } - - public void testAddCluster() throws Exception - { - String command = "--zkSvr localhost:2183 -addCluster clusterTest"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // malformed cluster name - command = "--zkSvr localhost:2183 -addCluster /ClusterTest"; - assertClusterSetupException(command); - - // Add the grand cluster - command = "--zkSvr localhost:2183 -addCluster \"Klazt3rz"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "--zkSvr localhost:2183 -addCluster \\ClusterTest"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // Add already exist cluster - command = "--zkSvr localhost:2183 -addCluster clusterTest"; - assertClusterSetupException(command); - - // delete cluster without resource and instance - Assert.assertTrue(ZKUtil.isClusterSetup("Klazt3rz", _gZkClient)); - Assert.assertTrue(ZKUtil.isClusterSetup("clusterTest", _gZkClient)); - Assert.assertTrue(ZKUtil.isClusterSetup("\\ClusterTest", _gZkClient)); - - command = "-zkSvr localhost:2183 -dropCluster \\ClusterTest"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -dropCluster clusterTest1"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -dropCluster clusterTest"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - Assert.assertFalse(_gZkClient.exists("/clusterTest")); - Assert.assertFalse(_gZkClient.exists("/\\ClusterTest")); - Assert.assertFalse(_gZkClient.exists("/clusterTest1")); - - command = "-zkSvr localhost:2183 -addCluster clusterTest1"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - } - - public void testAddResource() throws Exception - { - String command = "-zkSvr localhost:2183 -addResource clusterTest1 db_22 144 MasterSlave"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -addResource clusterTest1 db_11 44 MasterSlave"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // Add duplicate resource - command = "-zkSvr localhost:2183 -addResource clusterTest1 db_22 55 OnlineOffline"; - assertClusterSetupException(command); - - // drop resource now - command = "-zkSvr localhost:2183 -dropResource clusterTest1 db_11 "; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -addResource clusterTest1 db_11 44 MasterSlave"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - } - - private void testDeactivateCluster() throws Exception, InterruptedException - { - String command; - HelixDataAccessor accessor; - String path; - // deactivate cluster - command = "-zkSvr localhost:2183 -activateCluster clusterTest1 Klazt3rz false"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - Thread.sleep(6000); - - accessor = _startCMResultMap.get("localhost_1231")._manager.getHelixDataAccessor(); - path = accessor.keyBuilder().controllerLeader().getPath(); - Assert.assertFalse(_gZkClient.exists(path)); - - command = "-zkSvr localhost:2183 -dropCluster clusterTest1"; - assertClusterSetupException(command); - - // leader node should be gone - for(StartCMResult result : _startCMResultMap.values()) - { - result._manager.disconnect(); - result._thread.interrupt(); - } - - command = "-zkSvr localhost:2183 -dropCluster clusterTest1"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -dropCluster Klazt3rz"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - } - - private void testDropAddResource() throws Exception - { - ZNRecord record = _gSetupTool._admin.getResourceIdealState("clusterTest1", "db_11").getRecord(); - String x = ObjectToJson(record); - - FileWriter fos = new FileWriter("/tmp/temp.log"); - PrintWriter pw = new PrintWriter(fos); - pw.write(x); - pw.close(); - - String command = "-zkSvr localhost:2183 -dropResource clusterTest1 db_11 "; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - boolean verifyResult = - ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, - "clusterTest1")); - Assert.assertTrue(verifyResult); - - command = "-zkSvr localhost:2183 -addIdealState clusterTest1 db_11 \"/tmp/temp.log\""; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - verifyResult = - ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, - "clusterTest1")); - Assert.assertTrue(verifyResult); - - ZNRecord record2 = _gSetupTool._admin.getResourceIdealState("clusterTest1", "db_11").getRecord(); - Assert.assertTrue(record2.equals(record)); - } - - private void testExpandCluster() throws Exception - { - String command; - HelixDataAccessor accessor; - boolean verifyResult; - String path; - - command = "-zkSvr localhost:2183 -addNode clusterTest1 localhost:12331;localhost:12341;localhost:12351;localhost:12361"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -expandCluster clusterTest1"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - for(int i = 3; i<= 6; i++) - { - StartCMResult result = - TestHelper.startDummyProcess(ZK_ADDR, "clusterTest1", "localhost_123"+i+"1"); - _startCMResultMap.put("localhost_123"+i + "1", result); - } - - verifyResult = - ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, - "clusterTest1")); - Assert.assertTrue(verifyResult); - - verifyResult = - ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, - "clusterTest1")); - Assert.assertTrue(verifyResult); - } - - private void testInstanceOperations() throws Exception - { - String command; - HelixDataAccessor accessor; - boolean verifyResult; - // drop node should fail as not disabled - command = "-zkSvr localhost:2183 -dropNode clusterTest1 localhost:1232"; - assertClusterSetupException(command); - - // disabled node - command = "-zkSvr localhost:2183 -enableInstance clusterTest1 localhost:1232 false"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // Cannot drop / swap - command = "-zkSvr localhost:2183 -dropNode clusterTest1 localhost:1232"; - assertClusterSetupException(command); - - command = "-zkSvr localhost:2183 -swapInstance clusterTest1 localhost_1232 localhost_12320"; - assertClusterSetupException(command); - - // disconnect the node - _startCMResultMap.get("localhost_1232")._manager.disconnect(); - _startCMResultMap.get("localhost_1232")._thread.interrupt(); - - // add new node then swap instance - command = "-zkSvr localhost:2183 -addNode clusterTest1 localhost:12320"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // swap instance. The instance get swapped out should not exist anymore - command = "-zkSvr localhost:2183 -swapInstance clusterTest1 localhost_1232 localhost_12320"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - accessor = _startCMResultMap.get("localhost_1231")._manager.getHelixDataAccessor(); - String path = accessor.keyBuilder().instanceConfig("localhost_1232").getPath(); - Assert.assertFalse(_gZkClient.exists(path)); - - _startCMResultMap.put("localhost_12320", TestHelper.startDummyProcess(ZK_ADDR, "clusterTest1", "localhost_12320")); - } - - private void testStartCluster() throws Exception, InterruptedException - { - String command; - //start mock nodes - for(int i = 0; i < 6 ; i++) - { - StartCMResult result = - TestHelper.startDummyProcess(ZK_ADDR, "clusterTest1", "localhost_123"+i); - _startCMResultMap.put("localhost_123"+i, result); - } - - //start controller nodes - for(int i = 0; i< 2; i++) - { - StartCMResult result = - TestHelper.startController("Klazt3rz", "controller_900" + i, ZK_ADDR, HelixControllerMain.DISTRIBUTED); - - _startCMResultMap.put("controller_900" + i, result); - } - Thread.sleep(100); - - // activate clusters - // wrong grand clustername - command = "-zkSvr localhost:2183 -activateCluster clusterTest1 Klazters true"; - assertClusterSetupException(command); - - // wrong cluster name - command = "-zkSvr localhost:2183 -activateCluster clusterTest2 Klazt3rs true"; - assertClusterSetupException(command); - - command = "-zkSvr localhost:2183 -activateCluster clusterTest1 Klazt3rz true"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - Thread.sleep(500); - - command = "-zkSvr localhost:2183 -dropCluster clusterTest1"; - assertClusterSetupException(command); - - // verify leader node - HelixDataAccessor accessor = _startCMResultMap.get("controller_9001")._manager.getHelixDataAccessor(); - LiveInstance controllerLeader = accessor.getProperty(accessor.keyBuilder().controllerLeader()); - Assert.assertTrue(controllerLeader.getInstanceName().startsWith("controller_900")); - - accessor = _startCMResultMap.get("localhost_1232")._manager.getHelixDataAccessor(); - LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader()); - for(int i = 0; i < 5; i++) - { - if(leader != null) - { - break; - } - Thread.sleep(1000); - leader = accessor.getProperty(accessor.keyBuilder().controllerLeader()); - } - Assert.assertTrue(leader.getInstanceName().startsWith("controller_900")); - - boolean verifyResult = - ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, - "clusterTest1")); - Assert.assertTrue(verifyResult); - - verifyResult = - ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, - "clusterTest1")); - Assert.assertTrue(verifyResult); - } - - private void testRebalanceResource() throws Exception - { - String command = "-zkSvr localhost:2183 -rebalance clusterTest1 db_11 3"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -dropResource clusterTest1 db_11 "; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // re-add and rebalance - command = "-zkSvr localhost:2183 -addResource clusterTest1 db_11 48 MasterSlave"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -rebalance clusterTest1 db_11 3"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // rebalance with key prefix - command = "-zkSvr localhost:2183 -rebalance clusterTest1 db_22 2 -key alias"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - } - - private void testAddInstance() throws Exception - { - String command; - for(int i = 0; i < 3; i++) - { - command = "-zkSvr localhost:2183 -addNode clusterTest1 localhost:123"+i; - ClusterSetup.processCommandLineArgs(command.split(" ")); - } - command = "-zkSvr localhost:2183 -addNode clusterTest1 localhost:1233;localhost:1234;localhost:1235;localhost:1236"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // delete one node without disable - command = "-zkSvr localhost:2183 -dropNode clusterTest1 localhost:1236"; - assertClusterSetupException(command); - - // delete non-exist node - command = "-zkSvr localhost:2183 -dropNode clusterTest1 localhost:12367"; - assertClusterSetupException(command); - - // disable node - command = "-zkSvr localhost:2183 -enableInstance clusterTest1 localhost:1236 false"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -dropNode clusterTest1 localhost:1236"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // add node to controller cluster - command = "-zkSvr localhost:2183 -addNode Klazt3rz controller:9000;controller:9001"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - // add a dup host - command = "-zkSvr localhost:2183 -addNode clusterTest1 localhost:1234"; - assertClusterSetupException(command); - - // drop and add resource - command = "-zkSvr localhost:2183 -dropResource clusterTest1 db_11 "; - ClusterSetup.processCommandLineArgs(command.split(" ")); - - command = "-zkSvr localhost:2183 -addResource clusterTest1 db_11 12 MasterSlave"; - ClusterSetup.processCommandLineArgs(command.split(" ")); - } -}