Search in sources :

Example 16 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTableRestletResource method get.

/**
   * URI Mappings:
   * - "/tables", "/tables/": List all the tables
   * - "/tables/{tableName}", "/tables/{tableName}/": List config for specified table.
   *
   * - "/tables/{tableName}?state={state}"
   *   Set the state for the specified {tableName} to the specified {state} (enable|disable|drop).
   *
   * - "/tables/{tableName}?type={type}"
   *   List all tables of specified type, type can be one of {offline|realtime}.
   *
   *   Set the state for the specified {tableName} to the specified {state} (enable|disable|drop).
   *   * - "/tables/{tableName}?state={state}&type={type}"
   *
   *   Set the state for the specified {tableName} of specified type to the specified {state} (enable|disable|drop).
   *   Type here is type of the table, one of 'offline|realtime'.
   * {@inheritDoc}
   * @see org.restlet.resource.ServerResource#get()
   */
@Override
@Get
public Representation get() {
    final String tableName = (String) getRequest().getAttributes().get(TABLE_NAME);
    final String state = getReference().getQueryAsForm().getValues(STATE);
    final String tableType = getReference().getQueryAsForm().getValues(TABLE_TYPE);
    if (tableType != null && !isValidTableType(tableType)) {
        LOGGER.error(INVALID_TABLE_TYPE_ERROR);
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        return new StringRepresentation(INVALID_TABLE_TYPE_ERROR);
    }
    if (tableName == null) {
        try {
            return getAllTables();
        } catch (Exception e) {
            LOGGER.error("Caught exception while fetching table ", e);
            ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_GET_ERROR, 1L);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return PinotSegmentUploadRestletResource.exceptionToStringRepresentation(e);
        }
    }
    try {
        if (state == null) {
            return getTable(tableName, tableType);
        } else if (isValidState(state)) {
            return setTablestate(tableName, tableType, state);
        } else {
            LOGGER.error(INVALID_STATE_ERROR);
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new StringRepresentation(INVALID_STATE_ERROR);
        }
    } catch (Exception e) {
        LOGGER.error("Caught exception while fetching table ", e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_GET_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
        return PinotSegmentUploadRestletResource.exceptionToStringRepresentation(e);
    }
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JSONException(org.json.JSONException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Get(org.restlet.resource.Get)

Example 17 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTenantRestletResource method post.

/*
   * For tenant creation
   */
@Override
@Post("json")
public Representation post(Representation entity) {
    StringRepresentation presentation;
    try {
        final Tenant tenant = _objectMapper.readValue(entity.getText(), Tenant.class);
        presentation = createTenant(tenant);
    } catch (final Exception e) {
        presentation = exceptionToStringRepresentation(e);
        LOGGER.error("Caught exception while creating tenant ", e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_TENANT_CREATE_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
    }
    return presentation;
}
Also used : Tenant(com.linkedin.pinot.common.config.Tenant) StringRepresentation(org.restlet.representation.StringRepresentation) JSONException(org.json.JSONException) Post(org.restlet.resource.Post)

Example 18 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTenantRestletResource method get.

/**
   * URI Mappings:
   * "/tenants", "/tenants/": List all the tenants in the cluster.
   * "/tenants/{tenantName}", "/tenants/{tenantName}/": List all instances for the tenant.
   * "/tenants/{tenantName}?state={state}":
   * - Set the state for the specified tenant to the specified value, one of {enable|disable|drop}.
   *
   * {@inheritDoc}
   * @see org.restlet.resource.ServerResource#get()
   */
@Override
@Get
public Representation get() {
    StringRepresentation presentation = null;
    try {
        final String tenantName = (String) getRequest().getAttributes().get(TENANT_NAME);
        final String state = getReference().getQueryAsForm().getValues(STATE);
        ;
        final String type = getReference().getQueryAsForm().getValues(TABLE_TYPE);
        if (tenantName == null) {
            presentation = getAllTenants(type);
        } else if (state == null) {
            presentation = getTenant(tenantName, type);
        } else {
            if (isValidState(state)) {
                presentation = toggleTenantState(tenantName, state, type);
            } else {
                LOGGER.error(INVALID_STATE_ERROR);
                setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                return new StringRepresentation(INVALID_STATE_ERROR);
            }
        }
    } catch (final Exception e) {
        presentation = exceptionToStringRepresentation(e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_TENANT_GET_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
        LOGGER.error("Caught exception while fetching tenant ", e);
        setStatus(Status.SERVER_ERROR_INTERNAL);
    }
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) JSONException(org.json.JSONException) Get(org.restlet.resource.Get)

Example 19 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTenantRestletResource method put.

/*
   * For tenant update
   */
@Override
@Put("json")
public Representation put(Representation entity) {
    StringRepresentation presentation;
    try {
        final Tenant tenant = _objectMapper.readValue(ByteStreams.toByteArray(entity.getStream()), Tenant.class);
        presentation = updateTenant(tenant);
    } catch (final Exception e) {
        presentation = exceptionToStringRepresentation(e);
        LOGGER.error("Caught exception while updating tenant ", e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_TENANT_UPDATE_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
    }
    return presentation;
}
Also used : Tenant(com.linkedin.pinot.common.config.Tenant) StringRepresentation(org.restlet.representation.StringRepresentation) JSONException(org.json.JSONException) Put(org.restlet.resource.Put)

Example 20 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTenantRestletResource method toggleTenantState.

@HttpVerb("get")
@Summary("Enable, disable or drop a tenant")
@Tags({ "tenant" })
@Paths({ "/tenants/{tenantName}", "/tenants/{tenantName}/" })
private StringRepresentation toggleTenantState(@Parameter(name = "tenantName", in = "path", description = "The tenant name") String tenantName, @Parameter(name = "state", in = "query", description = "state to set for the tenant {enable|disable|drop}") String state, @Parameter(name = "type", in = "query", description = "The type of tenant, either SERVER or BROKER or NULL") String type) throws JSONException {
    Set<String> serverInstances = new HashSet<String>();
    Set<String> brokerInstances = new HashSet<String>();
    JSONObject instanceResult = new JSONObject();
    if ((type == null) || type.equalsIgnoreCase("server")) {
        serverInstances = _pinotHelixResourceManager.getAllInstancesForServerTenant(tenantName);
    }
    if ((type == null) || type.equalsIgnoreCase("broker")) {
        brokerInstances = _pinotHelixResourceManager.getAllInstancesForBrokerTenant(tenantName);
    }
    Set<String> allInstances = new HashSet<String>(serverInstances);
    allInstances.addAll(brokerInstances);
    if (StateType.DROP.name().equalsIgnoreCase(state)) {
        if (!allInstances.isEmpty()) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new StringRepresentation("Error: Tenant " + tenantName + " has live instances, cannot be dropped.");
        }
        _pinotHelixResourceManager.deleteBrokerTenantFor(tenantName);
        _pinotHelixResourceManager.deleteOfflineServerTenantFor(tenantName);
        _pinotHelixResourceManager.deleteRealtimeServerTenantFor(tenantName);
        return new StringRepresentation("Dropped tenant " + tenantName + " successfully.");
    }
    boolean enable = StateType.ENABLE.name().equalsIgnoreCase(state) ? true : false;
    for (String instance : allInstances) {
        if (enable) {
            instanceResult.put(instance, _pinotHelixResourceManager.enableInstance(instance));
        } else {
            instanceResult.put(instance, _pinotHelixResourceManager.disableInstance(instance));
        }
    }
    return new StringRepresentation(instanceResult.toString(), MediaType.APPLICATION_JSON);
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) HashSet(java.util.HashSet) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags)

Aggregations

StringRepresentation (org.restlet.representation.StringRepresentation)130 HttpVerb (com.linkedin.pinot.common.restlet.swagger.HttpVerb)30 Paths (com.linkedin.pinot.common.restlet.swagger.Paths)30 Summary (com.linkedin.pinot.common.restlet.swagger.Summary)30 IOException (java.io.IOException)30 Tags (com.linkedin.pinot.common.restlet.swagger.Tags)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 JSONObject (org.json.JSONObject)23 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)19 ZNRecord (org.apache.helix.ZNRecord)17 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)17 JSONException (org.json.JSONException)17 Representation (org.restlet.representation.Representation)15 Responses (com.linkedin.pinot.common.restlet.swagger.Responses)14 HelixException (org.apache.helix.HelixException)12 JSONArray (org.json.JSONArray)12 Builder (org.apache.helix.PropertyKey.Builder)11 Get (org.restlet.resource.Get)10 Post (org.restlet.resource.Post)9 ResourceException (org.restlet.resource.ResourceException)9