Search in sources :

Example 1 with StateModel

use of org.apache.helix.participant.statemachine.StateModel in project bookkeeper by apache.

the class HelixStorageContainerManager method doStart.

@Override
protected void doStart() {
    // create the controller
    try (HelixStorageController controller = new HelixStorageController(zkServers)) {
        controller.addNode(clusterName, endpoint, endpointName);
    }
    StateMachineEngine sme = this.manager.getStateMachineEngine();
    StateModelFactory<StateModel> smFactory = new WriteReadStateModelFactory(registry);
    sme.registerStateModelFactory(WriteReadSMD.NAME, smFactory);
    try {
        manager.connect();
        manager.addExternalViewChangeListener(rtProvider);
    } catch (Exception e) {
        throw new StorageRuntimeException(e);
    }
}
Also used : StateMachineEngine(org.apache.helix.participant.StateMachineEngine) StateModel(org.apache.helix.participant.statemachine.StateModel) StorageRuntimeException(org.apache.bookkeeper.stream.storage.exceptions.StorageRuntimeException) IOException(java.io.IOException) StorageRuntimeException(org.apache.bookkeeper.stream.storage.exceptions.StorageRuntimeException)

Example 2 with StateModel

use of org.apache.helix.participant.statemachine.StateModel in project helix by apache.

the class TestStateModelLeak method checkStateModelMap.

/**
 * check state-model factory contains state-models same as in expect-state-model map
 * @param fty
 * @param expectStateModelMap
 */
static void checkStateModelMap(StateModelFactory<? extends StateModel> fty, Map<String, String> expectStateModelMap) {
    Assert.assertEquals(fty.getPartitionSet("TestDB0").size(), expectStateModelMap.size());
    for (String partition : fty.getPartitionSet("TestDB0")) {
        StateModel stateModel = fty.getStateModel("TestDB0", partition);
        String actualState = stateModel.getCurrentState();
        String expectState = expectStateModelMap.get(partition);
        LOG.debug(partition + " actual state: " + actualState + ", expect state: " + expectState);
        Assert.assertEquals(actualState, expectState, "partition: " + partition + " should be in state: " + expectState + " but was " + actualState);
    }
}
Also used : StateModel(org.apache.helix.participant.statemachine.StateModel)

Example 3 with StateModel

use of org.apache.helix.participant.statemachine.StateModel in project helix by apache.

the class TestZkReconnect method testZKReconnect.

@Test(enabled = false)
public void testZKReconnect() throws Exception {
    final AtomicReference<ZkServer> zkServerRef = new AtomicReference<ZkServer>();
    final int zkPort = TestHelper.getRandomPort();
    final String zkAddr = String.format("localhost:%d", zkPort);
    ZkServer zkServer = TestHelper.startZkServer(zkAddr);
    zkServerRef.set(zkServer);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    // Setup cluster
    LOG.info("Setup clusters");
    ClusterSetup clusterSetup = new ClusterSetup(zkAddr);
    clusterSetup.addCluster(clusterName, true);
    // Registers and starts controller
    LOG.info("Starts controller");
    HelixManager controller = HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.CONTROLLER, zkAddr);
    controller.connect();
    // Registers and starts participant
    LOG.info("Starts participant");
    String hostname = "localhost";
    String instanceId = String.format("%s_%d", hostname, 1);
    clusterSetup.addInstanceToCluster(clusterName, instanceId);
    HelixManager participant = HelixManagerFactory.getZKHelixManager(clusterName, instanceId, InstanceType.PARTICIPANT, zkAddr);
    participant.connect();
    LOG.info("Register state machine");
    final CountDownLatch latch = new CountDownLatch(1);
    participant.getStateMachineEngine().registerStateModelFactory("OnlineOffline", new StateModelFactory<StateModel>() {

        @Override
        public StateModel createNewStateModel(String resource, String stateUnitKey) {
            return new SimpleStateModel(latch);
        }
    }, "test");
    String resourceName = "test-resource";
    LOG.info("Ideal state assignment");
    HelixAdmin helixAdmin = participant.getClusterManagmentTool();
    helixAdmin.addResource(clusterName, resourceName, 1, "OnlineOffline", IdealState.RebalanceMode.CUSTOMIZED.toString());
    IdealState idealState = helixAdmin.getResourceIdealState(clusterName, resourceName);
    idealState.setReplicas("1");
    idealState.setStateModelFactoryName("test");
    idealState.setPartitionState(resourceName + "_0", instanceId, "ONLINE");
    LOG.info("Shutdown ZK server");
    TestHelper.stopZkServer(zkServerRef.get());
    Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {

        @Override
        public void run() {
            try {
                LOG.info("Restart ZK server");
                // zkServer.set(TestUtils.startZookeeper(zkDir, zkPort));
                zkServerRef.set(TestHelper.startZkServer(zkAddr, null, false));
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
        }
    }, 2L, TimeUnit.SECONDS);
    // future.get();
    LOG.info("Before update ideal state");
    helixAdmin.setResourceIdealState(clusterName, resourceName, idealState);
    LOG.info("After update ideal state");
    LOG.info("Wait for OFFLINE->ONLINE state transition");
    try {
        Assert.assertTrue(latch.await(15, TimeUnit.SECONDS));
        // wait until stable state
        boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(zkAddr, clusterName));
        Assert.assertTrue(result);
    } finally {
        participant.disconnect();
        zkServerRef.get().shutdown();
    }
}
Also used : ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixManager(org.apache.helix.HelixManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterSetup(org.apache.helix.tools.ClusterSetup) CountDownLatch(java.util.concurrent.CountDownLatch) HelixAdmin(org.apache.helix.HelixAdmin) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) IdealState(org.apache.helix.model.IdealState) StateModel(org.apache.helix.participant.statemachine.StateModel) ZkServer(org.I0Itec.zkclient.ZkServer) Test(org.testng.annotations.Test)

