shishkovilja commented on code in PR #9694: URL: https://github.com/apache/ignite/pull/9694#discussion_r866836569
########## modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheMetricsManage.java: ########## @@ -0,0 +1,198 @@ +/* + * 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.commandline.cache; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.logging.Logger; +import org.apache.ignite.internal.client.GridClient; +import org.apache.ignite.internal.client.GridClientConfiguration; +import org.apache.ignite.internal.commandline.AbstractCommand; +import org.apache.ignite.internal.commandline.Command; +import org.apache.ignite.internal.commandline.CommandArgIterator; +import org.apache.ignite.internal.commandline.TaskExecutor; +import org.apache.ignite.internal.commandline.argument.CommandArgUtils; +import org.apache.ignite.internal.commandline.cache.argument.CacheMetricsManageCommandArg; +import org.apache.ignite.internal.commandline.systemview.SystemViewCommand; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.visor.cache.metrics.CacheMetricsManageSubCommand; +import org.apache.ignite.internal.visor.cache.metrics.VisorCacheMetricsManageTask; +import org.apache.ignite.internal.visor.cache.metrics.VisorCacheMetricsManageTaskArg; +import org.apache.ignite.internal.visor.cache.metrics.VisorCacheMetricsManageTaskResult; + +import static java.util.Arrays.asList; +import static org.apache.ignite.internal.commandline.CommandLogger.optional; +import static org.apache.ignite.internal.commandline.CommandLogger.or; +import static org.apache.ignite.internal.commandline.cache.CacheSubcommands.METRICS_MANAGE; +import static org.apache.ignite.internal.commandline.cache.argument.CacheMetricsManageCommandArg.ALL_CACHES; +import static org.apache.ignite.internal.commandline.cache.argument.CacheMetricsManageCommandArg.CACHES; +import static org.apache.ignite.internal.visor.cache.metrics.CacheMetricsManageSubCommand.DISABLE; +import static org.apache.ignite.internal.visor.cache.metrics.CacheMetricsManageSubCommand.ENABLE; +import static org.apache.ignite.internal.visor.cache.metrics.CacheMetricsManageSubCommand.STATUS; +import static org.apache.ignite.internal.visor.systemview.VisorSystemViewTask.SimpleType.STRING; + +/** + * Cache sub-command for a cache metrics collection management. It provides to enable, disable or show status. + */ +public class CacheMetricsManage extends AbstractCommand<VisorCacheMetricsManageTaskArg> { + /** Incorrect sub command message. */ + public static final String INCORRECT_SUB_COMMAND_MESSAGE = "Expected correct sub-command."; + + /** Incorrect cache argument message. */ + public static final String INCORRECT_CACHE_ARGUMENT_MESSAGE = + String.format("'%s' or '%s' arguments should be passed.", CACHES, ALL_CACHES); + + /** Invalid caches list message. */ + public static final String INVALID_CACHES_LIST_MESSAGE = "comma-separated list of cache names."; + + /** No caches processed message. */ + public static final String NONE_CACHES_PROCECCED_MESSAGE = "None of caches have been processed. " + + "Are there any caches in cluster?"; + + /** Duplicated '--caches' option message. */ + public static final String DUPLICATED_CACHES_OPTION_MESSAGE = "Duplicated '" + CACHES + "' option found."; + + /** Duplicated '--all-caches' option message. */ + public static final String DUPLICATED_ALL_CACHES_OPTION_MESSAGE = "Duplicated '" + ALL_CACHES + "' option found."; + + /** Success message. */ + public static final String SUCCESS_MESSAGE = "Command performed successfully."; + + /** Task argument. */ + private VisorCacheMetricsManageTaskArg arg; + + /** {@inheritDoc} */ + @Override public Object execute(GridClientConfiguration clientCfg, Logger log) throws Exception { + try (GridClient client = Command.startClient(clientCfg)) { + VisorCacheMetricsManageTaskResult taskResult = TaskExecutor.executeTaskByNameOnNode(client, + VisorCacheMetricsManageTask.class.getName(), arg, null, clientCfg); + + return processTaskResult(log, taskResult.result()); + } + } + + /** + * @param log Logger. + * @param result Task result. + */ + private String processTaskResult(Logger log, Object result) { + Objects.requireNonNull(result); + + switch (arg.subCommand()) { + case ENABLE: + case DISABLE: + String resultMsg = ((Integer)result) > 0 ? SUCCESS_MESSAGE : NONE_CACHES_PROCECCED_MESSAGE; + + log.info(resultMsg); + + break; + case STATUS: + Map<String, Boolean> statusTaskResult = (Map<String, Boolean>)result; + + if (statusTaskResult.isEmpty()) + log.info(NONE_CACHES_PROCECCED_MESSAGE); + else { + Collection<List<?>> values = F.viewReadOnly(statusTaskResult.entrySet(), + e -> asList(e.getKey(), e.getValue() ? "enabled" : "disabled")); + + SystemViewCommand.printTable(asList("Cache Name", "Status"), asList(STRING, STRING), values, log); + } + + break; + default: + throw new IllegalStateException("Unexpected value: " + arg.subCommand()); + } + + return null; + } + + /** {@inheritDoc} */ + @Override public void printUsage(Logger log) { + String desc = "Manages user cache metrics collection: enables, disables them or shows status."; + + usageCache(log, METRICS_MANAGE, desc, null, or(ENABLE, DISABLE, STATUS), Review Comment: Added extra description: ``` --cache metrics enable|disable|status --caches cache1[,...,cacheN]|--all-caches Manages user cache metrics collection: enables, disables it or shows status. Parameters: --caches cache1[,...,cacheN] - specifies a comma-separated list of cache names to which sub-command should be applied. --all-caches - applies sub-command to all user caches. ``` -- 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]
