Java Code Examples for org.apache.helix.model.IdealState#getResourceName()

The following examples show how to use org.apache.helix.model.IdealState#getResourceName() . 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: TestRoutingTableProviderFromCurrentStates.java    From helix with Apache License 2.0 6 votes vote down vote up
private boolean compare(IdealState idealState, RoutingTableProvider routingTableEV,
    RoutingTableProvider routingTableCurrentStates) {
  String db = idealState.getResourceName();
  Set<String> partitions = idealState.getPartitionSet();
  for (String partition : partitions) {
    List<InstanceConfig> masterInsEv =
        routingTableEV.getInstancesForResource(db, partition, "MASTER");
    List<InstanceConfig> masterInsCs =
        routingTableCurrentStates.getInstancesForResource(db, partition, "MASTER");
    if (masterInsEv.size() != 1 || masterInsCs.size() != 1 || !masterInsCs.equals(masterInsEv)) {
      return false;
    }
    List<InstanceConfig> slaveInsEv =
        routingTableEV.getInstancesForResource(db, partition, "SLAVE");
    List<InstanceConfig> slaveInsCs =
        routingTableCurrentStates.getInstancesForResource(db, partition, "SLAVE");
    if (slaveInsEv.size() != 2 || slaveInsCs.size() != 2 || !new HashSet(slaveInsCs)
        .equals(new HashSet(slaveInsEv))) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: SimpleClusterChangeHandler.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Triggered whenever the IdealState in current data center has changed (for now, it is usually updated by Helix
 * Bootstrap tool).
 * @param idealState a list of {@link IdealState} that specifies ideal location of replicas.
 * @param changeContext the {@link NotificationContext} associated.
 * @throws InterruptedException
 */
@Override
public void onIdealStateChange(List<IdealState> idealState, NotificationContext changeContext)
    throws InterruptedException {
  if (!idealStateInitialized.get()) {
    logger.info("Received initial notification for IdealState change from {}", dcName);
    idealStateInitialized.set(true);
  } else {
    logger.info("IdealState change triggered from {}", dcName);
  }
  // rebuild the entire partition-to-resource map in current dc
  ConcurrentHashMap<String, String> newPartitionToResourceMap = new ConcurrentHashMap<>();
  for (IdealState state : idealState) {
    String resourceName = state.getResourceName();
    for (String partitionStr : state.getPartitionSet()) {
      newPartitionToResourceMap.put(partitionStr, resourceName);
    }
  }
  partitionNameToResource = newPartitionToResourceMap;
  helixClusterManagerMetrics.idealStateChangeTriggerCount.inc();
}
 
Example 3
Source File: DynamicClusterChangeHandler.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Triggered whenever the IdealState in current data center has changed (for now, it is usually updated by Helix
 * Bootstrap tool).
 * @param idealState a list of {@link IdealState} that specifies ideal location of replicas.
 * @param changeContext the {@link NotificationContext} associated.
 */
@Override
public void onIdealStateChange(List<IdealState> idealState, NotificationContext changeContext) {
  if (!idealStateInitialized) {
    logger.info("Received initial notification for IdealState change from {}", dcName);
    idealStateInitialized = true;
  } else {
    logger.info("IdealState change triggered from {}", dcName);
  }
  logger.debug("Detailed ideal states in {} are: {}", dcName, idealState);
  // rebuild the entire partition-to-resource map in current dc
  ConcurrentHashMap<String, String> partitionToResourceMap = new ConcurrentHashMap<>();
  for (IdealState state : idealState) {
    String resourceName = state.getResourceName();
    state.getPartitionSet().forEach(partitionName -> partitionToResourceMap.put(partitionName, resourceName));
  }
  partitionNameToResource = partitionToResourceMap;
  helixClusterManagerMetrics.idealStateChangeTriggerCount.inc();
}
 
Example 4
Source File: DelayedAutoRebalancer.java    From helix with Apache License 2.0 5 votes vote down vote up
private ZNRecord getFinalDelayedMapping(IdealState idealState, ZNRecord newIdealMapping,
    ZNRecord newActiveMapping, Set<String> liveInstances, int numReplica, int minActiveReplica) {
  if (minActiveReplica >= numReplica) {
    return newIdealMapping;
  }
  ZNRecord finalMapping = new ZNRecord(idealState.getResourceName());
  finalMapping.setListFields(DelayedRebalanceUtil
      .getFinalDelayedMapping(newIdealMapping.getListFields(), newActiveMapping.getListFields(),
          liveInstances, minActiveReplica));
  return finalMapping;
}
 
Example 5
Source File: AbstractRebalancer.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Compute if an overwritten is necessary for the partition assignment in case that the proposed
 * assignment is not valid or empty.
 * @param stateModelDef
 * @param preferenceList
 * @param currentStateOutput
 * @param idealState
 * @param partition
 * @param monitoredResolver
 * @return An optional object which contains the assignment map if overwritten is necessary.
 * Otherwise return Optional.empty().
 */
protected Optional<Map<String, String>> computeStatesOverwriteForPartition(
    final StateModelDefinition stateModelDef, final List<String> preferenceList,
    final CurrentStateOutput currentStateOutput, IdealState idealState, final Partition partition,
    final MonitoredAbnormalResolver monitoredResolver) {
  String resourceName = idealState.getResourceName();
  Map<String, String> currentStateMap =
      currentStateOutput.getCurrentStateMap(resourceName, partition);

  // (1) If the partition is removed from IS or the IS is deleted.
  // Transit to DROPPED no matter the instance is disabled or not.
  if (preferenceList == null) {
    return Optional.of(computeBestPossibleMapForDroppedResource(currentStateMap));
  }

  // (2) If resource disabled altogether, transit to initial-state (e.g. OFFLINE) if it's not in ERROR.
  if (!idealState.isEnabled()) {
    return Optional.of(computeBestPossibleMapForDisabledResource(currentStateMap, stateModelDef));
  }

  // (3) If the current states are not valid, fix the invalid part first.
  if (!monitoredResolver
      .checkCurrentStates(currentStateOutput, resourceName, partition, stateModelDef)) {
    monitoredResolver.recordAbnormalState();
    Map<String, String> recoveryAssignment = monitoredResolver
        .computeRecoveryAssignment(currentStateOutput, resourceName, partition, stateModelDef,
            preferenceList);
    if (recoveryAssignment == null || !recoveryAssignment.keySet()
        .equals(currentStateMap.keySet())) {
      throw new HelixException(String.format(
          "Invalid recovery assignment %s since it changed the current partition placement %s",
          recoveryAssignment, currentStateMap));
    }
    monitoredResolver.recordRecoveryAttempt();
    return Optional.of(recoveryAssignment);
  }

  return Optional.empty();
}
 
Example 6
Source File: TestRoutingTableSnapshot.java    From helix with Apache License 2.0 5 votes vote down vote up
private void validateMapping(IdealState idealState, RoutingTableSnapshot routingTableSnapshot) {
  String db = idealState.getResourceName();
  Set<String> partitions = idealState.getPartitionSet();
  for (String partition : partitions) {
    List<InstanceConfig> masterInsEv =
        routingTableSnapshot.getInstancesForResource(db, partition, "MASTER");
    Assert.assertEquals(masterInsEv.size(), 1);

    List<InstanceConfig> slaveInsEv =
        routingTableSnapshot.getInstancesForResource(db, partition, "SLAVE");
    Assert.assertEquals(slaveInsEv.size(), 2);
  }
}
 
Example 7
Source File: PinotLLCRealtimeSegmentManager.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all LLC segments from the given IdealState.
 */
public void removeLLCSegments(IdealState idealState) {
  Preconditions.checkState(!_isStopping, "Segment manager is stopping");

  String realtimeTableName = idealState.getResourceName();
  LOGGER.info("Removing LLC segments for table: {}", realtimeTableName);

  List<String> segmentsToRemove = new ArrayList<>();
  for (String segmentName : idealState.getRecord().getMapFields().keySet()) {
    if (SegmentName.isLowLevelConsumerSegmentName(segmentName)) {
      segmentsToRemove.add(segmentName);
    }
  }
  _helixResourceManager.deleteSegments(realtimeTableName, segmentsToRemove);
}
 
Example 8
Source File: HelixClusterManagerTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that {@link HelixClusterManager} receives and handles initial cluster changes (i.e InstanceConfig, IdealState
 * change), and populates the in-mem clustermap correctly.
 * @param clusterManager the {@link HelixClusterManager} to use for verification.
 * @param helixCluster the {@link MockHelixCluster} to provide cluster infos
 */
private void verifyInitialClusterChanges(HelixClusterManager clusterManager, MockHelixCluster helixCluster,
    String[] dcs) {
  // get in-mem data structures populated based on initial notification
  Map<String, Map<String, String>> partitionToResouceByDc = clusterManager.getPartitionToResourceMap();
  Map<String, Set<AmbryDataNode>> dataNodesByDc = clusterManager.getDcToDataNodesMap();

  for (String dc : dcs) {
    // 1. verify all instanceConfigs from Helix are present in cluster manager
    List<InstanceConfig> instanceConfigsInCluster = helixCluster.getInstanceConfigsFromDcs(new String[]{dc});
    assertEquals("Mismatch in number of instances", instanceConfigsInCluster.size(), dataNodesByDc.get(dc).size());
    Set<String> hostsFromClusterManager =
        dataNodesByDc.get(dc).stream().map(AmbryDataNode::getHostname).collect(Collectors.toSet());
    Set<String> hostsFromHelix =
        instanceConfigsInCluster.stream().map(InstanceConfig::getHostName).collect(Collectors.toSet());
    assertEquals("Mismatch in hosts set", hostsFromHelix, hostsFromClusterManager);

    // 2. verify all resources and partitions from Helix are present in cluster manager
    Map<String, String> partitionToResourceMap = partitionToResouceByDc.get(dc);
    MockHelixAdmin helixAdmin = helixCluster.getHelixAdminFromDc(dc);
    List<IdealState> idealStates = helixAdmin.getIdealStates();
    for (IdealState idealState : idealStates) {
      String resourceName = idealState.getResourceName();
      Set<String> partitionSet = idealState.getPartitionSet();
      for (String partitionStr : partitionSet) {
        assertEquals("Mismatch in resource name", resourceName, partitionToResourceMap.get(partitionStr));
      }
    }
  }
  if (cloudDc != null) {
    // If one cloud DC is present, there should be exactly one virtual replica for every partition.
    for (PartitionId partitionId : clusterManager.getAllPartitionIds(null)) {
      List<? extends ReplicaId> replicaIds = partitionId.getReplicaIds();
      int count = 0;
      for (ReplicaId replicaId : replicaIds) {
        if (replicaId instanceof CloudServiceReplica) {
          count++;
        }
      }
      assertEquals("Unexpected number of CloudServiceReplicas in partition: " + replicaIds, 1, count);
    }
  }
}