forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yury-Fridlyand <[email protected]>
- Loading branch information
1 parent
eec4bc3
commit b7f9bbd
Showing
16 changed files
with
277 additions
and
395 deletions.
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
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 |
---|---|---|
@@ -1,50 +1,58 @@ | ||
package glide.api; | ||
|
||
import static glide.api.RedisClient.buildChannelHandler; | ||
import static glide.api.RedisClient.buildCommandManager; | ||
import static glide.api.RedisClient.buildConnectionManager; | ||
|
||
import glide.api.commands.ClusterBaseCommands; | ||
import glide.api.commands.Command; | ||
import glide.api.models.RedisValue; | ||
import glide.api.models.configuration.RedisClusterClientConfiguration; | ||
import glide.api.models.configuration.Route; | ||
import glide.connectors.handlers.ChannelHandler; | ||
import glide.managers.ClusterCommandManager; | ||
import glide.managers.CommandManager; | ||
import glide.managers.ConnectionManager; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Async (non-blocking) client for Redis in Cluster mode. Use {@link #CreateClient} to request a | ||
* client to Redis. | ||
*/ | ||
public class ClusterClient extends BaseClient<RedisValue> { | ||
public class ClusterClient extends BaseClient<RedisValue> | ||
implements ClusterBaseCommands<RedisValue> { | ||
|
||
protected ClusterClient( | ||
ConnectionManager connectionManager, ClusterCommandManager<RedisValue> commandManager) { | ||
super(connectionManager, commandManager); | ||
} | ||
protected ClusterClient(ConnectionManager connectionManager, CommandManager commandManager) { | ||
super(connectionManager, commandManager, RedisValue::of); | ||
} | ||
|
||
/** | ||
* Request an async (non-blocking) Redis client in Cluster mode. | ||
* | ||
* @param config - Redis Client Configuration | ||
* @return a Future to connect and return a RedisClient | ||
*/ | ||
public static CompletableFuture<ClusterClient> CreateClient( | ||
RedisClusterClientConfiguration config) { | ||
ChannelHandler channelHandler = buildChannelHandler(); | ||
ConnectionManager connectionManager = buildConnectionManager(channelHandler); | ||
ClusterCommandManager<RedisValue> commandManager = buildClusterCommandManager(channelHandler); | ||
// TODO: Support exception throwing, including interrupted exceptions | ||
return connectionManager | ||
.connectToRedis(config) | ||
.thenApplyAsync(ignored -> new ClusterClient(connectionManager, commandManager)); | ||
} | ||
/** | ||
* Async request for an async (non-blocking) Redis client in Cluster mode. | ||
* | ||
* @param config Redis cluster client Configuration | ||
* @return a Future to connect and return a ClusterClient | ||
*/ | ||
public static CompletableFuture<ClusterClient> CreateClient( | ||
RedisClusterClientConfiguration config) { | ||
ChannelHandler channelHandler = buildChannelHandler(); | ||
ConnectionManager connectionManager = buildConnectionManager(channelHandler); | ||
CommandManager commandManager = buildCommandManager(channelHandler); | ||
// TODO: Support exception throwing, including interrupted exceptions | ||
return connectionManager | ||
.connectToRedis(config) | ||
.thenApply(ignored -> new ClusterClient(connectionManager, commandManager)); | ||
} | ||
|
||
protected static ClusterCommandManager<RedisValue> buildClusterCommandManager( | ||
ChannelHandler channelHandler) { | ||
return new ClusterCommandManager<>(channelHandler, RedisValue::of); | ||
} | ||
@Override | ||
public CompletableFuture<RedisValue> customCommand(String[] args) { | ||
Command command = | ||
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build(); | ||
return commandManager.submitNewCommand(command, this::handleObjectResponse); | ||
} | ||
|
||
@Override | ||
public ClusterCommandManager<RedisValue> getCommandManager() { | ||
return (ClusterCommandManager<RedisValue>) commandManager; | ||
} | ||
@Override | ||
public CompletableFuture<RedisValue> customCommand(String[] args, Route route) { | ||
Command command = | ||
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build(); | ||
return commandManager.submitNewCommand(command, this::handleObjectResponse, route); | ||
} | ||
} |
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
33 changes: 11 additions & 22 deletions
33
java/client/src/main/java/glide/api/commands/BaseCommands.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 |
---|---|---|
@@ -1,31 +1,20 @@ | ||
package glide.api.commands; | ||
|
||
import glide.ffi.resolvers.RedisValueResolver; | ||
import glide.managers.BaseCommandResponseResolver; | ||
import java.util.concurrent.CompletableFuture; | ||
import response.ResponseOuterClass.Response; | ||
|
||
/** Base Commands interface to handle generic command and transaction requests. */ | ||
public interface BaseCommands { | ||
/** | ||
* Base Commands interface to handle generic command and transaction requests. | ||
* | ||
* @param <T> The data return type. | ||
*/ | ||
public interface BaseCommands<T> { | ||
|
||
/** | ||
* Extracts the response from the Protobuf response and either throws an exception or returns the | ||
* appropriate response has an Object | ||
* Executes a single custom command, without checking inputs. Every part of the command, including | ||
* subcommands, should be added as a separate value in args. | ||
* | ||
* @param response Redis protobuf message | ||
* @return Response Object | ||
* @param args command and arguments for the custom command call | ||
* @return CompletableFuture with the response | ||
*/ | ||
static Object handleObjectResponse(Response response) { | ||
// return function to convert protobuf.Response into the response object by | ||
// calling valueFromPointer | ||
return (new BaseCommandResponseResolver(RedisValueResolver::valueFromPointer)).apply(response); | ||
} | ||
|
||
/** | ||
* Execute a @see{Command} by sending command via socket manager | ||
* | ||
* @param args arguments for the custom command | ||
* @return a CompletableFuture with response result from Redis | ||
*/ | ||
CompletableFuture<Object> customCommand(String[] args); | ||
CompletableFuture<T> customCommand(String[] args); | ||
} |
22 changes: 22 additions & 0 deletions
22
java/client/src/main/java/glide/api/commands/ClusterBaseCommands.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,22 @@ | ||
package glide.api.commands; | ||
|
||
import glide.api.models.configuration.Route; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Base Commands interface to handle generic command and transaction requests with routing options. | ||
* | ||
* @param <T> The data return type. | ||
*/ | ||
public interface ClusterBaseCommands<T> extends BaseCommands<T> { | ||
|
||
/** | ||
* Executes a single custom command, without checking inputs. Every part of the command, including | ||
* subcommands, should be added as a separate value in args. | ||
* | ||
* @param args command and arguments for the custom command call | ||
* @param route node routing configuration for the command | ||
* @return CompletableFuture with the response | ||
*/ | ||
CompletableFuture<T> customCommand(String[] args, Route route); | ||
} |
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
48 changes: 48 additions & 0 deletions
48
java/client/src/main/java/glide/api/models/configuration/Route.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,48 @@ | ||
package glide.api.models.configuration; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
/** Request routing configuration. */ | ||
@Builder | ||
@Getter | ||
public class Route { | ||
|
||
public enum RouteType { | ||
/** Route request to all nodes. */ | ||
AllNodes, | ||
/** Route request to all primary nodes. */ | ||
AllPrimaries, | ||
/** Route request to a random node. */ | ||
Random, | ||
/** Route request to the primary node that contains the slot with the given id. */ | ||
PrimarySlotId, | ||
/** Route request to the replica node that contains the slot with the given id. */ | ||
ReplicaSlotId, | ||
/** Route request to the primary node that contains the slot that the given key matches. */ | ||
PrimarySlotKey, | ||
/** Route request to the replica node that contains the slot that the given key matches. */ | ||
ReplicaSlotKey, | ||
} | ||
|
||
/** | ||
* Request routing configuration overrides the {@link ReadFrom} connection configuration.<br> | ||
* If {@link RouteType#ReplicaSlotId} or {@link RouteType#ReplicaSlotKey} is used, the request | ||
* will be routed to a replica, even if the strategy is {@link ReadFrom#PRIMARY}. | ||
*/ | ||
private final RouteType routeType; | ||
|
||
/** | ||
* Slot number. There are 16384 slots in a redis cluster, and each shard manages a slot range. | ||
* Unless the slot is known, it's better to route using {@link RouteType#PrimarySlotKey} or {@link | ||
* RouteType#ReplicaSlotKey}.<br> | ||
* Could be used with {@link RouteType#PrimarySlotId} or {@link RouteType#ReplicaSlotId} only. | ||
*/ | ||
private final int slotId; | ||
|
||
/** | ||
* The request will be sent to nodes managing this key.<br> | ||
* Could be used with {@link RouteType#PrimarySlotKey} or {@link RouteType#ReplicaSlotKey} only. | ||
*/ | ||
private final String slotKey; | ||
} |
13 changes: 0 additions & 13 deletions
13
java/client/src/main/java/glide/api/models/configuration/Routes.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
java/client/src/main/java/glide/api/models/configuration/SimpleRoutes.java
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
java/client/src/main/java/glide/api/models/configuration/SlotIdRoute.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.