Skip to content

Commit

Permalink
make Command.create() return existing instance for known commands
Browse files Browse the repository at this point in the history
When the `Command.create()` method is used to create a `Command` instance
for a known command (not recommended, but also not forbidden), it used to
return a generic instance that doesn't know anything about where the keys
are in the command. Such generic instance is unusable with Redis cluster,
because the target node will be selected randomly, not based on the key,
and there's high chance such command will result in the `MOVED` redirect.

With this commit, `Command.create()` will return a pre-existing static
instance for known commands, which is key-aware and works with Redis cluster.
  • Loading branch information
Ladicek authored and vietj committed Nov 13, 2023
1 parent 5e5c2a2 commit cdffc70
Show file tree
Hide file tree
Showing 4 changed files with 482 additions and 10 deletions.
33 changes: 26 additions & 7 deletions src/main/java/io/vertx/redis/client/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* <b>Auto generated</b> API Commands to interact with REDIS.
*
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
* @version redis_version:7.0.0
* @version redis_version:7.0.12
*/
@VertxGen
public interface Command {
Expand All @@ -34,6 +34,7 @@ public interface Command {
Command ASKING = new CommandImpl("asking", 1, null, false, false);
Command AUTH = new CommandImpl("auth", -2, null, false, false);
Command BF_ADD = new CommandImpl("bf.add", -1, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command BF_CARD = new CommandImpl("bf.card", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command BF_DEBUG = new CommandImpl("bf.debug", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command BF_EXISTS = new CommandImpl("bf.exists", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command BF_INFO = new CommandImpl("bf.info", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Expand Down Expand Up @@ -93,16 +94,16 @@ public interface Command {
Command ECHO = new CommandImpl("echo", 2, null, false, false);
Command EVAL = new CommandImpl("eval", -3, null, false, false, new KeyLocator(false, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command EVALSHA = new CommandImpl("evalsha", -3, null, false, false, new KeyLocator(false, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command EVALSHA_RO = new CommandImpl("evalsha_ro", -3, null, false, false, new KeyLocator(true, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command EVAL_RO = new CommandImpl("eval_ro", -3, null, false, false, new KeyLocator(true, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command EVALSHA_RO = new CommandImpl("evalsha_ro", -3, true, false, false, new KeyLocator(true, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command EVAL_RO = new CommandImpl("eval_ro", -3, true, false, false, new KeyLocator(true, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command EXEC = new CommandImpl("exec", 1, null, false, false);
Command EXISTS = new CommandImpl("exists", -2, true, false, false, new KeyLocator(true, new BeginSearchIndex(1), new FindKeysRange(-1, 1, 0)));
Command EXPIRE = new CommandImpl("expire", -3, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command EXPIREAT = new CommandImpl("expireat", -3, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command EXPIRETIME = new CommandImpl("expiretime", 2, true, false, false, new KeyLocator(true, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command FAILOVER = new CommandImpl("failover", -1, null, false, false);
Command FCALL = new CommandImpl("fcall", -3, null, false, false, new KeyLocator(false, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command FCALL_RO = new CommandImpl("fcall_ro", -3, null, false, false, new KeyLocator(true, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command FCALL_RO = new CommandImpl("fcall_ro", -3, true, false, false, new KeyLocator(true, new BeginSearchIndex(2), new FindKeysKeynum(0, 1, 1)));
Command FLUSHALL = new CommandImpl("flushall", -1, false, false, false);
Command FLUSHDB = new CommandImpl("flushdb", -1, false, false, false);
Command FT_ADD = new CommandImpl("FT.ADD", -1, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(1, 1, 0)));
Expand Down Expand Up @@ -250,7 +251,7 @@ public interface Command {
Command PEXPIRETIME = new CommandImpl("pexpiretime", 2, true, false, false, new KeyLocator(true, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command PFADD = new CommandImpl("pfadd", -2, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command PFCOUNT = new CommandImpl("pfcount", -2, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(-1, 1, 0)));
Command PFDEBUG = new CommandImpl("pfdebug", -3, false, false, false, new KeyLocator(false, new BeginSearchIndex(2), new FindKeysRange(0, 1, 0)));
Command PFDEBUG = new CommandImpl("pfdebug", 3, false, false, false, new KeyLocator(false, new BeginSearchIndex(2), new FindKeysRange(0, 1, 0)));
Command PFMERGE = new CommandImpl("pfmerge", -2, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)), new KeyLocator(true, new BeginSearchIndex(2), new FindKeysRange(-1, 1, 0)));
Command PFSELFTEST = new CommandImpl("pfselftest", 1, null, false, false);
Command PING = new CommandImpl("ping", -1, null, false, false);
Expand Down Expand Up @@ -316,6 +317,20 @@ public interface Command {
Command SUNSUBSCRIBE = new CommandImpl("sunsubscribe", -1, null, true, false, new KeyLocator(null, new BeginSearchIndex(1), new FindKeysRange(-1, 1, 0)));
Command SWAPDB = new CommandImpl("swapdb", 3, false, false, false);
Command SYNC = new CommandImpl("sync", 1, null, false, false);
Command TDIGEST_ADD = new CommandImpl("tdigest.add", -1, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_BYRANK = new CommandImpl("tdigest.byrank", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_BYREVRANK = new CommandImpl("tdigest.byrevrank", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_CDF = new CommandImpl("tdigest.cdf", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_CREATE = new CommandImpl("tdigest.create", -1, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_INFO = new CommandImpl("tdigest.info", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_MAX = new CommandImpl("tdigest.max", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_MERGE = new CommandImpl("tdigest.merge", -1, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_MIN = new CommandImpl("tdigest.min", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_QUANTILE = new CommandImpl("tdigest.quantile", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_RANK = new CommandImpl("tdigest.rank", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_RESET = new CommandImpl("tdigest.reset", -1, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_REVRANK = new CommandImpl("tdigest.revrank", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TDIGEST_TRIMMED_MEAN = new CommandImpl("tdigest.trimmed_mean", -1, true, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command TIME = new CommandImpl("time", 1, null, false, false);
Command TIMESERIES_CLUSTERSET = new CommandImpl("timeseries.CLUSTERSET", -1, true, false, false);
Command TIMESERIES_CLUSTERSETFROMSHARD = new CommandImpl("timeseries.CLUSTERSETFROMSHARD", -1, true, false, false);
Expand Down Expand Up @@ -355,7 +370,7 @@ public interface Command {
Command UNSUBSCRIBE = new CommandImpl("unsubscribe", -1, null, true, false);
Command UNWATCH = new CommandImpl("unwatch", 1, null, false, false);
Command WAIT = new CommandImpl("wait", 3, null, false, false);
Command WATCH = new CommandImpl("watch", -2, null, false, false, new KeyLocator(null, new BeginSearchIndex(1), new FindKeysRange(-1, 1, 0)));
Command WATCH = new CommandImpl("watch", -2, null, false, false, new KeyLocator(true, new BeginSearchIndex(1), new FindKeysRange(-1, 1, 0)));
Command XACK = new CommandImpl("xack", -4, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command XADD = new CommandImpl("xadd", -5, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Command XAUTOCLAIM = new CommandImpl("xautoclaim", -6, false, false, false, new KeyLocator(false, new BeginSearchIndex(1), new FindKeysRange(0, 1, 0)));
Expand Down Expand Up @@ -409,11 +424,15 @@ public interface Command {

/**
* Generic command generator for extensions.
* <p>
* To avoid inconsistent behavior, when {@code command} is one of the known commands
* for which a static instance exists, the static instance is returned.
*
* @param command command name
* @return the cacheable immutable command instance
*/
static Command create(String command) {
return new CommandImpl(command, -1, null, false, true);
Command known = CommandMap.getKnownCommand(command);
return known != null ? known : new CommandImpl(command, -1, null, false, true);
}
}
Loading

0 comments on commit cdffc70

Please sign in to comment.