-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Accept query id and slug in QueuedStatement #23407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
import javax.ws.rs.GET; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
|
@@ -99,6 +100,7 @@ | |
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE; | ||
import static javax.ws.rs.core.Response.Status.BAD_REQUEST; | ||
import static javax.ws.rs.core.Response.Status.CONFLICT; | ||
import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
|
||
@Path("/") | ||
|
@@ -222,6 +224,58 @@ public Response postStatement( | |
return withCompressionConfiguration(Response.ok(query.getInitialQueryResults(uriInfo, xForwardedProto, xPrestoPrefixUrl, binaryResults)), compressionEnabled).build(); | ||
} | ||
|
||
/** | ||
* HTTP endpoint for submitting queries to the Presto Coordinator. | ||
* Presto performs lazy execution. The submission of a query returns | ||
* a placeholder for the result set, but the query gets | ||
* scheduled/dispatched only when the client polls for results. | ||
* This endpoint accepts a pre-minted queryId and slug, instead of | ||
* generating it. | ||
* | ||
* @param statement The statement or sql query string submitted | ||
* @param queryId Pre-minted query ID to associate with this query | ||
* @param slug Pre-minted slug to protect this query | ||
* @param xForwardedProto Forwarded protocol (http or https) | ||
* @param servletRequest The http request | ||
* @param uriInfo {@link javax.ws.rs.core.UriInfo} | ||
* @return {@link javax.ws.rs.core.Response} HTTP response code | ||
*/ | ||
@PUT | ||
@Path("/v1/statement/{queryId}") | ||
@Produces(APPLICATION_JSON) | ||
public Response putStatement( | ||
String statement, | ||
@PathParam("queryId") QueryId queryId, | ||
@QueryParam("slug") String slug, | ||
@DefaultValue("false") @QueryParam("binaryResults") boolean binaryResults, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added for fuzzer testing, and no client presently uses this. I'd remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that no client is using it, and I kept it here since the POST supports it. If we want to remove support for this, we should probably raise an issue first and remove it as part of a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I asked around and it looks like we are actually using this flag for our client, so we should keep it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you share more details on the client that's using it? Is it an internal client? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amitkdutta do you have any details on this client? |
||
@HeaderParam(X_FORWARDED_PROTO) String xForwardedProto, | ||
@HeaderParam(PRESTO_PREFIX_URL) String xPrestoPrefixUrl, | ||
@Context HttpServletRequest servletRequest, | ||
@Context UriInfo uriInfo) | ||
{ | ||
if (isNullOrEmpty(statement)) { | ||
throw badRequest(BAD_REQUEST, "SQL statement is empty"); | ||
} | ||
|
||
abortIfPrefixUrlInvalid(xPrestoPrefixUrl); | ||
|
||
// TODO: For future cases we may want to start tracing from client. Then continuation of tracing | ||
// will be needed instead of creating a new trace here. | ||
SessionContext sessionContext = new HttpRequestSessionContext( | ||
servletRequest, | ||
sqlParserOptions, | ||
tracerProviderManager.getTracerProvider(), | ||
Optional.of(sessionPropertyManager)); | ||
Query attemptedQuery = new Query(statement, sessionContext, dispatchManager, executingQueryResponseProvider, 0, queryId, slug); | ||
Query query = queries.computeIfAbsent(queryId, unused -> attemptedQuery); | ||
|
||
if (attemptedQuery != query && !attemptedQuery.getSlug().equals(query.getSlug()) || query.getLastToken() != 0) { | ||
throw badRequest(CONFLICT, "Query already exists"); | ||
} | ||
|
||
return withCompressionConfiguration(Response.ok(query.getInitialQueryResults(uriInfo, xForwardedProto, xPrestoPrefixUrl, binaryResults)), compressionEnabled).build(); | ||
} | ||
|
||
/** | ||
* HTTP endpoint for re-processing a failed query | ||
* @param queryId Query Identifier of the query to be retried | ||
|
@@ -386,21 +440,34 @@ private static final class Query | |
private final DispatchManager dispatchManager; | ||
private final ExecutingQueryResponseProvider executingQueryResponseProvider; | ||
private final QueryId queryId; | ||
private final String slug = "x" + randomUUID().toString().toLowerCase(ENGLISH).replace("-", ""); | ||
private final String slug; | ||
private final AtomicLong lastToken = new AtomicLong(); | ||
private final int retryCount; | ||
|
||
@GuardedBy("this") | ||
private ListenableFuture<?> querySubmissionFuture; | ||
|
||
public Query(String query, SessionContext sessionContext, DispatchManager dispatchManager, ExecutingQueryResponseProvider executingQueryResponseProvider, int retryCount) | ||
{ | ||
this(query, sessionContext, dispatchManager, executingQueryResponseProvider, retryCount, dispatchManager.createQueryId(), createSlug()); | ||
} | ||
|
||
public Query( | ||
String query, | ||
SessionContext sessionContext, | ||
DispatchManager dispatchManager, | ||
ExecutingQueryResponseProvider executingQueryResponseProvider, | ||
int retryCount, | ||
QueryId queryId, | ||
String slug) | ||
{ | ||
this.query = requireNonNull(query, "query is null"); | ||
this.sessionContext = requireNonNull(sessionContext, "sessionContext is null"); | ||
this.dispatchManager = requireNonNull(dispatchManager, "dispatchManager is null"); | ||
this.executingQueryResponseProvider = requireNonNull(executingQueryResponseProvider, "executingQueryResponseProvider is null"); | ||
this.queryId = dispatchManager.createQueryId(); | ||
this.retryCount = retryCount; | ||
this.queryId = requireNonNull(queryId, "queryId is null"); | ||
this.slug = requireNonNull(slug, "slug is null"); | ||
} | ||
|
||
/** | ||
|
@@ -584,6 +651,11 @@ private QueryResults createQueryResults(long token, UriInfo uriInfo, String xFor | |
dispatchInfo.getWaitingForPrerequisitesTime()); | ||
} | ||
|
||
private static String createSlug() | ||
{ | ||
return "x" + randomUUID().toString().toLowerCase(ENGLISH).replace("-", ""); | ||
} | ||
|
||
private URI getNextUri(long token, UriInfo uriInfo, String xForwardedProto, String xPrestoPrefixUrl, DispatchInfo dispatchInfo, boolean binaryResults) | ||
{ | ||
// if failed, query is complete | ||
|
Uh oh!
There was an error while loading. Please reload this page.