kfaraz commented on code in PR #12992: URL: https://github.com/apache/druid/pull/12992#discussion_r977181796
########## integration-tests/src/main/java/org/apache/druid/testing/utils/MsqTestQueryHelper.java: ########## @@ -0,0 +1,246 @@ +/* + * 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.druid.testing.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import com.google.inject.Inject; +import org.apache.druid.indexer.TaskState; +import org.apache.druid.indexer.TaskStatusPlus; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.RetryUtils; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.guava.Yielder; +import org.apache.druid.java.util.http.client.response.StatusResponseHolder; +import org.apache.druid.msq.indexing.report.MSQResultsReport; +import org.apache.druid.msq.indexing.report.MSQTaskReport; +import org.apache.druid.msq.indexing.report.MSQTaskReportPayload; +import org.apache.druid.msq.sql.SqlTaskStatus; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.sql.http.SqlQuery; +import org.apache.druid.testing.IntegrationTestingConfig; +import org.apache.druid.testing.clients.SqlResourceTestClient; +import org.apache.druid.testing.clients.msq.MsqOverlordResourceTestClient; +import org.jboss.netty.handler.codec.http.HttpResponseStatus; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +/** + * Helper class to aid out ITs for MSQ. + * This takes all the clients required to make the necessary calls to the client APIs for MSQ and performs the boilerplate + * functions for the tests + */ +public class MsqTestQueryHelper extends AbstractTestQueryHelper<MsqQueryWithResults> +{ + + private final ObjectMapper jsonMapper; + private final IntegrationTestingConfig config; + private final MsqOverlordResourceTestClient overlordClient; + private final SqlResourceTestClient msqClient; + + + @Inject + MsqTestQueryHelper( + final ObjectMapper jsonMapper, + final SqlResourceTestClient queryClient, + final IntegrationTestingConfig config, + final MsqOverlordResourceTestClient overlordClient, + final SqlResourceTestClient msqClient + ) + { + super(jsonMapper, queryClient, config); + this.jsonMapper = jsonMapper; + this.config = config; + this.overlordClient = overlordClient; + this.msqClient = msqClient; + } + + @Override + public String getQueryURL(String schemeAndHost) + { + return StringUtils.format("%s/druid/v2/sql/task", schemeAndHost); + } + + /** + * Submits a task to the MSQ API with the given query string, and default headers and parameters + */ + public String submitMsqTask(String sqlQueryString) throws ExecutionException, InterruptedException + { + return submitMsqTask(new SqlQuery(sqlQueryString, null, false, false, false, ImmutableMap.of(), null)); + } + + // Run the task, wait for it to complete, fetch the reports, verify the results, + + /** + * Submits a {@link SqlQuery} to the MSQ API for execution. This method waits for the task to be accepted by the cluster + * and returns the task id associated with the submitted task + */ + public String submitMsqTask(SqlQuery sqlQuery) throws ExecutionException, InterruptedException + { + String queryUrl = getQueryURL(config.getBrokerUrl()); + Future<StatusResponseHolder> responseHolderFuture = msqClient.queryAsync(queryUrl, sqlQuery); + // It is okay to block here for the result because MSQ tasks return the task Id associated with it, which shouldn't + // consume a lot of time + StatusResponseHolder statusResponseHolder = responseHolderFuture.get(); + + // Check if the task has been accepted successfully + HttpResponseStatus httpResponseStatus = statusResponseHolder.getStatus(); + if (!httpResponseStatus.equals(HttpResponseStatus.ACCEPTED)) { + throw new ISE( + "Unable to submit the task successfully. Received response status code [%d], and response content:\n[%s]", + httpResponseStatus, + statusResponseHolder.getContent() + ); + } + String content = statusResponseHolder.getContent(); + SqlTaskStatus sqlTaskStatus; + try { + sqlTaskStatus = jsonMapper.readValue(content, SqlTaskStatus.class); + } + catch (JsonProcessingException e) { + throw new ISE("Unable to parse the response"); + } + if (sqlTaskStatus.getState().isFailure()) { + throw new ISE("Unable to start the task successfully.\nPossible exception: %s", sqlTaskStatus.getError()); + } + return sqlTaskStatus.getTaskId(); + } + + /** + * The method retries till the task with taskId gets completed i.e. {@link TaskState#isComplete()}} returns true and + * returns the last fetched state {@link TaskState} of the task + */ + public TaskState pollTaskIdForCompletion(String taskId) throws Exception + { + return RetryUtils.retry( + () -> { + TaskStatusPlus taskStatusPlus = overlordClient.getTaskStatus(taskId); + TaskState statusCode = taskStatusPlus.getStatusCode(); + if (statusCode != null && statusCode.isComplete()) { + return taskStatusPlus.getStatusCode(); + } + throw new TaskStillRunningException(); + }, + (Throwable t) -> t instanceof TaskStillRunningException, + 100 + ); + } + + /** + * Fetches status reports for a given task + */ + public Map<String, MSQTaskReport> fetchStatusReports(String taskId) Review Comment: Yeah, we could just return the `MSQTaskReport` here. -- 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]
