Search in sources :

Example 11 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class TestDisableCustomCodeRunner method test.

@Test
public void test() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    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
    PARTITION_NUM, // number of nodes
    N, // replicas
    2, "MasterSlave", // do rebalance
    true);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // start participants
    Map<String, MockParticipantManager> participants = new HashMap<String, MockParticipantManager>();
    Map<String, HelixCustomCodeRunner> customCodeRunners = new HashMap<String, HelixCustomCodeRunner>();
    Map<String, DummyCallback> callbacks = new HashMap<String, DummyCallback>();
    for (int i = 0; i < N; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants.put(instanceName, new MockParticipantManager(ZK_ADDR, clusterName, instanceName));
        customCodeRunners.put(instanceName, new HelixCustomCodeRunner(participants.get(instanceName), ZK_ADDR));
        callbacks.put(instanceName, new DummyCallback());
        customCodeRunners.get(instanceName).invoke(callbacks.get(instanceName)).on(ChangeType.LIVE_INSTANCE).usingLeaderStandbyModel("TestParticLeader").start();
        participants.get(instanceName).syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Make sure callback is registered
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    final String customCodeRunnerResource = customCodeRunners.get("localhost_12918").getResourceName();
    ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
    Map<String, String> instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
    String leader = null;
    for (String instance : instanceStates.keySet()) {
        String state = instanceStates.get(instance);
        if ("LEADER".equals(state)) {
            leader = instance;
            break;
        }
    }
    Assert.assertNotNull(leader);
    for (String instance : callbacks.keySet()) {
        DummyCallback callback = callbacks.get(instance);
        if (instance.equals(leader)) {
            Assert.assertTrue(callback.isInitTypeInvoked());
        } else {
            Assert.assertFalse(callback.isInitTypeInvoked());
        }
        callback.reset();
    }
    // Disable custom-code runner resource
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.enableResource(clusterName, customCodeRunnerResource, false);
    // Verify that states of custom-code runner are all OFFLINE
    result = TestHelper.verify(new TestHelper.Verifier() {

        @Override
        public boolean verify() throws Exception {
            PropertyKey.Builder keyBuilder = accessor.keyBuilder();
            ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
            if (extView == null) {
                return false;
            }
            Set<String> partitionSet = extView.getPartitionSet();
            if (partitionSet == null || partitionSet.size() != PARTITION_NUM) {
                return false;
            }
            for (String partition : partitionSet) {
                Map<String, String> instanceStates = extView.getStateMap(partition);
                for (String state : instanceStates.values()) {
                    if (!"OFFLINE".equals(state)) {
                        return false;
                    }
                }
            }
            return true;
        }
    }, 10 * 1000);
    Assert.assertTrue(result);
    // Change live-instance should not invoke any custom-code runner
    LiveInstance fakeInstance = new LiveInstance("fakeInstance");
    fakeInstance.setSessionId("fakeSessionId");
    fakeInstance.setHelixVersion("0.6");
    accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
    Thread.sleep(1000);
    for (Map.Entry<String, DummyCallback> e : callbacks.entrySet()) {
        String instance = e.getKey();
        DummyCallback callback = e.getValue();
        Assert.assertFalse(callback.isInitTypeInvoked());
        Assert.assertFalse(callback.isCallbackTypeInvoked());
        // Ensure that we were told that a leader stopped being the leader
        if (instance.equals(leader)) {
            Assert.assertTrue(callback.isFinalizeTypeInvoked());
        }
    }
    // Remove fake instance
    accessor.removeProperty(keyBuilder.liveInstance("fakeInstance"));
    // Re-enable custom-code runner
    admin.enableResource(clusterName, customCodeRunnerResource, true);
    result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Verify that custom-invoke is invoked again
    extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
    instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
    leader = null;
    for (String instance : instanceStates.keySet()) {
        String state = instanceStates.get(instance);
        if ("LEADER".equals(state)) {
            leader = instance;
            break;
        }
    }
    Assert.assertNotNull(leader);
    for (String instance : callbacks.keySet()) {
        DummyCallback callback = callbacks.get(instance);
        if (instance.equals(leader)) {
            Assert.assertTrue(callback.isInitTypeInvoked());
        } else {
            Assert.assertFalse(callback.isInitTypeInvoked());
        }
        callback.reset();
    }
    // Add a fake instance should invoke custom-code runner
    accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
    Thread.sleep(1000);
    for (String instance : callbacks.keySet()) {
        DummyCallback callback = callbacks.get(instance);
        if (instance.equals(leader)) {
            Assert.assertTrue(callback.isCallbackTypeInvoked());
        } else {
            Assert.assertFalse(callback.isCallbackTypeInvoked());
        }
    }
    // Clean up
    controller.syncStop();
    for (MockParticipantManager participant : participants.values()) {
        participant.syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HashMap(java.util.HashMap) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) HelixCustomCodeRunner(org.apache.helix.participant.HelixCustomCodeRunner) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HashMap(java.util.HashMap) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 12 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class TestDisableNode method testDisableNode.

@Test()
public void testDisableNode() throws Exception {
    String command = "-zkSvr " + ZK_ADDR + " -enableInstance " + CLUSTER_NAME + " " + PARTICIPANT_PREFIX + "_12918" + " TestDB TestDB_0 false";
    ClusterSetup.processCommandLineArgs(command.split(" "));
    boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    ZKHelixAdmin tool = new ZKHelixAdmin(_gZkClient);
    tool.enableInstance(CLUSTER_NAME, PARTICIPANT_PREFIX + "_12918", true);
    result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Test(org.testng.annotations.Test)

Example 13 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class TestDisableResource method enableResource.

private void enableResource(String clusterName, boolean enabled) {
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.enableResource(clusterName, "TestDB0", enabled);
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin)

Example 14 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class TestResourceGroupEndtoEnd method beforeClass.

@BeforeClass
public void beforeClass() throws Exception {
    _admin = new ZKHelixAdmin(_gZkClient);
    // setup storage cluster
    _gSetupTool.addCluster(CLUSTER_NAME, true);
    List<String> instanceGroupTags = new ArrayList<String>();
    for (int i = 0; i < INSTANCE_GROUP_NR; i++) {
        String groupTag = "cluster_" + i;
        addInstanceGroup(CLUSTER_NAME, groupTag, GROUP_NODE_NR);
        instanceGroupTags.add(groupTag);
    }
    for (String tag : instanceGroupTags) {
        List<String> instances = _admin.getInstancesInClusterWithTag(CLUSTER_NAME, tag);
        IdealState idealState = createIdealState(TEST_DB, tag, instances, PARTITIONS, _replica, IdealState.RebalanceMode.CUSTOMIZED.toString(), BuiltInStateModelDefinitions.OnlineOffline.name());
        _gSetupTool.addResourceToCluster(CLUSTER_NAME, idealState.getResourceName(), idealState);
    }
    // start dummy participants
    int i = 0;
    for (String group : instanceGroupTags) {
        List<String> instances = _admin.getInstancesInClusterWithTag(CLUSTER_NAME, group);
        for (String instance : instances) {
            _participants[i] = new TestParticipantManager(ZK_ADDR, CLUSTER_NAME, TEST_DB, group, instance);
            _participants[i].syncStart();
            i++;
        }
    }
    // start controller
    String controllerName = CONTROLLER_PREFIX + "_0";
    _controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
    _controller.syncStart();
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    // start speculator
    _routingTableProvider = new RoutingTableProvider();
    _spectator = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "spectator", InstanceType.SPECTATOR, ZK_ADDR);
    _spectator.connect();
    _spectator.addExternalViewChangeListener(_routingTableProvider);
    Thread.sleep(1000);
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ArrayList(java.util.ArrayList) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) IdealState(org.apache.helix.model.IdealState) RoutingTableProvider(org.apache.helix.spectator.RoutingTableProvider) BeforeClass(org.testng.annotations.BeforeClass)

Example 15 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class TestEntropyFreeNodeBounce method testBounceAll.

@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
    // 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
    NUM_PARTITIONS, // number of nodes
    NUM_PARTICIPANTS, // replicas
    NUM_REPLICAS, // use FULL_AUTO mode to test node tagging
    "OnlineOffline", // use FULL_AUTO mode to test node tagging
    RebalanceMode.FULL_AUTO, // do rebalance
    true);
    // 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()));
    }
}
Also used : ExternalView(org.apache.helix.model.ExternalView) ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixManager(org.apache.helix.HelixManager) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)70 HelixAdmin (org.apache.helix.HelixAdmin)31 IdealState (org.apache.helix.model.IdealState)25 Test (org.testng.annotations.Test)23 Date (java.util.Date)21 InstanceConfig (org.apache.helix.model.InstanceConfig)16 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)14 ZNRecord (org.apache.helix.ZNRecord)13 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)13 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)12 ZkClient (org.apache.helix.manager.zk.ZkClient)12 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)11 StateModelDefinition (org.apache.helix.model.StateModelDefinition)11 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10 HelixDataAccessor (org.apache.helix.HelixDataAccessor)8 ExternalView (org.apache.helix.model.ExternalView)8 Test (org.junit.Test)8 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)7