in helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ResourceAccessor.java [235:319]
public Response addResource(@PathParam("clusterId") String clusterId,
@PathParam("resourceName") String resourceName,
@DefaultValue("-1") @QueryParam("numPartitions") int numPartitions,
@DefaultValue("") @QueryParam("stateModelRef") String stateModelRef,
@DefaultValue("SEMI_AUTO") @QueryParam("rebalancerMode") String rebalancerMode,
@DefaultValue("DEFAULT") @QueryParam("rebalanceStrategy") String rebalanceStrategy,
@DefaultValue("0") @QueryParam("bucketSize") int bucketSize,
@DefaultValue("-1") @QueryParam("maxPartitionsPerInstance") int maxPartitionsPerInstance,
@DefaultValue("addResource") @QueryParam("command") String command, String content) {
// Get the command. If not provided, the default would be "addResource"
Command cmd;
try {
cmd = Command.valueOf(command);
} catch (Exception e) {
return badRequest("Invalid command : " + command);
}
HelixAdmin admin = getHelixAdmin();
try {
switch (cmd) {
case addResource:
if (content.length() != 0) {
ZNRecord record;
try {
record = toZNRecord(content);
} catch (IOException e) {
_logger.error("Failed to deserialize user's input " + content + ", Exception: " + e);
return badRequest("Input is not a valid ZNRecord!");
}
if (record.getSimpleFields() != null) {
admin.addResource(clusterId, resourceName, new IdealState(record));
}
} else {
admin.addResource(clusterId, resourceName, numPartitions, stateModelRef, rebalancerMode,
rebalanceStrategy, bucketSize, maxPartitionsPerInstance);
}
break;
case addWagedResource:
// Check if content is valid
if (content == null || content.length() == 0) {
_logger.error("Input is null or empty!");
return badRequest("Input is null or empty!");
}
Map<String, ZNRecord> input;
// Content must supply both IdealState and ResourceConfig
try {
TypeReference<Map<String, ZNRecord>> typeRef =
new TypeReference<Map<String, ZNRecord>>() {
};
input = ZNRECORD_READER.forType(typeRef).readValue(content);
} catch (IOException e) {
_logger.error("Failed to deserialize user's input {}, Exception: {}", content, e);
return badRequest("Input is not a valid map of String-ZNRecord pairs!");
}
// Check if the map contains both IdealState and ResourceConfig
ZNRecord idealStateRecord =
input.get(ResourceAccessor.ResourceProperties.idealState.name());
ZNRecord resourceConfigRecord =
input.get(ResourceAccessor.ResourceProperties.resourceConfig.name());
if (idealStateRecord == null || resourceConfigRecord == null) {
_logger.error("Input does not contain both IdealState and ResourceConfig!");
return badRequest("Input does not contain both IdealState and ResourceConfig!");
}
// Add using HelixAdmin API
try {
admin.addResourceWithWeight(clusterId, new IdealState(idealStateRecord),
new ResourceConfig(resourceConfigRecord));
} catch (HelixException e) {
String errMsg = String.format("Failed to add resource %s with weight in cluster %s!",
idealStateRecord.getId(), clusterId);
_logger.error(errMsg, e);
return badRequest(errMsg);
}
break;
default:
_logger.error("Unsupported command :" + command);
return badRequest("Unsupported command :" + command);
}
} catch (Exception e) {
_logger.error("Error in adding a resource: " + resourceName, e);
return serverError(e);
}
return OK();
}