Skip to content

Commit

Permalink
IGNITE-22864 Added flag to ignore existing caches to the cache create…
Browse files Browse the repository at this point in the history
… command (#11460)
  • Loading branch information
maksaska authored Aug 5, 2024
1 parent 47a09b3 commit 9000d2e
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 7 deletions.
8 changes: 6 additions & 2 deletions docs/_docs/tools/control-script.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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, ", "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

/** */
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@ protected CacheCreateJob(CacheCreateCommandArg arg, boolean debug) {
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9000d2e

Please sign in to comment.