Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/_docs/tools/control-script.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,61 @@ For example, to cancel the transactions that have been running for more than 100
control.sh --tx --min-duration 100 --kill
----

== Mass Cancellation of Queries

The control script allows you to mass cancel SQL queries, scan queries, and continuous queries that match specific criteria.

The syntax for the mass cancellation command is as follows:

[tabs]
--
tab:Unix[]
[source,shell,subs="verbatim,quotes"]
----
control.sh --kill all <target> [--node-id <nodeId>] [--min-duration <seconds>]
----
tab:Windows[]
[source,shell,subs="verbatim,quotes"]
----
control.bat --kill all <target> [--node-id <nodeId>] [--min-duration <seconds>]
----
--

The following target types are supported:

[cols="1,3",opts="header"]
|===
|Target | Description
|`sql`| SQL queries
|`scan`| Scan queries
|`continuous`| Continuous queries
|===

Parameters:

[cols="2,5",opts="header"]
|===
|Parameter | Description
|--nodeId <nodeId>| Optional. UUID of the originator node to filter targets.
|--min-duration <seconds>| Optional. Minimum duration in seconds. Only objects that have been running longer than the specified value will be cancelled.
|===

Example commands:

[source, shell]
----
# Cancel all SQL queries that have been running for more than 60 seconds
control.sh --kill all sql --min-duration 60

# Cancel scan queries that have been running for more than 30 seconds on a specific node
control.sh --kill all scan --nodeId <uuid> --min-duration 30

# Cancel continuous queries
control.sh --kill all continuous
----

To cancel specific individual objects (without mass filtering) see link:sql-reference/operational-commands[Operational Commands] section.

== Contention Detection in Transactions

The `contention` command detects when multiple transactions are in contention to create a lock for the same key. The command is useful if you have long-running or hanging transactions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
import org.apache.ignite.internal.management.encryption.EncryptionChangeMasterKeyCommand;
import org.apache.ignite.internal.management.encryption.EncryptionCommand;
import org.apache.ignite.internal.management.event.EventCommand;
import org.apache.ignite.internal.management.kill.KillAllCommand;
import org.apache.ignite.internal.management.kill.KillAllCommandArg;
import org.apache.ignite.internal.management.kill.KillCommand;
import org.apache.ignite.internal.management.meta.MetaCommand;
import org.apache.ignite.internal.management.meta.MetaRemoveCommand;
Expand Down Expand Up @@ -478,6 +480,13 @@ else if (cmd.getClass() == CacheClearCommand.class) {

arg = (A)a;
}
else if (cmd.getClass() == KillAllCommand.class) {
KillAllCommandArg a = new KillAllCommandArg();

a.target(KillAllCommandArg.TargetType.SQL);

arg = (A)a;
}
else
arg = cmd.argClass().newInstance();

Expand Down Expand Up @@ -523,6 +532,8 @@ else if (cmd.getClass() == MetaUpdateCommand.class)
return;
else if (cmd.getClass() == MetaRemoveCommand.class)
cmdText = F.concat(cmdText, "--typeId", "1");
else if (cmd.getClass() == KillAllCommand.class)
cmdText = F.concat(cmdText, "SQL");

args = parseArgs(asList(cmdText));

