korlov42 commented on code in PR #4615: URL: https://github.com/apache/ignite-3/pull/4615#discussion_r1830970865
########## modules/sql-engine/src/integrationTest/java/org/apache/ignite/lang/ItQueryCancelTest.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.lang; + +import static org.apache.ignite.internal.sql.engine.QueryProperty.ALLOWED_QUERY_TYPES; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Set; +import java.util.concurrent.CompletionException; +import org.apache.ignite.internal.TestWrappers; +import org.apache.ignite.internal.app.IgniteImpl; +import org.apache.ignite.internal.sql.BaseSqlIntegrationTest; +import org.apache.ignite.internal.sql.engine.AsyncSqlCursor; +import org.apache.ignite.internal.sql.engine.InternalSqlRow; +import org.apache.ignite.internal.sql.engine.QueryProcessor; +import org.apache.ignite.internal.sql.engine.SqlQueryType; +import org.apache.ignite.internal.sql.engine.property.SqlProperties; +import org.apache.ignite.internal.sql.engine.property.SqlPropertiesHelper; +import org.apache.ignite.internal.tx.HybridTimestampTracker; +import org.apache.ignite.lang.ErrorGroups.Sql; +import org.apache.ignite.sql.SqlException; +import org.junit.jupiter.api.Test; + +/** Set of test cases for query cancellation. */ +public class ItQueryCancelTest extends BaseSqlIntegrationTest { + + @Test + public void testQueryCancel() { Review Comment: let's add test case description to javadoc. Please fix other tests too ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/QueryCancel.java: ########## @@ -22,13 +22,32 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; import org.apache.ignite.internal.util.Cancellable; +import org.apache.ignite.lang.CancelHandleHelper; +import org.apache.ignite.lang.CancellationToken; +import org.jetbrains.annotations.Nullable; /** * Holds query cancel state. */ public class QueryCancel { private final CompletableFuture<Reason> state = new CompletableFuture<>(); + /** Constructor. */ + public QueryCancel() { + this(null); + } + + /** Constructor. */ + public QueryCancel(@Nullable CancellationToken cancellationToken) { + if (cancellationToken != null) { + // If token is provided, add this cancellation to its handle. + CompletableFuture<Void> fut = new CompletableFuture<>(); + state.thenAccept(ignore -> fut.complete(null)); + + CancelHandleHelper.addCancelAction(cancellationToken, this::cancel, fut); Review Comment: cancellation like this doesn't meet mandatory requirement to release the future when all resources has been freed. For now let's complete cancellation future when at least locally there is nothing remains (e.g. when query is deregistered from `QueryExecutor#runningQueries`) ########## modules/sql-engine/src/integrationTest/java/org/apache/ignite/lang/ItQueryCancelTest.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.lang; + +import static org.apache.ignite.internal.sql.engine.QueryProperty.ALLOWED_QUERY_TYPES; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Set; +import java.util.concurrent.CompletionException; +import org.apache.ignite.internal.TestWrappers; +import org.apache.ignite.internal.app.IgniteImpl; +import org.apache.ignite.internal.sql.BaseSqlIntegrationTest; +import org.apache.ignite.internal.sql.engine.AsyncSqlCursor; +import org.apache.ignite.internal.sql.engine.InternalSqlRow; +import org.apache.ignite.internal.sql.engine.QueryProcessor; +import org.apache.ignite.internal.sql.engine.SqlQueryType; +import org.apache.ignite.internal.sql.engine.property.SqlProperties; +import org.apache.ignite.internal.sql.engine.property.SqlPropertiesHelper; +import org.apache.ignite.internal.tx.HybridTimestampTracker; +import org.apache.ignite.lang.ErrorGroups.Sql; +import org.apache.ignite.sql.SqlException; +import org.junit.jupiter.api.Test; + +/** Set of test cases for query cancellation. */ +public class ItQueryCancelTest extends BaseSqlIntegrationTest { Review Comment: let's move this class to package `org.apache.ignite.internal.sql.engine` ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImpl.java: ########## @@ -463,11 +464,9 @@ private AsyncDataCursor<InternalSqlRow> executeDdl(SqlOperationContext operation assert queryCancel != null; queryCancel.add(timeout -> { - if (!timeout) { - return; - } + String message = timeout ? QueryCancelledException.TIMEOUT_MSG : QueryCancelledException.CANCEL_MSG; - ret.completeExceptionally(new QueryCancelledException(QueryCancelledException.TIMEOUT_MSG)); + ret.completeExceptionally(new QueryCancelledException(message)); Review Comment: cancellation _must_ terminate operation, not just return error immediately. For DDL, when command is submitted to catalog there is no way to terminate the operation. Please check this condition in other places too ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/KeyValueGetPlan.java: ########## @@ -205,7 +208,11 @@ public <RowT> AsyncCursor<InternalSqlRow> execute( result.whenCompleteAsync((res, err) -> firstPageReadyCallback.onPrefetchComplete(err), executor); } - ctx.scheduleTimeout(result); + try { + ctx.subscribeToCancellation(result); + } catch (QueryCancelledException e) { + throw (SqlException) SqlExceptionMapperUtil.mapToPublicSqlException(e); Review Comment: all such re-mapping must be done in SqlQueryProcessor ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/QueryCancel.java: ########## @@ -22,13 +22,32 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; import org.apache.ignite.internal.util.Cancellable; +import org.apache.ignite.lang.CancelHandleHelper; +import org.apache.ignite.lang.CancellationToken; +import org.jetbrains.annotations.Nullable; /** * Holds query cancel state. */ public class QueryCancel { private final CompletableFuture<Reason> state = new CompletableFuture<>(); + /** Constructor. */ + public QueryCancel() { + this(null); + } + + /** Constructor. */ + public QueryCancel(@Nullable CancellationToken cancellationToken) { + if (cancellationToken != null) { Review Comment: it's almost always not a best idea to do additional work in constructor apart of related to object creation. -- 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