Java Code Examples for org.apache.helix.InstanceType#CONTROLLER

The following examples show how to use org.apache.helix.InstanceType#CONTROLLER . 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: HelixStateMachineEngine.java    From helix with Apache License 2.0 6 votes vote down vote up
private void sendNopMessage() {
  if (_manager.isConnected()) {
    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);
      }
      logger.info("Send NO_OP message to " + nopMsg.getTgtName() + ", msgId: " + nopMsg.getId());
    } catch (Exception e) {
      logger.error(e.toString());
    }
  }
}
 
Example 2
Source File: Message.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Create a reply based on an incoming message
 * @param srcMessage the incoming message
 * @param instanceName the instance that is the source of the reply
 * @param taskResultMap the result of executing the incoming message
 * @return the reply Message
 */
public static Message createReplyMessage(Message srcMessage, String instanceName,
    Map<String, String> taskResultMap) {
  if (srcMessage.getCorrelationId() == null) {
    throw new HelixException(
        "Message " + srcMessage.getMsgId() + " does not contain correlation id");
  }
  Message replyMessage = new Message(MessageType.TASK_REPLY, UUID.randomUUID().toString());
  replyMessage.setCorrelationId(srcMessage.getCorrelationId());
  replyMessage.setResultMap(taskResultMap);
  replyMessage.setTgtSessionId("*");
  replyMessage.setMsgState(MessageState.NEW);
  replyMessage.setSrcName(instanceName);
  if (srcMessage.getSrcInstanceType() == InstanceType.CONTROLLER) {
    replyMessage.setTgtName(InstanceType.CONTROLLER.name());
  } else {
    replyMessage.setTgtName(srcMessage.getMsgSrc());
  }
  return replyMessage;
}
 
Example 3
Source File: DefaultMessagingService.java    From helix with Apache License 2.0 6 votes vote down vote up
public Map<InstanceType, List<Message>> generateMessage(final Criteria recipientCriteria,
    final Message message) {
  Map<InstanceType, List<Message>> messagesToSendMap = new HashMap<InstanceType, List<Message>>();
  InstanceType instanceType = recipientCriteria.getRecipientInstanceType();

  HelixDataAccessor targetDataAccessor = getRecipientDataAccessor(recipientCriteria);

    List<Message> messages = Collections.EMPTY_LIST;
    if (instanceType == InstanceType.CONTROLLER) {
      messages = generateMessagesForController(message);
    } else if (instanceType == InstanceType.PARTICIPANT) {
      messages =
          generateMessagesForParticipant(recipientCriteria, message, targetDataAccessor);
    }
    messagesToSendMap.put(instanceType, messages);
    return messagesToSendMap;
}
 
Example 4
Source File: DefaultMessagingService.java    From helix with Apache License 2.0 6 votes vote down vote up
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 5
Source File: AbstractYarnAppSecurityManager.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected void sendTokenFileUpdatedMessage(InstanceType instanceType, String instanceName) {
  Criteria criteria = new Criteria();
  criteria.setInstanceName(Strings.isNullOrEmpty(instanceName) ? "%" : instanceName);
  criteria.setResource("%");
  criteria.setPartition("%");
  criteria.setPartitionState("%");
  criteria.setRecipientInstanceType(instanceType);
  /**
   * #HELIX-0.6.7-WORKAROUND
   * Add back when LIVESTANCES messaging is ported to 0.6 branch
   if (instanceType == InstanceType.PARTICIPANT) {
   criteria.setDataSource(Criteria.DataSource.LIVEINSTANCES);
   }
   **/
  criteria.setSessionSpecific(true);

  Message tokenFileUpdatedMessage = new Message(Message.MessageType.USER_DEFINE_MSG,
      HelixMessageSubTypes.TOKEN_FILE_UPDATED.toString().toLowerCase() + UUID.randomUUID().toString());
  tokenFileUpdatedMessage.setMsgSubType(HelixMessageSubTypes.TOKEN_FILE_UPDATED.toString());
  tokenFileUpdatedMessage.setMsgState(Message.MessageState.NEW);
  if (instanceType == InstanceType.CONTROLLER) {
    tokenFileUpdatedMessage.setTgtSessionId("*");
  }

  // #HELIX-0.6.7-WORKAROUND
  // Temporarily bypass the default messaging service to allow upgrade to 0.6.7 which is missing support
  // for messaging to instances
  //int messagesSent = this.helixManager.getMessagingService().send(criteria, tokenFileUpdatedMessage);
  GobblinHelixMessagingService messagingService = new GobblinHelixMessagingService(helixManager);

  int messagesSent = messagingService.send(criteria, tokenFileUpdatedMessage);
  LOGGER.info(String.format("Sent %d token file updated message(s) to the %s", messagesSent, instanceType));
}
 