Expand Down Expand Up @@ -694,6 +705,14 @@ public void testKillArguments() {

assertParseArgsThrows("String representation of \"java.util.UUID\" is exepected", IllegalArgumentException.class,
"--kill", "continuous", UUID.randomUUID().toString(), "not_a_uuid");

// Kill all command format errors.
assertParseArgsThrows("Argument target required.", "--kill", "all");
assertParseArgsThrows("Can't parse value 'unknown'", "--kill", "all", "unknown");
assertParseArgsThrows("Argument is invalid: --min-duration", "--kill", "all", "sql", "--min-duration", "-1");
assertParseArgsThrows("Argument is invalid: --min-duration", "--kill", "all", "sql", "--min-duration", "0");
assertParseArgsThrows("Argument is invalid: --minDuration is not supported for CONTINUOUS queries",
"--kill", "all", "continuous", "--min-duration", "60");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.ignite.util.GridCommandHandlerWithSslFactoryTest;
import org.apache.ignite.util.GridCommandHandlerWithSslTest;
import org.apache.ignite.util.GridPersistenceCommandsTest;
import org.apache.ignite.util.KilAlllCommandsControlShTest;
import org.apache.ignite.util.KillCommandsControlShTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand Down Expand Up @@ -76,6 +77,7 @@
GridCommandHandlerLegacyClientTest.class,

KillCommandsControlShTest.class,
KilAlllCommandsControlShTest.class,

BaselineEventsLocalTest.class,
BaselineEventsRemoteTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/*
* 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.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.query.ContinuousQuery;
import org.apache.ignite.cache.query.Query;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.processors.cache.query.GridCacheDistributedQueryManager;
import org.apache.ignite.internal.util.GridTestClockTimer;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.systemview.view.SqlQueryView;
import org.junit.Test;

import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK;
import static org.apache.ignite.internal.processors.query.running.RunningQueryManager.SQL_QRY_VIEW;
import static org.apache.ignite.testframework.GridTestUtils.assertContains;
import static org.apache.ignite.testframework.GridTestUtils.assertThrows;

/**
* Test for mass queries cancellation.
*/
public class KilAlllCommandsControlShTest extends GridCommandHandlerClusterByClassAbstractTest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class KilAlllCommandsControlShTest extends GridCommandHandlerClusterByClassAbstractTest {
public class KillAllCommandsControlShTest extends GridCommandHandlerClusterByClassAbstractTest {

/** Operations timeout. */
public static final int TIMEOUT = 10_000;

/** */
private static CountDownLatch latch;

/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();

IgniteCache<Object, Object> cache = client.getOrCreateCache(
new CacheConfiguration<>(DEFAULT_CACHE_NAME)
.setIndexedTypes(Integer.class, Integer.class)
.setSqlFunctionClasses(SqlTestFunctions.class));

for (int i = 0; i < 1000; i++)
cache.put(i, i);
}

/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
super.beforeTest();

latch = new CountDownLatch(1);
}

/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
latch.countDown();
}

/** */
@Test
public void testKillAllSql() {
String sql = "SELECT * FROM Integer WHERE latch()";

checkKillAll("sql", () -> new SqlFieldsQuery(sql), KilAlllCommandsControlShTest::sqlQueriesCnt);
}

