This is an automated email from the ASF dual-hosted git repository.

namelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 9000d2e07ef IGNITE-22864 Added flag to ignore existing caches to the 
cache create command (#11460)
9000d2e07ef is described below

commit 9000d2e07efa363d7c5111a78750bb5527f44ed4
Author: Maksim Davydov <[email protected]>
AuthorDate: Mon Aug 5 14:39:56 2024 +0300

    IGNITE-22864 Added flag to ignore existing caches to the cache create 
command (#11460)
---
 docs/_docs/tools/control-script.adoc               |  8 +++--
 .../util/GridCommandHandlerClusterByClassTest.java |  9 ++++--
 .../cache-create-correct-skip-existing-check.xml   | 36 ++++++++++++++++++++++
 .../management/cache/CacheCreateCommand.java       |  7 +++++
 .../management/cache/CacheCreateCommandArg.java    | 16 ++++++++++
 .../internal/management/cache/CacheCreateTask.java | 12 +++++++-
 ...mandHandlerClusterByClassTest_cache_help.output |  3 +-
 ...dlerClusterByClassWithSSLTest_cache_help.output |  3 +-
 8 files changed, 87 insertions(+), 7 deletions(-)

diff --git a/docs/_docs/tools/control-script.adoc 
b/docs/_docs/tools/control-script.adoc
index 808cbdff438..904afb22333 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 99a2b156432..32954a57a8f 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 class GridCommandHandlerClusterByClassTest 
extends GridCommandHandlerClus
                 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 00000000000..ef1d7bd36e3
--- /dev/null
+++ 
b/modules/control-utility/src/test/resources/config/cache/cache-create-correct-skip-existing-check.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+        http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd";>
+    <bean class="org.apache.ignite.configuration.CacheConfiguration">
+        <property name="name" value="cache1"/>
+    </bean>
+    <bean class="org.apache.ignite.configuration.CacheConfiguration">
+        <property name="name" value="cache2"/>
+    </bean>
+    <bean class="org.apache.ignite.configuration.CacheConfiguration">
+        <property name="name" value="cache3"/>
+    </bean>
+    <bean class="org.apache.ignite.configuration.CacheConfiguration">
+        <property name="name" value="cache4"/>
+    </bean>
+</beans>
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 8b5a4af5654..690e6354961 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<CacheCreateCommandArg,
     @Override public Class<CacheCreateTask> taskClass() {
         return CacheCreateTask.class;
     }
+
+    /** {@inheritDoc} */
+    @Override public void printResult(CacheCreateCommandArg arg, Set<String> 
res, Consumer<String> 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 996b3a95618..d77d9be0f77 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 @@ public class CacheCreateCommandArg extends 
IgniteDataTransferObject {
     @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 class CacheCreateCommandArg extends 
IgniteDataTransferObject {
         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 df9831db903..4d2bdfc04fa 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 @@ public class CacheCreateTask extends 
VisorOneNodeTask<CacheCreateCommandArg, Set
                     CacheConfiguration.class.getName() + "' beans.", e);
             }
 
-            Collection<IgniteCache> caches = ignite.createCaches(ccfgs);
+            Collection<IgniteCache> caches;
+
+            if (arg.skipExisting()) {
+                Collection<String> 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 db5136e1668..ff0a15de650 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 db5136e1668..ff0a15de650 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

Reply via email to