Java Code Examples for org.apache.helix.HelixManager#getInstanceName()

The following examples show how to use org.apache.helix.HelixManager#getInstanceName() . 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: ParticipantManager.java    From helix with Apache License 2.0 6 votes vote down vote up
public ParticipantManager(HelixManager manager, RealmAwareZkClient zkclient, int sessionTimeout,
    LiveInstanceInfoProvider liveInstanceInfoProvider,
    List<PreConnectCallback> preConnectCallbacks, final String sessionId,
    HelixManagerProperty helixManagerProperty) {
  _zkclient = zkclient;
  _manager = manager;
  _clusterName = manager.getClusterName();
  _instanceName = manager.getInstanceName();
  _keyBuilder = new PropertyKey.Builder(_clusterName);
  _sessionId = sessionId;
  _sessionTimeout = sessionTimeout;
  _configAccessor = manager.getConfigAccessor();
  _instanceType = manager.getInstanceType();
  _helixAdmin = manager.getClusterManagmentTool();
  _dataAccessor = (ZKHelixDataAccessor) manager.getHelixDataAccessor();
  _messagingService = (DefaultMessagingService) manager.getMessagingService();
  _stateMachineEngine = manager.getStateMachineEngine();
  _liveInstanceInfoProvider = liveInstanceInfoProvider;
  _preConnectCallbacks = preConnectCallbacks;
  _helixManagerProperty = helixManagerProperty;
}
 
Example 2
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 6 votes vote down vote up
private void updateHistory(HelixManager manager) {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();
  final String clusterName = manager.getClusterName();
  final String instanceName = manager.getInstanceName();
  final String version = manager.getVersion();

  // Record a MaintenanceSignal history
  if (!accessor.getBaseDataAccessor().update(keyBuilder.controllerLeaderHistory().getPath(),
      oldRecord -> {
        if (oldRecord == null) {
          oldRecord = new ZNRecord(PropertyType.HISTORY.toString());
        }
        return new ControllerHistory(oldRecord).updateHistory(clusterName, instanceName,
            version);
      }, AccessOption.PERSISTENT)) {
    LOG.error("Failed to persist leader history to ZK!");
  }
}
 
Example 3
Source File: CompatibilityCheckStage.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ClusterEvent event) throws Exception {
  HelixManager manager = event.getAttribute(AttributeName.helixmanager.name());
  BaseControllerDataProvider cache = event.getAttribute(AttributeName.ControllerDataProvider.name());
  if (manager == null || cache == null) {
    throw new StageException("Missing attributes in event:" + event
        + ". Requires HelixManager | DataCache");
  }

  HelixManagerProperties properties = manager.getProperties();
  Map<String, LiveInstance> liveInstanceMap = cache.getLiveInstances();
  for (LiveInstance liveInstance : liveInstanceMap.values()) {
    String participantVersion = liveInstance.getHelixVersion();
    if (!properties.isParticipantCompatible(participantVersion)) {
      String errorMsg =
          "incompatible participant. pipeline will not continue. " + "controller: "
              + manager.getInstanceName() + ", controllerVersion: " + properties.getVersion()
              + ", minimumSupportedParticipantVersion: "
              + properties.getProperty("minimum_supported_version.participant")
              + ", participant: " + liveInstance.getInstanceName() + ", participantVersion: "
              + participantVersion;
      LogUtil.logError(LOG, event.getEventId(), errorMsg);
      throw new StageException(errorMsg);
    }
  }
}
 
Example 4
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 5
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if the current manager created a new controller node successfully.
 */
private boolean tryCreateController(HelixManager manager) {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();

  LiveInstance newLeader = new LiveInstance(manager.getInstanceName());
  newLeader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
  newLeader.setSessionId(manager.getSessionId());
  newLeader.setHelixVersion(manager.getVersion());
  try {
    if (accessor.createControllerLeader(newLeader)) {
      updateHistory(manager);
      return true;
    } else {
      LOG.info(
          "Unable to become leader probably because some other controller became the leader.");
    }
  } catch (Exception e) {
    LOG.error(
        "Exception when trying to updating leader record in cluster:" + manager.getClusterName()
            + ". Need to check again whether leader node has been created or not.", e);
  }

  LiveInstance currentLeader = accessor.getProperty(keyBuilder.controllerLeader());
  if (currentLeader != null) {
    String currentSession = currentLeader.getEphemeralOwner();
    LOG.info("Leader exists for cluster: " + manager.getClusterName() + ", currentLeader: "
        + currentLeader.getInstanceName() + ", leaderSessionId: " + currentSession);
    if (currentSession != null && currentSession.equals(newLeader.getEphemeralOwner())) {
      return true;
    } else {
      LOG.warn("The existing leader has a different session. Expected session Id: " + newLeader
          .getEphemeralOwner());
    }
  }
  return false;
}
 
