Search in sources :

Example 1 with StringRepresentation

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

the class PqlQueryResource method get.

@Override
public Representation get() {
    try {
        Form query = getQuery();
        LOGGER.debug("Running query: " + query);
        String pqlQuery = query.getValues("pql");
        String traceEnabled = query.getValues("trace");
        // Get resource table name.
        String tableName;
        try {
            tableName = REQUEST_COMPILER.compileToBrokerRequest(pqlQuery).getQuerySource().getTableName();
        } catch (Exception e) {
            LOGGER.error("Caught exception while compiling PQL query: " + pqlQuery, e);
            return new StringRepresentation(QueryException.getException(QueryException.PQL_PARSING_ERROR, e).toString());
        }
        // Get brokers for the resource table.
        List<String> instanceIds = _pinotHelixResourceManager.getBrokerInstancesFor(tableName);
        if (instanceIds.isEmpty()) {
            return new StringRepresentation(QueryException.BROKER_RESOURCE_MISSING_ERROR.toString());
        }
        // Retain only online brokers.
        instanceIds.retainAll(_pinotHelixResourceManager.getOnlineInstanceList());
        if (instanceIds.isEmpty()) {
            return new StringRepresentation(QueryException.BROKER_INSTANCE_MISSING_ERROR.toString());
        }
        // Send query to a random broker.
        String instanceId = instanceIds.get(RANDOM.nextInt(instanceIds.size()));
        InstanceConfig instanceConfig = _pinotHelixResourceManager.getHelixInstanceConfig(instanceId);
        String url = "http://" + instanceConfig.getHostName().split("_")[1] + ":" + instanceConfig.getPort() + "/query";
        return new StringRepresentation(sendPQLRaw(url, pqlQuery, traceEnabled));
    } catch (Exception e) {
        LOGGER.error("Caught exception while processing get request", e);
        return new StringRepresentation(QueryException.getException(QueryException.INTERNAL_ERROR, e).toString());
    }
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) Form(org.restlet.data.Form) StringRepresentation(org.restlet.representation.StringRepresentation) IOException(java.io.IOException) QueryException(com.linkedin.pinot.common.exception.QueryException)

Example 2 with StringRepresentation

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

the class PinotSegmentRestletResource method getAllSegmentsMetadataForTable.

@HttpVerb("get")
@Summary("Lists segment metadata for a given table")
@Tags({ "segment", "table" })
@Paths({ "/tables/{tableName}/segments/metadata", "/tables/{tableName}/segments/metadata/" })
@Responses({ @Response(statusCode = "200", description = "A list of segment metadata"), @Response(statusCode = "404", description = "The table does not exist") })
private Representation getAllSegmentsMetadataForTable(@Parameter(name = "tableName", in = "path", description = "The name of the table for which to list segment metadata", required = true) String tableName, @Parameter(name = "type", in = "query", description = "Type of table {offline|realtime}", required = false) String tableType) throws JSONException, JsonProcessingException {
    boolean foundRealtimeTable = false;
    boolean foundOfflineTable = false;
    JSONArray ret = new JSONArray();
    if ((tableType == null || TableType.REALTIME.name().equalsIgnoreCase(tableType)) && _pinotHelixResourceManager.hasRealtimeTable(tableName)) {
        String realtimeTableName = TableNameBuilder.REALTIME_TABLE_NAME_BUILDER.forTable(tableName);
        JSONObject realtime = new JSONObject();
        realtime.put(TABLE_NAME, realtimeTableName);
        realtime.put("segments", new ObjectMapper().writeValueAsString(_pinotHelixResourceManager.getInstanceToSegmentsInATableMap(realtimeTableName)));
        ret.put(realtime);
        foundRealtimeTable = true;
    }
    if ((tableType == null || TableType.OFFLINE.name().equalsIgnoreCase(tableType)) && _pinotHelixResourceManager.hasOfflineTable(tableName)) {
        String offlineTableName = TableNameBuilder.OFFLINE_TABLE_NAME_BUILDER.forTable(tableName);
        JSONObject offline = new JSONObject();
        offline.put(TABLE_NAME, offlineTableName);
        offline.put("segments", new ObjectMapper().writeValueAsString(_pinotHelixResourceManager.getInstanceToSegmentsInATableMap(offlineTableName)));
        ret.put(offline);
        foundOfflineTable = true;
    }
    if (foundOfflineTable || foundRealtimeTable) {
        return new StringRepresentation(ret.toString());
    } else {
        setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        return new StringRepresentation("Table " + tableName + " not found.");
    }
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) JSONArray(org.json.JSONArray) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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) Responses(com.linkedin.pinot.common.restlet.swagger.Responses)