Example 6
Source File: ZKHelixManager.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isLeader() {
  String warnLogPrefix = String
      .format("Instance %s is not leader of cluster %s due to", _instanceName, _clusterName);
  if (_instanceType != InstanceType.CONTROLLER
      && _instanceType != InstanceType.CONTROLLER_PARTICIPANT) {
    LOG.warn(String
        .format("%s instance type %s does not match to CONTROLLER/CONTROLLER_PARTICIPANT",
            warnLogPrefix, _instanceType.name()));
    return false;
  }

  if (!isConnected()) {
    LOG.warn(String.format("%s HelixManager is not connected", warnLogPrefix));
    return false;
  }

  try {
    LiveInstance leader = _dataAccessor.getProperty(_keyBuilder.controllerLeader());
    if (leader != null) {
      String leaderName = leader.getInstanceName();
      String sessionId = leader.getEphemeralOwner();
      if (leaderName != null && leaderName.equals(_instanceName) && sessionId
          .equals(_sessionId)) {
        return true;
      }
      LOG.warn(String
          .format("%s current session %s does not match leader session %s", warnLogPrefix,
              _sessionId, sessionId));
    } else {
      LOG.warn(String.format("%s leader ZNode is null", warnLogPrefix));
    }
  } catch (Exception e) {
    LOG.warn(String.format("%s exception happen when session check", warnLogPrefix), e);
  }
  return false;
}
 
Example 7
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 5 votes vote down vote up
public DistributedLeaderElection(HelixManager manager, GenericHelixController controller,
    List<HelixTimerTask> controllerTimerTasks) {
  _manager = manager;
  _controller = controller;
  _controllerTimerTasks = controllerTimerTasks;

  InstanceType type = _manager.getInstanceType();
  if (type != InstanceType.CONTROLLER && type != InstanceType.CONTROLLER_PARTICIPANT) {
    throw new HelixException(
        "fail to become controller because incorrect instanceType (was " + type.toString()
            + ", requires CONTROLLER | CONTROLLER_PARTICIPANT)");
  }
}
 
Example 8
Source File: HelixTask.java    From helix with Apache License 2.0 5 votes vote down vote up
private void sendReply(HelixDataAccessor replyDataAccessor, Message message,
    HelixTaskResult taskResult) {
  if (message.getCorrelationId() != null && !message.getMsgType()
      .equals(MessageType.TASK_REPLY.name())) {
    logger.info("Sending reply for message " + message.getCorrelationId());
    _statusUpdateUtil.logInfo(message, HelixTask.class, "Sending reply", _manager);

    taskResult.getTaskResultMap().put("SUCCESS", "" + taskResult.isSuccess());
    taskResult.getTaskResultMap().put("INTERRUPTED", "" + taskResult.isInterrupted());
    if (!taskResult.isSuccess()) {
      taskResult.getTaskResultMap().put("ERRORINFO", taskResult.getMessage());
    }
    Message replyMessage = Message
        .createReplyMessage(message, _manager.getInstanceName(), taskResult.getTaskResultMap());
    replyMessage.setSrcInstanceType(_manager.getInstanceType());

    Builder keyBuilder = replyDataAccessor.keyBuilder();
    if (message.getSrcInstanceType() == InstanceType.PARTICIPANT) {
      replyDataAccessor
          .setProperty(keyBuilder.message(message.getMsgSrc(), replyMessage.getMsgId()),
              replyMessage);
    } else if (message.getSrcInstanceType() == InstanceType.CONTROLLER) {
      replyDataAccessor
          .setProperty(keyBuilder.controllerMessage(replyMessage.getMsgId()), replyMessage);
    }
    _statusUpdateUtil.logInfo(message, HelixTask.class, String
        .format("1 msg replied to %s in cluster %s.", replyMessage.getTgtName(),
            message.getSrcClusterName() == null ?
                _manager.getClusterName() :
                message.getSrcClusterName()), _manager);
  }
}
 
