Search in sources :

Example 86 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixAdmin(org.apache.helix.HelixAdmin) ZNRecord(org.apache.helix.ZNRecord)

Example 87 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) Builder(org.apache.helix.PropertyKey.Builder)

Example 88 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) Builder(org.apache.helix.PropertyKey.Builder)

Example 89 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) Builder(org.apache.helix.PropertyKey.Builder)

Example 90 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ClusterSetup(org.apache.helix.tools.ClusterSetup) Map(java.util.Map) ZNRecord(org.apache.helix.ZNRecord) IdealState(org.apache.helix.model.IdealState) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException)

Aggregations

ZkClient (org.apache.helix.manager.zk.ZkClient)109 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)39 ZNRecord (org.apache.helix.ZNRecord)29 StringRepresentation (org.restlet.representation.StringRepresentation)29 ClusterSetup (org.apache.helix.tools.ClusterSetup)26 HelixException (org.apache.helix.HelixException)23 Builder (org.apache.helix.PropertyKey.Builder)18 IOException (java.io.IOException)17 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)15 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)10 Test (org.testng.annotations.Test)10 Date (java.util.Date)9 BeforeClass (org.testng.annotations.BeforeClass)9 PropertyKey (org.apache.helix.PropertyKey)8 IdealState (org.apache.helix.model.IdealState)8 InstanceConfig (org.apache.helix.model.InstanceConfig)8 StateModelDefinition (org.apache.helix.model.StateModelDefinition)7