Search in sources :

Example 6 with StateMachineEngine

use of org.apache.helix.participant.StateMachineEngine in project helix by apache.

the class ExampleProcess method start.

public void start() throws Exception {
    manager = HelixManagerFactory.getZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, zkConnectString);
    if ("MasterSlave".equalsIgnoreCase(stateModelType)) {
        stateModelFactory = new MasterSlaveStateModelFactory(this.instanceName, delay);
    } else if ("OnlineOffline".equalsIgnoreCase(stateModelType)) {
        stateModelFactory = new OnlineOfflineStateModelFactory(this.instanceName, delay);
    } else if ("LeaderStandby".equalsIgnoreCase(stateModelType)) {
        stateModelFactory = new LeaderStandbyStateModelFactory(this.instanceName, delay);
    }
    // genericStateMachineHandler = new StateMachineEngine();
    // genericStateMachineHandler.registerStateModelFactory(stateModelType,
    // stateModelFactory);
    StateMachineEngine stateMach = manager.getStateMachineEngine();
    stateMach.registerStateModelFactory(stateModelType, stateModelFactory);
    manager.connect();
    manager.getMessagingService().registerMessageHandlerFactory(MessageType.STATE_TRANSITION.name(), stateMach);
}
Also used : StateMachineEngine(org.apache.helix.participant.StateMachineEngine)

Example 7 with StateMachineEngine

use of org.apache.helix.participant.StateMachineEngine in project helix by apache.

the class Worker method connect.

public void connect() {
    try {
        _manager = HelixManagerFactory.getZKHelixManager(_clusterName, _instanceName, InstanceType.PARTICIPANT, _zkAddr);
        StateMachineEngine stateMach = _manager.getStateMachineEngine();
        TaskStateModelFactory modelFactory = new TaskStateModelFactory(_instanceName, _taskFactory, _taskResultStore);
        stateMach.registerStateModelFactory(TaskCluster.DEFAULT_STATE_MODEL, modelFactory);
        _manager.connect();
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        System.err.println(" [-] " + _instanceName + " is interrupted ...");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        disconnect();
    }
}
Also used : StateMachineEngine(org.apache.helix.participant.StateMachineEngine)

Example 8 with StateMachineEngine

use of org.apache.helix.participant.StateMachineEngine in project helix by apache.

the class TestJobQueuesResource method test.

@Test
public void test() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    final int n = 5;
    final int p = 20;
    final int r = 3;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    _gSetupTool.addCluster(clusterName, true);
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        _gSetupTool.addInstanceToCluster(clusterName, instanceName);
    }
    // Set up target db
    _gSetupTool.addResourceToCluster(clusterName, WorkflowGenerator.DEFAULT_TGT_DB, p, "MasterSlave");
    _gSetupTool.rebalanceStorageCluster(clusterName, WorkflowGenerator.DEFAULT_TGT_DB, r);
    Map<String, TaskFactory> taskFactoryReg = new HashMap<String, TaskFactory>();
    taskFactoryReg.put("DummyTask", new TaskFactory() {

        @Override
        public Task createNewTask(TaskCallbackContext context) {
            return new MockTask(context);
        }
    });
    // Start dummy 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);
        // Register a Task state model factory.
        StateMachineEngine stateMachine = participants[i].getStateMachineEngine();
        stateMachine.registerStateModelFactory("Task", new TaskStateModelFactory(participants[i], taskFactoryReg));
        participants[i].syncStart();
    }
    // start controller
    String controllerName = "controller";
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, controllerName);
    controller.syncStart();
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Start a queue
    String queueName = "myQueue1";
    LOG.info("Starting job-queue: " + queueName);
    String jobQueueYamlConfig = "name: " + queueName;
    String resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues";
    ZNRecord postRet = AdminTestHelper.post(_gClient, resourceUrl, jobQueueYamlConfig);
    LOG.info("Started job-queue: " + queueName + ", ret: " + postRet);
    LOG.info("Getting all job-queues");
    ZNRecord getRet = AdminTestHelper.get(_gClient, resourceUrl);
    LOG.info("Got job-queues: " + getRet);
    // Enqueue job
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
    WorkflowBean wfBean = new WorkflowBean();
    wfBean.name = queueName;
    JobBean jBean1 = new JobBean();
    jBean1.name = "myJob1";
    jBean1.command = "DummyTask";
    jBean1.targetResource = WorkflowGenerator.DEFAULT_TGT_DB;
    jBean1.targetPartitionStates = Lists.newArrayList("MASTER");
    JobBean jBean2 = new JobBean();
    jBean2.name = "myJob2";
    jBean2.command = "DummyTask";
    jBean2.targetResource = WorkflowGenerator.DEFAULT_TGT_DB;
    jBean2.targetPartitionStates = Lists.newArrayList("SLAVE");
    wfBean.jobs = Lists.newArrayList(jBean1, jBean2);
    String jobYamlConfig = new Yaml().dump(wfBean);
    LOG.info("Enqueuing jobs: " + jobQueueYamlConfig);
    Map<String, String> paraMap = new HashMap<String, String>();
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.start.toString());
    String postBody = String.format("%s=%s&%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap), ResourceUtil.YamlParamKey.NEW_JOB.toString(), jobYamlConfig);
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Enqueued job, ret: " + postRet);
    // Get job
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName + "/" + jBean1.name;
    getRet = AdminTestHelper.get(_gClient, resourceUrl);
    LOG.info("Got job: " + getRet);
    // Stop job queue
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.stop.toString());
    postBody = String.format("%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap));
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Stopped job-queue, ret: " + postRet);
    // Delete a job
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName + "/" + jBean2.name;
    AdminTestHelper.delete(_gClient, resourceUrl);
    LOG.info("Delete a job: ");
    // Resume job queue
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.resume.toString());
    postBody = String.format("%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap));
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Resumed job-queue, ret: " + postRet);
    // Flush job queue
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, "flush");
    postBody = JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap);
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Flushed job-queue, ret: " + postRet);
    // clean up
    controller.syncStop();
    for (int i = 0; i < n; i++) {
        if (participants[i] != null && participants[i].isConnected()) {
            participants[i].syncStop();
        }
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : Task(org.apache.helix.task.Task) MockTask(org.apache.helix.integration.task.MockTask) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HashMap(java.util.HashMap) MockTask(org.apache.helix.integration.task.MockTask) WorkflowBean(org.apache.helix.task.beans.WorkflowBean) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) TaskCallbackContext(org.apache.helix.task.TaskCallbackContext) Date(java.util.Date) Yaml(org.yaml.snakeyaml.Yaml) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) JobBean(org.apache.helix.task.beans.JobBean) TaskFactory(org.apache.helix.task.TaskFactory) TaskStateModelFactory(org.apache.helix.task.TaskStateModelFactory) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 9 with StateMachineEngine

