Skip to content

Commit

Permalink
IGNITE-15629 Cache metrics command implemented (#10745)
Browse files Browse the repository at this point in the history
  • Loading branch information
nizhikov committed Jun 2, 2023
1 parent ab91727 commit 3f82300
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 244 deletions.

This file was deleted.

Expand Up @@ -33,6 +33,7 @@
import org.apache.ignite.internal.management.cache.CacheIndexesListCommand;
import org.apache.ignite.internal.management.cache.CacheIndexesRebuildStatusCommand;
import org.apache.ignite.internal.management.cache.CacheListCommand;
import org.apache.ignite.internal.management.cache.CacheMetricsCommand;
import org.apache.ignite.internal.management.cache.CacheResetLostPartitionsCommand;
import org.apache.ignite.internal.management.cache.CacheScheduleIndexesRebuildCommand;
import org.apache.ignite.internal.management.cache.CacheValidateIndexesCommand;
Expand Down Expand Up @@ -122,7 +123,7 @@ public enum CacheSubcommands {
/**
* Enable / disable cache metrics collection or show metrics collection status.
*/
METRICS("metrics", null, new CacheMetrics()),
METRICS(new CacheMetricsCommand()),

/**
* Index rebuild via the maintenance mode.
Expand Down
Expand Up @@ -23,7 +23,7 @@
import java.util.regex.Pattern;
import org.apache.ignite.Ignite;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.commandline.cache.CacheMetrics;
import org.apache.ignite.internal.management.cache.CacheMetricsCommand;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.G;
import org.apache.ignite.internal.visor.cache.metrics.CacheMetricsOperation;
Expand All @@ -34,16 +34,11 @@
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK;
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_UNEXPECTED_ERROR;
import static org.apache.ignite.internal.commandline.CommandList.CACHE;
import static org.apache.ignite.internal.commandline.cache.CacheMetrics.ALL_CACHES_ARGUMENT;
import static org.apache.ignite.internal.commandline.cache.CacheMetrics.CACHES_ARGUMENT;
import static org.apache.ignite.internal.commandline.cache.CacheMetrics.EXPECTED_CACHES_LIST_MESSAGE;
import static org.apache.ignite.internal.commandline.cache.CacheMetrics.INCORRECT_CACHE_ARGUMENT_MESSAGE;
import static org.apache.ignite.internal.commandline.cache.CacheMetrics.INCORRECT_METRICS_OPERATION_MESSAGE;
import static org.apache.ignite.internal.commandline.cache.CacheSubcommands.METRICS;
import static org.apache.ignite.internal.util.lang.GridFunc.asMap;

/**
* Test for {@link CacheMetrics} command.
* Test for {@link CacheMetricsCommand} command.
*/
public class CacheMetricsCommandTest extends GridCommandHandlerAbstractTest {
/** Enable operation. */
Expand All @@ -61,6 +56,21 @@ public class CacheMetricsCommandTest extends GridCommandHandlerAbstractTest {
/** Cache two. */
private static final String CACHE_TWO = "cache-2";

/** */
private static final String ALL_CACHES_ARGUMENT = "--all-caches";

/** */
private static final String CACHES_ARGUMENT = "--caches";

/** Incorrect metrics operation message. */
public static final String INCORRECT_METRICS_OPERATION_MESSAGE = "Argument operation required";

/** Expected caches list message. */
public static final String EXPECTED_CACHES_LIST_MESSAGE = "Please specify a value for argument: --caches";

/** Incorrect cache argument message. */
public static final String INCORRECT_CACHE_ARGUMENT_MESSAGE = "One of [--all-caches, --caches] required";

/** {@inheritDoc} */
@Override public void beforeTest() throws Exception {
super.beforeTest();
Expand Down Expand Up @@ -148,34 +158,34 @@ public void testInvalidArguments() {
checkInvalidArguments(checkArgs + INCORRECT_METRICS_OPERATION_MESSAGE);

// Check when unknown operation passed
checkInvalidArguments(checkArgs + INCORRECT_METRICS_OPERATION_MESSAGE, "bad-command");
checkInvalidArguments(checkArgs + "Can't parse value 'bad-command'", "bad-command");

// Check when no --caches/--all-caches arguments passed
checkInvalidArguments(checkArgs + INCORRECT_CACHE_ARGUMENT_MESSAGE, ENABLE);
checkInvalidArguments(checkArgs + INCORRECT_CACHE_ARGUMENT_MESSAGE, STATUS);

String invalidCacheListFullMsg = "Check arguments. Expected " + EXPECTED_CACHES_LIST_MESSAGE;
String invalidCacheListFullMsg = checkArgs + EXPECTED_CACHES_LIST_MESSAGE;

// Check when --caches argument passed without list of caches
checkInvalidArguments(invalidCacheListFullMsg, ENABLE, CACHES_ARGUMENT);
checkInvalidArguments(invalidCacheListFullMsg, STATUS, CACHES_ARGUMENT);

String incorrectCacheArgFullMsg = checkArgs + INCORRECT_CACHE_ARGUMENT_MESSAGE;
String incorrectCacheArgFullMsg = checkArgs + "Unexpected argument: --arg";

// Check when unknown argument is passed after metric operation
checkInvalidArguments(incorrectCacheArgFullMsg, ENABLE, "--arg");
checkInvalidArguments(incorrectCacheArgFullMsg, STATUS, "--arg");

String unexpectedCacheArgMsg = "Unexpected argument of --cache subcommand: ";
String unexpectedCacheArgMsg = checkArgs + "Only one of [--all-caches, --caches] allowed";

// Check when extra argument passed after correct command
checkInvalidArguments(unexpectedCacheArgMsg + ALL_CACHES_ARGUMENT, ENABLE, CACHES_ARGUMENT, CACHE_ONE,
checkInvalidArguments(unexpectedCacheArgMsg, ENABLE, CACHES_ARGUMENT, CACHE_ONE,
ALL_CACHES_ARGUMENT);
checkInvalidArguments(unexpectedCacheArgMsg + CACHES_ARGUMENT, STATUS, ALL_CACHES_ARGUMENT, CACHES_ARGUMENT,
checkInvalidArguments(unexpectedCacheArgMsg, STATUS, ALL_CACHES_ARGUMENT, CACHES_ARGUMENT,
CACHE_ONE);

// Check when after --caches argument extra argument is passed instead of list of caches
checkInvalidArguments(unexpectedCacheArgMsg + ALL_CACHES_ARGUMENT, ENABLE, CACHES_ARGUMENT, ALL_CACHES_ARGUMENT);
checkInvalidArguments("Unexpected value: --all-caches", ENABLE, CACHES_ARGUMENT, ALL_CACHES_ARGUMENT);
}

/**
Expand Down
Expand Up @@ -1247,7 +1247,7 @@ public void testCacheCreate() {

assertContains(log, executeCommand(EXIT_CODE_INVALID_ARGUMENTS,
"--cache", CacheSubcommands.CREATE.name(), SPRING_XML_CONFIG),
"Please specify a value for argument: --springxmlconfig");
"Please specify a value for argument: --springXmlConfig");

autoConfirmation = true;

Expand Down
Expand Up @@ -407,8 +407,14 @@ else if (type == UUID.class) {
else if (type == IgniteUuid.class) {
return (T)IgniteUuid.fromString(val);
}
else if (type.isEnum())
return (T)Enum.valueOf((Class<Enum>)type, val.toUpperCase());
else if (type.isEnum()) {
try {
return (T)Enum.valueOf((Class<Enum>)type, val.toUpperCase());
}
catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Can't parse value '" + val + "', expected type: " + type.getName());
}
}

throw new IgniteException("Unsupported argument type: " + type.getName());
}
Expand Down
Expand Up @@ -35,6 +35,7 @@ public CacheCommand() {
new CacheContentionCommand(),
new CacheResetLostPartitionsCommand(),
new CacheIndexesListCommand(),
new CacheMetricsCommand(),
new CacheIndexesRebuildStatusCommand(),
new CacheIndexesForceRebuildCommand(),
new CacheScheduleIndexesRebuildCommand()
Expand Down
@@ -0,0 +1,62 @@
/*
* 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.management.cache;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.apache.ignite.IgniteException;
import org.apache.ignite.internal.management.api.ComputeCommand;
import org.apache.ignite.internal.visor.cache.metrics.VisorCacheMetricsTask;
import org.apache.ignite.internal.visor.cache.metrics.VisorCacheMetricsTaskResult;
import static java.util.Arrays.asList;
import static org.apache.ignite.internal.management.SystemViewCommand.printTable;
import static org.apache.ignite.internal.visor.systemview.VisorSystemViewTask.SimpleType.STRING;

/** */
public class CacheMetricsCommand implements ComputeCommand<CacheMetricsCommandArg, VisorCacheMetricsTaskResult> {
/** {@inheritDoc} */
@Override public String description() {
return "Manages user cache metrics collection: enables, disables it or shows status";
}

/** {@inheritDoc} */
@Override public Class<CacheMetricsCommandArg> argClass() {
return CacheMetricsCommandArg.class;
}

/** {@inheritDoc} */
@Override public Class<VisorCacheMetricsTask> taskClass() {
return VisorCacheMetricsTask.class;
}

/** {@inheritDoc} */
@Override public void printResult(CacheMetricsCommandArg arg, VisorCacheMetricsTaskResult res, Consumer<String> printer) {
try {
List<List<?>> values = res.result().entrySet()
.stream()
.map(e -> asList(e.getKey(), e.getValue() ? "enabled" : "disabled"))
.collect(Collectors.toList());

printTable(asList("Cache Name", "Metrics Status"), asList(STRING, STRING), values, printer);
}
catch (Exception e) {
throw new IgniteException(e);
}
}
}

0 comments on commit 3f82300

Please sign in to comment.