Search in sources :

Example 26 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class WorkflowRebalancer method scheduleSingleJob.

/**
 * Posts new job to cluster
 */
private void scheduleSingleJob(String jobResource, JobConfig jobConfig) {
    HelixAdmin admin = _manager.getClusterManagmentTool();
    IdealState jobIS = admin.getResourceIdealState(_manager.getClusterName(), jobResource);
    if (jobIS != null) {
        LOG.info("Job " + jobResource + " idealstate already exists!");
        return;
    }
    // Set up job resource based on partitions from target resource
    TaskUtil.createUserContent(_manager.getHelixPropertyStore(), jobResource, new ZNRecord(TaskUtil.USER_CONTENT_NODE));
    int numIndependentTasks = jobConfig.getTaskConfigMap().size();
    int numPartitions = numIndependentTasks;
    if (numPartitions == 0) {
        IdealState targetIs = admin.getResourceIdealState(_manager.getClusterName(), jobConfig.getTargetResource());
        if (targetIs == null) {
            LOG.warn("Target resource does not exist for job " + jobResource);
        // do not need to fail here, the job will be marked as failure immediately when job starts running.
        } else {
            numPartitions = targetIs.getPartitionSet().size();
        }
    }
    admin.addResource(_manager.getClusterName(), jobResource, numPartitions, TaskConstants.STATE_MODEL_NAME);
    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
    // Set the job configuration
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    HelixProperty resourceConfig = new HelixProperty(jobResource);
    resourceConfig.getRecord().getSimpleFields().putAll(jobConfig.getResourceConfigMap());
    Map<String, TaskConfig> taskConfigMap = jobConfig.getTaskConfigMap();
    if (taskConfigMap != null) {
        for (TaskConfig taskConfig : taskConfigMap.values()) {
            resourceConfig.getRecord().setMapField(taskConfig.getId(), taskConfig.getConfigMap());
        }
    }
    accessor.setProperty(keyBuilder.resourceConfig(jobResource), resourceConfig);
    // Push out new ideal state based on number of target partitions
    IdealStateBuilder builder = new CustomModeISBuilder(jobResource);
    builder.setRebalancerMode(IdealState.RebalanceMode.TASK);
    builder.setNumReplica(1);
    builder.setNumPartitions(numPartitions);
    builder.setStateModel(TaskConstants.STATE_MODEL_NAME);
    if (jobConfig.getInstanceGroupTag() != null) {
        builder.setNodeGroup(jobConfig.getInstanceGroupTag());
    }
    if (jobConfig.isDisableExternalView()) {
        builder.disableExternalView();
    }
    jobIS = builder.build();
    for (int i = 0; i < numPartitions; i++) {
        jobIS.getRecord().setListField(jobResource + "_" + i, new ArrayList<String>());
        jobIS.getRecord().setMapField(jobResource + "_" + i, new HashMap<String, String>());
    }
    jobIS.setRebalancerClassName(JobRebalancer.class.getName());
    admin.setResourceIdealState(_manager.getClusterName(), jobResource, jobIS);
}
Also used : CustomModeISBuilder(org.apache.helix.model.builder.CustomModeISBuilder) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) IdealStateBuilder(org.apache.helix.model.builder.IdealStateBuilder) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixProperty(org.apache.helix.HelixProperty) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey)

Example 27 with HelixAdmin

use of org.apache.helix.HelixAdmin 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 28 with HelixAdmin

use of org.apache.helix.HelixAdmin 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 29 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class TestReelectedPipelineCorrectness method testReelection.

