Skip to content

Commit

Permalink
IGNITE-19830 Added a description check if the command argument is enu…
Browse files Browse the repository at this point in the history
…m type. (#10818)
  • Loading branch information
NSAmelchev authored Nov 8, 2023
1 parent 91a1eaa commit bc73d09
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import static org.apache.ignite.internal.management.api.CommandUtils.asOptional;
import static org.apache.ignite.internal.management.api.CommandUtils.cmdText;
import static org.apache.ignite.internal.management.api.CommandUtils.executable;
import static org.apache.ignite.internal.management.api.CommandUtils.hasDescription;
import static org.apache.ignite.internal.management.api.CommandUtils.join;
import static org.apache.ignite.internal.management.api.CommandUtils.parameterExample;
import static org.apache.ignite.internal.management.api.CommandUtils.toFormattedCommandName;
Expand Down Expand Up @@ -867,16 +868,15 @@ public static void usage(Command<?, ?> cmd, List<Command<?, ?>> parents, IgniteL
);
};

if (!fld.isAnnotationPresent(EnumDescription.class)) {
logParam.accept(
parameterExample(fld, false),
fld.getAnnotation(Argument.class).description()
);
}
else {
logParam.accept(
parameterExample(fld, false),
fld.getAnnotation(Argument.class).description()
);

if (fld.isAnnotationPresent(EnumDescription.class)) {
EnumDescription enumDesc = fld.getAnnotation(EnumDescription.class);

String[] names = enumDesc.names();
String[] names = formattedEnumNames(fld);
String[] descriptions = enumDesc.descriptions();

for (int i = 0; i < names.length; i++)
Expand Down Expand Up @@ -964,19 +964,29 @@ private static void printExample(Command<?, ?> cmd, List<Command<?, ?>> parents,
logger.info(bldr.toString());
}

/** */
private static String[] formattedEnumNames(Field fld) {
EnumDescription desc = fld.getAnnotation(EnumDescription.class);

String indent = fld.isAnnotationPresent(Positional.class) ? "" : INDENT;

return Arrays.stream(desc.names()).map(s -> indent + s).toArray(String[]::new);
}

/** */
private static class LengthCalculator implements Consumer<Field> {
/** */
int length;

/** {@inheritDoc} */
@Override public void accept(Field fld) {
if (!hasDescription(fld))
return;

length = Math.max(length, parameterExample(fld, false).length());

if (fld.isAnnotationPresent(EnumDescription.class)) {
EnumDescription enumDesc = fld.getAnnotation(EnumDescription.class);

for (String name : enumDesc.names())
for (String name : formattedEnumNames(fld))
length = Math.max(length, name.length());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.internal.commandline;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -27,6 +28,7 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.ignite.IgniteCheckedException;
Expand All @@ -44,11 +46,15 @@
import org.apache.ignite.internal.management.ShutdownPolicyCommandArg;
import org.apache.ignite.internal.management.SystemViewCommand;
import org.apache.ignite.internal.management.WarmUpCommand;
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.BeforeNodeStartCommand;
import org.apache.ignite.internal.management.api.Command;
import org.apache.ignite.internal.management.api.CommandsRegistry;
import org.apache.ignite.internal.management.api.ComputeCommand;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.management.api.HelpCommand;
import org.apache.ignite.internal.management.api.LocalCommand;
import org.apache.ignite.internal.management.api.Positional;
import org.apache.ignite.internal.management.baseline.BaselineAddCommand;
import org.apache.ignite.internal.management.baseline.BaselineAddCommandArg;
import org.apache.ignite.internal.management.baseline.BaselineCommand;
Expand Down Expand Up @@ -87,6 +93,7 @@
import org.junit.rules.TestRule;

import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static org.apache.ignite.IgniteSystemProperties.IGNITE_ENABLE_EXPERIMENTAL_COMMAND;
Expand All @@ -95,6 +102,8 @@
import static org.apache.ignite.internal.commandline.CommandHandler.DFLT_HOST;
import static org.apache.ignite.internal.commandline.CommandHandler.DFLT_PORT;
import static org.apache.ignite.internal.management.api.CommandUtils.cmdText;
import static org.apache.ignite.internal.management.api.CommandUtils.executable;
import static org.apache.ignite.internal.management.api.CommandUtils.visitCommandParams;
import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
import static org.apache.ignite.util.CdcCommandTest.DELETE_LOST_SEGMENT_LINKS;
import static org.apache.ignite.util.GridCommandHandlerIndexingCheckSizeTest.CACHE;
Expand Down Expand Up @@ -1283,6 +1292,56 @@ public void testWarmUpArgs() {
assertNotNull(parseArgs(asList("--warm-up", "--stop")));
}

/** Tests that enum {@link Argument} has enum constants description: {@link EnumDescription}. */
@Test
public void testEnumParameterDescription() {
new IgniteCommandRegistry().commands().forEachRemaining(e -> checkEnumDescription(e.getValue()));
}

/** */
private void checkEnumDescription(Command<?, ?> cmd) {
if (cmd instanceof CommandsRegistry)
((CommandsRegistry<?, ?>)cmd).commands().forEachRemaining(e -> checkEnumDescription(e.getValue()));

if (!executable(cmd))
return;

Consumer<Field> fldCnsmr = fld -> {
if (!fld.getType().isEnum())
return;

EnumDescription descAnn = fld.getAnnotation(EnumDescription.class);

assertNotNull("Please, specify a description to the enum parameter using " +
"@" + EnumDescription.class.getSimpleName() + " annotation. " +
"Parameter: " + cmd.argClass().getSimpleName() + "#" + fld.getName(),
descAnn);

assertEquals("Please, specify a description to enum constants: " +
stream(fld.getType().getEnumConstants())
.filter(e -> stream(descAnn.names()).noneMatch(n -> n.equals(((Enum<?>)e).name())))
.collect(Collectors.toSet()) +
". Parameter: " + cmd.argClass().getSimpleName() + "#" + fld.getName(),
fld.getType().getEnumConstants().length, descAnn.names().length);

Argument argAnn = fld.getAnnotation(Argument.class);
Positional posAnn = fld.getAnnotation(Positional.class);

if (posAnn == null) {
assertFalse("Please, set a description for the argument: " +
cmd.argClass().getSimpleName() + "#" + fld.getName(),
argAnn.description().isEmpty());
}
else {
assertTrue("Please, remove a description for the positional argument: " +
cmd.argClass().getSimpleName() + "#" + fld.getName(),
argAnn.description().isEmpty());
}
};

visitCommandParams(cmd.argClass(), fldCnsmr, fldCnsmr, (grp, flds) -> flds.forEach(fldCnsmr));
}

/**
* @param args Raw arg list.
* @return Common parameters container object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.ignite.ShutdownPolicy;
import org.apache.ignite.internal.dto.IgniteDataTransferObject;
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.management.api.Positional;
import org.apache.ignite.internal.util.typedef.internal.U;

Expand All @@ -34,6 +35,16 @@ public class ShutdownPolicyCommandArg extends IgniteDataTransferObject {
/** */
@Positional
@Argument(optional = true)
@EnumDescription(
names = {
"IMMEDIATE",
"GRACEFUL"
},
descriptions = {
"Stop immediately as soon as all components are ready",
"Node will stop if and only if it does not store any unique partitions, that don't have another copies in the cluster"
}
)
private ShutdownPolicy shutdownPolicy;

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,23 +467,20 @@ public static boolean hasDescribedParameters(Command<?, ?> cmd) {

visitCommandParams(
cmd.argClass(),
fld -> res.compareAndSet(false,
!fld.getAnnotation(Argument.class).description().isEmpty() ||
fld.isAnnotationPresent(EnumDescription.class)
),
fld -> res.compareAndSet(false,
!fld.getAnnotation(Argument.class).description().isEmpty() ||
fld.isAnnotationPresent(EnumDescription.class)
),
(argGrp, flds) -> flds.forEach(fld -> res.compareAndSet(false,
!fld.getAnnotation(Argument.class).description().isEmpty() ||
fld.isAnnotationPresent(EnumDescription.class)
))
fld -> res.compareAndSet(false, hasDescription(fld)),
fld -> res.compareAndSet(false, hasDescription(fld)),
(argGrp, flds) -> flds.forEach(fld -> res.compareAndSet(false, hasDescription(fld)))
);

return res.get();
}

/** @return {@code True} if argument has description. */
public static boolean hasDescription(Field fld) {
return !fld.getAnnotation(Argument.class).description().isEmpty() ||
fld.isAnnotationPresent(EnumDescription.class);
}

/**
* @param nodes Nodes.
* @return Coordinator ID or null is {@code nodes} are empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.ArgumentGroup;
import org.apache.ignite.internal.management.api.CliConfirmArgument;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.management.api.Positional;
import org.apache.ignite.internal.management.baseline.BaselineCommand.BaselineTaskArg;
import org.apache.ignite.internal.util.typedef.internal.U;
Expand All @@ -37,6 +38,16 @@ public class BaselineAutoAdjustCommandArg extends BaselineTaskArg {
/** */
@Positional
@Argument(optional = true)
@EnumDescription(
names = {
"ENABLE",
"DISABLE"
},
descriptions = {
"Enable baseline auto adjust",
"Disable baseline auto adjust"
}
)
private Enabled enabled;

/** */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.ignite.IgniteException;
import org.apache.ignite.internal.dto.IgniteDataTransferObject;
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.management.api.Positional;
import org.apache.ignite.internal.util.typedef.internal.U;

Expand Down Expand Up @@ -54,7 +55,25 @@ public class CacheIdleVerifyCommandArg extends IgniteDataTransferObject {
private String[] excludeCaches;

/** */
@Argument(optional = true)
@Argument(optional = true, description = "Type of cache(s)")
@EnumDescription(
names = {
"DEFAULT",
"SYSTEM",
"PERSISTENT",
"NOT_PERSISTENT",
"USER",
"ALL"
},
descriptions = {
"Default - user only, or all caches specified by name",
"System",
"Persistent",
"Not persistent",
"User",
"All"
}
)
private CacheFilterEnum cacheFilter = CacheFilterEnum.DEFAULT;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.ignite.internal.dto.IgniteDataTransferObject;
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.ArgumentGroup;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.management.api.Positional;
import org.apache.ignite.internal.util.typedef.internal.U;

Expand All @@ -35,6 +36,18 @@ public class CacheMetricsCommandArg extends IgniteDataTransferObject {
/** */
@Positional
@Argument
@EnumDescription(
names = {
"ENABLE",
"DISABLE",
"STATUS"
},
descriptions = {
"Enable metrics",
"Disable metrics",
"Status"
}
)
private CacheMetricsOperation operation;

/** */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.ignite.cache.ReadRepairStrategy;
import org.apache.ignite.internal.dto.IgniteDataTransferObject;
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.util.typedef.internal.U;

/** */
Expand All @@ -40,6 +41,22 @@ public class ConsistencyRepairCommandArg extends IgniteDataTransferObject {

/** Strategy. */
@Argument(description = "Repair strategy")
@EnumDescription(
names = {
"LWW",
"PRIMARY",
"RELATIVE_MAJORITY",
"REMOVE",
"CHECK_ONLY"
},
descriptions = {
"Last write (the newest entry) wins",
"Value from the primary node wins",
"The relative majority, any value found more times than any other wins",
"Inconsistent entries will be removed",
"Only check will be performed"
}
)
ReadRepairStrategy strategy;

/** */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.apache.ignite.internal.management.api.Argument;
import org.apache.ignite.internal.management.api.EnumDescription;
import org.apache.ignite.internal.management.tracing.TracingConfigurationCommand.TracingConfigurationCommandArg;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.tracing.Scope;
Expand All @@ -31,7 +32,23 @@ public class TracingConfigurationGetAllCommandArg extends TracingConfigurationCo
private static final long serialVersionUID = 0;

/** */
@Argument(optional = true)
@Argument(optional = true, description = "Tracing span scope")
@EnumDescription(
names = {
"DISCOVERY",
"EXCHANGE",
"COMMUNICATION",
"TX",
"SQL"
},
descriptions = {
"Discovery scope",
"Exchange scope",
"Communication scope",
"Transactional scope",
"SQL scope"
}
)
private Scope scope;

/** {@inheritDoc} */
Expand Down
Loading

0 comments on commit bc73d09

Please sign in to comment.