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

The following examples show how to use org.apache.helix.InstanceType#PARTICIPANT . 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: DefaultMessagingService.java    From helix with Apache License 2.0 6 votes vote down vote up
public DefaultMessagingService(HelixManager manager) {
  _manager = manager;
  _evaluator = new CriteriaEvaluator();

  boolean isParticipant = false;
  if (manager.getInstanceType() == InstanceType.PARTICIPANT || manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
    isParticipant = true;
  }

  _taskExecutor = new HelixTaskExecutor(
      new ParticipantStatusMonitor(isParticipant, manager.getInstanceName()),
      new MessageQueueMonitor(manager.getClusterName(), manager.getInstanceName()));
  _asyncCallbackService = new AsyncCallbackService();
  _taskExecutor.registerMessageHandlerFactory(MessageType.TASK_REPLY.name(),
      _asyncCallbackService);
}
 
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: HelixAgentMain.java    From helix with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  CommandLine cmd = processCommandLineArgs(args);
  String zkAddress = cmd.getOptionValue(zkAddr);
  String clusterName = cmd.getOptionValue(cluster);
  String instance = cmd.getOptionValue(instanceName);
  String stateModelName = cmd.getOptionValue(stateModel);

  HelixManager manager =
      new ZKHelixManager(clusterName, instance, InstanceType.PARTICIPANT, zkAddress);

  StateMachineEngine stateMach = manager.getStateMachineEngine();
  stateMach.registerStateModelFactory(stateModelName, new AgentStateModelFactory());

  Runtime.getRuntime().addShutdownHook(new HelixAgentShutdownHook(manager));

  try {
    manager.connect();
    Thread.currentThread().join();
  } catch (Exception e) {
    LOG.error(e.toString());
  } finally {
    if (manager != null && manager.isConnected()) {
      manager.disconnect();
    }
  }
}
 
Example 6
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 6 votes vote down vote up
public void start() throws Exception {
  _helixManager =
      new ZKHelixManager(_clusterName, _instanceName, InstanceType.PARTICIPANT, ZK_ADDR);
  {
    // hack to set sessionTimeout
    Field sessionTimeout = ZKHelixManager.class.getDeclaredField("_sessionTimeout");
    sessionTimeout.setAccessible(true);
    sessionTimeout.setInt(_helixManager, 1000);
  }

  StateMachineEngine stateMach = _helixManager.getStateMachineEngine();
  stateMach.registerStateModelFactory("MasterSlave", new MyStateModelFactory(_helixManager));
  _helixManager.connect();

  // StatusPrinter statusPrinter = new StatusPrinter();
  // statusPrinter.registerWith(_helixManager);
}
 
Example 7
Source File: TestDistControllerElection.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test()
public void testParticipant() throws Exception {
  String className = getShortClassName();
  LOG.info("RUN " + className + " at " + new Date(System.currentTimeMillis()));

  final String clusterName = CLUSTER_PREFIX + "_" + className + "_" + "testParticipant";
  TestHelper.setupEmptyCluster(_gZkClient, clusterName);

  final String controllerName = "participant_0";
  HelixManager manager =
      new MockZKHelixManager(clusterName, controllerName, InstanceType.PARTICIPANT, _gZkClient);
  GenericHelixController participant0 = new GenericHelixController();
  List<HelixTimerTask> timerTasks = Collections.emptyList();

  try {
    DistributedLeaderElection election =
        new DistributedLeaderElection(manager, participant0, timerTasks);
    Assert.fail(
        "Should not be able construct DistributedLeaderElection object using participant manager.");
  } catch (HelixException ex) {
    // expected
  }

  TestHelper.dropCluster(clusterName, _gZkClient);
}
 
Example 8
Source File: ZKUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
public static boolean isInstanceSetup(RealmAwareZkClient zkclient, String clusterName,
    String instanceName, InstanceType type) {
  if (type == InstanceType.PARTICIPANT || type == InstanceType.CONTROLLER_PARTICIPANT) {
    List<String> requiredPaths = new ArrayList<>();
    requiredPaths.add(PropertyPathBuilder.instanceConfig(clusterName, instanceName));
    requiredPaths.add(PropertyPathBuilder.instanceMessage(clusterName, instanceName));
    requiredPaths.add(PropertyPathBuilder.instanceCurrentState(clusterName, instanceName));
    requiredPaths.add(PropertyPathBuilder.instanceStatusUpdate(clusterName, instanceName));
    requiredPaths.add(PropertyPathBuilder.instanceError(clusterName, instanceName));
    boolean isValid = true;

    for (String path : requiredPaths) {
      if (!zkclient.exists(path)) {
        isValid = false;
        logger.info("Invalid instance setup, missing znode path: {}", path);
      }
    }

    if (isValid) {
      // Create the instance history node if it does not exist.
      // This is for back-compatibility.
      String historyPath = PropertyPathBuilder.instanceHistory(clusterName, instanceName);
      if (!zkclient.exists(historyPath)) {
        zkclient.createPersistent(historyPath, true);
      }
    }
    return isValid;
  }

  return true;
}
 
