Java Code Examples for org.apache.helix.model.Message#setSrcSessionId()

The following examples show how to use org.apache.helix.model.Message#setSrcSessionId() . 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: TestGroupCommitAddBackData.java    From helix with Apache License 2.0 6 votes vote down vote up
private Message generateMessage(String from, String to) {
  String uuid = UUID.randomUUID().toString();
  Message message = new Message(Message.MessageType.STATE_TRANSITION, uuid);
  message.setSrcName("ADMIN");
  message.setTgtName(_participant.getInstanceName());
  message.setMsgState(Message.MessageState.NEW);
  message.setPartitionName("P");
  message.setResourceName(WorkflowGenerator.DEFAULT_TGT_DB);
  message.setFromState(from);
  message.setToState(to);
  message.setTgtSessionId(_participant.getSessionId());
  message.setSrcSessionId(_manager.getSessionId());
  message.setStateModelDef("OnlineOffline");
  message.setStateModelFactoryName("DEFAULT");
  return message;
}
 
Example 2
Source File: TestMsgSelectionStage.java    From helix with Apache License 2.0 6 votes vote down vote up
private Message newMessage(String resourceName, String partitionName, String instanceName,
    String fromState, String toState) {
  String uuid = UUID.randomUUID().toString();
  Message message = new Message(MessageType.STATE_TRANSITION, uuid);
  message.setSrcName("controller");
  message.setTgtName(instanceName);
  message.setMsgState(MessageState.NEW);
  message.setResourceName(resourceName);
  message.setPartitionName(partitionName);
  message.setFromState(fromState);
  message.setToState(toState);
  message.setTgtSessionId("sessionId");
  message.setSrcSessionId("sessionId");
  message.setStateModelDef("MasterSlave");
  message.setStateModelFactoryName("DEFAULT");
  message.setBucketSize(0);
  return message;
}
 
Example 3
Source File: MessageGenerationPhase.java    From helix with Apache License 2.0 5 votes vote down vote up
private Message createStateTransitionMessage(HelixManager manager, Resource resource,
    String partitionName, String instanceName, String currentState, String nextState,
    String sessionId, String stateModelDefName) {
  String uuid = UUID.randomUUID().toString();
  Message message = new Message(MessageType.STATE_TRANSITION, uuid);
  message.setSrcName(manager.getInstanceName());
  message.setTgtName(instanceName);
  message.setMsgState(MessageState.NEW);
  message.setPartitionName(partitionName);
  message.setResourceName(resource.getResourceName());
  message.setFromState(currentState);
  message.setToState(nextState);
  message.setTgtSessionId(sessionId);
  message.setSrcSessionId(manager.getSessionId());
  message.setStateModelDef(stateModelDefName);
  message.setStateModelFactoryName(resource.getStateModelFactoryname());
  message.setBucketSize(resource.getBucketSize());

  if (resource.getResourceGroupName() != null) {
    message.setResourceGroupName(resource.getResourceGroupName());
  }
  if (resource.getResourceTag() != null) {
    message.setResourceTag(resource.getResourceTag());
  }

  return message;
}
 
Example 4
Source File: MessageGenerationPhase.java    From helix with Apache License 2.0 5 votes vote down vote up
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 5
Source File: TestMessagePartitionStateMismatch.java    From helix with Apache License 2.0 4 votes vote down vote up
@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 6
Source File: TestSyncSessionToController.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testSyncSessionToController() throws Exception {
  System.out
      .println("START testSyncSessionToController at " + new Date(System.currentTimeMillis()));

  String clusterName = getShortClassName();
  MockParticipantManager[] participants = new MockParticipantManager[5];
  int resourceNb = 10;
  TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
      "localhost", // participant name prefix
      "TestDB", // resource name prefix
      resourceNb, // resources
      1, // partitions per resource
      5, // number of nodes
      1, // replicas
      "MasterSlave", true); // do rebalance

  ClusterControllerManager controller =
      new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
  controller.syncStart();

  // start participants
  for (int i = 0; i < 5; i++) {
    String instanceName = "localhost_" + (12918 + i);
    participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
    participants[i].syncStart();
  }

  ZKHelixManager zkHelixManager = new ZKHelixManager(clusterName, "controllerMessageListener",
      InstanceType.CONTROLLER, ZK_ADDR);
  zkHelixManager.connect();
  MockMessageListener mockMessageListener = new MockMessageListener();
  zkHelixManager.addControllerMessageListener(mockMessageListener);

  PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
  ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<>(_gZkClient);
  String path = keyBuilder.liveInstance("localhost_12918").getPath();
  Stat stat = new Stat();
  ZNRecord data = accessor.get(path, stat, 2);
  data.getSimpleFields().put("SESSION_ID", "invalid-id");
  accessor.set(path, data, 2);
  Thread.sleep(2000);
  // Since we always read the content from ephemeral nodes, sync message won't be sent
  Assert.assertFalse(mockMessageListener.isSessionSyncMessageSent());

  // Even after reconnect, session sync won't happen
  ZkTestHelper.expireSession(participants[0].getZkClient());
  Assert.assertFalse(mockMessageListener.isSessionSyncMessageSent());

  // Inject an invalid session message to trigger sync message
  PropertyKey messageKey = keyBuilder.message("localhost_12918", "Mocked Invalid Message");
  Message msg = new Message(Message.MessageType.STATE_TRANSITION, "Mocked Invalid Message");
  msg.setSrcName(controller.getInstanceName());
  msg.setTgtSessionId("invalid-id");
  msg.setMsgState(Message.MessageState.NEW);
  msg.setMsgId("Mocked Invalid Message");
  msg.setTgtName("localhost_12918");
  msg.setPartitionName("foo");
  msg.setResourceName("bar");
  msg.setFromState("SLAVE");
  msg.setToState("MASTER");
  msg.setSrcSessionId(controller.getSessionId());
  msg.setStateModelDef("MasterSlave");
  msg.setStateModelFactoryName("DEFAULT");
  HelixDataAccessor dataAccessor = new ZKHelixDataAccessor(clusterName, accessor);
  dataAccessor.setProperty(messageKey, msg);
  Assert.assertTrue(TestHelper.verify(() -> mockMessageListener.isSessionSyncMessageSent(), 1500));

  // Cleanup
  controller.syncStop();
  zkHelixManager.disconnect();
  for (int i = 0; i < 5; i++) {
    participants[i].syncStop();
  }
  deleteCluster(clusterName);
}