vrajat commented on code in PR #14110: URL: https://github.com/apache/pinot/pull/14110#discussion_r1827409028
########## pinot-broker/src/main/java/org/apache/pinot/broker/api/resources/ResponseStoreResource.java: ########## @@ -0,0 +1,190 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.broker.api.resources; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiKeyAuthDefinition; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Authorization; +import io.swagger.annotations.SecurityDefinition; +import io.swagger.annotations.SwaggerDefinition; +import java.util.Collection; +import javax.inject.Inject; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.container.AsyncResponse; +import javax.ws.rs.container.Suspended; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.apache.pinot.common.cursors.AbstractResponseStore; +import org.apache.pinot.common.metrics.BrokerMeter; +import org.apache.pinot.common.metrics.BrokerMetrics; +import org.apache.pinot.common.response.BrokerResponse; +import org.apache.pinot.common.response.CursorResponse; +import org.apache.pinot.core.auth.Actions; +import org.apache.pinot.core.auth.Authorize; +import org.apache.pinot.core.auth.ManualAuthorization; +import org.apache.pinot.core.auth.TargetType; +import org.apache.pinot.spi.env.PinotConfiguration; +import org.apache.pinot.spi.utils.CommonConstants; +import org.glassfish.jersey.server.ManagedAsync; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.pinot.spi.utils.CommonConstants.SWAGGER_AUTHORIZATION_KEY; + + +/** + * + */ +@Api(tags = "ResponseStore", authorizations = {@Authorization(value = SWAGGER_AUTHORIZATION_KEY)}) +@SwaggerDefinition(securityDefinition = @SecurityDefinition(apiKeyAuthDefinitions = @ApiKeyAuthDefinition(name = + HttpHeaders.AUTHORIZATION, in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, key = SWAGGER_AUTHORIZATION_KEY, + description = "The format of the key is ```\"Basic <token>\" or \"Bearer <token>\"```"))) +@Path("/responseStore") +public class ResponseStoreResource { + private static final Logger LOGGER = LoggerFactory.getLogger(ResponseStoreResource.class); + + @Inject + private PinotConfiguration _brokerConf; + + @Inject + private BrokerMetrics _brokerMetrics; + + @Inject + private AbstractResponseStore _responseStore; + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/") + @Authorize(targetType = TargetType.CLUSTER, action = Actions.Cluster.GET_RESULT_STORE) + @ApiOperation(value = "Get requestIds of all responses in the result store.", notes = "Get requestIds of all " + + "query stores in the result store") + public Collection<CursorResponse> getResults(@Context HttpHeaders headers) { + try { + return _responseStore.getAllStoredResponses(); + } catch (Exception e) { + throw new WebApplicationException(e, + Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build()); + } + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("{requestId}") + @ApiOperation(value = "Response without ResultTable for a requestId") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Query response"), @ApiResponse(code = 500, message = "Internal Server Error") + }) + @ManualAuthorization + public BrokerResponse getSqlQueryMetadata( + @ApiParam(value = "Request ID of the query", required = true) @PathParam("requestId") String requestId) { + try { + if (_responseStore.exists(requestId)) { + return _responseStore.readResponse(requestId); + } else { + throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND) + .entity(String.format("Query results for %s not found.", requestId)).build()); + } + } catch (WebApplicationException wae) { + throw wae; + } catch (Exception e) { + LOGGER.error("Caught exception while processing GET request", e); + _brokerMetrics.addMeteredGlobalValue(BrokerMeter.UNCAUGHT_POST_EXCEPTIONS, 1L); + throw new WebApplicationException(e, + Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build()); + } + } + + @GET + @ManagedAsync + @Produces(MediaType.APPLICATION_JSON) + @Path("{requestId}/results") + @ApiOperation(value = "Get result set from a result store for a query") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Query response"), @ApiResponse(code = 500, message = "Internal Server Error") + }) + @ManualAuthorization + public void getSqlQueryResult( Review Comment: I've merged support for authorization. My initial plan was to store the user who submitted the query. However that will require a lot of API changes in access control classes. Instead the response store contains the list of tables queried and that is used to authorize. So two users who have access to the same tables can read each other's cursors. I have not added tests yet as I havent found the infra to test these cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