Example 3 with StringRepresentation

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

the class PinotSegmentRestletResource method getSegmentMetaData.

/**
   * Get meta-data for segment of table. Table name is the suffixed (offline/realtime)
   * name.
   * @param tableName: Suffixed (realtime/offline) table Name
   * @param segmentName: Segment for which to get the meta-data.
   * @return
   * @throws JSONException
   */
private StringRepresentation getSegmentMetaData(String tableName, String segmentName, TableType tableType) throws JSONException {
    if (!ZKMetadataProvider.isSegmentExisted(_pinotHelixResourceManager.getPropertyStore(), tableName, segmentName)) {
        String error = new String("Error: segment " + segmentName + " not found.");
        LOGGER.error(error);
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        return new StringRepresentation(error);
    }
    JSONArray ret = new JSONArray();
    JSONObject jsonObj = new JSONObject();
    jsonObj.put(TABLE_NAME, tableName);
    ZkHelixPropertyStore<ZNRecord> propertyStore = _pinotHelixResourceManager.getPropertyStore();
    if (tableType == tableType.OFFLINE) {
        OfflineSegmentZKMetadata offlineSegmentZKMetadata = ZKMetadataProvider.getOfflineSegmentZKMetadata(propertyStore, tableName, segmentName);
        jsonObj.put(STATE, offlineSegmentZKMetadata.toMap());
    }
    if (tableType == TableType.REALTIME) {
        RealtimeSegmentZKMetadata realtimeSegmentZKMetadata = ZKMetadataProvider.getRealtimeSegmentZKMetadata(propertyStore, tableName, segmentName);
        jsonObj.put(STATE, realtimeSegmentZKMetadata.toMap());
    }
    ret.put(jsonObj);
    return new StringRepresentation(ret.toString());
}
Also used : RealtimeSegmentZKMetadata(com.linkedin.pinot.common.metadata.segment.RealtimeSegmentZKMetadata) JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) OfflineSegmentZKMetadata(com.linkedin.pinot.common.metadata.segment.OfflineSegmentZKMetadata) JSONArray(org.json.JSONArray) ZNRecord(org.apache.helix.ZNRecord)

Example 4 with StringRepresentation

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

the class PinotSegmentRestletResource method getSegmentMetadataForTable.

@HttpVerb("get")
@Summary("Gets segment metadata for a given segment")
@Tags({ "segment", "table" })
@Paths({ "/tables/{tableName}/segments/{segmentName}/metadata", "/tables/{tableName}/segments/{segmentName}/metadata/" })
private Representation getSegmentMetadataForTable(@Parameter(name = "tableName", in = "path", description = "The name of the table for which to list segment metadata", required = true) String tableName, @Parameter(name = "segmentName", in = "path", description = "The name of the segment for which to fetch metadata", required = true) String segmentName, @Parameter(name = "type", in = "query", description = "Type of table {offline|realtime}", required = false) String tableType) throws JsonProcessingException, JSONException {
    JSONArray ret = new JSONArray();
    if ((tableType == null || TableType.OFFLINE.name().equalsIgnoreCase(tableType)) && _pinotHelixResourceManager.hasOfflineTable(tableName)) {
        String offlineTableName = TableNameBuilder.OFFLINE_TABLE_NAME_BUILDER.forTable(tableName);
        ret.put(getSegmentMetaData(offlineTableName, segmentName, TableType.OFFLINE));
    }
    if ((tableType == null || TableType.REALTIME.name().equalsIgnoreCase(tableType)) && _pinotHelixResourceManager.hasRealtimeTable(tableName)) {
        String realtimeTableName = TableNameBuilder.REALTIME_TABLE_NAME_BUILDER.forTable(tableName);
        ret.put(getSegmentMetaData(realtimeTableName, segmentName, TableType.REALTIME));
    }
    return new StringRepresentation(ret.toString());
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) JSONArray(org.json.JSONArray) 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)

