diff --git a/docs/_docs/tools/control-script.adoc b/docs/_docs/tools/control-script.adoc index 808cbdff438f8..904afb22333d0 100644 --- a/docs/_docs/tools/control-script.adoc +++ b/docs/_docs/tools/control-script.adoc @@ -430,7 +430,7 @@ NOTE: The 'ignite-spring' module should be enabled. [source, shell] ---- -control.sh|bat --cache create --springXmlConfig springXmlFilePath +control.sh|bat --cache create --springXmlConfig springXmlFilePath --skip-existing ---- Parameters: @@ -440,13 +440,17 @@ Parameters: | Parameter | Description | `--springXmlConfig springXmlConfigPath` | Path to the Spring XML configuration that contains 'org.apache.ignite.configuration.CacheConfiguration' beans to create caches from. +|`--skip-existing`| Optional flag to skip existing caches. |=== Examples: [source, shell] ---- # Create caches from the `/ignite/config/userCaches.xml` configuration. -control.sh|bat --cache create --springXmlConfig /ignite/config/userCaches.xml` +control.sh|bat --cache create --springXmlConfig /ignite/config/userCaches.xml + +# Create caches from the `/ignite/config/userCaches.xml` configuration except existing ones. +control.sh|bat --cache create --springXmlConfig /ignite/config/userCaches.xml --skip-existing ---- == Destroying Caches diff --git a/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java b/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java index 99a2b15643204..32954a57a8fa1 100644 --- a/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java +++ b/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java @@ -1378,11 +1378,16 @@ public void testCacheCreate() { SPRING_XML_CONFIG, cfgPath + "/unknown.xml"), "Failed to create caches. Spring XML configuration file not found"); - assertEquals(CommandHandler.EXIT_CODE_OK, execute("--cache", CREATE, SPRING_XML_CONFIG, - cfgPath + "/cache-create-correct.xml")); + assertContains(log, executeCommand(EXIT_CODE_OK, "--cache", CREATE, SPRING_XML_CONFIG, + cfgPath + "/cache-create-correct.xml"), "Created caches: cache1, cache2"); assertTrue(crd.cacheNames().containsAll(F.asList("cache1", "cache2"))); + assertContains(log, executeCommand(EXIT_CODE_OK, "--cache", CREATE, SPRING_XML_CONFIG, cfgPath + + "/cache-create-correct-skip-existing-check.xml", "--skip-existing"), "Created caches: cache3, cache4"); + + assertTrue(crd.cacheNames().containsAll(F.asList("cache1", "cache2", "cache3", "cache4"))); + int expSize = G.allGrids().size(); String out = executeCommand(EXIT_CODE_UNEXPECTED_ERROR, "--cache", CREATE, diff --git a/modules/control-utility/src/test/resources/config/cache/cache-create-correct-skip-existing-check.xml b/modules/control-utility/src/test/resources/config/cache/cache-create-correct-skip-existing-check.xml new file mode 100644 index 0000000000000..ef1d7bd36e3ca --- /dev/null +++ b/modules/control-utility/src/test/resources/config/cache/cache-create-correct-skip-existing-check.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + diff --git a/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommand.java b/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommand.java index 8b5a4af56541e..690e6354961b2 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommand.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommand.java @@ -18,7 +18,9 @@ package org.apache.ignite.internal.management.cache; import java.util.Set; +import java.util.function.Consumer; import org.apache.ignite.internal.management.api.ComputeCommand; +import org.apache.ignite.internal.util.typedef.F; import static org.apache.ignite.internal.IgniteComponentType.SPRING; @@ -39,4 +41,9 @@ public class CacheCreateCommand implements ComputeCommand taskClass() { return CacheCreateTask.class; } + + /** {@inheritDoc} */ + @Override public void printResult(CacheCreateCommandArg arg, Set res, Consumer printer) { + printer.accept(res.isEmpty() ? "No cache was created" : "Created caches: " + F.concat(res, ", ")); + } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommandArg.java b/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommandArg.java index 996b3a956189f..d77d9be0f7730 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommandArg.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateCommandArg.java @@ -36,6 +36,10 @@ public class CacheCreateCommandArg extends IgniteDataTransferObject { "'org.apache.ignite.configuration.CacheConfiguration' beans to create caches from", example = "springXmlConfigPath") private String springxmlconfig; + /** */ + @Argument(description = "Optional flag to skip existing caches", optional = true) + private boolean skipExisting; + /** */ private String fileContent; @@ -59,12 +63,14 @@ private void readFile() { @Override protected void writeExternalData(ObjectOutput out) throws IOException { U.writeString(out, springxmlconfig); U.writeString(out, fileContent); + out.writeBoolean(skipExisting); } /** {@inheritDoc} */ @Override protected void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException { springxmlconfig = U.readString(in); fileContent = U.readString(in); + skipExisting = in.readBoolean(); } /** */ @@ -78,6 +84,16 @@ public void springxmlconfig(String springxmlconfig) { readFile(); } + /** */ + public boolean skipExisting() { + return skipExisting; + } + + /** */ + public void skipExisting(boolean skipExisting) { + this.skipExisting = skipExisting; + } + /** */ public void fileContent(String fileContent) { this.fileContent = fileContent; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateTask.java b/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateTask.java index df9831db903b1..4d2bdfc04fab8 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateTask.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/management/cache/CacheCreateTask.java @@ -91,7 +91,17 @@ protected CacheCreateJob(CacheCreateCommandArg arg, boolean debug) { CacheConfiguration.class.getName() + "' beans.", e); } - Collection caches = ignite.createCaches(ccfgs); + Collection caches; + + if (arg.skipExisting()) { + Collection existingCacheNames = ignite.cacheNames(); + + ccfgs.removeIf(ccfg -> ccfg.getName() != null && existingCacheNames.contains(ccfg.getName())); + + caches = ignite.getOrCreateCaches(ccfgs); + } + else + caches = ignite.createCaches(ccfgs); return caches.stream().map(Cache::getName).collect(Collectors.toCollection(TreeSet::new)); } diff --git a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output index db5136e16682c..ff0a15de65086 100644 --- a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output +++ b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output @@ -53,10 +53,11 @@ Arguments: --cache help --yes --seq - print information about sequences. Create caches from Spring XML configuration. Note that the 'ignite-spring' module should be enabled: - control.(sh|bat) --cache create --springxmlconfig springXmlConfigPath + control.(sh|bat) --cache create --springxmlconfig springXmlConfigPath [--skip-existing] Parameters: --springxmlconfig springXmlConfigPath - Path to the Spring XML configuration that contains 'org.apache.ignite.configuration.CacheConfiguration' beans to create caches from. + --skip-existing - Optional flag to skip existing caches. Permanently destroy specified caches: control.(sh|bat) --cache destroy --caches cache1,...,cacheN|--destroy-all-caches diff --git a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output index db5136e16682c..ff0a15de65086 100644 --- a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output +++ b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output @@ -53,10 +53,11 @@ Arguments: --cache help --yes --seq - print information about sequences. Create caches from Spring XML configuration. Note that the 'ignite-spring' module should be enabled: - control.(sh|bat) --cache create --springxmlconfig springXmlConfigPath + control.(sh|bat) --cache create --springxmlconfig springXmlConfigPath [--skip-existing] Parameters: --springxmlconfig springXmlConfigPath - Path to the Spring XML configuration that contains 'org.apache.ignite.configuration.CacheConfiguration' beans to create caches from. + --skip-existing - Optional flag to skip existing caches. Permanently destroy specified caches: control.(sh|bat) --cache destroy --caches cache1,...,cacheN|--destroy-all-caches