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);
}
}
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);
}
}
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();
}
}
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);
}
}
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.
}
}
}
}
}
Aggregations