Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

import java.util.logging.Logger;

import org.apache.ignite.internal.client.GridClientConfiguration;

/**
* {@inheritDoc}
*/
public abstract class AbstractCommand<T> implements Command<T> {

/** Use verbose mode or not. */
protected boolean verbose;

/**
* {@inheritDoc}
*/
@Override public Object execute(GridClientConfiguration clientCfg, Logger log, boolean verbose) throws Exception {
this.verbose = verbose;
return this.execute(clientCfg, log);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @deprecated Use {@link ClusterStateChangeCommand} instead.
*/
@Deprecated
public class ActivateCommand implements Command<Void> {
public class ActivateCommand extends AbstractCommand<Void> {
/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
Command.usage(logger, "Activate cluster (deprecated. Use " + SET_STATE.toString() + " instead):", ACTIVATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
package org.apache.ignite.internal.commandline;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.ignite.internal.client.GridClient;
import org.apache.ignite.internal.client.GridClientConfiguration;
import org.apache.ignite.internal.client.GridClientNode;
Expand All @@ -37,8 +43,10 @@
import org.apache.ignite.internal.visor.baseline.VisorBaselineTask;
import org.apache.ignite.internal.visor.baseline.VisorBaselineTaskArg;
import org.apache.ignite.internal.visor.baseline.VisorBaselineTaskResult;
import org.apache.ignite.internal.visor.util.VisorTaskUtils;

import static java.lang.Boolean.TRUE;
import static java.util.Collections.singletonMap;
import static org.apache.ignite.internal.commandline.CommandHandler.DELIM;
import static org.apache.ignite.internal.commandline.CommandList.BASELINE;
import static org.apache.ignite.internal.commandline.CommandLogger.DOUBLE_INDENT;
Expand All @@ -50,15 +58,16 @@
/**
* Commands associated with baseline functionality.
*/
public class BaselineCommand implements Command<BaselineArguments> {
public class BaselineCommand extends AbstractCommand<BaselineArguments> {
/** Arguments. */
private BaselineArguments baselineArgs;

/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
final String constistIds = "consistentId1[,consistentId2,....,consistentIdN]";

Command.usage(logger, "Print cluster baseline topology:", BASELINE);
Command.usage(logger, "Print cluster baseline topology:", BASELINE,
singletonMap("verbose", "Show the full list of node ips."), optional("--verbose"));
Command.usage(logger, "Add nodes into baseline topology:", BASELINE, BaselineSubcommands.ADD.text(),
constistIds, optional(CMD_AUTO_CONFIRMATION));
Command.usage(logger, "Remove nodes from baseline topology:", BASELINE, BaselineSubcommands.REMOVE.text(),
Expand Down Expand Up @@ -166,13 +175,38 @@ else if (res.getRemainingTimeToBaselineAdjust() < 0)
Map<String, VisorBaselineNode> srvs = res.getServers();

// if task runs on a node with VisorBaselineNode of old version (V1) we'll get order=null for all nodes.
Function<VisorBaselineNode, String> extractFormattedAddrs = node -> {
Stream<String> sortedByIpHosts =
Optional.ofNullable(node)
.map(addrs -> node.getAddrs())
.orElse(Collections.emptyList())
.stream()
.sorted(Comparator
.comparing(resolvedAddr -> new VisorTaskUtils.SortableAddress(resolvedAddr.address())))
.map(resolvedAddr -> {
if (!resolvedAddr.hostname().equals(resolvedAddr.address()))
return resolvedAddr.hostname() + "/" + resolvedAddr.address();
else
return resolvedAddr.address();
});
if (verbose) {
String hosts = String.join(",", sortedByIpHosts.collect(Collectors.toList()));

if (!hosts.isEmpty())
return ", Addresses=" + hosts;
else
return "";
} else
return sortedByIpHosts.findFirst().map(ip -> ", Address=" + ip).orElse("");
};

String crdStr = srvs.values().stream()
// check for not null
.filter(node -> node.getOrder() != null)
.min(Comparator.comparing(VisorBaselineNode::getOrder))
// format
.map(crd -> " (Coordinator: ConsistentId=" + crd.getConsistentId() + ", Order=" + crd.getOrder() + ")")
.map(crd -> " (Coordinator: ConsistentId=" + crd.getConsistentId() + extractFormattedAddrs.apply(crd) +
", Order=" + crd.getOrder() + ")")
.orElse("");

logger.info("Current topology version: " + res.getTopologyVersion() + crdStr);
Expand All @@ -190,7 +224,8 @@ else if (res.getRemainingTimeToBaselineAdjust() < 0)

String order = srvNode != null ? ", Order=" + srvNode.getOrder() : "";

logger.info(DOUBLE_INDENT + "ConsistentId=" + node.getConsistentId() + state + order);
logger.info(DOUBLE_INDENT + "ConsistentId=" + node.getConsistentId() +
extractFormattedAddrs.apply(srvNode) + state + order);
}

logger.info(DELIM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Command to access cluster ID and tag functionality.
*/
public class ClusterChangeTagCommand implements Command<String> {
public class ClusterChangeTagCommand extends AbstractCommand<String> {
/** */
private static final String ERR_NO_NEW_TAG_PROVIDED = "Please provide new tag.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Command to change cluster state.
*/
public class ClusterStateChangeCommand implements Command<ClusterState> {
public class ClusterStateChangeCommand extends AbstractCommand<ClusterState> {
/** Flag of forced cluster deactivation. */
static final String FORCE_COMMAND = "--force";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ public static String extendToLen(String s, int targetLen) {
*/
public Object execute(GridClientConfiguration clientCfg, Logger logger) throws Exception;

/**
* Actual command execution with verbose mode if needed.
* Implement it if your command supports verbose mode.
*
* @see Command#execute(GridClientConfiguration, Logger)
*
* @param clientCfg Thin client configuration if connection to cluster is necessary.
* @param logger Logger to use.
* @param verbose Use verbose mode or not
* @return Result of operation (mostly usable for tests).
* @throws Exception If error occur.
*/
default Object execute(GridClientConfiguration clientCfg, Logger logger, boolean verbose) throws Exception {
return execute(clientCfg, logger);
}

/**
* Prepares confirmation for the command.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public int execute(List<String> rawArgs) {
logger.info("Arguments: " + String.join(" ", rawArgs));
logger.info(DELIM);

lastOperationRes = command.execute(clientCfg, logger);
lastOperationRes = command.execute(clientCfg, logger, args.verbose());

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @deprecated Use {@link ClusterStateChangeCommand} instead.
*/
@Deprecated
public class DeactivateCommand implements Command<Void> {
public class DeactivateCommand extends AbstractCommand<Void> {
/** Cluster name. */
private String clusterName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* Command for change or display policy for shutdown.
*/
public class ShutdownPolicyCommand implements Command<ShutdownPolicyArgument> {
public class ShutdownPolicyCommand extends AbstractCommand<ShutdownPolicyArgument> {
/** Arguments. */
private ShutdownPolicyArgument shutdownPolicyArgument;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* Command to print cluster state.
*/
public class StateCommand implements Command<Void> {
public class StateCommand extends AbstractCommand<Void> {
/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
Command.usage(logger, "Print current cluster state:", STATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
/**
* Commands associated with tracing configuration functionality.
*/
public class TracingConfigurationCommand implements Command<TracingConfigurationArguments> {
public class TracingConfigurationCommand extends AbstractCommand<TracingConfigurationArguments> {
/** Arguments. */
private TracingConfigurationArguments args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
/**
* Transaction commands.
*/
public class TxCommands implements Command<VisorTxTaskArg> {
public class TxCommands extends AbstractCommand<VisorTxTaskArg> {
/** Arguments */
private VisorTxTaskArg args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/**
* Wal commands.
*/
public class WalCommands implements Command<T2<String, String>> {
public class WalCommands extends AbstractCommand<T2<String, String>> {
/** */
static final String WAL_PRINT = "print";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* Command for interacting with warm-up.
*/
public class WarmUpCommand implements Command<Void> {
public class WarmUpCommand extends AbstractCommand<Void> {
/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
Command.usage(logger, "Stop warm-up:", WARM_UP, WarmUpCommandArg.STOP.argName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.logging.Logger;
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.CommandLogger;
Expand All @@ -42,7 +43,7 @@
/**
* High-level "cache" command implementation.
*/
public class CacheCommands implements Command<CacheSubcommands> {
public class CacheCommands extends AbstractCommand<CacheSubcommands> {
/** Empty group name. */
public static final String EMPTY_GROUP_NAME = "no_group";

Expand Down Expand Up @@ -75,7 +76,7 @@ public class CacheCommands implements Command<CacheSubcommands> {
if (command == null)
throw new IllegalStateException("Unknown command " + subcommand);

return command.execute(clientCfg, logger);
return command.execute(clientCfg, logger, verbose);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.CommandLogger;
Expand All @@ -39,7 +40,7 @@
/**
* Cache contention detection subcommand.
*/
public class CacheContention implements Command<CacheContention.Arguments> {
public class CacheContention extends AbstractCommand<CacheContention.Arguments> {
/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
String description = "Show the keys that are point of contention for multiple transactions.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.CommandHandler;
Expand All @@ -46,7 +47,7 @@
/**
* Would collect and print info about how data is spread between nodes and partitions.
*/
public class CacheDistribution implements Command<CacheDistribution.Arguments> {
public class CacheDistribution extends AbstractCommand<CacheDistribution.Arguments> {
/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
String CACHES = "cacheName1,...,cacheNameN";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand All @@ -50,7 +51,7 @@
/**
* Cache subcommand that triggers indexes force rebuild.
*/
public class CacheIndexesForceRebuild implements Command<CacheIndexesForceRebuild.Arguments> {
public class CacheIndexesForceRebuild extends AbstractCommand<CacheIndexesForceRebuild.Arguments> {
/** Command parsed arguments. */
private Arguments args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.regex.PatternSyntaxException;
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;
Expand All @@ -47,7 +48,7 @@
/**
* Cache subcommand that allows to show indexes.
*/
public class CacheIndexesList implements Command<CacheIndexesList.Arguments> {
public class CacheIndexesList extends AbstractCommand<CacheIndexesList.Arguments> {
/** Command parsed arguments. */
private Arguments args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -41,7 +42,7 @@
/**
* Cache subcommand that allows to show caches that have
*/
public class CacheIndexesRebuildStatus implements Command<CacheIndexesRebuildStatus.Arguments> {
public class CacheIndexesRebuildStatus extends AbstractCommand<CacheIndexesRebuildStatus.Arguments> {
/** Command parsed arguments. */
private Arguments args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.CommandLogger;
Expand Down Expand Up @@ -62,7 +63,7 @@
/**
* Validate indexes command.
*/
public class CacheValidateIndexes implements Command<CacheValidateIndexes.Arguments> {
public class CacheValidateIndexes extends AbstractCommand<CacheValidateIndexes.Arguments> {
/** {@inheritDoc} */
@Override public void printUsage(Logger logger) {
String CACHES = "cacheName1,...,cacheNameN";
Expand Down
Loading