Search in sources :

Example 71 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup 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)

Example 72 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup 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 73 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup 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 74 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup 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 75 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup in project helix by apache.

the class ResourceGroupResource method delete.

@Override
public Representation delete() {
    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);
        setupTool.dropResourceFromCluster(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("", 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)

Aggregations

ClusterSetup (org.apache.helix.tools.ClusterSetup)79 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)33 BeforeClass (org.testng.annotations.BeforeClass)33 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)28 ZkClient (org.apache.helix.manager.zk.ZkClient)26 Date (java.util.Date)23 IOException (java.io.IOException)14 HelixException (org.apache.helix.HelixException)14 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)11 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)11 Test (org.testng.annotations.Test)11 ConfigAccessor (org.apache.helix.ConfigAccessor)9 ZNRecord (org.apache.helix.ZNRecord)9 HelixDataAccessor (org.apache.helix.HelixDataAccessor)8 IdealState (org.apache.helix.model.IdealState)8 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)7 HelixAdmin (org.apache.helix.HelixAdmin)6 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)6 StringRepresentation (org.restlet.representation.StringRepresentation)6 ClusterDistributedController (org.apache.helix.integration.manager.ClusterDistributedController)5