Example 4 with StateModel

use of org.apache.helix.participant.statemachine.StateModel in project helix by apache.

the class HelixStateMachineEngine method createHandler.

@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
    String type = message.getMsgType();
    if (!type.equals(MessageType.STATE_TRANSITION.name()) && !type.equals(MessageType.STATE_TRANSITION_CANCELLATION.name())) {
        throw new HelixException("Expect state-transition message type, but was " + message.getMsgType() + ", msgId: " + message.getMsgId());
    }
    String partitionKey = message.getPartitionName();
    String stateModelName = message.getStateModelDef();
    String resourceName = message.getResourceName();
    String sessionId = message.getTgtSessionId();
    int bucketSize = message.getBucketSize();
    if (stateModelName == null) {
        logger.error("Fail to create msg-handler because message does not contain stateModelDef. msgId: " + message.getId());
        return null;
    }
    String factoryName = message.getStateModelFactoryName();
    if (factoryName == null) {
        factoryName = HelixConstants.DEFAULT_STATE_MODEL_FACTORY;
    }
    StateModelFactory<? extends StateModel> stateModelFactory = getStateModelFactory(stateModelName, factoryName);
    if (stateModelFactory == null) {
        logger.warn("Fail to create msg-handler because cannot find stateModelFactory for model: " + stateModelName + " using factoryName: " + factoryName + " for resource: " + resourceName);
        return null;
    }
    // check if the state model definition exists and cache it
    if (!_stateModelDefs.containsKey(stateModelName)) {
        HelixDataAccessor accessor = _manager.getHelixDataAccessor();
        Builder keyBuilder = accessor.keyBuilder();
        StateModelDefinition stateModelDef = accessor.getProperty(keyBuilder.stateModelDef(stateModelName));
        if (stateModelDef == null) {
            throw new HelixException("fail to create msg-handler because stateModelDef for " + stateModelName + " does NOT exist");
        }
        _stateModelDefs.put(stateModelName, stateModelDef);
    }
    if (message.getBatchMessageMode() == false) {
        String initState = _stateModelDefs.get(message.getStateModelDef()).getInitialState();
        StateModel stateModel = stateModelFactory.getStateModel(resourceName, partitionKey);
        if (stateModel == null) {
            stateModel = stateModelFactory.createAndAddStateModel(resourceName, partitionKey);
            stateModel.updateState(initState);
        }
        if (message.getMsgType().equals(MessageType.STATE_TRANSITION_CANCELLATION.name())) {
            return new HelixStateTransitionCancellationHandler(stateModel, message, context);
        } else {
            // create currentStateDelta for this partition
            // TODO: move currentStateDelta to StateTransitionMsgHandler
            CurrentState currentStateDelta = new CurrentState(resourceName);
            currentStateDelta.setSessionId(sessionId);
            currentStateDelta.setStateModelDefRef(stateModelName);
            currentStateDelta.setStateModelFactoryName(factoryName);
            currentStateDelta.setBucketSize(bucketSize);
            currentStateDelta.setState(partitionKey, (stateModel.getCurrentState() == null) ? initState : stateModel.getCurrentState());
            return new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta);
        }
    } else {
        BatchMessageWrapper wrapper = stateModelFactory.getBatchMessageWrapper(resourceName);
        if (wrapper == null) {
            wrapper = stateModelFactory.createAndAddBatchMessageWrapper(resourceName);
        }
        // get executor-service for the message
        TaskExecutor executor = (TaskExecutor) context.get(MapKey.TASK_EXECUTOR.toString());
        if (executor == null) {
            logger.error("fail to get executor-service for batch message: " + message.getId() + ". msgType: " + message.getMsgType() + ", resource: " + message.getResourceName());
            return null;
        }
        return new BatchMessageHandler(message, context, this, wrapper, executor);
    }
}
Also used : HelixStateTransitionCancellationHandler(org.apache.helix.messaging.handling.HelixStateTransitionCancellationHandler) Builder(org.apache.helix.PropertyKey.Builder) BatchMessageWrapper(org.apache.helix.messaging.handling.BatchMessageWrapper) HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) TaskExecutor(org.apache.helix.messaging.handling.TaskExecutor) StateModelDefinition(org.apache.helix.model.StateModelDefinition) CurrentState(org.apache.helix.model.CurrentState) StateModel(org.apache.helix.participant.statemachine.StateModel) HelixStateTransitionHandler(org.apache.helix.messaging.handling.HelixStateTransitionHandler) BatchMessageHandler(org.apache.helix.messaging.handling.BatchMessageHandler)

