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

The following examples show how to use org.apache.helix.HelixManager#getClusterName() . 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: HelixUtils.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public static Set<TopicPartition> getUnassignedPartitions(HelixManager helixManager) {
  Set<TopicPartition> unassignedPartitions = new HashSet<TopicPartition>();
  HelixAdmin helixAdmin = helixManager.getClusterManagmentTool();
  String helixClusterName = helixManager.getClusterName();
  for (String topic : helixAdmin.getResourcesInCluster(helixClusterName)) {
    IdealState is = helixAdmin.getResourceIdealState(helixClusterName, topic);
    int numPartitions = is.getNumPartitions();
    for (int partition = 0; partition < numPartitions; ++partition) {
      if (is.getInstanceSet(Integer.toString(partition)).isEmpty()) {
        TopicPartition tpi = new TopicPartition(topic, partition);
        unassignedPartitions.add(tpi);
      }
    }
  }
  return unassignedPartitions;
}
 
Example 2
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 3
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 4
Source File: WagedRebalancer.java    From helix with Apache License 2.0 6 votes vote down vote up
public WagedRebalancer(HelixManager helixManager) {
  this(helixManager == null ? null
          : constructAssignmentStore(helixManager.getMetadataStoreConnectionString(),
              helixManager.getClusterName()),
      DEFAULT_REBALANCE_ALGORITHM,
      // Use DelayedAutoRebalancer as the mapping calculator for the final assignment output.
      // Mapping calculator will translate the best possible assignment into the applicable state
      // mapping based on the current states.
      // TODO abstract and separate the main mapping calculator logic from DelayedAutoRebalancer
      new DelayedAutoRebalancer(),
      // Helix Manager is required for the rebalancer scheduler
      helixManager,
      // If HelixManager is null, we just pass in a non-functioning WagedRebalancerMetricCollector
      // that will not be registered to MBean.
      // This is to handle two cases: 1. HelixManager is null for non-testing cases. In this case,
      // WagedRebalancer will not read/write to metadata store and just use CurrentState-based
      // rebalancing. 2. Tests that require instrumenting the rebalancer for verifying whether the
      // cluster has converged.
      helixManager == null ? new WagedRebalancerMetricCollector()
          : new WagedRebalancerMetricCollector(helixManager.getClusterName()),
      ClusterConfig.DEFAULT_GLOBAL_REBALANCE_ASYNC_MODE_ENABLED);
  _preference = ImmutableMap.copyOf(ClusterConfig.DEFAULT_GLOBAL_REBALANCE_PREFERENCE);
}
 
Example 5
Source File: GenericLeaderStandbyModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "STANDBY", from = "LEADER")
public void onBecomeStandbyFromLeader(Message message, NotificationContext context) {
  LOG.info("Become STANDBY from LEADER");
  HelixManager manager = context.getManager();
  if (manager == null) {
    throw new IllegalArgumentException("Require HelixManager in notification conext");
  }

  Builder keyBuilder = new Builder(manager.getClusterName());
  for (ChangeType notificationType : _notificationTypes) {
    if (notificationType == ChangeType.LIVE_INSTANCE) {
      manager.removeListener(keyBuilder.liveInstances(), _particHolder);
    } else if (notificationType == ChangeType.CONFIG) {
      manager.removeListener(keyBuilder.instanceConfigs(), _particHolder);
    } else if (notificationType == ChangeType.EXTERNAL_VIEW) {
      manager.removeListener(keyBuilder.externalViews(), _particHolder);
    } else {
      LOG.error("Unsupport notificationType:" + notificationType.toString());
    }
  }
}
 
Example 6
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 7
Source File: AutoRebalanceLiveInstanceChangeListener.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
private void assignIdealStates(HelixManager helixManager,
    Map<String, IdealState> idealStatesFromAssignment) {
  HelixAdmin helixAdmin = helixManager.getClusterManagmentTool();
  String helixClusterName = helixManager.getClusterName();
  for (String topic : idealStatesFromAssignment.keySet()) {
    IdealState idealState = idealStatesFromAssignment.get(topic);
    helixAdmin.setResourceIdealState(helixClusterName, topic, idealState);
  }
}
 
Example 8
Source File: HelixUtils.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
/**
 * From IdealStates.
 *
 * @return InstanceToNumTopicPartitionMap
 */
public static Map<String, Set<TopicPartition>> getInstanceToTopicPartitionsMap(
    HelixManager helixManager,
    Map<String, KafkaBrokerTopicObserver> clusterToObserverMap) {
  Map<String, Set<TopicPartition>> instanceToNumTopicPartitionMap = new HashMap<>();
  HelixAdmin helixAdmin = helixManager.getClusterManagmentTool();
  String helixClusterName = helixManager.getClusterName();
  for (String topic : helixAdmin.getResourcesInCluster(helixClusterName)) {
    IdealState is = helixAdmin.getResourceIdealState(helixClusterName, topic);
    for (String partition : is.getPartitionSet()) {
      TopicPartition tpi;
      if (partition.startsWith("@")) {
        if (clusterToObserverMap != null) {
          TopicPartition topicParition = clusterToObserverMap.get(getSrcFromRoute(partition))
              .getTopicPartitionWithRefresh(topic);
          int trueNumPartition = topicParition != null ? topicParition.getPartition() : -1;
          tpi = new TopicPartition(topic, trueNumPartition, partition);
        } else {
          tpi = new TopicPartition(topic, -1, partition);
        }
      } else {
        // route
        tpi = new TopicPartition(topic, Integer.parseInt(partition));
      }
      for (String instance : is.getInstanceSet(partition)) {
        instanceToNumTopicPartitionMap.putIfAbsent(instance, new HashSet<>());
        instanceToNumTopicPartitionMap.get(instance).add(tpi);
      }
    }
  }
  return instanceToNumTopicPartitionMap;
}
 
Example 9
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 10
Source File: TestEnablePartitionDuringDisable.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context) {
  HelixManager manager = context.getManager();
  String clusterName = manager.getClusterName();

  String instance = message.getTgtName();
  String partitionName = message.getPartitionName();
  String fromState = message.getFromState();
  String toState = message.getToState();
  if (instance.equals("localhost_12919") && partitionName.equals("TestDB0_0")) {
    if (fromState.equals("SLAVE") && toState.equals("OFFLINE")) {
      slaveToOfflineCnt++;

      try {
        String command = "--zkSvr " + ZK_ADDR + " --enablePartition true " + clusterName
            + " localhost_12919 TestDB0 TestDB0_0";

        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
      } catch (Exception e) {
        LOG.error("Exception in cluster setup", e);
      }

    } else if (slaveToOfflineCnt > 0 && fromState.equals("OFFLINE")
        && toState.equals("SLAVE")) {
      offlineToSlave++;
    }
  }
}
 
Example 11
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 12
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 13
Source File: TaskDriver.java    From helix with Apache License 2.0 4 votes vote down vote up
public TaskDriver(HelixManager manager) {
  this(manager.getClusterManagmentTool(), manager.getHelixDataAccessor(),
      manager.getHelixPropertyStore(), manager.getClusterName());
}