|
18 | 18 | import com.facebook.airlift.http.client.HttpUriBuilder;
|
19 | 19 | import com.facebook.airlift.http.client.Request;
|
20 | 20 | import com.facebook.airlift.http.client.StatusResponseHandler;
|
| 21 | +import com.facebook.airlift.http.client.UnexpectedResponseException; |
21 | 22 | import com.facebook.airlift.http.client.jetty.JettyHttpClient;
|
22 | 23 | import com.facebook.airlift.json.JsonCodec;
|
23 | 24 | import com.facebook.airlift.testing.Closeables;
|
|
26 | 27 | import com.facebook.presto.common.Page;
|
27 | 28 | import com.facebook.presto.common.block.BlockEncodingManager;
|
28 | 29 | import com.facebook.presto.common.type.TimeZoneNotSupportedException;
|
| 30 | +import com.facebook.presto.execution.QueryIdGenerator; |
29 | 31 | import com.facebook.presto.execution.buffer.PagesSerdeFactory;
|
30 | 32 | import com.facebook.presto.server.testing.TestingPrestoServer;
|
31 | 33 | import com.facebook.presto.spi.QueryId;
|
|
48 | 50 |
|
49 | 51 | import static com.facebook.airlift.http.client.FullJsonResponseHandler.createFullJsonResponseHandler;
|
50 | 52 | import static com.facebook.airlift.http.client.JsonResponseHandler.createJsonResponseHandler;
|
| 53 | +import static com.facebook.airlift.http.client.Request.Builder.fromRequest; |
51 | 54 | import static com.facebook.airlift.http.client.Request.Builder.prepareGet;
|
52 | 55 | import static com.facebook.airlift.http.client.Request.Builder.prepareHead;
|
53 | 56 | import static com.facebook.airlift.http.client.Request.Builder.preparePost;
|
| 57 | +import static com.facebook.airlift.http.client.Request.Builder.preparePut; |
54 | 58 | import static com.facebook.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator;
|
55 | 59 | import static com.facebook.airlift.http.client.StatusResponseHandler.createStatusResponseHandler;
|
56 | 60 | import static com.facebook.airlift.json.JsonCodec.jsonCodec;
|
@@ -262,6 +266,103 @@ public void testQuery()
|
262 | 266 | assertEquals(rows, ImmutableList.of(ImmutableList.of("system")));
|
263 | 267 | }
|
264 | 268 |
|
| 269 | + @Test |
| 270 | + public void testQueryWithPreMintedQueryIdAndSlug() |
| 271 | + { |
| 272 | + QueryId queryId = new QueryIdGenerator().createNextQueryId(); |
| 273 | + String slug = "xxx"; |
| 274 | + Request request = preparePut() |
| 275 | + .setUri(uriFor("/v1/statement/", queryId, slug)) |
| 276 | + .setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)) |
| 277 | + .setHeader(PRESTO_USER, "user") |
| 278 | + .setHeader(PRESTO_SOURCE, "source") |
| 279 | + .setHeader(PRESTO_CATALOG, "catalog") |
| 280 | + .setHeader(PRESTO_SCHEMA, "schema") |
| 281 | + .build(); |
| 282 | + |
| 283 | + QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 284 | + |
| 285 | + // verify slug in nextUri is same as requested |
| 286 | + assertEquals(queryResults.getNextUri().getQuery(), "slug=xxx"); |
| 287 | + |
| 288 | + // verify nextUri points to requested query id |
| 289 | + assertEquals(queryResults.getNextUri().getPath(), format("/v1/statement/queued/%s/1", queryId)); |
| 290 | + |
| 291 | + while (queryResults.getNextUri() != null) { |
| 292 | + queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 293 | + } |
| 294 | + |
| 295 | + if (queryResults.getError() != null) { |
| 296 | + fail(queryResults.getError().toString()); |
| 297 | + } |
| 298 | + |
| 299 | + // verify query id was passed down properly |
| 300 | + assertEquals(server.getDispatchManager().getQueryInfo(queryId).getQueryId(), queryId); |
| 301 | + } |
| 302 | + |
| 303 | + @Test |
| 304 | + public void testPutStatementIdempotency() |
| 305 | + { |
| 306 | + QueryId queryId = new QueryIdGenerator().createNextQueryId(); |
| 307 | + Request request = preparePut() |
| 308 | + .setUri(uriFor("/v1/statement/", queryId, "slug")) |
| 309 | + .setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)) |
| 310 | + .setHeader(PRESTO_USER, "user") |
| 311 | + .setHeader(PRESTO_SOURCE, "source") |
| 312 | + .setHeader(PRESTO_CATALOG, "catalog") |
| 313 | + .setHeader(PRESTO_SCHEMA, "schema") |
| 314 | + .build(); |
| 315 | + |
| 316 | + client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 317 | + // Execute PUT request again should succeed |
| 318 | + QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 319 | + |
| 320 | + while (queryResults.getNextUri() != null) { |
| 321 | + queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 322 | + } |
| 323 | + if (queryResults.getError() != null) { |
| 324 | + fail(queryResults.getError().toString()); |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + @Test(expectedExceptions = UnexpectedResponseException.class, expectedExceptionsMessageRegExp = "Expected response code to be \\[.*\\], but was 409") |
| 329 | + public void testPutStatementWithDifferentSlugFails() |
| 330 | + { |
| 331 | + QueryId queryId = new QueryIdGenerator().createNextQueryId(); |
| 332 | + Request request = preparePut() |
| 333 | + .setUri(uriFor("/v1/statement/", queryId, "slug")) |
| 334 | + .setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)) |
| 335 | + .setHeader(PRESTO_USER, "user") |
| 336 | + .setHeader(PRESTO_SOURCE, "source") |
| 337 | + .setHeader(PRESTO_CATALOG, "catalog") |
| 338 | + .setHeader(PRESTO_SCHEMA, "schema") |
| 339 | + .build(); |
| 340 | + client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 341 | + |
| 342 | + Request badRequest = fromRequest(request) |
| 343 | + .setUri(uriFor("/v1/statement/", queryId, "different_slug")) |
| 344 | + .build(); |
| 345 | + client.execute(badRequest, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 346 | + } |
| 347 | + |
| 348 | + @Test(expectedExceptions = UnexpectedResponseException.class, expectedExceptionsMessageRegExp = "Expected response code to be \\[.*\\], but was 409") |
| 349 | + public void testPutStatementAfterGetFails() |
| 350 | + { |
| 351 | + QueryId queryId = new QueryIdGenerator().createNextQueryId(); |
| 352 | + Request request = preparePut() |
| 353 | + .setUri(uriFor("/v1/statement/", queryId, "slug")) |
| 354 | + .setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)) |
| 355 | + .setHeader(PRESTO_USER, "user") |
| 356 | + .setHeader(PRESTO_SOURCE, "source") |
| 357 | + .setHeader(PRESTO_CATALOG, "catalog") |
| 358 | + .setHeader(PRESTO_SCHEMA, "schema") |
| 359 | + .build(); |
| 360 | + |
| 361 | + QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 362 | + client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 363 | + client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); |
| 364 | + } |
| 365 | + |
265 | 366 | @Test
|
266 | 367 | public void testTransactionSupport()
|
267 | 368 | {
|
@@ -327,4 +428,13 @@ public URI uriFor(String path)
|
327 | 428 | {
|
328 | 429 | return HttpUriBuilder.uriBuilderFrom(server.getBaseUrl()).replacePath(path).build();
|
329 | 430 | }
|
| 431 | + |
| 432 | + public URI uriFor(String path, QueryId queryId, String slug) |
| 433 | + { |
| 434 | + return HttpUriBuilder.uriBuilderFrom(server.getBaseUrl()) |
| 435 | + .replacePath(path) |
| 436 | + .appendPath(queryId.getId()) |
| 437 | + .addParameter("slug", slug) |
| 438 | + .build(); |
| 439 | + } |
330 | 440 | }
|
0 commit comments