xtern commented on code in PR #5080: URL: https://github.com/apache/ignite-3/pull/5080#discussion_r1925275668
########## modules/rest-api/src/main/java/org/apache/ignite/internal/rest/api/transaction/TransactionApi.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.ignite.internal.rest.api.transaction; + +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; +import static org.apache.ignite.internal.rest.constants.MediaType.APPLICATION_JSON; + +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Delete; +import io.micronaut.http.annotation.Get; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.Collection; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.internal.rest.api.Problem; + +/** + * API for managing transaction. Review Comment: ```suggestion * API for managing transactions. ``` ########## modules/rest/src/integrationTest/java/org/apache/ignite/internal/rest/sql/ItSqlQueryControllerTest.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.ignite.internal.rest.sql; + +import static io.micronaut.http.HttpRequest.DELETE; +import static io.micronaut.http.HttpStatus.NOT_FOUND; +import static org.apache.ignite.internal.rest.matcher.MicronautHttpResponseMatcher.assertThrowsProblem; +import static org.apache.ignite.internal.rest.matcher.ProblemMatcher.isProblem; +import static org.awaitility.Awaitility.await; +import static org.awaitility.Awaitility.waitAtMost; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.is; + +import io.micronaut.core.type.Argument; +import io.micronaut.http.HttpRequest; +import io.micronaut.http.client.HttpClient; +import io.micronaut.http.client.annotation.Client; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import jakarta.inject.Inject; +import java.time.Duration; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +import org.apache.ignite.internal.ClusterPerClassIntegrationTest; +import org.apache.ignite.internal.rest.api.sql.SqlQueryInfo; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +/** + * Integration tests for {@link SqlQueryController}. + */ +@MicronautTest +public class ItSqlQueryControllerTest extends ClusterPerClassIntegrationTest { + private static final String SQL_QUERY_URL = "/management/v1/sql/"; + + @Inject + @Client("http://localhost:10300" + SQL_QUERY_URL) + HttpClient client; + + @AfterEach + void tearDown() { + try { + sql("DROP TABLE large_table"); + } catch (Exception ignore) { + // nothing to do + } + } + + @Test + void shouldReturnAllSqlQueries() { + // Create table + sql("CREATE TABLE large_table (id int primary key, value1 DOUBLE, value2 DOUBLE)"); + + // Run long running query async + String sql = "INSERT INTO large_table (id, value1, value2) SELECT x, RAND() * 100, RAND() * 100 FROM TABLE(SYSTEM_RANGE(1, 100));"; + CompletableFuture.runAsync(() -> + sql(sql) Review Comment: I suggest to rework this part of tests a bit. The query must be "active" until query cursor is closed. `sql(sql)` fetches all results and closes cursor. I suggest to keep cursor open and check results, instead of asynchronous busy wait. something like this ``` String sql = "SELECT x FROM TABLE(SYSTEM_RANGE(1, 100));"; IgniteSql igniteSql = CLUSTER.aliveNode().sql(); // run query with results pageSize=1 ResultSet<SqlRow> rs = CLUSTER.aliveNode().sql() .execute(null, igniteSql.statementBuilder().query(sql).pageSize(1).build()); // the query must be active until cursor is closed Map<UUID, SqlQueryInfo> queries = getSqlQueries(client); assertThat(queries, aMapWithSize(1)); SqlQueryInfo queryInfo = queries.entrySet().iterator().next().getValue(); assertThat(queryInfo.sql(), is(sql)); assertThat(queryInfo.schema(), is("PUBLIC")); assertThat(queryInfo.type(), is("QUERY")); rs.close(); // don't forget to close resultset ``` ########## modules/rest/src/main/java/org/apache/ignite/internal/rest/sql/SqlQueryController.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.ignite.internal.rest.sql; + +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; +import static org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture; + +import io.micronaut.http.annotation.Controller; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.function.Predicate; +import org.apache.ignite.internal.rest.ResourceHolder; +import org.apache.ignite.internal.rest.api.sql.SqlQueryApi; +import org.apache.ignite.internal.rest.api.sql.SqlQueryInfo; +import org.apache.ignite.internal.rest.sql.exception.SqlQueryNotFoundException; +import org.apache.ignite.internal.sql.engine.api.kill.CancellableOperationType; +import org.apache.ignite.internal.sql.engine.api.kill.KillHandlerRegistry; +import org.apache.ignite.sql.IgniteSql; +import org.apache.ignite.sql.ResultSet; +import org.apache.ignite.sql.SqlRow; +import org.apache.ignite.sql.Statement; +import org.jetbrains.annotations.Nullable; + +/** + * REST endpoint allows to manage sql queries. + */ +@Controller("/management/v1/sql") +public class SqlQueryController implements SqlQueryApi, ResourceHolder { + + private IgniteSql igniteSql; + + private KillHandlerRegistry killHandlerRegistry; + + public SqlQueryController(IgniteSql igniteSql, KillHandlerRegistry killHandlerRegistry) { + this.igniteSql = igniteSql; + this.killHandlerRegistry = killHandlerRegistry; + } + + @Override + public CompletableFuture<Collection<SqlQueryInfo>> queries() { + return completedFuture(sqlQueryInfos()); + } + + @Override + public CompletableFuture<SqlQueryInfo> query(UUID queryId) { + return completedFuture(sqlQueryInfos(uuid -> uuid.equals(queryId))).thenApply(queryInfo -> { + if (queryInfo.isEmpty()) { + throw new SqlQueryNotFoundException(queryId.toString()); + } else { + return queryInfo.get(0); + } + }); + } + + @Override + public CompletableFuture<Void> cancelQuery(UUID queryId) { + return killHandlerRegistry.handler(CancellableOperationType.QUERY).cancelAsync(queryId.toString()) + .thenCompose(result -> handleOperationResult(queryId, result)); + } + + private static CompletableFuture<Void> handleOperationResult(UUID queryId, @Nullable Boolean result) { + if (result != null && !result) { + return failedFuture(new SqlQueryNotFoundException(queryId.toString())); + } else { + return nullCompletedFuture(); + } + } + + @Override + public void cleanResources() { + igniteSql = null; + killHandlerRegistry = null; + } + + private List<SqlQueryInfo> sqlQueryInfos() { + return sqlQueryInfos(null); + } + + private List<SqlQueryInfo> sqlQueryInfos(Predicate<UUID> predicate) { Review Comment: Better to replace predicate with explicit UUID and use "query with filter" if it was specified, e.g. "SELECT * FROM SYSTEM.SQL_QUERIES WHERE ID=?" instead of "SELECT * FROM SYSTEM.SQL_QUERIES ORDER BY START_TIME" -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org