Skip to content

Commit

Permalink
Add support for random single-zone stoppable check
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkGaox committed Oct 18, 2023
1 parent 6d56cce commit 98362eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public StoppableInstancesSelector(String clusterId, List<String> orderOfZone,
*/
public void getStoppableInstancesInSingleZone(List<String> instances) throws IOException {
List<String> zoneBasedInstance =
getZoneBasedInstances(instances, _orderOfZone, _clusterTopology.toZoneMapping());
getZoneBasedInstances(instances, _clusterTopology.toZoneMapping());
Map<String, StoppableCheck> instancesStoppableChecks =
_maintenanceService.batchGetInstancesStoppableChecks(_clusterId, zoneBasedInstance,
_customizedInput);
Expand Down Expand Up @@ -110,6 +110,30 @@ public void getStoppableInstancesCrossZones() {
throw new NotImplementedException("Not Implemented");
}

/**
* Determines the order of zones. If an order is provided by the user, it will be used directly.
* Otherwise, zones will be ordered by their associated instance count in descending order.
*
* If `isRandom` is true, the order of zones will be randomized regardless of any previous order.
*
* @param isRandom Indicates whether to randomize the order of zones.
*/
public void calculateOrderOfZone(boolean isRandom) {
// If the orderedZones is not specified, we will order all zones by their instances count in descending order.
if (_orderOfZone == null) {
_orderOfZone =
new ArrayList<>(getOrderedZoneToInstancesMap(_clusterTopology.toZoneMapping()).keySet());
}

if (_orderOfZone.isEmpty()) {
return;
}

if (isRandom) {
Collections.shuffle(_orderOfZone);
}
}

/**
* Get instances belongs to the first zone. If the zone is already empty, Helix will iterate zones
* by order until find the zone contains instances.
Expand All @@ -118,23 +142,17 @@ public void getStoppableInstancesCrossZones() {
* zones by the number of associated instances in descending order.
*
* @param instances
* @param orderedZones
* @param zoneMapping
* @return
*/
private List<String> getZoneBasedInstances(List<String> instances, List<String> orderedZones,
private List<String> getZoneBasedInstances(List<String> instances,
Map<String, Set<String>> zoneMapping) {

// If the orderedZones is not specified, we will order all zones by their instances count in descending order.
if (orderedZones == null) {
orderedZones = new ArrayList<>(getOrderedZoneToInstancesMap(zoneMapping).keySet());
}

if (orderedZones.isEmpty()) {
return orderedZones;
if (_orderOfZone.isEmpty()) {
return _orderOfZone;
}

Set<String> instanceSet = null;
for (String zone : orderedZones) {
for (String zone : _orderOfZone) {
instanceSet = new TreeSet<>(instances);
Set<String> currentZoneInstanceSet = new HashSet<>(zoneMapping.get(zone));
instanceSet.retainAll(currentZoneInstanceSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public Response instancesOperations(@PathParam("clusterId") String clusterId,
@QueryParam("command") String command,
@QueryParam("continueOnFailures") boolean continueOnFailures,
@QueryParam("skipZKRead") boolean skipZKRead,
@QueryParam("skipHealthCheckCategories") String skipHealthCheckCategories, String content) {
@QueryParam("skipHealthCheckCategories") String skipHealthCheckCategories,
@QueryParam("random") boolean random, String content) {
Command cmd;
try {
cmd = Command.valueOf(command);
Expand Down Expand Up @@ -193,7 +194,7 @@ public Response instancesOperations(@PathParam("clusterId") String clusterId,
break;
case stoppable:
return batchGetStoppableInstances(clusterId, node, skipZKRead, continueOnFailures,
skipHealthCheckCategorySet);
skipHealthCheckCategorySet, random);
default:
_logger.error("Unsupported command :" + command);
return badRequest("Unsupported command :" + command);
Expand All @@ -210,8 +211,8 @@ public Response instancesOperations(@PathParam("clusterId") String clusterId,
}

private Response batchGetStoppableInstances(String clusterId, JsonNode node, boolean skipZKRead,
boolean continueOnFailures, Set<StoppableCheck.Category> skipHealthCheckCategories)
throws IOException {
boolean continueOnFailures, Set<StoppableCheck.Category> skipHealthCheckCategories,
boolean random) throws IOException {
try {
// TODO: Process input data from the content
InstancesAccessor.InstanceHealthSelectionBase selectionBase =
Expand Down Expand Up @@ -258,6 +259,7 @@ private Response batchGetStoppableInstances(String clusterId, JsonNode node, boo
.setMaintenanceService(maintenanceService)
.setClusterTopology(clusterTopology)
.build();
stoppableInstancesSelector.calculateOrderOfZone(random);
switch (selectionBase) {
case zone_based:
stoppableInstancesSelector.getStoppableInstancesInSingleZone(instances);
Expand Down

0 comments on commit 98362eb

Please sign in to comment.