Example 6
Source File: CustomCodeInvoker.java    From helix with Apache License 2.0 5 votes vote down vote up
private void callParticipantCode(NotificationContext context) {
  // since ZkClient.unsubscribe() does not immediately remove listeners
  // from zk, it is possible that two listeners exist when leadership transfers
  // therefore, double check to make sure only one participant invokes the code
  if (context.getType() == Type.CALLBACK) {
    HelixManager manager = context.getManager();
    // DataAccessor accessor = manager.getDataAccessor();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();

    String instance = manager.getInstanceName();
    String sessionId = manager.getSessionId();

    // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0"
    String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_'));

    CurrentState curState =
        accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName));
    if (curState == null) {
      return;
    }

    String state = curState.getState(_partitionKey);
    if (state == null || !state.equalsIgnoreCase("LEADER")) {
      return;
    }
  }

  try {
    _callback.onCallback(context);
  } catch (Exception e) {
    LOG.error("Error invoking callback:" + _callback, e);
  }
}
 
Example 7
Source File: HelixTask.java    From helix with Apache License 2.0 5 votes vote down vote up
private void reportMessageStat(HelixManager manager, Message message, HelixTaskResult taskResult) {
  // report stat
  if (!message.getMsgType().equals(MessageType.STATE_TRANSITION.name())) {
    return;
  }
  long now = new Date().getTime();
  long msgReadTime = message.getReadTimeStamp();
  long msgExecutionStartTime = message.getExecuteStartTimeStamp();
  if (msgReadTime != 0 && msgExecutionStartTime != 0) {
    long totalDelay = now - msgReadTime;
    long executionDelay = now - msgExecutionStartTime;
    long msgLatency = msgReadTime - message.getCreateTimeStamp();
    if (totalDelay >= 0 && executionDelay >= 0) {
      String fromState = message.getFromState();
      String toState = message.getToState();
      String transition = fromState + "--" + toState;

      StateTransitionContext cxt =
          new StateTransitionContext(manager.getClusterName(), manager.getInstanceName(),
              message.getResourceName(), transition);

      StateTransitionDataPoint data =
          new StateTransitionDataPoint(totalDelay, executionDelay, msgLatency,
              taskResult.isSuccess());
      _executor.getParticipantMonitor().reportTransitionStat(cxt, data);
    }
  } else {
    logger.warn(
        "message read time and start execution time not recorded. State transition delay time is not available, message read time {}, Execute start time {}.",
        msgReadTime, msgExecutionStartTime);
  }
}
 
Example 8
Source File: TestControllerLeadershipChange.java    From helix with Apache License 2.0 5 votes vote down vote up
private void setLeader(HelixManager manager) throws Exception {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  final LiveInstance leader = new LiveInstance(manager.getInstanceName());
  leader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
  leader.setSessionId(manager.getSessionId());
  leader.setHelixVersion(manager.getVersion());

  // Delete the current controller leader node so it will trigger leader election
  while (!manager.isLeader()) {
    accessor.getBaseDataAccessor()
        .remove(PropertyPathBuilder.controllerLeader(manager.getClusterName()),
            AccessOption.EPHEMERAL);
    Thread.sleep(50);
  }
}
 
Example 9
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 10
Source File: FileStoreStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
public FileStoreStateModel(HelixManager manager, String resource, String partition) {
  String clusterName = manager.getClusterName();
  String instanceName = manager.getInstanceName();
  instanceConfig = manager.getClusterManagmentTool().getInstanceConfig(clusterName, instanceName);
  replicator = new Replicator(instanceConfig, resource, partition);
  try {
    manager.addExternalViewChangeListener(replicator);
  } catch (Exception e) {
    e.printStackTrace();
  }
  _partition = partition;
  _serverId = instanceName;
}
 
