Skip to content

Commit

Permalink
Add REST API endpoints for WAGED Rebalancer (#611)
Browse files Browse the repository at this point in the history
We want to make WAGED rebalancer (weight-aware) easier to use. One way to do this is to allow the user to easily add resources with weight configuration set by providing REST endpoints. This change adds the relevant REST endpoints based on the HelixAdmin APIs added in (#570).

Basically, this commit uses existing REST endpoints whose hierarchy is defined by REST resource. What this commit does to the existing endpoints is 1) Add extra commands 2) Add a WAGED command as a QueryParam so that WAGED logic could be included.

This change is backward-compatible because it keeps the original behavior when no commands are provided by using @DefaultValue annotation.
  • Loading branch information
narendly authored and Jiajun Wang committed Feb 4, 2020
1 parent 0f8d39f commit 6694dbc
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ public enum Command {
rebalance,
reset,
resetPartitions,
removeInstanceTag
removeInstanceTag,
addResource,
addWagedResource,
getResource,
validateWeight,
enableWagedRebalance,
enableWagedRebalanceForAllResources,
getInstance,
getAllInstances
}

@Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,15 @@ public Response updateCluster(@PathParam("clusterId") String clusterId,
helixAdmin.manuallyEnableMaintenanceMode(clusterId, command == Command.enableMaintenanceMode,
content, customFieldsMap);
break;

case enableWagedRebalanceForAllResources:
// Enable WAGED rebalance for all resources in the cluster
List<String> resources = helixAdmin.getResourcesInCluster(clusterId);
try {
helixAdmin.enableWagedRebalance(clusterId, resources);
} catch (HelixException e) {
return badRequest(e.getMessage());
}
break;
default:
return badRequest("Unsupported command " + command);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package org.apache.helix.rest.server.resources.helix;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -8,6 +27,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -61,41 +81,65 @@ public enum InstanceHealthSelectionBase {
}

@GET
public Response getAllInstances(@PathParam("clusterId") String clusterId) {
public Response getAllInstances(@PathParam("clusterId") String clusterId,
@DefaultValue("getAllInstances") @QueryParam("command") String command) {
// Get the command. If not provided, the default would be "getAllInstances"
Command cmd;
try {
cmd = Command.valueOf(command);
} catch (Exception e) {
return badRequest("Invalid command : " + command);
}

HelixDataAccessor accessor = getDataAccssor(clusterId);
List<String> instances = accessor.getChildNames(accessor.keyBuilder().instanceConfigs());

if (instances == null) {
return notFound();
}

ObjectNode root = JsonNodeFactory.instance.objectNode();
root.put(Properties.id.name(), JsonNodeFactory.instance.textNode(clusterId));
switch (cmd) {
case getAllInstances:
ObjectNode root = JsonNodeFactory.instance.objectNode();
root.put(Properties.id.name(), JsonNodeFactory.instance.textNode(clusterId));

ArrayNode instancesNode = root.putArray(InstancesAccessor.InstancesProperties.instances.name());
instancesNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(instances));
ArrayNode onlineNode = root.putArray(InstancesAccessor.InstancesProperties.online.name());
ArrayNode disabledNode = root.putArray(InstancesAccessor.InstancesProperties.disabled.name());
ArrayNode instancesNode =
root.putArray(InstancesAccessor.InstancesProperties.instances.name());
instancesNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(instances));
ArrayNode onlineNode = root.putArray(InstancesAccessor.InstancesProperties.online.name());
ArrayNode disabledNode = root.putArray(InstancesAccessor.InstancesProperties.disabled.name());

List<String> liveInstances = accessor.getChildNames(accessor.keyBuilder().liveInstances());
ClusterConfig clusterConfig = accessor.getProperty(accessor.keyBuilder().clusterConfig());
List<String> liveInstances = accessor.getChildNames(accessor.keyBuilder().liveInstances());
ClusterConfig clusterConfig = accessor.getProperty(accessor.keyBuilder().clusterConfig());

for (String instanceName : instances) {
InstanceConfig instanceConfig =
accessor.getProperty(accessor.keyBuilder().instanceConfig(instanceName));
if (instanceConfig != null) {
if (!instanceConfig.getInstanceEnabled() || (clusterConfig.getDisabledInstances() != null
&& clusterConfig.getDisabledInstances().containsKey(instanceName))) {
disabledNode.add(JsonNodeFactory.instance.textNode(instanceName));
}
for (String instanceName : instances) {
InstanceConfig instanceConfig =
accessor.getProperty(accessor.keyBuilder().instanceConfig(instanceName));
if (instanceConfig != null) {
if (!instanceConfig.getInstanceEnabled() || (clusterConfig.getDisabledInstances() != null
&& clusterConfig.getDisabledInstances().containsKey(instanceName))) {
disabledNode.add(JsonNodeFactory.instance.textNode(instanceName));
}

if (liveInstances.contains(instanceName)){
onlineNode.add(JsonNodeFactory.instance.textNode(instanceName));
if (liveInstances.contains(instanceName)) {
onlineNode.add(JsonNodeFactory.instance.textNode(instanceName));
}
}
}
return JSONRepresentation(root);
case validateWeight:
// Validate all instances for WAGED rebalance
HelixAdmin admin = getHelixAdmin();
Map<String, Boolean> validationResultMap;
try {
validationResultMap = admin.validateInstancesForWagedRebalance(clusterId, instances);
} catch (HelixException e) {
return badRequest(e.getMessage());
}
return JSONRepresentation(validationResultMap);
default:
_logger.error("Unsupported command :" + command);
return badRequest("Unsupported command :" + command);
}

return JSONRepresentation(root);
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
*/

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
Expand Down Expand Up @@ -81,16 +84,41 @@ public enum PerInstanceProperties {

@GET
public Response getInstanceById(@PathParam("clusterId") String clusterId,
@PathParam("instanceName") String instanceName) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
// TODO reduce GC by dependency injection
InstanceService instanceService =
new InstanceServiceImpl(new HelixDataAccessorWrapper((ZKHelixDataAccessor) dataAccessor), getConfigAccessor());
InstanceInfo instanceInfo = instanceService.getInstanceInfo(clusterId, instanceName,
InstanceService.HealthCheck.STARTED_AND_HEALTH_CHECK_LIST);
@PathParam("instanceName") String instanceName,
@DefaultValue("getInstance") @QueryParam("command") String command) throws IOException {
// Get the command. If not provided, the default would be "getInstance"
Command cmd;
try {
cmd = Command.valueOf(command);
} catch (Exception e) {
return badRequest("Invalid command : " + command);
}

return OK(objectMapper.writeValueAsString(instanceInfo));
switch (cmd) {
case getInstance:
ObjectMapper objectMapper = new ObjectMapper();
HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
// TODO reduce GC by dependency injection
InstanceService instanceService = new InstanceServiceImpl(
new HelixDataAccessorWrapper((ZKHelixDataAccessor) dataAccessor), getConfigAccessor());
InstanceInfo instanceInfo = instanceService.getInstanceInfo(clusterId, instanceName,
InstanceService.HealthCheck.STARTED_AND_HEALTH_CHECK_LIST);
return OK(objectMapper.writeValueAsString(instanceInfo));
case validateWeight:
// Validates instanceConfig for WAGED rebalance
HelixAdmin admin = getHelixAdmin();
Map<String, Boolean> validationResultMap;
try {
validationResultMap = admin.validateInstancesForWagedRebalance(clusterId,
Collections.singletonList(instanceName));
} catch (HelixException e) {
return badRequest(e.getMessage());
}
return JSONRepresentation(validationResultMap);
default:
LOG.error("Unsupported command :" + command);
return badRequest("Unsupported command :" + command);
}
}

@POST
Expand Down Expand Up @@ -345,7 +373,8 @@ public Response getResourceOnInstance(@PathParam("clusterId") String clusterId,
return notFound();
}

@GET @Path("errors")
@GET
@Path("errors")
public Response getErrorsOnInstance(@PathParam("clusterId") String clusterId,
@PathParam("instanceName") String instanceName) throws IOException {
HelixDataAccessor accessor = getDataAccssor(clusterId);
Expand Down
Loading

0 comments on commit 6694dbc

Please sign in to comment.