Example 9
Source File: TestParticipantManager.java    From helix with Apache License 2.0 5 votes vote down vote up
private void verifyHelixManagerMetrics(InstanceType type, MonitorLevel monitorLevel,
    String instanceName) throws MalformedObjectNameException {
  // check HelixCallback Monitor
  Set<ObjectInstance> objs =
      _server.queryMBeans(buildCallbackMonitorObjectName(type, clusterName, instanceName), null);
  Assert.assertEquals(objs.size(), 18);

  // check HelixZkClient Monitors
  objs =
      _server.queryMBeans(buildZkClientMonitorObjectName(type, clusterName, instanceName), null);
  Assert.assertEquals(objs.size(), 1);

  objs = _server.queryMBeans(buildZkClientPathMonitorObjectName(type, clusterName, instanceName),
      null);

  int expectedZkPathMonitor;
  switch (monitorLevel) {
  case ALL:
    expectedZkPathMonitor = 10;
    break;
  case AGGREGATED_ONLY:
    expectedZkPathMonitor = 1;
    break;
  default:
    expectedZkPathMonitor =
        type == InstanceType.CONTROLLER || type == InstanceType.CONTROLLER_PARTICIPANT ? 10 : 1;
  }
  Assert.assertEquals(objs.size(), expectedZkPathMonitor);
}
 
Example 10
Source File: GobblinYarnAppLauncherTest.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "testCreateHelixCluster")
public void testSendShutdownRequest() throws Exception {
  this.helixManager.connect();
  this.helixManager.getMessagingService().registerMessageHandlerFactory(GobblinHelixConstants.SHUTDOWN_MESSAGE_TYPE,
      new TestShutdownMessageHandlerFactory(this));

  this.gobblinYarnAppLauncher.connectHelixManager();
  this.gobblinYarnAppLauncher.sendShutdownRequest();

  Assert.assertEquals(this.curatorFramework.checkExists()
      .forPath(String.format("/%s/CONTROLLER/MESSAGES", GobblinYarnAppLauncherTest.class.getSimpleName()))
      .getVersion(), 0);
  YarnSecurityManagerTest.GetHelixMessageNumFunc getCtrlMessageNum =
      new YarnSecurityManagerTest.GetHelixMessageNumFunc(GobblinYarnAppLauncherTest.class.getSimpleName(), InstanceType.CONTROLLER, "",
          this.curatorFramework);
  AssertWithBackoff assertWithBackoff =
      AssertWithBackoff.create().logger(LoggerFactory.getLogger("testSendShutdownRequest")).timeoutMs(20000);
  assertWithBackoff.assertEquals(getCtrlMessageNum, 1, "1 controller message queued");

  // Give Helix sometime to handle the message
  assertWithBackoff.assertEquals(getCtrlMessageNum, 0, "all controller messages processed");

  this.helixManagerManagedHelix.connect();
  this.helixManagerManagedHelix.getMessagingService().registerMessageHandlerFactory(GobblinHelixConstants.SHUTDOWN_MESSAGE_TYPE,
      new TestShutdownMessageHandlerFactory(this));

  this.gobblinYarnAppLauncherManagedHelix.connectHelixManager();
  this.gobblinYarnAppLauncherManagedHelix.sendShutdownRequest();

  Assert.assertEquals(this.curatorFramework.checkExists()
      .forPath(String.format("/%s/INSTANCES/%s/MESSAGES", this.configManagedHelix.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY), TEST_HELIX_INSTANCE_NAME_MANAGED))
      .getVersion(), 0);
  YarnSecurityManagerTest.GetHelixMessageNumFunc getInstanceMessageNum =
      new YarnSecurityManagerTest.GetHelixMessageNumFunc(this.configManagedHelix.getString(
          GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY),
          InstanceType.PARTICIPANT, TEST_HELIX_INSTANCE_NAME_MANAGED, this.curatorFramework);
  assertWithBackoff =
      AssertWithBackoff.create().logger(LoggerFactory.getLogger("testSendShutdownRequest")).timeoutMs(20000);
  assertWithBackoff.assertEquals(getInstanceMessageNum, 1, "1 controller message queued");

  // Give Helix sometime to handle the message
  assertWithBackoff.assertEquals(getInstanceMessageNum, 0, "all controller messages processed");
}
 