Example 11
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test()
public void testDuplicatedMessage() throws InterruptedException {
  System.out.println("START TestHelixTaskExecutor.testDuplicatedMessage()");
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();
  HelixDataAccessor dataAccessor = manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuilder = dataAccessor.keyBuilder();

  TestStateTransitionHandlerFactory stateTransitionFactory =
      new TestStateTransitionHandlerFactory(Message.MessageType.STATE_TRANSITION.name(), 1000);
  executor.registerMessageHandlerFactory(Message.MessageType.STATE_TRANSITION.name(),
      stateTransitionFactory);

  NotificationContext changeContext = new NotificationContext(manager);
  List<Message> msgList = new ArrayList<Message>();

  int nMsgs = 3;
  String instanceName = manager.getInstanceName();
  for (int i = 0; i < nMsgs; i++) {
    Message msg =
        new Message(Message.MessageType.STATE_TRANSITION.name(), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    msg.setCreateTimeStamp((long) i);
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setPartitionName("Partition");
    msg.setResourceName("Resource");
    msg.setStateModelDef("DummyMasterSlave");
    msg.setFromState("SLAVE");
    msg.setToState("MASTER");
    dataAccessor.setProperty(msg.getKey(keyBuilder, instanceName), msg);
    msgList.add(msg);
  }

  AssertJUnit
      .assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
          nMsgs);

  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
  executor.onMessage(instanceName, msgList, changeContext);

  Thread.sleep(200);

  // only 1 message is left over - state transition takes 1sec
  Assert.assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
      1);

  // While a state transition message is going on, another state transition message for same
  // resource / partition comes in, it should be discarded by message handler

  // Mock accessor is modifying message state in memory so we set it back to NEW
  msgList.get(2).setMsgState(MessageState.NEW);
  dataAccessor.setProperty(msgList.get(2).getKey(keyBuilder, instanceName), msgList.get(2));
  executor.onMessage(instanceName, Arrays.asList(msgList.get(2)), changeContext);
  Thread.sleep(200);
  Assert.assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
      1);

  Thread.sleep(1000);
  Assert.assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
      0);
  System.out.println("END TestHelixTaskExecutor.testDuplicatedMessage()");
}
 
Example 12
Source File: LeadControllerManager.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
public LeadControllerManager(HelixManager helixParticipantManager, ControllerMetrics controllerMetrics) {
  _helixManager = helixParticipantManager;
  _controllerMetrics = controllerMetrics;
  _instanceId = helixParticipantManager.getInstanceName();
  _leadForPartitions = ConcurrentHashMap.newKeySet();

  // Create a thread to periodically fetch controller leadership as a work-around of Helix callback delay
  _controllerLeadershipFetchingThread = new Thread("ControllerLeadershipFetchingThread") {
    @Override
    public void run() {
      while (true) {
        try {
          synchronized (LeadControllerManager.this) {
            if (_isShuttingDown) {
              return;
            }
            if (isHelixLeader()) {
              if (!_amIHelixLeader) {
                _amIHelixLeader = true;
                LOGGER.warn("Becoming leader without getting Helix change callback");
                _controllerMetrics
                    .addMeteredGlobalValue(ControllerMeter.CONTROLLER_LEADERSHIP_CHANGE_WITHOUT_CALLBACK, 1L);
              }
              _controllerMetrics.setValueOfGlobalGauge(ControllerGauge.PINOT_CONTROLLER_LEADER, 1L);
            } else {
              if (_amIHelixLeader) {
                _amIHelixLeader = false;
                LOGGER.warn("Losing leadership without getting Helix change callback");
                _controllerMetrics
                    .addMeteredGlobalValue(ControllerMeter.CONTROLLER_LEADERSHIP_CHANGE_WITHOUT_CALLBACK, 1L);
              }
              _controllerMetrics.setValueOfGlobalGauge(ControllerGauge.PINOT_CONTROLLER_LEADER, 0L);
            }
            LeadControllerManager.this.wait(CONTROLLER_LEADERSHIP_FETCH_INTERVAL_MS);
          }
        } catch (Exception e) {
          // Ignore all exceptions. The thread keeps running until LeadControllerManager.stop() is invoked.
          LOGGER.error("Caught exception within controller leadership fetching thread", e);
        }
      }
    }
  };
}