/** */
@Test
public void testKillAllScan() {
checkKillAll("scan", () -> new ScanQuery<>().setPageSize(1).setFilter((k, v) -> {
try {
latch.await(10, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}

return true;
}), KilAlllCommandsControlShTest::scanQueriesCnt);
}

/** */
@Test
public void testKillAllContinuous() {
assertTrue(SERVER_NODE_CNT >= 2);

client.cache(DEFAULT_CACHE_NAME).query(new ContinuousQuery<>().setLocalListener(evts -> {}));
grid(0).cache(DEFAULT_CACHE_NAME).query(new ContinuousQuery<>().setLocalListener(evts -> {}));
grid(1).cache(DEFAULT_CACHE_NAME).query(new ContinuousQuery<>().setLocalListener(evts -> {}));

// Kill all queries using --node-id argument.
assertEquals(EXIT_CODE_OK, execute("--kill", "all", "continuous",
"--node-id", grid(0).context().localNodeId().toString()));

assertEquals(1, client.context().continuous().localRoutineInfos().size());
assertEquals(0, grid(0).context().continuous().localRoutineInfos().size());
assertEquals(1, grid(1).context().continuous().localRoutineInfos().size());
assertEquals(0, client.context().continuous().remoteRoutineInfos().size());
assertEquals(2, grid(0).context().continuous().remoteRoutineInfos().size());
assertEquals(1, grid(1).context().continuous().remoteRoutineInfos().size());

// Kill all queries without arguments.
assertEquals(EXIT_CODE_OK, execute("--kill", "all", "continuous"));

assertEquals(0, client.context().continuous().localRoutineInfos().size());
assertEquals(0, grid(0).context().continuous().localRoutineInfos().size());
assertEquals(0, grid(1).context().continuous().localRoutineInfos().size());
assertEquals(0, client.context().continuous().remoteRoutineInfos().size());
assertEquals(0, grid(0).context().continuous().remoteRoutineInfos().size());
assertEquals(0, grid(1).context().continuous().remoteRoutineInfos().size());
}

/** */
public void checkKillAll(String target, Supplier<Query<?>> qryFactory, ToIntFunction<IgniteEx> qryCntProvider) {
try {
assertTrue(SERVER_NODE_CNT >= 2);

long ts = U.currentTimeMillis();
GridTestClockTimer.timeSupplier(() -> ts);

List<QueryCursor<?>> curs = new ArrayList<>();

curs.add(client.cache(DEFAULT_CACHE_NAME).query(qryFactory.get()));

for (int i = 0; i < 2; i++)
curs.add(grid(i).cache(DEFAULT_CACHE_NAME).query(qryFactory.get()));

GridTestClockTimer.timeSupplier(() -> ts + 1001L);

curs.add(client.cache(DEFAULT_CACHE_NAME).query(qryFactory.get()));

for (int i = 0; i < 2; i++)
curs.add(grid(i).cache(DEFAULT_CACHE_NAME).query(qryFactory.get()));

injectTestSystemOut();

// Kill all queries using both --min-duration and --node-id arguments.
assertEquals(EXIT_CODE_OK, execute("--kill", "all", target, "--min-duration", "1",
"--node-id", client.context().localNodeId().toString()));

assertContains(log, testOut.toString(), "Node ID: " + client.context().localNodeId() + " Killed: 1");
assertContains(log, testOut.toString(), "Total killed: 1");

assertEquals(1, qryCntProvider.applyAsInt(client));
assertEquals(2, qryCntProvider.applyAsInt(grid(0)));
assertEquals(2, qryCntProvider.applyAsInt(grid(1)));
assertThrows(log, () -> curs.get(0).getAll(), Exception.class, "");

testOut.reset();

// Kill all queries using --min-duration argument.
assertEquals(EXIT_CODE_OK, execute("--kill", "all", target, "--min-duration", "1"));
assertContains(log, testOut.toString(), "Node ID: " + grid(0).context().localNodeId() + " Killed: 1");
assertContains(log, testOut.toString(), "Node ID: " + grid(1).context().localNodeId() + " Killed: 1");
assertContains(log, testOut.toString(), "Total killed: 2");

assertEquals(1, qryCntProvider.applyAsInt(client));
assertEquals(1, qryCntProvider.applyAsInt(grid(0)));
assertEquals(1, qryCntProvider.applyAsInt(grid(1)));

assertThrows(log, () -> curs.get(1).getAll(), Exception.class, "");
assertThrows(log, () -> curs.get(2).getAll(), Exception.class, "");

testOut.reset();

// Kill all queries using --node-id argument.
assertEquals(EXIT_CODE_OK, execute("--kill", "all", target, "--node-id",
grid(0).context().localNodeId().toString()));

assertContains(log, testOut.toString(), "Node ID: " + grid(0).context().localNodeId() + " Killed: 1");
assertContains(log, testOut.toString(), "Total killed: 1");

assertEquals(1, qryCntProvider.applyAsInt(client));
assertEquals(0, qryCntProvider.applyAsInt(grid(0)));
assertEquals(1, qryCntProvider.applyAsInt(grid(1)));

assertThrows(log, () -> curs.get(4).getAll(), Exception.class, "");

testOut.reset();

// Kill all queries without arguments.
assertEquals(EXIT_CODE_OK, execute("--kill", "all", target));

assertContains(log, testOut.toString(), "Node ID: " + client.context().localNodeId() + " Killed: 1");
assertContains(log, testOut.toString(), "Node ID: " + grid(1).context().localNodeId() + " Killed: 1");
assertContains(log, testOut.toString(), "Total killed: 2");

assertEquals(0, qryCntProvider.applyAsInt(client));
assertEquals(0, qryCntProvider.applyAsInt(grid(0)));
assertEquals(0, qryCntProvider.applyAsInt(grid(1)));

assertThrows(log, () -> curs.get(3).getAll(), Exception.class, "");
assertThrows(log, () -> curs.get(5).getAll(), Exception.class, "");
}
finally {
GridTestClockTimer.timeSupplier(GridTestClockTimer.DFLT_TIME_SUPPLIER);
}
}

/** */
private static int sqlQueriesCnt(IgniteEx ignite) {
return F.size(ignite.context().systemView().<SqlQueryView>view(SQL_QRY_VIEW).iterator(), v -> !v.mapQuery());
}

/** */
private static int scanQueriesCnt(IgniteEx ignite) {
return ((GridCacheDistributedQueryManager<?, ?>)ignite.cachex(DEFAULT_CACHE_NAME).context().queries())
.distributedQueryFutures().size();
}

/** */
public static class SqlTestFunctions {
/** */
@QuerySqlFunction
public static boolean latch() {
try {
latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
}
catch (InterruptedException ignored) {
return false;
}

return true;
}
}
}
Loading
Loading