-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GG-39034 [IGNITE-18546] Add cache clear command to control.sh #3139
base: master
Are you sure you want to change the base?
Changes from all commits
be77910
1f9ea3a
ad5b9ef
029d2eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2024 GridGain Systems, Inc. and Contributors. | ||
* | ||
* Licensed under the GridGain Community Edition License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license | ||
* | ||
* 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.logging.Logger; | ||
import java.util.stream.Collectors; | ||
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 static org.apache.ignite.internal.commandline.cache.CacheCommands.usageCache; | ||
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"; | ||
|
||
/** Confirmation message format. */ | ||
public static final String CONFIRM_MSG = "Warning! The command will clear all data from %d caches: %s.\n" + | ||
"If you continue, it will be impossible to recover cleared data."; | ||
|
||
/** 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, Logger logger) throws Exception { | ||
try (GridClient client = Command.startClient(clientCfg)) { | ||
ClearCachesTaskResult res = TaskExecutor.executeTask(client, ClearCachesTask.class, arg(), clientCfg); | ||
|
||
if (!F.isEmpty(res.clearedCaches())) | ||
logger.info(String.format(CLEAR_MSG, String.join(", ", res.clearedCaches()))); | ||
|
||
if (!F.isEmpty(res.nonExistentCaches())) | ||
logger.warning(String.format(SKIP_CLEAR_MSG, String.join(", ", res.nonExistentCaches()))); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public String confirmationPrompt() { | ||
return String.format(CONFIRM_MSG, arg.caches().size(), String.join(", ", arg.caches())); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public ClearCachesTaskArg arg() { | ||
return arg; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override public void printUsage(Logger 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2024 GridGain Systems, Inc. and Contributors. | ||
* | ||
* Licensed under the GridGain Community Edition License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license | ||
* | ||
* 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This operation may take a long time. |
||
|
||
clearedCaches.add(cache); | ||
} | ||
} | ||
|
||
return new ClearCachesTaskResult(clearedCaches, nonExistentCaches); | ||
} | ||
|
||
/** */ | ||
@IgniteInstanceResource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can annotate the field directly.
Anyway, it's up to you. |
||
public void setIgnite(Ignite ignite) { | ||
this.ignite = ignite; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check that GridCommandHandlerPasscodeAuthenticatorClusterByClassTest passed too.
It requires updating GridEntSecurityProcessor