Example 5 with StateModel

use of org.apache.helix.participant.statemachine.StateModel in project helix by apache.

the class HelixStateMachineEngine method reset.

@Override
public void reset() {
    for (Map<String, StateModelFactory<? extends StateModel>> ftyMap : _stateModelFactoryMap.values()) {
        for (StateModelFactory<? extends StateModel> stateModelFactory : ftyMap.values()) {
            for (String resourceName : stateModelFactory.getResourceSet()) {
                for (String partitionKey : stateModelFactory.getPartitionSet(resourceName)) {
                    StateModel stateModel = stateModelFactory.getStateModel(resourceName, partitionKey);
                    stateModel.reset();
                    String initialState = _stateModelParser.getInitialState(stateModel.getClass());
                    stateModel.updateState(initialState);
                // TODO probably should update the state on ZK. Shi confirm what needs
                // to be done here.
                }
            }
        }
    }
}
Also used : StateModelFactory(org.apache.helix.participant.statemachine.StateModelFactory) StateModel(org.apache.helix.participant.statemachine.StateModel)

Aggregations

StateModel (org.apache.helix.participant.statemachine.StateModel)5 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ZkServer (org.I0Itec.zkclient.ZkServer)1 StorageRuntimeException (org.apache.bookkeeper.stream.storage.exceptions.StorageRuntimeException)1 HelixAdmin (org.apache.helix.HelixAdmin)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1 Builder (org.apache.helix.PropertyKey.Builder)1 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)1 BatchMessageHandler (org.apache.helix.messaging.handling.BatchMessageHandler)1 BatchMessageWrapper (org.apache.helix.messaging.handling.BatchMessageWrapper)1 HelixStateTransitionCancellationHandler (org.apache.helix.messaging.handling.HelixStateTransitionCancellationHandler)1 HelixStateTransitionHandler (org.apache.helix.messaging.handling.HelixStateTransitionHandler)1 TaskExecutor (org.apache.helix.messaging.handling.TaskExecutor)1 CurrentState (org.apache.helix.model.CurrentState)1 IdealState (org.apache.helix.model.IdealState)1 StateModelDefinition (org.apache.helix.model.StateModelDefinition)1