Example 5 with StringRepresentation

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

the class PinotSegmentRestletResource method toggleSegmentState.

/**
   * Handler to toggle state of segment for a given table.
   *
   * @param tableName: External name for the table
   * @param segmentName: Segment to set the state for
   * @param state: Value of state to set
   * @param tableType: Offline or realtime
   * @return
   * @throws JsonProcessingException
   * @throws JSONException
   */
protected Representation toggleSegmentState(String tableName, String segmentName, String state, String tableType) throws JsonProcessingException, JSONException {
    JSONArray ret = new JSONArray();
    List<String> segmentsToToggle = new ArrayList<>();
    String offlineTableName = TableNameBuilder.OFFLINE_TABLE_NAME_BUILDER.forTable(tableName);
    String realtimeTableName = TableNameBuilder.REALTIME_TABLE_NAME_BUILDER.forTable(tableName);
    String tableNameWithType = "";
    List<String> realtimeSegments = _pinotHelixResourceManager.getAllSegmentsForResource(realtimeTableName);
    List<String> offlineSegments = _pinotHelixResourceManager.getAllSegmentsForResource(offlineTableName);
    if (tableType == null) {
        PinotResourceManagerResponse responseRealtime = toggleSegmentsForTable(realtimeSegments, realtimeTableName, segmentName, state);
        PinotResourceManagerResponse responseOffline = toggleSegmentsForTable(offlineSegments, offlineTableName, segmentName, state);
        setStatus(responseRealtime.isSuccessful() && responseOffline.isSuccessful() ? Status.SUCCESS_OK : Status.SERVER_ERROR_INTERNAL);
        List<PinotResourceManagerResponse> responses = new ArrayList<>();
        responses.add(responseRealtime);
        responses.add(responseOffline);
        ret.put(responses);
        return new StringRepresentation(ret.toString());
    } else if (TableType.REALTIME.name().equalsIgnoreCase(tableType)) {
        if (_pinotHelixResourceManager.hasRealtimeTable(tableName)) {
            tableNameWithType = realtimeTableName;
            if (segmentName != null) {
                segmentsToToggle = Collections.singletonList(segmentName);
            } else {
                segmentsToToggle.addAll(realtimeSegments);
            }
        } else {
            throw new UnsupportedOperationException("There is no realtime table for " + tableName);
        }
    } else {
        if (_pinotHelixResourceManager.hasOfflineTable(tableName)) {
            tableNameWithType = offlineTableName;
            if (segmentName != null) {
                segmentsToToggle = Collections.singletonList(segmentName);
            } else {
                segmentsToToggle.addAll(offlineSegments);
            }
        } else {
            throw new UnsupportedOperationException("There is no offline table for " + tableName);
        }
    }
    PinotResourceManagerResponse resourceManagerResponse = toggleSegmentsForTable(segmentsToToggle, tableNameWithType, segmentName, state);
    setStatus(resourceManagerResponse.isSuccessful() ? Status.SUCCESS_OK : Status.SERVER_ERROR_INTERNAL);
    ret.put(resourceManagerResponse);
    return new StringRepresentation(ret.toString());
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) PinotResourceManagerResponse(com.linkedin.pinot.controller.helix.core.PinotResourceManagerResponse) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList)

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