@Test
public void testReelection() throws Exception {
    final int NUM_CONTROLLERS = 2;
    final int NUM_PARTICIPANTS = 4;
    final int NUM_PARTITIONS = 8;
    final int NUM_REPLICAS = 2;
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
    // 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, "MasterSlave", RebalanceMode.FULL_AUTO, // do rebalance
    true);
    // configure distributed controllers
    String controllerCluster = clusterName + "_controllers";
    setupTool.addCluster(controllerCluster, true);
    for (int i = 0; i < NUM_CONTROLLERS; i++) {
        setupTool.addInstanceToCluster(controllerCluster, "controller_" + i);
    }
    setupTool.activateCluster(clusterName, controllerCluster, true);
    // start participants
    MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
    for (int i = 0; i < NUM_PARTICIPANTS; i++) {
        final String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    // start controllers
    ClusterDistributedController[] controllers = new ClusterDistributedController[NUM_CONTROLLERS];
    for (int i = 0; i < NUM_CONTROLLERS; i++) {
        controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerCluster, "controller_" + i);
        controllers[i].syncStart();
    }
    Thread.sleep(1000);
    // Ensure a balanced cluster
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Disable the leader, resulting in a leader election
    HelixDataAccessor accessor = participants[0].getHelixDataAccessor();
    LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    int totalWait = 0;
    while (leader == null && totalWait < CHECK_TIMEOUT) {
        Thread.sleep(CHECK_INTERVAL);
        totalWait += CHECK_INTERVAL;
        leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    }
    if (totalWait >= CHECK_TIMEOUT) {
        Assert.fail("No leader was ever elected!");
    }
    String leaderId = leader.getId();
    String standbyId = (leaderId.equals("controller_0")) ? "controller_1" : "controller_0";
    HelixAdmin admin = setupTool.getClusterManagementTool();
    admin.enableInstance(controllerCluster, leaderId, false);
    // Stop a participant to make sure that the leader election worked
    Thread.sleep(500);
    participants[0].syncStop();
    Thread.sleep(500);
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Disable the original standby (leaving 0 active controllers) and kill another participant
    admin.enableInstance(controllerCluster, standbyId, false);
    Thread.sleep(500);
    participants[1].syncStop();
    // Also change the ideal state
    IdealState idealState = admin.getResourceIdealState(clusterName, "TestDB0");
    idealState.setMaxPartitionsPerInstance(1);
    admin.setResourceIdealState(clusterName, "TestDB0", idealState);
    Thread.sleep(500);
    // Also disable an instance in the main cluster
    admin.enableInstance(clusterName, "localhost_12920", false);
    // Re-enable the original leader
    admin.enableInstance(controllerCluster, leaderId, true);
    // Now check that both the ideal state and the live instances are adhered to by the rebalance
    Thread.sleep(500);
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // cleanup
    for (int i = 0; i < NUM_CONTROLLERS; i++) {
        controllers[i].syncStop();
    }
    for (int i = 2; i < NUM_PARTICIPANTS; i++) {
        participants[i].syncStop();
    }
    System.out.println("STOP " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixAdmin(org.apache.helix.HelixAdmin) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) Test(org.testng.annotations.Test)

Example 30 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class TestInvalidResourceRebalance method testResourceRebalanceSkipped.

/**
 * Ensure that the Helix controller doesn't attempt to rebalance resources with invalid ideal
 * states
 */
@Test
public void testResourceRebalanceSkipped() throws Exception {
    final int NUM_PARTICIPANTS = 2;
    final int NUM_PARTITIONS = 4;
    final int NUM_REPLICAS = 2;
    final String RESOURCE_NAME = "TestDB0";
    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 SEMI_AUTO mode
    "MasterSlave", // use SEMI_AUTO mode
    RebalanceMode.SEMI_AUTO, // do rebalance
    true);
    // start controller
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // add the ideal state spec (prevents non-CUSTOMIZED MasterSlave ideal states)
    HelixAdmin helixAdmin = controller.getClusterManagmentTool();
    Map<String, String> properties = Maps.newHashMap();
    properties.put("IdealStateRule!sampleRuleName", "IDEAL_STATE_MODE=CUSTOMIZED,STATE_MODEL_DEF_REF=MasterSlave");
    helixAdmin.setConfig(new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build(), properties);
    // start participants
    MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
    for (int i = 0; i < NUM_PARTICIPANTS; i++) {
        final String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    Thread.sleep(1000);
    boolean result = ClusterStateVerifier.verifyByZkCallback(new EmptyZkVerifier(clusterName, RESOURCE_NAME));
    Assert.assertTrue(result, "External view and current state must be empty");
    // cleanup
    for (int i = 0; i < NUM_PARTICIPANTS; i++) {
        participants[i].syncStop();
    }
    controller.syncStop();
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixAdmin(org.apache.helix.HelixAdmin) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

HelixAdmin (org.apache.helix.HelixAdmin)83 Test (org.testng.annotations.Test)40 IdealState (org.apache.helix.model.IdealState)36 ZNRecord (org.apache.helix.ZNRecord)28 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)23 Date (java.util.Date)22 HashMap (java.util.HashMap)19 ExternalView (org.apache.helix.model.ExternalView)16 InstanceConfig (org.apache.helix.model.InstanceConfig)15 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)13 ArrayList (java.util.ArrayList)12 HelixDataAccessor (org.apache.helix.HelixDataAccessor)12 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)12 PropertyKey (org.apache.helix.PropertyKey)11 StateModelDefinition (org.apache.helix.model.StateModelDefinition)11 HelixException (org.apache.helix.HelixException)10 ControllerMetrics (com.linkedin.pinot.common.metrics.ControllerMetrics)9 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)9 Path (javax.ws.rs.Path)9 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)9