Skip to content

Commit

Permalink
Add host identifier to resource info (#2679)
Browse files Browse the repository at this point in the history
Adding hostname as an identifier to find the resource.
  • Loading branch information
justinlin-linkedin authored Dec 22, 2023
1 parent 84cef6d commit 71875f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
5 changes: 5 additions & 0 deletions ambry-api/src/main/java/com/github/ambry/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ public static final class Headers {
* Request header to carry resource name;
*/
public final static String RESOURCE = "x-ambry-resource";

/**
* Request header to carry hostname (with port);
*/
public final static String HOSTNAME = "x-ambry-hostname";
}

public static final class TrackingHeaders {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
*/
public class HelixClusterManager implements ClusterMap {
private static final Logger logger = LoggerFactory.getLogger(HelixClusterManager.class);
private static final int DEFAULT_NUM_REPLICAS = 3;
private final String clusterName;
private final String selfInstanceName;
private final MetricRegistry metricRegistry;
Expand Down Expand Up @@ -1793,10 +1794,14 @@ private void updatePartitionResourceMappingFromIdealStates(Collection<IdealState
String resourceName = state.getResourceName();
String tag = state.getInstanceGroupTag();
int numPartitions = state.getNumPartitions();
int replicationFactor = state.getReplicaCount(DEFAULT_NUM_REPLICAS);
if (replicationFactor == 0) {
replicationFactor = DEFAULT_NUM_REPLICAS;
}
if (!Strings.isEmpty(tag)) {
resourceNameToTag.put(resourceName, tag);
ResourceProperty resourceProperty =
new ResourceProperty(resourceName, numPartitions, state.getRebalanceMode());
new ResourceProperty(resourceName, numPartitions, replicationFactor, state.getRebalanceMode());
tagToProperty.put(tag, resourceProperty);
}
resourceToPartitionMap.put(resourceName, new HashSet<>(state.getPartitionSet()));
Expand Down Expand Up @@ -2169,11 +2174,13 @@ ReplicaSealStatus resolveReplicaSealStatus(String partitionName, Collection<Stri
static class ResourceProperty {
final String name;
final int numPartitions;
final int replicationFactor; // number of replicas
final IdealState.RebalanceMode rebalanceMode;

ResourceProperty(String name, int numPartitions, IdealState.RebalanceMode rebalanceMode) {
ResourceProperty(String name, int numPartitions, int replicationFactor, IdealState.RebalanceMode rebalanceMode) {
this.numPartitions = numPartitions;
this.name = name;
this.replicationFactor = replicationFactor;
this.rebalanceMode = rebalanceMode;
}
}
Expand Down Expand Up @@ -2249,6 +2256,34 @@ public List<String> identifyResources(HelixClusterManager helixClusterManager) {
}
}

/**
* An implementation of {@link ResourceIdentifier} to return the resource name the given hostname belongs to .
*/
public static class HostnameIdentifier implements ResourceIdentifier {
private final String hostname;

/**
* Constructor to create a {@link HostnameIdentifier}.
* @param hostname
*/
public HostnameIdentifier(String hostname) {
this.hostname = hostname;
}

@Override
public List<String> identifyResources(HelixClusterManager helixClusterManager) {
String dcName = helixClusterManager.clusterMapConfig.clusterMapDatacenterName;
DataNodeId dataNodeId = helixClusterManager.instanceNameToAmbryDataNode.get(hostname);
if (dataNodeId == null || !dataNodeId.getDatacenterName().equals(dcName)) {
throw new IllegalArgumentException("Host " + hostname + " doesn't exist in this datacenter");
}
List<String> tags = helixClusterManager.instanceNameToInstanceConfig.get(hostname).getTags();
return tags.stream()
.map(tag -> helixClusterManager.dcToTagToResourceProperty.get(dataNodeId.getDatacenterName()).get(tag).name)
.collect(Collectors.toList());
}
}

/**
* An implementation to return all the resource this cluster map has.
*/
Expand Down Expand Up @@ -2283,8 +2318,7 @@ private ResourceInfo getResourceInfo(String resourceName) {
List<String> liveInstances = allInstances.stream()
.map(instanceNameToAmbryDataNode::get)
.filter(dn -> dn.getState() == HardwareState.AVAILABLE)
.map(ClusterMapUtils::getInstanceName)
.collect(Collectors.toList());
.map(ClusterMapUtils::getInstanceName).collect(Collectors.toList());
List<String> unavailableInstances = allInstances.stream()
.map(instanceNameToAmbryDataNode::get)
.filter(dn -> dn.getState() == HardwareState.UNAVAILABLE)
Expand All @@ -2294,21 +2328,17 @@ private ResourceInfo getResourceInfo(String resourceName) {
long liveCapacity = getResourceAvailableRegisteredHostDiskCapacity(resourceName);
long unavailableCapacity = totalCapacity - liveCapacity;
int numPartitions = getNumberOfPartitionsInResource(resourceName);
int replicationFactor = 3; // by default it is 3;
String numReplicaStr = getResourceConfig(resourceName, dcName).getNumReplica();
if (numReplicaStr != null) {
replicationFactor = Integer.parseInt(numReplicaStr);
}
String tag = dcToResourceNameToTag.get(dcName).get(resourceName);
int replicationFactor = dcToTagToResourceProperty.get(dcName).get(tag).replicationFactor;
int numExpectedReplicas = numPartitions * replicationFactor;
int numCurrentReplicas = getReplicaCountForStateInResource(null, resourceName);
int expectedTotalReplicaWeight = getResourceExpectedTotalDiskCapacityUsage(resourceName, replicationFactor);
int currentTotalReplicaWeight = getResourceTotalDiskCapacityUsage(resourceName);
Map<String, Set<String>> failedDisks = new HashMap<>();
for (String instanceName : getAllInstancesForResource(resourceName)) {
Set<AmbryDisk> disks = ambryDataNodeToAmbryDisks.get(instanceNameToAmbryDataNode.get(instanceName));
Set<String> failedDiskMountPath = disks.stream()
.filter(disk -> disk.getState() == HardwareState.UNAVAILABLE)
.map(AmbryDisk::getMountPath)
Set<String> failedDiskMountPath =
disks.stream().filter(disk -> disk.getState() == HardwareState.UNAVAILABLE).map(AmbryDisk::getMountPath)
.collect(Collectors.toSet());
if (!failedDiskMountPath.isEmpty()) {
failedDisks.put(instanceName, failedDiskMountPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ private Callback<Void> securityPostProcessRequestCallback() {

private List<ResourceInfo> queryResourceInfo() throws RestServiceException {
String partitionName = RestUtils.getHeader(restRequest.getArgs(), RestUtils.Headers.PARTITION, false);
String hostname = RestUtils.getHeader(restRequest.getArgs(), RestUtils.Headers.HOSTNAME, false);
String resourceName = RestUtils.getHeader(restRequest.getArgs(), RestUtils.Headers.RESOURCE, false);
HelixClusterManager.ResourceIdentifier resourceIdentifier;
if (partitionName != null) {
resourceIdentifier = new HelixClusterManager.PartitionIdIdentifier(partitionName);
} else if (hostname != null) {
resourceIdentifier = new HelixClusterManager.HostnameIdentifier(hostname);
} else if (resourceName != null) {
resourceIdentifier = new HelixClusterManager.ResourceNameIdentifier(resourceName);
} else {
Expand Down

0 comments on commit 71875f4

Please sign in to comment.