Search in sources :

Example 91 with ZkClient

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

the class InstanceResource method post.

@Override
public Representation post(Representation entity) {
    try {
        String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
        String instanceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_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.enableInstance)) {
            jsonParameters.verifyCommand(ClusterSetup.enableInstance);
            boolean enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
            setupTool.getClusterManagementTool().enableInstance(clusterName, instanceName, enabled);
        } else if (command.equalsIgnoreCase(ClusterSetup.enablePartition)) {
            jsonParameters.verifyCommand(ClusterSetup.enablePartition);
            boolean enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
            String[] partitions = jsonParameters.getParameter(JsonParameters.PARTITION).split(";");
            String resource = jsonParameters.getParameter(JsonParameters.RESOURCE);
            setupTool.getClusterManagementTool().enablePartition(enabled, clusterName, instanceName, resource, Arrays.asList(partitions));
        } else if (command.equalsIgnoreCase(ClusterSetup.resetPartition)) {
            jsonParameters.verifyCommand(ClusterSetup.resetPartition);
            String resource = jsonParameters.getParameter(JsonParameters.RESOURCE);
            String[] partitionNames = jsonParameters.getParameter(JsonParameters.PARTITION).split("\\s+");
            setupTool.getClusterManagementTool().resetPartition(clusterName, instanceName, resource, Arrays.asList(partitionNames));
        } else if (command.equalsIgnoreCase(ClusterSetup.resetInstance)) {
            jsonParameters.verifyCommand(ClusterSetup.resetInstance);
            setupTool.getClusterManagementTool().resetInstance(clusterName, Arrays.asList(instanceName));
        } else if (command.equalsIgnoreCase(ClusterSetup.addInstanceTag)) {
            jsonParameters.verifyCommand(ClusterSetup.addInstanceTag);
            String tag = jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
            setupTool.getClusterManagementTool().addInstanceTag(clusterName, instanceName, tag);
        } else if (command.equalsIgnoreCase(ClusterSetup.removeInstanceTag)) {
            jsonParameters.verifyCommand(ClusterSetup.removeInstanceTag);
            String tag = jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
            setupTool.getClusterManagementTool().removeInstanceTag(clusterName, instanceName, tag);
        } else {
            throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.enableInstance + ", " + ClusterSetup.enablePartition + ", " + ClusterSetup.resetInstance + "]");
        }
        getResponse().setEntity(getInstanceRepresentation());
        getResponse().setStatus(Status.SUCCESS_OK);
    } catch (Exception e) {
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
        LOG.error("Exception in post instance", 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 92 with ZkClient

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

the class InstanceResource method delete.

@Override
public Representation delete() {
    try {
        String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
        String instanceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_NAME);
        ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
        ClusterSetup setupTool = new ClusterSetup(zkclient);
        setupTool.dropInstanceFromCluster(clusterName, instanceName);
        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 delete instance", e);
    }
    return null;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) 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 93 with ZkClient

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

the class InstanceResource method getInstanceRepresentation.

StringRepresentation getInstanceRepresentation() throws JsonGenerationException, JsonMappingException, IOException {
    String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
    String instanceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_NAME);
    Builder keyBuilder = new PropertyKey.Builder(clusterName);
    ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.RAW_ZKCLIENT);
    String instanceCfgStr = ResourceUtil.readZkAsBytes(zkclient, keyBuilder.instanceConfig(instanceName));
    StringRepresentation representation = new StringRepresentation(instanceCfgStr, 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 94 with ZkClient

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

the class InstancesResource method post.

@Override
public Representation post(Representation entity) {
    try {
        String clusterName = (String) getRequest().getAttributes().get("clusterName");
        JsonParameters jsonParameters = new JsonParameters(entity);
        String command = jsonParameters.getCommand();
        ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
        ClusterSetup setupTool = new ClusterSetup(zkClient);
        if (command.equalsIgnoreCase(ClusterSetup.addInstance) || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.addInstance).contains(command)) {
            if (jsonParameters.getParameter(JsonParameters.INSTANCE_NAME) != null) {
                setupTool.addInstanceToCluster(clusterName, jsonParameters.getParameter(JsonParameters.INSTANCE_NAME));
            } else if (jsonParameters.getParameter(JsonParameters.INSTANCE_NAMES) != null) {
                setupTool.addInstancesToCluster(clusterName, jsonParameters.getParameter(JsonParameters.INSTANCE_NAMES).split(";"));
            } else {
                throw new HelixException("Missing Json paramaters: '" + JsonParameters.INSTANCE_NAME + "' or '" + JsonParameters.INSTANCE_NAMES + "' ");
            }
        } else if (command.equalsIgnoreCase(ClusterSetup.swapInstance)) {
            if (jsonParameters.getParameter(JsonParameters.NEW_INSTANCE) == null || jsonParameters.getParameter(JsonParameters.OLD_INSTANCE) == null) {
                throw new HelixException("Missing Json paramaters: '" + JsonParameters.NEW_INSTANCE + "' or '" + JsonParameters.OLD_INSTANCE + "' ");
            }
            setupTool.swapInstance(clusterName, jsonParameters.getParameter(JsonParameters.OLD_INSTANCE), jsonParameters.getParameter(JsonParameters.NEW_INSTANCE));
        } else {
            throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.addInstance + ", " + ClusterSetup.swapInstance + "]");
        }
        getResponse().setEntity(getInstancesRepresentation(clusterName));
        getResponse().setStatus(Status.SUCCESS_OK);
    } catch (Exception e) {
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
        LOG.error("", 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 95 with ZkClient

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

the class JobQueueResource method getHostedEntitiesRepresentation.

StringRepresentation getHostedEntitiesRepresentation(String clusterName, String jobQueueName) throws Exception {
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    TaskDriver taskDriver = new TaskDriver(zkClient, clusterName);
    // Get job queue config
    // TODO: fix this to use workflowConfig.
    ResourceConfig jobQueueConfig = accessor.getProperty(keyBuilder.resourceConfig(jobQueueName));
    // Get job queue context
    WorkflowContext ctx = taskDriver.getWorkflowContext(jobQueueName);
    // Create the result
    ZNRecord hostedEntitiesRecord = new ZNRecord(jobQueueName);
    if (jobQueueConfig != null) {
        hostedEntitiesRecord.merge(jobQueueConfig.getRecord());
    }
    if (ctx != null) {
        hostedEntitiesRecord.merge(ctx.getRecord());
    }
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) StringRepresentation(org.restlet.representation.StringRepresentation) TaskDriver(org.apache.helix.task.TaskDriver) WorkflowContext(org.apache.helix.task.WorkflowContext) ResourceConfig(org.apache.helix.model.ResourceConfig) PropertyKey(org.apache.helix.PropertyKey) 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