Example 9
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 10
Source File: TestEntropyFreeNodeBounce.java    From helix with Apache License 2.0 5 votes vote down vote up
private HelixManager createParticipant(String clusterName, String instanceName) {
  HelixManager participant =
      new ZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, ZK_ADDR);
  participant.getStateMachineEngine().registerStateModelFactory("OnlineOffline",
      new MockStateModelFactory());
  return participant;
}
 
Example 11
Source File: MockParticipantManager.java    From helix with Apache License 2.0 5 votes vote down vote up
public MockParticipantManager(String zkAddr, String clusterName, String instanceName,
    int transDelay, HelixCloudProperty helixCloudProperty) {
  super(zkAddr, clusterName, instanceName, InstanceType.PARTICIPANT);
  _transDelay = transDelay;
  _msModelFactory = new MockMSModelFactory(null);
  _lsModelFactory = new DummyLeaderStandbyStateModelFactory(_transDelay);
  _ofModelFactory = new DummyOnlineOfflineStateModelFactory(_transDelay);
  _helixCloudProperty = helixCloudProperty;
}
 
Example 12
Source File: MinionStarter.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public MinionStarter(String zkAddress, String helixClusterName, Configuration config)
    throws Exception {
  _config = config;
  _instanceId = config.getString(CommonConstants.Helix.Instance.INSTANCE_ID_KEY,
      CommonConstants.Helix.PREFIX_OF_MINION_INSTANCE + NetUtil.getHostAddress() + "_"
          + CommonConstants.Minion.DEFAULT_HELIX_PORT);
  setupHelixSystemProperties();
  _helixManager = new ZKHelixManager(helixClusterName, _instanceId, InstanceType.PARTICIPANT, zkAddress);
  _taskExecutorFactoryRegistry = new TaskExecutorFactoryRegistry();
  _eventObserverFactoryRegistry = new EventObserverFactoryRegistry();
}
 
Example 13
Source File: TestP2PNoDuplicatedMessage.java    From helix with Apache License 2.0 5 votes vote down vote up
public MockMessagingService(HelixManager manager) {
  super(manager);
  _manager = manager;

  boolean isParticipant = false;
  if (manager.getInstanceType() == InstanceType.PARTICIPANT
      || manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
    isParticipant = true;
  }

  _taskExecutor = new MockHelixTaskExecutor(
      new ParticipantStatusMonitor(isParticipant, manager.getInstanceName()),
      new MessageQueueMonitor(manager.getClusterName(), manager.getInstanceName()));
}
 
Example 14
Source File: HelixExternalViewBasedQueryQuotaManagerTest.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
private HelixManager initHelixManager(String helixClusterName) {
  return new FakeHelixManager(helixClusterName, BROKER_INSTANCE_ID, InstanceType.PARTICIPANT,
      ZkStarter.DEFAULT_ZK_STR);
}
 
Example 15
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 16
Source File: TestDefaultMessagingService.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public InstanceType getInstanceType() {
  return InstanceType.PARTICIPANT;
}
 
Example 17
Source File: MockManager.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public InstanceType getInstanceType() {
  return InstanceType.PARTICIPANT;
}
 
Example 18
Source File: TestResourceGroupEndtoEnd.java    From helix with Apache License 2.0 4 votes vote down vote up
public TestParticipantManager(String zkAddr, String clusterName, String resourceName,
    String instanceGroup, String instanceName) {
  super(clusterName, instanceName, InstanceType.PARTICIPANT, zkAddr);
  _instanceGroup = instanceGroup;
  _resourceName = resourceName;
}
 
Example 19
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 20
Source File: DefaultMessagingService.java    From helix with Apache License 2.0 4 votes vote down vote up
void registerMessageHandlerFactoryInternal(String type, MessageHandlerFactory factory) {
  _logger.info("registering msg factory for type " + type);
  int threadpoolSize = HelixTaskExecutor.DEFAULT_PARALLEL_TASKS;
  String threadpoolSizeStr = null;
  String key = type + "." + HelixTaskExecutor.MAX_THREADS;

  ConfigAccessor configAccessor = _manager.getConfigAccessor();
  if (configAccessor != null) {
    ConfigScope scope = null;

    // Read the participant config and cluster config for the per-message type thread pool size.
    // participant config will override the cluster config.

    if (_manager.getInstanceType() == InstanceType.PARTICIPANT
        || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
      scope =
          new ConfigScopeBuilder().forCluster(_manager.getClusterName())
              .forParticipant(_manager.getInstanceName()).build();
      threadpoolSizeStr = configAccessor.get(scope, key);
    }

    if (threadpoolSizeStr == null) {
      scope = new ConfigScopeBuilder().forCluster(_manager.getClusterName()).build();
      threadpoolSizeStr = configAccessor.get(scope, key);
    }
  }

  if (threadpoolSizeStr != null) {
    try {
      threadpoolSize = Integer.parseInt(threadpoolSizeStr);
      if (threadpoolSize <= 0) {
        threadpoolSize = 1;
      }
    } catch (Exception e) {
      _logger.error("", e);
    }
  }

  _taskExecutor.registerMessageHandlerFactory(type, factory, threadpoolSize);
  // Self-send a no-op message, so that the onMessage() call will be invoked
  // again, and
  // we have a chance to process the message that we received with the new
  // added MessageHandlerFactory
  // before the factory is added.
  sendNopMessageInternal();
}