use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ConfigResource method getConfigKeys.
StringRepresentation getConfigKeys(ConfigScopeProperty scopeProperty, String... keys) throws Exception {
StringRepresentation representation = null;
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
HelixAdmin admin = setupTool.getClusterManagementTool();
ZNRecord record = new ZNRecord(scopeProperty + " Config");
HelixConfigScope scope = new HelixConfigScopeBuilder(scopeProperty, keys).build();
List<String> configKeys = admin.getConfigKeys(scope);
record.setListField(scopeProperty.toString(), configKeys);
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ControllerStatusUpdateResource method getControllerStatusUpdateRepresentation.
StringRepresentation getControllerStatusUpdateRepresentation(String zkServerAddress, String clusterName, String sessionId, String messageType, String messageId) throws JsonGenerationException, JsonMappingException, IOException {
Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
String message = ClusterRepresentationUtil.getPropertyAsString(zkClient, clusterName, keyBuilder.controllerTaskStatus(messageType, messageId), MediaType.APPLICATION_JSON);
StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class CurrentStateResource method getInstanceCurrentStateRepresentation.
StringRepresentation getInstanceCurrentStateRepresentation(String clusterName, String instanceName, String resourceGroup) throws JsonGenerationException, JsonMappingException, IOException {
ZkClient zkClient = (ZkClient) getRequest().getAttributes().get(RestAdminApplication.ZKCLIENT);
String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
Builder keyBuilder = new PropertyKey.Builder(clusterName);
String message = ClusterRepresentationUtil.getInstancePropertyAsString(zkClient, clusterName, keyBuilder.currentState(instanceName, instanceSessionId, resourceGroup), MediaType.APPLICATION_JSON);
StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ErrorResource method getInstanceErrorsRepresentation.
StringRepresentation getInstanceErrorsRepresentation(String clusterName, String instanceName, String resourceGroup) throws JsonGenerationException, JsonMappingException, IOException {
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
Builder keyBuilder = new PropertyKey.Builder(clusterName);
String message = ClusterRepresentationUtil.getInstancePropertiesAsString(zkClient, clusterName, keyBuilder.stateTransitionErrors(instanceName, instanceSessionId, resourceGroup), // + resourceGroup,
MediaType.APPLICATION_JSON);
StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class IdealStateResource method post.
/**
* Set ideal state
* <p>
* Usage:
* <p>
* <li>Add ideal state:
* <code>curl -d @'{newIdealState.json}' -H 'Content-Type: application/json'
* http://{host:port}/clusters/{cluster}/resourceGroups/{resource}/idealState
* <pre>
* newIdealState:
* jsonParameters={"command":"addIdealState"}&newIdealState={
* "id" : "{MyDB}",
* "simpleFields" : {
* "IDEAL_STATE_MODE" : "AUTO",
* "NUM_PARTITIONS" : "{8}",
* "REBALANCE_MODE" : "SEMI_AUTO",
* "REPLICAS" : "0",
* "STATE_MODEL_DEF_REF" : "MasterSlave",
* "STATE_MODEL_FACTORY_NAME" : "DEFAULT"
* },
* "listFields" : {
* },
* "mapFields" : {
* "{MyDB_0}" : {
* "{localhost_1001}" : "MASTER",
* "{localhost_1002}" : "SLAVE"
* }
* }
* }
* </pre>
* <li>Rebalance cluster:
* <code>curl -d 'jsonParameters={"command":"rebalance","replicas":"{3}"}'
* -H "Content-Type: application/json" http://{host:port}/clusters/{cluster}/resourceGroups/{resource}/idealState
* <li>Expand resource: <code>n/a
* <li>Add resource property:
* <code>curl -d 'jsonParameters={"command":"addResourceProperty","{REBALANCE_TIMER_PERIOD}":"{500}"}'
* -H "Content-Type: application/json" http://{host:port}/clusters/{cluster}/resourceGroups/{resource}/idealState
*/
@Override
public Representation post(Representation entity) {
try {
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String resourceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.RESOURCE_NAME);
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
JsonParameters jsonParameters = new JsonParameters(entity);
String command = jsonParameters.getCommand();
if (command.equalsIgnoreCase(ClusterSetup.addIdealState)) {
ZNRecord newIdealState = jsonParameters.getExtraParameter(JsonParameters.NEW_IDEAL_STATE);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
accessor.setProperty(accessor.keyBuilder().idealStates(resourceName), new IdealState(newIdealState));
} else if (command.equalsIgnoreCase(ClusterSetup.rebalance)) {
int replicas = Integer.parseInt(jsonParameters.getParameter(JsonParameters.REPLICAS));
String keyPrefix = jsonParameters.getParameter(JsonParameters.RESOURCE_KEY_PREFIX);
String groupTag = jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
setupTool.rebalanceCluster(clusterName, resourceName, replicas, keyPrefix, groupTag);
} else if (command.equalsIgnoreCase(ClusterSetup.expandResource)) {
setupTool.expandResource(clusterName, resourceName);
} else if (command.equalsIgnoreCase(ClusterSetup.addResourceProperty)) {
Map<String, String> parameterMap = jsonParameters.cloneParameterMap();
parameterMap.remove(JsonParameters.MANAGEMENT_COMMAND);
for (String key : parameterMap.keySet()) {
setupTool.addResourceProperty(clusterName, resourceName, key, parameterMap.get(key));
}
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.addIdealState + ", " + ClusterSetup.rebalance + ", " + ClusterSetup.expandResource + ", " + ClusterSetup.addResourceProperty + "]");
}
getResponse().setEntity(getIdealStateRepresentation(clusterName, resourceName));
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("Error in posting " + entity, e);
}
return null;
}
Aggregations