Java Code Examples for org.apache.helix.model.Message#setTgtName()
The following examples show how to use
org.apache.helix.model.Message#setTgtName() .
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: TestPerInstanceAccessor.java From helix with Apache License 2.0 | 6 votes |
@Test(dependsOnMethods = "testIsInstanceStoppable") public void testGetAllMessages() throws IOException { System.out.println("Start test :" + TestHelper.getTestMethodName()); String testInstance = CLUSTER_NAME + "localhost_12926"; //Non-live instance String messageId = "msg1"; Message message = new Message(Message.MessageType.STATE_TRANSITION, messageId); message.setStateModelDef("MasterSlave"); message.setFromState("OFFLINE"); message.setToState("SLAVE"); message.setResourceName("testResourceName"); message.setPartitionName("testResourceName_1"); message.setTgtName("localhost_3"); message.setTgtSessionId("session_3"); HelixDataAccessor helixDataAccessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor); helixDataAccessor.setProperty(helixDataAccessor.keyBuilder().message(testInstance, messageId), message); String body = new JerseyUriRequestBuilder("clusters/{}/instances/{}/messages") .isBodyReturnExpected(true).format(CLUSTER_NAME, testInstance).get(this); JsonNode node = OBJECT_MAPPER.readTree(body); int newMessageCount = node.get(PerInstanceAccessor.PerInstanceProperties.total_message_count.name()).getIntValue(); Assert.assertEquals(newMessageCount, 1); System.out.println("End test :" + TestHelper.getTestMethodName()); }
Example 2
Source File: DefaultMessagingService.java From helix with Apache License 2.0 | 6 votes |
private void sendNopMessageInternal() { try { Message nopMsg = new Message(MessageType.NO_OP, UUID.randomUUID().toString()); nopMsg.setSrcName(_manager.getInstanceName()); HelixDataAccessor accessor = _manager.getHelixDataAccessor(); Builder keyBuilder = accessor.keyBuilder(); if (_manager.getInstanceType() == InstanceType.CONTROLLER || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) { nopMsg.setTgtName(InstanceType.CONTROLLER.name()); accessor.setProperty(keyBuilder.controllerMessage(nopMsg.getId()), nopMsg); } if (_manager.getInstanceType() == InstanceType.PARTICIPANT || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) { nopMsg.setTgtName(_manager.getInstanceName()); accessor.setProperty(keyBuilder.message(nopMsg.getTgtName(), nopMsg.getId()), nopMsg); } } catch (Exception e) { _logger.error(e.toString()); } }
Example 3
Source File: MessagePoster.java From helix with Apache License 2.0 | 6 votes |
public void post(String zkServer, Message message, String clusterName, String instanceName) { HelixZkClient client = SharedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig( zkServer)); try { client.setZkSerializer(new ZNRecordSerializer()); String path = PropertyPathBuilder.instanceMessage(clusterName, instanceName, message.getId()); client.delete(path); ZNRecord record = client.readData(PropertyPathBuilder.liveInstance(clusterName, instanceName)); message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString())); message.setTgtName(record.getId()); // System.out.println(message); client.createPersistent(path, message.getRecord()); } finally { client.close(); } }
Example 4
Source File: MockController.java From helix with Apache License 2.0 | 6 votes |
void sendMessage(String msgId, String instanceName, String fromState, String toState, String partitionKey, int partitionId) throws InterruptedException, JsonGenerationException, JsonMappingException, IOException { Message message = new Message(MessageType.STATE_TRANSITION, msgId); message.setMsgId(msgId); message.setSrcName(srcName); message.setTgtName(instanceName); message.setMsgState(MessageState.NEW); message.setFromState(fromState); message.setToState(toState); // message.setPartitionId(partitionId); message.setPartitionName(partitionKey); String path = PropertyPathBuilder.instanceMessage(clusterName, instanceName, message.getId()); ObjectMapper mapper = new ObjectMapper(); StringWriter sw = new StringWriter(); mapper.writeValueUsingView(sw, message, Message.class); System.out.println(sw.toString()); client.delete(path); Thread.sleep(10000); ZNRecord record = client.readData(PropertyPathBuilder.liveInstance(clusterName, instanceName)); message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString()) .toString()); client.createPersistent(path, message); }
Example 5
Source File: TestDistControllerStateModel.java From helix with Apache License 2.0 | 5 votes |
@Test() public void testOnBecomeOfflineFromStandby() { Message message = new Message(MessageType.STATE_TRANSITION, "0"); message.setPartitionName(clusterName); message.setTgtName("controller_0"); stateModel.onBecomeOfflineFromStandby(message, null); }
Example 6
Source File: MessageGenerationPhase.java From helix with Apache License 2.0 | 5 votes |
private Message createStateTransitionCancellationMessage(HelixManager manager, Resource resource, String partitionName, String instanceName, String sessionId, String stateModelDefName, String fromState, String toState, String nextState, Message cancellationMessage, boolean isCancellationEnabled, String currentState) { if (isCancellationEnabled && cancellationMessage == null) { LogUtil.logInfo(logger, _eventId, "Send cancellation message of the state transition for " + resource.getResourceName() + "." + partitionName + " on " + instanceName + ", currentState: " + currentState + ", nextState: " + (nextState == null ? "N/A" : nextState)); String uuid = UUID.randomUUID().toString(); Message message = new Message(MessageType.STATE_TRANSITION_CANCELLATION, uuid); message.setSrcName(manager.getInstanceName()); message.setTgtName(instanceName); message.setMsgState(MessageState.NEW); message.setPartitionName(partitionName); message.setResourceName(resource.getResourceName()); message.setFromState(fromState); message.setToState(toState); message.setTgtSessionId(sessionId); message.setSrcSessionId(manager.getSessionId()); message.setStateModelDef(stateModelDefName); message.setStateModelFactoryName(resource.getStateModelFactoryname()); message.setBucketSize(resource.getBucketSize()); return message; } return null; }
Example 7
Source File: DefaultMessagingService.java From helix with Apache License 2.0 | 5 votes |
private List<Message> generateMessagesForParticipant(Criteria recipientCriteria, Message message, HelixDataAccessor targetDataAccessor) { List<Message> messages = new ArrayList<Message>(); List<Map<String, String>> matchedList = _evaluator.evaluateCriteria(recipientCriteria, targetDataAccessor); if (!matchedList.isEmpty()) { Map<String, String> sessionIdMap = new HashMap<String, String>(); if (recipientCriteria.isSessionSpecific()) { Builder keyBuilder = targetDataAccessor.keyBuilder(); // For backward compatibility, allow partial read for the live instances. // Note that this may cause the pending message to be sent with null target session Id. List<LiveInstance> liveInstances = targetDataAccessor.getChildValues(keyBuilder.liveInstances(), false); for (LiveInstance liveInstance : liveInstances) { sessionIdMap.put(liveInstance.getInstanceName(), liveInstance.getEphemeralOwner()); } } for (Map<String, String> map : matchedList) { String id = UUID.randomUUID().toString(); Message newMessage = new Message(message.getRecord(), id); String srcInstanceName = _manager.getInstanceName(); String tgtInstanceName = map.get("instanceName"); // Don't send message to self if (recipientCriteria.isSelfExcluded() && srcInstanceName.equalsIgnoreCase(tgtInstanceName)) { continue; } newMessage.setSrcName(srcInstanceName); newMessage.setTgtName(tgtInstanceName); newMessage.setResourceName(map.get("resourceName")); newMessage.setPartitionName(map.get("partitionName")); if (recipientCriteria.isSessionSpecific()) { newMessage.setTgtSessionId(sessionIdMap.get(tgtInstanceName)); } messages.add(newMessage); } } return messages; }
Example 8
Source File: TestPerInstanceAccessor.java From helix with Apache License 2.0 | 5 votes |
@Test(dependsOnMethods = "testGetAllMessages") public void testGetMessagesByStateModelDef() throws IOException { System.out.println("Start test :" + TestHelper.getTestMethodName()); String testInstance = CLUSTER_NAME + "localhost_12926"; //Non-live instance String messageId = "msg1"; Message message = new Message(Message.MessageType.STATE_TRANSITION, messageId); message.setStateModelDef("MasterSlave"); message.setFromState("OFFLINE"); message.setToState("SLAVE"); message.setResourceName("testResourceName"); message.setPartitionName("testResourceName_1"); message.setTgtName("localhost_3"); message.setTgtSessionId("session_3"); HelixDataAccessor helixDataAccessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor); helixDataAccessor.setProperty(helixDataAccessor.keyBuilder().message(testInstance, messageId), message); String body = new JerseyUriRequestBuilder("clusters/{}/instances/{}/messages?stateModelDef=MasterSlave") .isBodyReturnExpected(true).format(CLUSTER_NAME, testInstance).get(this); JsonNode node = OBJECT_MAPPER.readTree(body); int newMessageCount = node.get(PerInstanceAccessor.PerInstanceProperties.total_message_count.name()).getIntValue(); Assert.assertEquals(newMessageCount, 1); body = new JerseyUriRequestBuilder("clusters/{}/instances/{}/messages?stateModelDef=LeaderStandBy") .isBodyReturnExpected(true).format(CLUSTER_NAME, testInstance).get(this); node = OBJECT_MAPPER.readTree(body); newMessageCount = node.get(PerInstanceAccessor.PerInstanceProperties.total_message_count.name()).getIntValue(); Assert.assertEquals(newMessageCount, 0); System.out.println("End test :" + TestHelper.getTestMethodName()); }
Example 9
Source File: GobblinHelixMessagingService.java From incubator-gobblin with Apache License 2.0 | 5 votes |
private List<Message> generateMessagesForController(Message message) { List<Message> messages = new ArrayList<Message>(); String id = UUID.randomUUID().toString(); Message newMessage = new Message(message.getRecord(), id); newMessage.setMsgId(id); newMessage.setSrcName(_manager.getInstanceName()); newMessage.setTgtName("Controller"); messages.add(newMessage); return messages; }
Example 10
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 5 votes |
@Test() public void testRetryOnce() throws InterruptedException { // Logger.getRootLogger().setLevel(Level.INFO); // String p = "test_"; // System.out.println(p.substring(p.lastIndexOf('_')+1)); HelixTaskExecutor executor = new HelixTaskExecutor(); HelixManager manager = new MockClusterManager(); CancellableHandlerFactory factory = new CancellableHandlerFactory(); for (String type : factory.getMessageTypes()) { executor.registerMessageHandlerFactory(type, factory); } NotificationContext changeContext = new NotificationContext(manager); List<Message> msgList = new ArrayList<Message>(); // factory.reset(); // msgList.clear(); // Test the case that the message are executed for the second time int nMsgs2 = 4; for (int i = 0; i < nMsgs2; i++) { Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString()); msg.setTgtSessionId("*"); msg.setTgtName("Localhost_1123"); msg.setSrcName("127.101.1.23_2234"); msg.setExecutionTimeout((i + 1) * 600); msg.setRetryCount(1); msgList.add(msg); } changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE); executor.onMessage("someInstance", msgList, changeContext); Thread.sleep(3500); AssertJUnit.assertEquals(factory._processedMsgIds.size(), 3); AssertJUnit.assertTrue(msgList.get(0).getRecord().getSimpleField("Cancelcount").equals("2")); AssertJUnit.assertTrue(msgList.get(1).getRecord().getSimpleField("Cancelcount").equals("1")); AssertJUnit.assertEquals(factory._timedOutMsgIds.size(), 2); AssertJUnit.assertTrue(executor._taskMap.size() == 0); }
Example 11
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 5 votes |
@Test() public void testNoRetry() throws InterruptedException { // String p = "test_"; // System.out.println(p.substring(p.lastIndexOf('_')+1)); HelixTaskExecutor executor = new HelixTaskExecutor(); HelixManager manager = new MockClusterManager(); CancellableHandlerFactory factory = new CancellableHandlerFactory(); for (String type : factory.getMessageTypes()) { executor.registerMessageHandlerFactory(type, factory); } NotificationContext changeContext = new NotificationContext(manager); List<Message> msgList = new ArrayList<Message>(); int nMsgs2 = 4; // Test the case in which retry = 0 for (int i = 0; i < nMsgs2; i++) { Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString()); msg.setTgtSessionId("*"); msg.setTgtName("Localhost_1123"); msg.setSrcName("127.101.1.23_2234"); msg.setExecutionTimeout((i + 1) * 600); msgList.add(msg); } changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE); executor.onMessage("someInstance", msgList, changeContext); Thread.sleep(4000); AssertJUnit.assertTrue(factory._handlersCreated == nMsgs2); AssertJUnit.assertEquals(factory._timedOutMsgIds.size(), 2); // AssertJUnit.assertFalse(msgList.get(0).getRecord().getSimpleFields().containsKey("TimeOut")); for (int i = 0; i < nMsgs2 - 2; i++) { if (factory.getMessageTypes().contains(msgList.get(i).getMsgType())) { AssertJUnit.assertTrue(msgList.get(i).getRecord().getSimpleFields() .containsKey("Cancelcount")); AssertJUnit.assertTrue(factory._timedOutMsgIds.containsKey(msgList.get(i).getId())); } } }
Example 12
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 4 votes |
@Test public void testStateTransitionCancellationMsg() throws InterruptedException { HelixTaskExecutor executor = new HelixTaskExecutor(); HelixManager manager = new MockClusterManager(); TestStateTransitionHandlerFactory stateTransitionFactory = new TestStateTransitionHandlerFactory(Message.MessageType.STATE_TRANSITION.name()); TestStateTransitionHandlerFactory cancelFactory = new TestStateTransitionHandlerFactory(Message.MessageType.STATE_TRANSITION_CANCELLATION .name()); executor.registerMessageHandlerFactory(Message.MessageType.STATE_TRANSITION.name(), stateTransitionFactory); executor.registerMessageHandlerFactory(Message.MessageType.STATE_TRANSITION_CANCELLATION.name(), cancelFactory); NotificationContext changeContext = new NotificationContext(manager); List<Message> msgList = new ArrayList<Message>(); Message msg1 = new Message(Message.MessageType.STATE_TRANSITION, UUID.randomUUID().toString()); msg1.setTgtSessionId("*"); msg1.setPartitionName("P1"); msg1.setResourceName("R1"); msg1.setTgtName("Localhost_1123"); msg1.setSrcName("127.101.1.23_2234"); msg1.setFromState("SLAVE"); msg1.setToState("MASTER"); msgList.add(msg1); Message msg2 = new Message(Message.MessageType.STATE_TRANSITION_CANCELLATION, UUID.randomUUID().toString()); msg2.setTgtSessionId("*"); msg2.setPartitionName("P1"); msg2.setResourceName("R1"); msg2.setTgtName("Localhost_1123"); msg2.setSrcName("127.101.1.23_2234"); msg2.setFromState("SLAVE"); msg2.setToState("MASTER"); msgList.add(msg2); changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE); executor.onMessage("someInstance", msgList, changeContext); Thread.sleep(3000); AssertJUnit.assertEquals(cancelFactory._processedMsgIds.size(), 0); AssertJUnit.assertEquals(stateTransitionFactory._processedMsgIds.size(), 0); }
Example 13
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 4 votes |
@Test() public void testCMTaskExecutor() throws Exception { System.out.println("START TestCMTaskExecutor"); String msgId = "TestMessageId"; Message message = new Message(MessageType.TASK_REPLY, msgId); message.setMsgId(msgId); message.setSrcName("cm-instance-0"); message.setTgtName("cm-instance-1"); message.setTgtSessionId("1234"); message.setFromState("Offline"); message.setToState("Slave"); message.setPartitionName("TestDB_0"); message.setResourceName("TestDB"); message.setStateModelDef("MasterSlave"); MockManager manager = new MockManager("clusterName"); HelixDataAccessor accessor = manager.getHelixDataAccessor(); StateModelDefinition stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave()); Builder keyBuilder = accessor.keyBuilder(); accessor.setProperty(keyBuilder.stateModelDef("MasterSlave"), stateModelDef); MockHelixTaskExecutor executor = new MockHelixTaskExecutor(); MockMasterSlaveStateModel stateModel = new MockMasterSlaveStateModel(); executor.registerMessageHandlerFactory(MessageType.TASK_REPLY.name(), new AsyncCallbackService()); NotificationContext context = new NotificationContext(manager); CurrentState currentStateDelta = new CurrentState("TestDB"); currentStateDelta.setState("TestDB_0", "OFFLINE"); StateModelFactory<MockMasterSlaveStateModel> stateModelFactory = new StateModelFactory<MockMasterSlaveStateModel>() { @Override public MockMasterSlaveStateModel createNewStateModel(String resource, String partitionName) { // TODO Auto-generated method stub return new MockMasterSlaveStateModel(); } }; HelixStateTransitionHandler handler = new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta); HelixTask task = new HelixTask(message, context, handler, executor); executor.scheduleTask(task); for (int i = 0; i < 10; i++) { if (!executor.isDone(task.getTaskId())) { Thread.sleep(500); } } AssertJUnit.assertTrue(stateModel.stateModelInvoked); System.out.println("END TestCMTaskExecutor"); }
Example 14
Source File: TestHelixTaskHandler.java From helix with Apache License 2.0 | 4 votes |
@Test() public void testInvocationAnnotated() throws Exception { System.out.println("START TestCMTaskHandler.testInvocationAnnotated() at " + new Date(System.currentTimeMillis())); HelixTaskExecutor executor = new HelixTaskExecutor(); Message message = new Message(MessageType.STATE_TRANSITION, "Some unique id"); message.setSrcName("cm-instance-0"); message.setTgtSessionId("1234"); message.setFromState("Offline"); message.setToState("Slave"); message.setPartitionName("TestDB_0"); message.setMsgId("Some unique message id"); message.setResourceName("TestDB"); message.setTgtName("localhost"); message.setStateModelDef("MasterSlave"); message.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY); MockStateModelAnnotated stateModel = new MockStateModelAnnotated(); NotificationContext context; MockManager manager = new MockManager("clusterName"); HelixDataAccessor accessor = manager.getHelixDataAccessor(); StateModelDefinition stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave()); Builder keyBuilder = accessor.keyBuilder(); accessor.setProperty(keyBuilder.stateModelDef("MasterSlave"), stateModelDef); context = new NotificationContext(manager); CurrentState currentStateDelta = new CurrentState("TestDB"); currentStateDelta.setState("TestDB_0", "OFFLINE"); StateModelFactory<MockStateModelAnnotated> stateModelFactory = new StateModelFactory<MockStateModelAnnotated>() { @Override public MockStateModelAnnotated createNewStateModel(String resource, String partitionName) { // TODO Auto-generated method stub return new MockStateModelAnnotated(); } }; HelixStateTransitionHandler stHandler = new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta); HelixTask handler = new HelixTask(message, context, stHandler, executor); handler.call(); AssertJUnit.assertTrue(stateModel.stateModelInvoked); System.out.println("END TestCMTaskHandler.testInvocationAnnotated() at " + new Date(System.currentTimeMillis())); }
Example 15
Source File: TestSchedulerMessage.java From helix with Apache License 2.0 | 4 votes |
@Test public void testSchedulerZeroMsg() throws Exception { _factory._results.clear(); HelixManager manager = null; for (int i = 0; i < NODE_NR; i++) { _participants[i].getMessagingService() .registerMessageHandlerFactory(_factory.getMessageTypes(), _factory); manager = _participants[i]; // _startCMResultMap.get(hostDest)._manager; } Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG + "", UUID.randomUUID().toString()); schedulerMessage.setTgtSessionId("*"); schedulerMessage.setTgtName("CONTROLLER"); // TODO: change it to "ADMIN" ? schedulerMessage.setSrcName("CONTROLLER"); // Template for the individual message sent to each participant Message msg = new Message(_factory.getMessageTypes().get(0), "Template"); msg.setTgtSessionId("*"); msg.setMsgState(MessageState.NEW); // Criteria to send individual messages Criteria cr = new Criteria(); cr.setInstanceName("localhost_DOESNOTEXIST"); cr.setRecipientInstanceType(InstanceType.PARTICIPANT); cr.setSessionSpecific(false); cr.setResource("%"); cr.setPartition("%"); ObjectMapper mapper = new ObjectMapper(); SerializationConfig serializationConfig = mapper.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); StringWriter sw = new StringWriter(); mapper.writeValue(sw, cr); String crString = sw.toString(); schedulerMessage.getRecord().setSimpleField("Criteria", crString); schedulerMessage.getRecord().setMapField("MessageTemplate", msg.getRecord().getSimpleFields()); schedulerMessage.getRecord().setSimpleField("TIMEOUT", "-1"); HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor(); Builder keyBuilder = helixDataAccessor.keyBuilder(); PropertyKey controllerMessageKey = keyBuilder.controllerMessage(schedulerMessage.getMsgId()); helixDataAccessor.setProperty(controllerMessageKey, schedulerMessage); Thread.sleep(3000); Assert.assertEquals(0, _factory._results.size()); PropertyKey controllerTaskStatus = keyBuilder .controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), schedulerMessage.getMsgId()); for (int i = 0; i < 10; i++) { StatusUpdate update = helixDataAccessor.getProperty(controllerTaskStatus); if (update == null || update.getRecord().getMapField("SentMessageCount") == null) { Thread.sleep(1000); } } ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord(); Assert.assertEquals(statusUpdate.getMapField("SentMessageCount").get("MessageCount"), "0"); int count = 0; for (Set<String> val : _factory._results.values()) { count += val.size(); } Assert.assertEquals(count, 0); }
Example 16
Source File: TestZkSessionExpiry.java From helix with Apache License 2.0 | 4 votes |
private static Message newMsg() { Message msg = new Message(DUMMY_MSG_TYPE, UUID.randomUUID().toString()); msg.setTgtSessionId("*"); msg.setTgtName("localhost_12918"); return msg; }
Example 17
Source File: TestMessagePartitionStateMismatch.java From helix with Apache License 2.0 | 4 votes |
@Test public void testStateMismatch() throws InterruptedException { // String controllerName = CONTROLLER_PREFIX + "_0"; HelixManager manager = _controller; // _startCMResultMap.get(controllerName)._manager; HelixDataAccessor accessor = manager.getHelixDataAccessor(); Builder kb = accessor.keyBuilder(); ExternalView ev = accessor.getProperty(kb.externalView(TEST_DB)); Map<String, LiveInstance> liveinstanceMap = accessor.getChildValuesMap(accessor.keyBuilder().liveInstances(), true); for (String instanceName : liveinstanceMap.keySet()) { String sessionid = liveinstanceMap.get(instanceName).getEphemeralOwner(); for (String partition : ev.getPartitionSet()) { if (ev.getStateMap(partition).containsKey(instanceName)) { String uuid = UUID.randomUUID().toString(); Message message = new Message(MessageType.STATE_TRANSITION, uuid); boolean rand = new Random().nextInt(10) > 5; if (ev.getStateMap(partition).get(instanceName).equals("MASTER")) { message.setSrcName(manager.getInstanceName()); message.setTgtName(instanceName); message.setMsgState(MessageState.NEW); message.setPartitionName(partition); message.setResourceName(TEST_DB); message.setFromState(rand ? "SLAVE" : "OFFLINE"); message.setToState(rand ? "MASTER" : "SLAVE"); message.setTgtSessionId(sessionid); message.setSrcSessionId(manager.getSessionId()); message.setStateModelDef("MasterSlave"); message.setStateModelFactoryName("DEFAULT"); } else if (ev.getStateMap(partition).get(instanceName).equals("SLAVE")) { message.setSrcName(manager.getInstanceName()); message.setTgtName(instanceName); message.setMsgState(MessageState.NEW); message.setPartitionName(partition); message.setResourceName(TEST_DB); message.setFromState(rand ? "MASTER" : "OFFLINE"); message.setToState(rand ? "SLAVE" : "SLAVE"); message.setTgtSessionId(sessionid); message.setSrcSessionId(manager.getSessionId()); message.setStateModelDef("MasterSlave"); message.setStateModelFactoryName("DEFAULT"); } accessor.setProperty(accessor.keyBuilder().message(instanceName, message.getMsgId()), message); } } } Thread.sleep(3000); ExternalView ev2 = accessor.getProperty(kb.externalView(TEST_DB)); Assert.assertTrue(ev.equals(ev2)); }
Example 18
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 4 votes |
@Test() public void testCreateHandlerException() throws InterruptedException { System.out.println("START TestCMTaskExecutor.testCreateHandlerException()"); HelixTaskExecutor executor = new HelixTaskExecutor(); HelixManager manager = new MockClusterManager(); TestMessageHandlerFactory factory = new TestMessageHandlerFactory(); for (String type : factory.getMessageTypes()) { executor.registerMessageHandlerFactory(type, factory); } NotificationContext changeContext = new NotificationContext(manager); List<Message> msgList = new ArrayList<Message>(); int nMsgs1 = 5; for (int i = 0; i < nMsgs1; i++) { Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString()); msg.setTgtSessionId(manager.getSessionId()); msg.setTgtName("Localhost_1123"); msg.setSrcName("127.101.1.23_2234"); msg.setCorrelationId(UUID.randomUUID().toString()); msgList.add(msg); } Message exceptionMsg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString()); exceptionMsg.setTgtSessionId(manager.getSessionId()); exceptionMsg.setMsgSubType("EXCEPTION"); exceptionMsg.setTgtName("Localhost_1123"); exceptionMsg.setSrcName("127.101.1.23_2234"); exceptionMsg.setCorrelationId(UUID.randomUUID().toString()); msgList.add(exceptionMsg); changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE); executor.onMessage("someInstance", msgList, changeContext); Thread.sleep(1000); AssertJUnit.assertTrue(factory._processedMsgIds.size() == nMsgs1); AssertJUnit.assertTrue(factory._handlersCreated == nMsgs1); AssertJUnit.assertTrue(exceptionMsg.getMsgState() == MessageState.UNPROCESSABLE); System.out.println("END TestCMTaskExecutor.testCreateHandlerException()"); }
Example 19
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 4 votes |
@Test public void testMessageReadOptimization() throws InterruptedException { HelixTaskExecutor executor = new HelixTaskExecutor(); HelixManager manager = new MockClusterManager(); TestMessageHandlerFactory factory = new TestMessageHandlerFactory(); for (String type : factory.getMessageTypes()) { executor.registerMessageHandlerFactory(type, factory); } HelixDataAccessor accessor = manager.getHelixDataAccessor(); PropertyKey.Builder keyBuilder = accessor.keyBuilder(); List<String> messageIds = new ArrayList<>(); int nMsgs1 = 5; for (int i = 0; i < nMsgs1; i++) { Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString()); msg.setTgtSessionId(manager.getSessionId()); msg.setTgtName("Localhost_1123"); msg.setSrcName("127.101.1.23_2234"); msg.setCorrelationId(UUID.randomUUID().toString()); accessor.setProperty(keyBuilder.message("someInstance", msg.getId()), msg); messageIds.add(msg.getId()); } NotificationContext changeContext = new NotificationContext(manager); changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE); // Simulate read message already, then processing message. Should read and handle no message. executor._knownMessageIds.addAll(messageIds); executor.onMessage("someInstance", Collections.EMPTY_LIST, changeContext); Thread.sleep(3000); AssertJUnit.assertEquals(0, factory._processedMsgIds.size()); executor._knownMessageIds.clear(); // Processing message normally executor.onMessage("someInstance", Collections.EMPTY_LIST, changeContext); Thread.sleep(3000); AssertJUnit.assertEquals(nMsgs1, factory._processedMsgIds.size()); // After all messages are processed, _knownMessageIds should be empty. Assert.assertTrue(executor._knownMessageIds.isEmpty()); }
Example 20
Source File: TestHelixTaskExecutor.java From helix with Apache License 2.0 | 4 votes |
@Test public void testNoWriteReadStateForRemovedMessage() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { HelixTaskExecutor executor = new HelixTaskExecutor(); HelixManager manager = new MockClusterManager(); TestMessageHandlerFactory factory = new TestMessageHandlerFactory(); for (String type : factory.getMessageTypes()) { executor.registerMessageHandlerFactory(type, factory); } HelixDataAccessor accessor = manager.getHelixDataAccessor(); PropertyKey.Builder keyBuilder = accessor.keyBuilder(); String instanceName = "someInstance"; List<String> messageIds = new ArrayList<>(); List<Message> messages = new ArrayList<>(); int nMsgs1 = 5; for (int i = 0; i < nMsgs1; i++) { Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString()); msg.setTgtSessionId(manager.getSessionId()); msg.setTgtName("Localhost_1123"); msg.setSrcName("127.101.1.23_2234"); msg.setCorrelationId(UUID.randomUUID().toString()); accessor.setProperty(keyBuilder.message(instanceName, msg.getId()), msg); messageIds.add(msg.getId()); messages.add(msg); } Method updateMessageState = HelixTaskExecutor.class .getDeclaredMethod("updateMessageState", List.class, HelixDataAccessor.class, String.class); updateMessageState.setAccessible(true); updateMessageState.invoke(executor, messages, accessor, instanceName); Assert.assertEquals(accessor.getChildNames(keyBuilder.messages(instanceName)).size(), nMsgs1); accessor.removeProperty(keyBuilder.message(instanceName, messageIds.get(0))); System.out.println(accessor.getChildNames(keyBuilder.messages(instanceName)).size()); updateMessageState.invoke(executor, messages, accessor, instanceName); Assert .assertEquals(accessor.getChildNames(keyBuilder.messages(instanceName)).size(), nMsgs1 - 1); }