forked from apache/ignite
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IGNITE-18546 Add cache clear command to control.sh (apache#10577)
- Loading branch information
1 parent
1f23884
commit e220e80
Showing
10 changed files
with
466 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ontrol-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheClear.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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.cache; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.apache.ignite.IgniteLogger; | ||
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; | ||
import org.apache.ignite.internal.processors.cache.ClearCachesTask; | ||
import org.apache.ignite.internal.processors.cache.ClearCachesTaskArg; | ||
import org.apache.ignite.internal.processors.cache.ClearCachesTaskResult; | ||
import org.apache.ignite.internal.util.typedef.F; | ||
import org.apache.ignite.internal.util.typedef.internal.U; | ||
|
||
import static org.apache.ignite.internal.commandline.cache.CacheSubcommands.CLEAR; | ||
|
||
/** Command that clears specified caches. */ | ||
public class CacheClear extends AbstractCommand<ClearCachesTaskArg> { | ||
/** Message that contains cleared caches. */ | ||
public static final String CLEAR_MSG = "The following caches have been cleared: %s"; | ||
|
||
/** Message that contains not-cleared caches (they don't exist). */ | ||
public static final String SKIP_CLEAR_MSG = "The following caches don't exist: %s"; | ||
|
||
/** Comma-separated list of cache names. */ | ||
public static final String CACHES = "--caches"; | ||
|
||
/** Command parsed arguments. */ | ||
private ClearCachesTaskArg arg; | ||
|
||
/** {@inheritDoc} */ | ||
@Override public Object execute(GridClientConfiguration clientCfg, IgniteLogger log) throws Exception { | ||
try (GridClient client = Command.startClient(clientCfg)) { | ||
ClearCachesTaskResult res = TaskExecutor.executeTask(client, ClearCachesTask.class, arg(), clientCfg); | ||
|
||
if (!F.isEmpty(res.clearedCaches())) | ||
U.log(log, String.format(CLEAR_MSG, String.join(", ", res.clearedCaches()))); | ||
|
||
if (!F.isEmpty(res.nonExistentCaches())) | ||
U.warn(log, String.format(SKIP_CLEAR_MSG, String.join(", ", res.nonExistentCaches()))); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public ClearCachesTaskArg arg() { | ||
return arg; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public void printUsage(IgniteLogger logger) { | ||
usageCache( | ||
logger, | ||
CacheSubcommands.CLEAR, | ||
"Clear specified caches.", | ||
F.asMap(CACHES, "specifies a comma-separated list of cache names to be cleared.")); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public void parseArguments(CommandArgIterator argIter) { | ||
String cmdArg = argIter.nextArg("Command arguments are expected."); | ||
|
||
if (cmdArg == null) | ||
throw new IllegalArgumentException("Unknown argument: " + cmdArg); | ||
|
||
if (CACHES.equals(cmdArg)) { | ||
String cacheNamesArg = argIter.nextArg("Expected a comma-separated cache names."); | ||
|
||
List<String> caches = Arrays.stream(cacheNamesArg.split(",")).collect(Collectors.toList()); | ||
|
||
arg = new ClearCachesTaskArg(caches); | ||
} | ||
else | ||
throw new IllegalArgumentException("Unknown argument: " + cmdArg); | ||
|
||
if (argIter.hasNextSubArg()) { | ||
throw new IllegalArgumentException( | ||
"Invalid argument \"" + argIter.peekNextArg() + "\", no more arguments are expected."); | ||
} | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public String name() { | ||
return CLEAR.text().toUpperCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/ClearCachesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.processors.cache; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.ignite.Ignite; | ||
import org.apache.ignite.IgniteCache; | ||
import org.apache.ignite.IgniteException; | ||
import org.apache.ignite.internal.processors.task.GridInternal; | ||
import org.apache.ignite.internal.visor.VisorJob; | ||
import org.apache.ignite.internal.visor.VisorOneNodeTask; | ||
import org.apache.ignite.resources.IgniteInstanceResource; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** Clears specified caches. */ | ||
@GridInternal | ||
public class ClearCachesTask extends VisorOneNodeTask<ClearCachesTaskArg, ClearCachesTaskResult> { | ||
/** */ | ||
private static final long serialVersionUID = 0L; | ||
|
||
/** */ | ||
@Override protected VisorJob<ClearCachesTaskArg, ClearCachesTaskResult> job(ClearCachesTaskArg arg) { | ||
return new ClearCacheJob(arg, debug); | ||
} | ||
|
||
/** Job clears specified caches. */ | ||
private static class ClearCacheJob extends VisorJob<ClearCachesTaskArg, ClearCachesTaskResult> { | ||
/** */ | ||
private static final long serialVersionUID = 0L; | ||
|
||
/** Local Ignite instance. */ | ||
private Ignite ignite; | ||
|
||
/** */ | ||
private ClearCacheJob(ClearCachesTaskArg arg, boolean debug) { | ||
super(arg, debug); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override protected ClearCachesTaskResult run(@Nullable ClearCachesTaskArg arg) throws IgniteException { | ||
List<String> clearedCaches = new ArrayList<>(); | ||
List<String> nonExistentCaches = new ArrayList<>(); | ||
|
||
for (String cache: arg.caches()) { | ||
IgniteCache<?, ?> ignCache = ignite.cache(cache); | ||
|
||
if (ignCache == null) | ||
nonExistentCaches.add(cache); | ||
else { | ||
ignCache.clear(); | ||
|
||
clearedCaches.add(cache); | ||
} | ||
} | ||
|
||
return new ClearCachesTaskResult(clearedCaches, nonExistentCaches); | ||
} | ||
|
||
/** */ | ||
@IgniteInstanceResource | ||
public void setIgnite(Ignite ignite) { | ||
this.ignite = ignite; | ||
} | ||
} | ||
} |
Oops, something went wrong.