Java Code Examples for org.apache.helix.model.CurrentState#setStateModelDefRef()
The following examples show how to use
org.apache.helix.model.CurrentState#setStateModelDefRef() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRebalancePipeline.java From helix with Apache License 2.0 | 5 votes |
private void setCurrentState(String clusterName, String instance, String resourceGroupName, String resourceKey, String sessionId, String state, boolean updateTimestamp) { ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<>(_gZkClient)); Builder keyBuilder = accessor.keyBuilder(); CurrentState curState = new CurrentState(resourceGroupName); curState.setState(resourceKey, state); curState.setSessionId(sessionId); curState.setStateModelDefRef("MasterSlave"); if (updateTimestamp) { curState.setEndTime(resourceKey, System.currentTimeMillis()); } accessor.setProperty(keyBuilder.currentState(instance, sessionId, resourceGroupName), curState); }
Example 2
Source File: HelixStateMachineEngine.java From helix with Apache License 2.0 | 4 votes |
@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()) { String initState = _stateModelDefs.get(message.getStateModelDef()).getInitialState(); StateModel stateModel = stateModelFactory.getStateModel(resourceName, partitionKey); if (stateModel == null) { stateModel = stateModelFactory.createAndAddStateModel(resourceName, partitionKey); if (stateModelName.equals(TaskConstants.STATE_MODEL_NAME) && message.getToState().equals(TaskPartitionState.DROPPED.name())) { // If stateModel is null, that means there was a reboot of the Participant. Then the // purpose of this first message must be to drop the task. We manually set the current // state to be the same state of fromState (which Controller inferred from JobContext) to // allow the Participant to successfully process this dropping transition stateModel.updateState(message.getFromState()); } else { 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); } }
Example 3
Source File: TestZkCallbackHandlerLeak.java From helix with Apache License 2.0 | 4 votes |
@Test public void testCurrentStatePathLeakingByAsycRemoval() throws Exception { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String clusterName = className + "_" + methodName; final int n = 3; final String zkAddr = ZK_ADDR; final int mJobUpdateCnt = 500; System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); TestHelper.setupCluster(clusterName, zkAddr, 12918, "localhost", "TestDB", 1, // resource 32, // partitions n, // nodes 2, // replicas "MasterSlave", true); final ClusterControllerManager controller = new ClusterControllerManager(zkAddr, clusterName, "controller_0"); controller.syncStart(); MockParticipantManager[] participants = new MockParticipantManager[n]; for (int i = 0; i < n; i++) { String instanceName = "localhost_" + (12918 + i); participants[i] = new MockParticipantManager(zkAddr, clusterName, instanceName); participants[i].syncStart(); } Boolean result = ClusterStateVerifier.verifyByZkCallback( new ClusterStateVerifier.BestPossAndExtViewZkVerifier(zkAddr, clusterName)); Assert.assertTrue(result); ClusterSpectatorManager rpManager = new ClusterSpectatorManager(ZK_ADDR, clusterName, "router"); rpManager.syncStart(); RoutingTableProvider rp = new RoutingTableProvider(rpManager, PropertyType.CURRENTSTATES); LOG.info("add job"); MockParticipantManager jobParticipant = participants[0]; String jobSessionId = jobParticipant.getSessionId(); HelixDataAccessor jobAccesor = jobParticipant.getHelixDataAccessor(); PropertyKey.Builder jobKeyBuilder = new PropertyKey.Builder(clusterName); PropertyKey db0key = jobKeyBuilder.currentState(jobParticipant.getInstanceName(), jobSessionId, "TestDB0"); CurrentState db0 = jobAccesor.getProperty(db0key); PropertyKey jobKey = jobKeyBuilder.currentState(jobParticipant.getInstanceName(), jobSessionId, "BackupQueue"); CurrentState cs = new CurrentState("BackupQueue"); cs.setSessionId(jobSessionId); cs.setStateModelDefRef(db0.getStateModelDefRef()); LOG.info("add job"); boolean rtJob = false; for (int i = 0; i < mJobUpdateCnt; i++) { rtJob = jobAccesor.setProperty(jobKey, cs); } LOG.info("remove job"); rtJob = jobParticipant.getZkClient().delete(jobKey.getPath()); // validate the job watch is not leaked. Thread.sleep(5000); Map<String, Set<String>> listenersByZkPath = ZkTestHelper.getListenersByZkPath(ZK_ADDR); boolean jobKeyExists = listenersByZkPath.keySet().contains(jobKey.getPath()); Assert.assertFalse(jobKeyExists); Map<String, List<String>> rpWatchPaths = ZkTestHelper.getZkWatch(rpManager.getZkClient()); List<String> existWatches = rpWatchPaths.get("existWatches"); Assert.assertTrue(existWatches.isEmpty()); }
Example 4
Source File: TestZKCallback.java From helix with Apache License 2.0 | 4 votes |
@Test() public void testInvocation() throws Exception { HelixManager testHelixManager = HelixManagerFactory.getZKHelixManager(clusterName, "localhost_8900", InstanceType.PARTICIPANT, ZK_ADDR); testHelixManager.connect(); TestZKCallback test = new TestZKCallback(); TestZKCallback.TestCallbackListener testListener = test.new TestCallbackListener(); testHelixManager.addMessageListener(testListener, "localhost_8900"); testHelixManager.addCurrentStateChangeListener(testListener, "localhost_8900", testHelixManager.getSessionId()); testHelixManager.addCustomizedStateRootChangeListener(testListener, "localhost_8900"); testHelixManager.addConfigChangeListener(testListener); testHelixManager.addIdealStateChangeListener(testListener); testHelixManager.addExternalViewChangeListener(testListener); testHelixManager.addLiveInstanceChangeListener(testListener); // Initial add listener should trigger the first execution of the // listener callbacks AssertJUnit.assertTrue(testListener.configChangeReceived & testListener.currentStateChangeReceived & testListener.externalViewChangeReceived & testListener.idealStateChangeReceived & testListener.liveInstanceChangeReceived & testListener.messageChangeReceived); testListener.Reset(); HelixDataAccessor accessor = testHelixManager.getHelixDataAccessor(); Builder keyBuilder = accessor.keyBuilder(); ExternalView extView = new ExternalView("db-12345"); accessor.setProperty(keyBuilder.externalView("db-12345"), extView); Thread.sleep(100); AssertJUnit.assertTrue(testListener.externalViewChangeReceived); testListener.Reset(); CurrentState curState = new CurrentState("db-12345"); curState.setSessionId("sessionId"); curState.setStateModelDefRef("StateModelDef"); accessor.setProperty(keyBuilder.currentState("localhost_8900", testHelixManager.getSessionId(), curState.getId()), curState); Thread.sleep(100); AssertJUnit.assertTrue(testListener.currentStateChangeReceived); testListener.Reset(); IdealState idealState = new IdealState("db-1234"); idealState.setNumPartitions(400); idealState.setReplicas(Integer.toString(2)); idealState.setStateModelDefRef("StateModeldef"); accessor.setProperty(keyBuilder.idealStates("db-1234"), idealState); Thread.sleep(100); AssertJUnit.assertTrue(testListener.idealStateChangeReceived); testListener.Reset(); // dummyRecord = new ZNRecord("db-12345"); // dataAccessor.setProperty(PropertyType.IDEALSTATES, idealState, "db-12345" // ); // Thread.sleep(100); // AssertJUnit.assertTrue(testListener.idealStateChangeReceived); // testListener.Reset(); // dummyRecord = new ZNRecord("localhost:8900"); // List<ZNRecord> recList = new ArrayList<ZNRecord>(); // recList.add(dummyRecord); testListener.Reset(); Message message = new Message(MessageType.STATE_TRANSITION, UUID.randomUUID().toString()); message.setTgtSessionId("*"); message.setResourceName("testResource"); message.setPartitionName("testPartitionKey"); message.setStateModelDef("MasterSlave"); message.setToState("toState"); message.setFromState("fromState"); message.setTgtName("testTarget"); message.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY); accessor.setProperty(keyBuilder.message("localhost_8900", message.getId()), message); Thread.sleep(500); AssertJUnit.assertTrue(testListener.messageChangeReceived); // dummyRecord = new ZNRecord("localhost_9801"); LiveInstance liveInstance = new LiveInstance("localhost_9801"); liveInstance.setSessionId(UUID.randomUUID().toString()); liveInstance.setHelixVersion(UUID.randomUUID().toString()); accessor.setProperty(keyBuilder.liveInstance("localhost_9801"), liveInstance); Thread.sleep(500); AssertJUnit.assertTrue(testListener.liveInstanceChangeReceived); testListener.Reset(); // dataAccessor.setNodeConfigs(recList); Thread.sleep(100); // AssertJUnit.assertTrue(testListener.configChangeReceived); // testListener.Reset(); accessor.removeProperty(keyBuilder.liveInstance("localhost_8900")); accessor.removeProperty(keyBuilder.liveInstance("localhost_9801")); }