use of org.apache.helix.participant.StateMachineEngine in project helix by apache.

the class HelixAgentMain method main.

public static void main(String[] args) throws Exception {
    CommandLine cmd = processCommandLineArgs(args);
    String zkAddress = cmd.getOptionValue(zkAddr);
    String clusterName = cmd.getOptionValue(cluster);
    String instance = cmd.getOptionValue(instanceName);
    String stateModelName = cmd.getOptionValue(stateModel);
    HelixManager manager = new ZKHelixManager(clusterName, instance, InstanceType.PARTICIPANT, zkAddress);
    StateMachineEngine stateMach = manager.getStateMachineEngine();
    stateMach.registerStateModelFactory(stateModelName, new AgentStateModelFactory());
    Runtime.getRuntime().addShutdownHook(new HelixAgentShutdownHook(manager));
    try {
        manager.connect();
        Thread.currentThread().join();
    } catch (Exception e) {
        LOG.error(e.toString());
    } finally {
        if (manager != null && manager.isConnected()) {
            manager.disconnect();
        }
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) ParseException(org.apache.commons.cli.ParseException)

Example 10 with StateMachineEngine

use of org.apache.helix.participant.StateMachineEngine in project helix by apache.

the class TestSemiAutoStateTransition method testOfflineToSecondTopState.

@Test
public void testOfflineToSecondTopState() throws Exception {
    _participants[0].syncStop();
    Thread.sleep(2000L);
    ExternalView externalView = _accessor.getProperty(_keyBuilder.externalView(WorkflowGenerator.DEFAULT_TGT_DB + "0"));
    Map<String, String> stateMap = externalView.getStateMap(WorkflowGenerator.DEFAULT_TGT_DB + "0_0");
    Assert.assertEquals("MASTER", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 1)));
    Assert.assertEquals("SLAVE", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 2)));
    String instanceName = PARTICIPANT_PREFIX + "_" + _startPort;
    _participants[0] = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instanceName);
    // add a state model with non-OFFLINE initial state
    StateMachineEngine stateMach = _participants[0].getStateMachineEngine();
    MockDelayMSStateModelFactory delayFactory = new MockDelayMSStateModelFactory().setDelay(300000L);
    stateMach.registerStateModelFactory(MASTER_SLAVE_STATE_MODEL, delayFactory);
    _participants[0].syncStart();
    Thread.sleep(2000L);
    externalView = _accessor.getProperty(_keyBuilder.externalView(WorkflowGenerator.DEFAULT_TGT_DB + "0"));
    stateMap = externalView.getStateMap(WorkflowGenerator.DEFAULT_TGT_DB + "0_0");
    Assert.assertEquals("OFFLINE", stateMap.get(PARTICIPANT_PREFIX + "_" + _startPort));
    Assert.assertEquals("MASTER", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 1)));
    Assert.assertEquals("SLAVE", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 2)));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) MockDelayMSStateModelFactory(org.apache.helix.mock.participant.MockDelayMSStateModelFactory) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Test(org.testng.annotations.Test)

Aggregations

StateMachineEngine (org.apache.helix.participant.StateMachineEngine)30 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)11 HashMap (java.util.HashMap)9 HelixManager (org.apache.helix.HelixManager)7 Task (org.apache.helix.task.Task)7 TaskCallbackContext (org.apache.helix.task.TaskCallbackContext)7 TaskFactory (org.apache.helix.task.TaskFactory)7 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)6 TaskStateModelFactory (org.apache.helix.task.TaskStateModelFactory)6 ClusterSetup (org.apache.helix.tools.ClusterSetup)4 BeforeClass (org.testng.annotations.BeforeClass)4 IOException (java.io.IOException)3 MockTask (org.apache.helix.integration.task.MockTask)3 TaskDriver (org.apache.helix.task.TaskDriver)3 Test (org.testng.annotations.Test)3 Date (java.util.Date)2 ParseException (org.apache.commons.cli.ParseException)2 ZNRecord (org.apache.helix.ZNRecord)2 MockDelayMSStateModelFactory (org.apache.helix.mock.participant.MockDelayMSStateModelFactory)2 MockTaskStateModelFactory (org.apache.helix.mock.statemodel.MockTaskStateModelFactory)2