Search in sources :

Example 11 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ClusterResource method post.

/**
 * Activate/deactivate a cluster in distributed controller mode
 * <p>
 * Usage: <code> curl -d 'jsonParameters=
 * {"command":"activateCluster","grandCluster":"{controllerCluster}","enabled":"{true/false}"}' -H
 * "Content-Type: application/json" http://{host:port}/clusters/{clusterName}}
 */
@Override
public Representation post(Representation entity) {
    try {
        String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_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 == null) {
            throw new HelixException("Could NOT find 'command' in parameterMap: " + jsonParameters._parameterMap);
        } else if (command.equalsIgnoreCase(ClusterSetup.activateCluster) || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.activateCluster).contains(command)) {
            jsonParameters.verifyCommand(ClusterSetup.activateCluster);
            boolean enabled = true;
            if (jsonParameters.getParameter(JsonParameters.ENABLED) != null) {
                enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
            }
            String grandCluster = jsonParameters.getParameter(JsonParameters.GRAND_CLUSTER);
            setupTool.activateCluster(clusterName, grandCluster, enabled);
        } else if (command.equalsIgnoreCase(ClusterSetup.expandCluster)) {
            setupTool.expandCluster(clusterName);
        } else {
            throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.activateCluster + ", " + ClusterSetup.expandCluster + "]");
        }
        getResponse().setEntity(getClusterRepresentation(clusterName));
        getResponse().setStatus(Status.SUCCESS_OK);
    } catch (Exception e) {
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
    }
    return getResponseEntity();
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) ClusterSetup(org.apache.helix.tools.ClusterSetup) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException)

Example 12 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ClustersResource method post.

/**
 * Add a new Helix cluster
 * <p>
 * Usage: <code> curl -d 'jsonParameters={"command":"addCluster","clusterName":"{clusterName}"}' -H
 * "Content-Type: application/json" http://{host:port}/clusters
 */
@Override
public Representation post(Representation entity) {
    try {
        JsonParameters jsonParameters = new JsonParameters(entity);
        String command = jsonParameters.getCommand();
        if (command == null) {
            throw new HelixException("Could NOT find 'command' in parameterMap: " + jsonParameters._parameterMap);
        } else if (command.equalsIgnoreCase(ClusterSetup.addCluster)) {
            jsonParameters.verifyCommand(ClusterSetup.addCluster);
            ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
            ClusterSetup setupTool = new ClusterSetup(zkClient);
            setupTool.addCluster(jsonParameters.getParameter(JsonParameters.CLUSTER_NAME), false);
        } else {
            throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.addCluster + "]");
        }
        getResponse().setEntity(getClustersRepresentation());
        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) ClusterSetup(org.apache.helix.tools.ClusterSetup) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException)

Example 13 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ClustersResource method getClustersRepresentation.

StringRepresentation getClustersRepresentation() throws JsonGenerationException, JsonMappingException, IOException {
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    List<String> clusters = setupTool.getClusterManagementTool().getClusters();
    ZNRecord clustersRecord = new ZNRecord("Clusters Summary");
    clustersRecord.setListField("clusters", clusters);
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clustersRecord), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) ClusterSetup(org.apache.helix.tools.ClusterSetup) ZNRecord(org.apache.helix.ZNRecord)

Example 14 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ConfigResource method setConfigs.

/**
 * set or remove configs depends on "command" field of jsonParameters in POST body
 * @param entity
 * @param scopeStr
 * @throws Exception
 */
void setConfigs(Representation entity, ConfigScopeProperty type, String scopeArgs) throws Exception {
    JsonParameters jsonParameters = new JsonParameters(entity);
    String command = jsonParameters.getCommand();
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    if (command.equalsIgnoreCase(ClusterSetup.setConfig)) {
        jsonParameters.verifyCommand(ClusterSetup.setConfig);
        String propertiesStr = jsonParameters.getParameter(JsonParameters.CONFIGS);
        setupTool.setConfig(type, scopeArgs, propertiesStr);
    } else if (command.equalsIgnoreCase(ClusterSetup.removeConfig)) {
        jsonParameters.verifyCommand(ClusterSetup.removeConfig);
        String propertiesStr = jsonParameters.getParameter(JsonParameters.CONFIGS);
        setupTool.removeConfig(type, scopeArgs, propertiesStr);
    } else {
        throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.setConfig + ", " + ClusterSetup.removeConfig + "]");
    }
    getResponse().setEntity(get());
    getResponse().setStatus(Status.SUCCESS_OK);
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) ClusterSetup(org.apache.helix.tools.ClusterSetup)

Example 15 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ConfigResource method getConfigs.

StringRepresentation getConfigs(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);
    Map<String, String> configs = admin.getConfig(scope, configKeys);
    record.setSimpleFields(configs);
    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)

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