Example 11
Source File: ClusterControllerManager.java    From helix with Apache License 2.0 4 votes vote down vote up
public ClusterControllerManager(String zkAddr, String clusterName, String controllerName) {
  super(zkAddr, clusterName, controllerName, InstanceType.CONTROLLER);
}
 
Example 12
Source File: TestParticipantManager.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleIntegrationTest() throws Exception {
  int n = 1;

  TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
      "localhost", // participant name prefix
      "TestDB", // resource name prefix
      1, // resources
      4, // partitions per resource
      n, // number of nodes
      1, // replicas
      "MasterSlave", true); // do rebalance

  HelixManager participant =
      new ZKHelixManager(clusterName, "localhost_12918", InstanceType.PARTICIPANT, ZK_ADDR);
  participant.getStateMachineEngine().registerStateModelFactory("MasterSlave",
      new MockMSModelFactory());
  participant.connect();

  HelixManager controller =
      new ZKHelixManager(clusterName, "controller_0", InstanceType.CONTROLLER, ZK_ADDR);
  controller.connect();

  verifyHelixManagerMetrics(InstanceType.PARTICIPANT, MonitorLevel.DEFAULT,
      participant.getInstanceName());
  verifyHelixManagerMetrics(InstanceType.CONTROLLER, MonitorLevel.DEFAULT,
      controller.getInstanceName());

  BestPossibleExternalViewVerifier verifier =
      new BestPossibleExternalViewVerifier.Builder(clusterName).setZkClient(_gZkClient)
          .setZkAddr(ZK_ADDR).build();
  Assert.assertTrue(verifier.verifyByPolling());

  // cleanup
  controller.disconnect();
  participant.disconnect();

  // verify all live-instances and leader nodes are gone
  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
  Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
}
 
Example 13
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);
}
 
