Search in sources :

Example 16 with HelixAdmin

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

Example 17 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class ConstraintResource method get.

/**
 * List all constraints
 * <p>
 * Usage: <code>curl http://{host:port}/clusters/{clusterName}/constraints/MESSAGE_CONSTRAINT
 */
@Override
public Representation get() {
    StringRepresentation representation = null;
    String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
    String constraintTypeStr = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CONSTRAINT_TYPE);
    String constraintId = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CONSTRAINT_ID);
    try {
        ConstraintType constraintType = ConstraintType.valueOf(constraintTypeStr);
        ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
        HelixAdmin admin = new ZKHelixAdmin(zkClient);
        ZNRecord record = admin.getConstraints(clusterName, constraintType).getRecord();
        if (constraintId == null) {
            // get all message constraints
            representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
        } else {
            // get a specific constraint
            Map<String, String> constraint = record.getMapField(constraintId);
            if (constraint == null) {
                representation = new StringRepresentation("No constraint of type: " + constraintType + " associated with id: " + constraintId, MediaType.APPLICATION_JSON);
            } else {
                ZNRecord subRecord = new ZNRecord(record.getId());
                subRecord.setMapField(constraintId, constraint);
                representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(subRecord), MediaType.APPLICATION_JSON);
            }
        }
    } catch (IllegalArgumentException e) {
        representation = new StringRepresentation("constraint-type: " + constraintTypeStr + " not recognized.", MediaType.APPLICATION_JSON);
    } catch (Exception e) {
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        representation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
        LOG.error("Exception get constraints", e);
    }
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StringRepresentation(org.restlet.representation.StringRepresentation) ConstraintType(org.apache.helix.model.ClusterConstraints.ConstraintType) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZNRecord(org.apache.helix.ZNRecord)

Example 18 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class ClusterAccessor method getClusters.

@GET
public Response getClusters() {
    HelixAdmin helixAdmin = getHelixAdmin();
    List<String> clusters = helixAdmin.getClusters();
    Map<String, List<String>> dataMap = new HashMap<>();
    dataMap.put(ClusterProperties.clusters.name(), clusters);
    return JSONRepresentation(dataMap);
}
Also used : HashMap(java.util.HashMap) List(java.util.List) HelixAdmin(org.apache.helix.HelixAdmin) GET(javax.ws.rs.GET)

Example 19 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class InstanceAccessor method updateInstances.

@POST
public Response updateInstances(@PathParam("clusterId") String clusterId, @QueryParam("command") String command, String content) {
    Command cmd;
    try {
        cmd = Command.valueOf(command);
    } catch (Exception e) {
        return badRequest("Invalid command : " + command);
    }
    HelixAdmin admin = getHelixAdmin();
    try {
        JsonNode node = null;
        if (content.length() != 0) {
            node = OBJECT_MAPPER.readTree(content);
        }
        if (node == null) {
            return badRequest("Invalid input for content : " + content);
        }
        List<String> enableInstances = OBJECT_MAPPER.readValue(node.get(InstanceProperties.instances.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class));
        switch(cmd) {
            case enable:
                admin.enableInstance(clusterId, enableInstances, true);
                break;
            case disable:
                admin.enableInstance(clusterId, enableInstances, false);
                break;
            default:
                _logger.error("Unsupported command :" + command);
                return badRequest("Unsupported command :" + command);
        }
    } catch (Exception e) {
        _logger.error("Failed in updating instances : " + content, e);
        return badRequest(e.getMessage());
    }
    return OK();
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) List(java.util.List) HelixAdmin(org.apache.helix.HelixAdmin) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) POST(javax.ws.rs.POST)

Example 20 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class InstanceAccessor method updateInstance.

@POST
@Path("{instanceName}")
public Response updateInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName, @QueryParam("command") String command, String content) {
    Command cmd;
    try {
        cmd = Command.valueOf(command);
    } catch (Exception e) {
        return badRequest("Invalid command : " + command);
    }
    HelixAdmin admin = getHelixAdmin();
    try {
        JsonNode node = null;
        if (content.length() != 0) {
            node = OBJECT_MAPPER.readTree(content);
        }
        switch(cmd) {
            case enable:
                admin.enableInstance(clusterId, instanceName, true);
                break;
            case disable:
                admin.enableInstance(clusterId, instanceName, false);
                break;
            case reset:
                if (!validInstance(node, instanceName)) {
                    return badRequest("Instance names are not match!");
                }
                admin.resetPartition(clusterId, instanceName, node.get(InstanceProperties.resource.name()).toString(), (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.partitions.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class)));
                break;
            case addInstanceTag:
                if (!validInstance(node, instanceName)) {
                    return badRequest("Instance names are not match!");
                }
                for (String tag : (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.instanceTags.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class))) {
                    admin.addInstanceTag(clusterId, instanceName, tag);
                }
                break;
            case removeInstanceTag:
                if (!validInstance(node, instanceName)) {
                    return badRequest("Instance names are not match!");
                }
                for (String tag : (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.instanceTags.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class))) {
                    admin.removeInstanceTag(clusterId, instanceName, tag);
                }
                break;
            case enablePartitions:
                admin.enablePartition(true, clusterId, instanceName, node.get(InstanceProperties.resource.name()).getTextValue(), (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.partitions.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class)));
                break;
            case disablePartitions:
                admin.enablePartition(false, clusterId, instanceName, node.get(InstanceProperties.resource.name()).getTextValue(), (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.partitions.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class)));
                break;
            default:
                _logger.error("Unsupported command :" + command);
                return badRequest("Unsupported command :" + command);
        }
    } catch (Exception e) {
        _logger.error("Failed in updating instance : " + instanceName, e);
        return badRequest(e.getMessage());
    }
    return OK();
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) List(java.util.List) HelixAdmin(org.apache.helix.HelixAdmin) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

HelixAdmin (org.apache.helix.HelixAdmin)83 Test (org.testng.annotations.Test)40 IdealState (org.apache.helix.model.IdealState)36 ZNRecord (org.apache.helix.ZNRecord)28 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)23 Date (java.util.Date)22 HashMap (java.util.HashMap)19 ExternalView (org.apache.helix.model.ExternalView)16 InstanceConfig (org.apache.helix.model.InstanceConfig)15 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)13 ArrayList (java.util.ArrayList)12 HelixDataAccessor (org.apache.helix.HelixDataAccessor)12 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)12 PropertyKey (org.apache.helix.PropertyKey)11 StateModelDefinition (org.apache.helix.model.StateModelDefinition)11 HelixException (org.apache.helix.HelixException)10 ControllerMetrics (com.linkedin.pinot.common.metrics.ControllerMetrics)9 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)9 Path (javax.ws.rs.Path)9 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)9