Example 14
Source File: TestHandleSession.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "testHandleNewSession")
public void testAcquireLeadershipOnNewSession() throws Exception {
  String className = getShortClassName();
  final String clusterName =
      CLUSTER_PREFIX + "_" + className + "_" + "testAcquireLeadershipOnNewSession";
  final ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
  final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  TestHelper.setupEmptyCluster(_gZkClient, clusterName);

  // Create controller leader
  final String controllerName = "controller_0";
  final BlockingHandleNewSessionZkHelixManager manager =
      new BlockingHandleNewSessionZkHelixManager(clusterName, controllerName,
          InstanceType.CONTROLLER, ZK_ADDR);
  GenericHelixController controller0 = new GenericHelixController();
  DistributedLeaderElection election =
      new DistributedLeaderElection(manager, controller0, Collections.EMPTY_LIST);
  manager.connect();

  // Ensure the controller successfully acquired leadership.
  Assert.assertTrue(TestHelper.verify(() -> {
    LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
    return liveInstance != null && controllerName.equals(liveInstance.getInstanceName())
        && manager.getSessionId().equals(liveInstance.getEphemeralOwner());
  }, 1000));
  // Record the original connection info.
  final String originalSessionId = manager.getSessionId();
  final long originalCreationTime =
      accessor.getProperty(keyBuilder.controllerLeader()).getStat().getCreationTime();

  int handlerCount = manager.getHandlers().size();

  // 1. lock the zk event processing to simulate long backlog queue.
  ((ZkClient) manager._zkclient).getEventLock().lockInterruptibly();
  // 2. add a controller leader node change event to the queue, that will not be processed.
  accessor.removeProperty(keyBuilder.controllerLeader());
  // 3. expire the session and create a new session
  ZkTestHelper.asyncExpireSession(manager._zkclient);
  Assert.assertTrue(TestHelper
      .verify(() -> !((ZkClient) manager._zkclient).getConnection().getZookeeperState().isAlive(),
          3000));
  // 4. start processing event again
  ((ZkClient) manager._zkclient).getEventLock().unlock();

  // Wait until the ZkClient has got a new session, and the original leader node gone
  Assert.assertTrue(TestHelper.verify(() -> {
    try {
      return !Long.toHexString(manager._zkclient.getSessionId()).equals(originalSessionId);
    } catch (HelixException hex) {
      return false;
    }
  }, 2000));
  // ensure that the manager has not process the new session event yet
  Assert.assertEquals(manager.getSessionId(), originalSessionId);

  // Wait until an invalid leader node created again.
  // Note that this is the expected behavior but NOT desired behavior. Ideally, the new node should
  // be created with the right session directly. We will need to improve this.
  // TODO We should recording session Id in the zk event so the stale events are discarded instead of processed. After this is done, there won't be invalid node.
  Assert.assertTrue(TestHelper.verify(() -> {
    // Newly created node should have a new creating time but with old session.
    LiveInstance invalidLeaderNode = accessor.getProperty(keyBuilder.controllerLeader());
    // node exist
    if (invalidLeaderNode == null) {
      return false;
    }
    // node is newly created
    if (invalidLeaderNode.getStat().getCreationTime() == originalCreationTime) {
      return false;
    }
    // node has the same session as the old one, so it's invalid
    if (!invalidLeaderNode.getSessionId().equals(originalSessionId)) {
      return false;
    }
    return true;
  }, 2000));
  Assert.assertFalse(manager.isLeader());

  // 5. proceed the new session handling, so the manager will get the new session.
  manager.proceedNewSessionHandling();
  // Since the new session handling will re-create the leader node, a new valid node shall be created.
  Assert.assertTrue(TestHelper.verify(() -> manager.isLeader(), 1000));
  // All the callback handlers shall be recovered.
  Assert.assertTrue(TestHelper.verify(() -> manager.getHandlers().size() == handlerCount, 3000));
  Assert.assertTrue(manager.getHandlers().stream().allMatch(handler -> handler.isReady()));

  manager.disconnect();
  TestHelper.dropCluster(clusterName, _gZkClient);
}
 
Example 15
Source File: TestDistControllerElection.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "testController")
public void testCompeteLeadership() throws Exception {
  final int managerCount = 3;
  String className = getShortClassName();
  final String clusterName = CLUSTER_PREFIX + "_" + className + "_" + "testCompeteLeadership";
  final ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
  final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  TestHelper.setupEmptyCluster(_gZkClient, clusterName);

  // Create controller leaders
  final Map<String, ZKHelixManager> managerList = new HashMap<>();
  for (int i = 0; i < managerCount; i++) {
    String controllerName = "controller_" + i;
    ZKHelixManager manager =
        new ZKHelixManager(clusterName, controllerName, InstanceType.CONTROLLER, ZK_ADDR);
    GenericHelixController controller0 = new GenericHelixController();
    DistributedLeaderElection election =
        new DistributedLeaderElection(manager, controller0, Collections.EMPTY_LIST);
    manager.connect();
    managerList.put(manager.getInstanceName(), manager);
  }

  // Remove leader manager one by one, and verify if the leader node exists
  while(!managerList.isEmpty()) {
    // Ensure a controller successfully acquired leadership.
    Assert.assertTrue(TestHelper.verify(new TestHelper.Verifier() {
      @Override
      public boolean verify() {
        LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
        if (liveInstance != null) {
          // disconnect the current leader manager
          managerList.remove(liveInstance.getInstanceName()).disconnect();
          return true;
        } else {
          return false;
        }
      }
    }, 1000));
  }

  TestHelper.dropCluster(clusterName, _gZkClient);
}
 
Example 16
Source File: HelixControllerManager.java    From ambry with Apache License 2.0 4 votes vote down vote up
public HelixControllerManager(String zkAddr, String clusterName, String controllerName) {
  super(clusterName, controllerName, InstanceType.CONTROLLER, zkAddr);
}