diff --git a/java/client/src/main/java/glide/api/BaseClient.java b/java/client/src/main/java/glide/api/BaseClient.java index 752fd3020a..1a5d42fa27 100644 --- a/java/client/src/main/java/glide/api/BaseClient.java +++ b/java/client/src/main/java/glide/api/BaseClient.java @@ -4,6 +4,8 @@ import static glide.ffi.resolvers.SocketListenerResolver.getSocket; import static glide.utils.ArrayTransformUtils.castArray; import static glide.utils.ArrayTransformUtils.convertMapToArgArray; +import static redis_request.RedisRequestOuterClass.RequestType.ClientGetName; +import static redis_request.RedisRequestOuterClass.RequestType.ClientId; import static redis_request.RedisRequestOuterClass.RequestType.Decr; import static redis_request.RedisRequestOuterClass.RequestType.DecrBy; import static redis_request.RedisRequestOuterClass.RequestType.GetString; @@ -22,7 +24,7 @@ import static redis_request.RedisRequestOuterClass.RequestType.SRem; import static redis_request.RedisRequestOuterClass.RequestType.SetString; -import glide.api.commands.ConnectionManagementCommands; +import glide.api.commands.ConnectionManagementBaseCommands; import glide.api.commands.HashCommands; import glide.api.commands.SetCommands; import glide.api.commands.StringCommands; @@ -53,10 +55,11 @@ @AllArgsConstructor public abstract class BaseClient implements AutoCloseable, - ConnectionManagementCommands, + ConnectionManagementBaseCommands, StringCommands, HashCommands, SetCommands { + /** Redis simple string response with "OK" */ public static final String OK = ConstantResponse.OK.toString(); @@ -188,7 +191,7 @@ protected Object[] handleArrayOrNullResponse(Response response) throws RedisExce /** * @param response A Protobuf response * @return A map of String to V - * @param Value type could be even map too + * @param Value type, could be even map too */ @SuppressWarnings("unchecked") // raw Map cast to Map protected Map handleMapResponse(Response response) throws RedisException { @@ -309,4 +312,14 @@ public CompletableFuture> smembers(String key) { public CompletableFuture scard(String key) { return commandManager.submitNewCommand(SCard, new String[] {key}, this::handleLongResponse); } + + public CompletableFuture clientId() { + return commandManager.submitNewCommand(ClientId, new String[0], this::handleLongResponse); + } + + @Override + public CompletableFuture clientGetName() { + return commandManager.submitNewCommand( + ClientGetName, new String[0], this::handleStringOrNullResponse); + } } diff --git a/java/client/src/main/java/glide/api/RedisClient.java b/java/client/src/main/java/glide/api/RedisClient.java index 94af58bd42..fc40b73661 100644 --- a/java/client/src/main/java/glide/api/RedisClient.java +++ b/java/client/src/main/java/glide/api/RedisClient.java @@ -1,6 +1,8 @@ /** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ package glide.api; +import static redis_request.RedisRequestOuterClass.RequestType.ClientGetName; +import static redis_request.RedisRequestOuterClass.RequestType.ClientId; import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand; import static redis_request.RedisRequestOuterClass.RequestType.Info; @@ -54,4 +56,15 @@ public CompletableFuture info() { public CompletableFuture info(@NonNull InfoOptions options) { return commandManager.submitNewCommand(Info, options.toArgs(), this::handleStringResponse); } + + @Override + public CompletableFuture clientId() { + return commandManager.submitNewCommand(ClientId, new String[0], this::handleLongResponse); + } + + @Override + public CompletableFuture clientGetName() { + return commandManager.submitNewCommand( + ClientGetName, new String[0], this::handleStringOrNullResponse); + } } diff --git a/java/client/src/main/java/glide/api/RedisClusterClient.java b/java/client/src/main/java/glide/api/RedisClusterClient.java index cfdf1bd072..7aeb762af3 100644 --- a/java/client/src/main/java/glide/api/RedisClusterClient.java +++ b/java/client/src/main/java/glide/api/RedisClusterClient.java @@ -1,6 +1,8 @@ /** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ package glide.api; +import static redis_request.RedisRequestOuterClass.RequestType.ClientGetName; +import static redis_request.RedisRequestOuterClass.RequestType.ClientId; import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand; import static redis_request.RedisRequestOuterClass.RequestType.Info; import static redis_request.RedisRequestOuterClass.RequestType.Ping; @@ -53,7 +55,8 @@ public CompletableFuture> customCommand(@NonNull String[] a } @Override - public CompletableFuture> customCommand(String[] args, Route route) { + public CompletableFuture> customCommand( + @NonNull String[] args, @NonNull Route route) { return commandManager.submitNewCommand( CustomCommand, args, route, response -> handleCustomCommandResponse(route, response)); } @@ -132,4 +135,38 @@ public CompletableFuture> info( ? ClusterValue.of(handleStringResponse(response)) : ClusterValue.of(handleMapResponse(response))); } + + @Override + public CompletableFuture clientId() { + return super.clientId(); + } + + @Override + public CompletableFuture> clientId(@NonNull Route route) { + return commandManager.submitNewCommand( + ClientId, + new String[0], + route, + response -> + route.isSingleNodeRoute() + ? ClusterValue.of(handleLongResponse(response)) + : ClusterValue.of(handleMapResponse(response))); + } + + @Override + public CompletableFuture clientGetName() { + return super.clientGetName(); + } + + @Override + public CompletableFuture> clientGetName(@NonNull Route route) { + return commandManager.submitNewCommand( + ClientGetName, + new String[0], + route, + response -> + route.isSingleNodeRoute() + ? ClusterValue.of(handleStringOrNullResponse(response)) + : ClusterValue.of(handleMapResponse(response))); + } } diff --git a/java/client/src/main/java/glide/api/commands/ConnectionManagementBaseCommands.java b/java/client/src/main/java/glide/api/commands/ConnectionManagementBaseCommands.java new file mode 100644 index 0000000000..ebda8aa6b0 --- /dev/null +++ b/java/client/src/main/java/glide/api/commands/ConnectionManagementBaseCommands.java @@ -0,0 +1,57 @@ +/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ +package glide.api.commands; + +import java.util.concurrent.CompletableFuture; + +/** + * Connection Management Commands interface for both standalone and cluster clients. + * + * @see Connection Management Commands + */ +public interface ConnectionManagementBaseCommands { + + /** + * Ping the Redis server. + * + * @see redis.io for details. + * @return Response from Redis containing a String with "PONG". + */ + CompletableFuture ping(); + + /** + * Ping the Redis server. + * + * @see redis.io for details. + * @param str The ping argument that will be returned. + * @return Response from Redis containing a String with a copy of the argument + * str. + */ + CompletableFuture ping(String str); + + /** + * Gets the current connection id. + * + * @see redis.io for details. + * @return The id of the client. + * @example + *
+     * long id = client.clientId().get();
+     * assert id > 0
+     * 
+ */ + CompletableFuture clientId(); + + /** + * Gets the name of the current connection. + * + * @see redis.io for details. + * @return The name of the client connection as a string if a name is set, or null if + * no name is assigned. + * @example + *
+     * String clientName = client.clientGetName().get();
+     * assert clientName != null
+     * 
+ */ + CompletableFuture clientGetName(); +} diff --git a/java/client/src/main/java/glide/api/commands/ConnectionManagementClusterCommands.java b/java/client/src/main/java/glide/api/commands/ConnectionManagementClusterCommands.java index a3d5f03e6b..683fb4c8c5 100644 --- a/java/client/src/main/java/glide/api/commands/ConnectionManagementClusterCommands.java +++ b/java/client/src/main/java/glide/api/commands/ConnectionManagementClusterCommands.java @@ -1,13 +1,14 @@ /** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ package glide.api.commands; +import glide.api.models.ClusterValue; import glide.api.models.configuration.RequestRoutingConfiguration.Route; import java.util.concurrent.CompletableFuture; /** - * Connection Management Commands interface. + * Connection Management Commands interface for cluster client. * - * @see: Connection Management Commands + * @see Connection Management Commands */ public interface ConnectionManagementClusterCommands { @@ -32,4 +33,80 @@ public interface ConnectionManagementClusterCommands { * str. */ CompletableFuture ping(String str, Route route); + + /** + * Gets the current connection id.
+ * The command will be routed a random node. + * + * @see redis.io for details. + * @return The id of the client. + * @example + *
+     * long id = client.clientId().get();
+     * assert id > 0
+     * 
+ */ + CompletableFuture clientId(); + + /** + * Gets the current connection id. + * + * @see redis.io for details. + * @param route Routing configuration for the command. Client will route the command to the nodes + * defined. + * @return A {@link ClusterValue} which holds a single value if single node route is used or a + * dictionary where each address is the key and its corresponding node response is the value. + * The value is the id of the client on that node. + * @example + *
+     * long id = client.clientId(new SlotIdRoute(...)).get().getSingleValue();
+     * assert id > 0
+     * 
+ * + * @example + *
+     * Map<String, Long> idPerNode = client.clientId(ALL_NODES).get().getMultiValue();
+     * assert idPerNode.get("<node 1 address>") > 0
+     * 
+ */ + CompletableFuture> clientId(Route route); + + /** + * Gets the name of the current connection.
+ * The command will be routed a random node. + * + * @see redis.io for details. + * @return The name of the client connection as a string if a name is set, or null if + * no name is assigned. + * @example + *
+     * String clientName = client.clientGetName().get();
+     * assert clientName != null
+     * 
+ */ + CompletableFuture clientGetName(); + + /** + * Gets the name of the current connection. + * + * @see redis.io for details. + * @param route Routing configuration for the command. Client will route the command to the nodes + * defined. + * @return A {@link ClusterValue} which holds a single value if single node route is used or a + * dictionary where each address is the key and its corresponding node response is the value. + * The value is the name of the client connection as a string if a name is set, or null if no + * name is assigned. + * @example + *
+     * String clientName = client.clientGetName(new SlotIdRoute(...)).get().getSingleValue();
+     * assert clientName != null
+     * 
+ * + * @example + *
+     * Map<String, String> clientNamePerNode = client.clientGetName(ALL_NODES).get().getMultiValue();
+     * assert clientNamePerNode.get("<node 1 address>") != null
+     * 
+ */ + CompletableFuture> clientGetName(Route route); } diff --git a/java/client/src/main/java/glide/api/commands/ConnectionManagementCommands.java b/java/client/src/main/java/glide/api/commands/ConnectionManagementCommands.java deleted file mode 100644 index 5f4027bdd1..0000000000 --- a/java/client/src/main/java/glide/api/commands/ConnectionManagementCommands.java +++ /dev/null @@ -1,30 +0,0 @@ -/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ -package glide.api.commands; - -import java.util.concurrent.CompletableFuture; - -/** - * Connection Management Commands interface. - * - * @see: Connection Management Commands - */ -public interface ConnectionManagementCommands { - - /** - * Ping the Redis server. - * - * @see redis.io for details. - * @return Response from Redis containing a String with "PONG". - */ - CompletableFuture ping(); - - /** - * Ping the Redis server. - * - * @see redis.io for details. - * @param str The ping argument that will be returned. - * @return Response from Redis containing a String with a copy of the argument - * str. - */ - CompletableFuture ping(String str); -} diff --git a/java/client/src/main/java/glide/api/models/BaseTransaction.java b/java/client/src/main/java/glide/api/models/BaseTransaction.java index ad087ed457..5b32db1d6f 100644 --- a/java/client/src/main/java/glide/api/models/BaseTransaction.java +++ b/java/client/src/main/java/glide/api/models/BaseTransaction.java @@ -2,6 +2,8 @@ package glide.api.models; import static glide.utils.ArrayTransformUtils.convertMapToArgArray; +import static redis_request.RedisRequestOuterClass.RequestType.ClientGetName; +import static redis_request.RedisRequestOuterClass.RequestType.ClientId; import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand; import static redis_request.RedisRequestOuterClass.RequestType.Decr; import static redis_request.RedisRequestOuterClass.RequestType.DecrBy; @@ -413,6 +415,29 @@ public T scard(String key) { return getThis(); } + /** + * Get the current connection id. + * + * @see redis.io for details. + * @return Command response - The id of the client. + */ + public T clientId() { + protobufTransaction.addCommands(buildCommand(ClientId)); + return getThis(); + } + + /** + * Get the name of the current connection. + * + * @see redis.io for details. + * @return Command response - The name of the client connection as a string if a name is set, or + * null if no name is assigned. + */ + public T clientGetName() { + protobufTransaction.addCommands(buildCommand(ClientGetName)); + return getThis(); + } + /** Build protobuf {@link Command} object for given command and arguments. */ protected Command buildCommand(RequestType requestType) { return buildCommand(requestType, buildArgs()); diff --git a/java/client/src/test/java/glide/api/RedisClusterClientTest.java b/java/client/src/test/java/glide/api/RedisClusterClientTest.java index d79150daed..fe525b3e59 100644 --- a/java/client/src/test/java/glide/api/RedisClusterClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClusterClientTest.java @@ -11,6 +11,8 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static redis_request.RedisRequestOuterClass.RequestType.ClientGetName; +import static redis_request.RedisRequestOuterClass.RequestType.ClientId; import static redis_request.RedisRequestOuterClass.RequestType.Info; import static redis_request.RedisRequestOuterClass.RequestType.Ping; @@ -118,7 +120,6 @@ public TestClient(CommandManager commandManager, Object objectToReturn) { object = objectToReturn; } - @SuppressWarnings("unchecked") @Override protected T handleRedisResponse(Class classType, boolean isNullable, Response response) { return (T) object; @@ -314,4 +315,84 @@ public void info_with_options_and_multi_node_route_returns_multi_value() { assertAll( () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); } + + @SneakyThrows + @Test + public void clientId_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn(42L); + when(commandManager.submitNewCommand(eq(ClientId), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.clientId(); + + // verify + assertEquals(testResponse, response); + assertEquals(42L, response.get()); + } + + @Test + @SneakyThrows + public void clientId_with_multi_node_route_returns_success() { + var commandManager = new TestCommandManager(null); + + var data = Map.of("n1", 42L); + var client = new TestClient(commandManager, data); + + var value = client.clientId(ALL_NODES).get(); + assertEquals(data, value.getMultiValue()); + } + + @Test + @SneakyThrows + public void clientId_with_single_node_route_returns_success() { + var commandManager = new TestCommandManager(null); + + var client = new TestClient(commandManager, 42L); + + var value = client.clientId(RANDOM).get(); + assertEquals(42, value.getSingleValue()); + } + + @SneakyThrows + @Test + public void clientGetName_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn("TEST"); + when(commandManager.submitNewCommand(eq(ClientGetName), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.clientGetName(); + + // verify + assertEquals(testResponse, response); + assertEquals("TEST", response.get()); + } + + @Test + @SneakyThrows + public void clientGetName_with_single_node_route_returns_success() { + var commandManager = new TestCommandManager(null); + + var client = new TestClient(commandManager, "TEST"); + + var value = client.clientGetName(RANDOM).get(); + assertEquals("TEST", value.getSingleValue()); + } + + @Test + @SneakyThrows + public void clientGetName_with_multi_node_route_returns_success() { + var commandManager = new TestCommandManager(null); + + var data = Map.of("n1", "TEST"); + var client = new TestClient(commandManager, data); + + var value = client.clientGetName(ALL_NODES).get(); + assertEquals(data, value.getMultiValue()); + } } diff --git a/java/client/src/test/java/glide/api/models/TransactionTests.java b/java/client/src/test/java/glide/api/models/TransactionTests.java index 1c74305613..98d9e3077c 100644 --- a/java/client/src/test/java/glide/api/models/TransactionTests.java +++ b/java/client/src/test/java/glide/api/models/TransactionTests.java @@ -3,6 +3,8 @@ import static glide.api.models.commands.SetOptions.RETURN_OLD_VALUE; import static org.junit.jupiter.api.Assertions.assertEquals; +import static redis_request.RedisRequestOuterClass.RequestType.ClientGetName; +import static redis_request.RedisRequestOuterClass.RequestType.ClientId; import static redis_request.RedisRequestOuterClass.RequestType.Decr; import static redis_request.RedisRequestOuterClass.RequestType.DecrBy; import static redis_request.RedisRequestOuterClass.RequestType.GetString; @@ -116,6 +118,10 @@ public void transaction_builds_protobuf_request() { transaction.scard("key"); results.add(Pair.of(SCard, ArgsArray.newBuilder().addArgs("key").build())); + transaction.clientId().clientGetName(); + results.add(Pair.of(ClientId, ArgsArray.newBuilder().build())); + results.add(Pair.of(ClientGetName, ArgsArray.newBuilder().build())); + var protobufTransaction = transaction.getProtobufTransaction().build(); for (int idx = 0; idx < protobufTransaction.getCommandsCount(); idx++) { diff --git a/java/integTest/src/test/java/glide/SharedCommandTests.java b/java/integTest/src/test/java/glide/SharedCommandTests.java index 534e2e73fd..e191fabf0b 100644 --- a/java/integTest/src/test/java/glide/SharedCommandTests.java +++ b/java/integTest/src/test/java/glide/SharedCommandTests.java @@ -252,7 +252,7 @@ public void set_missing_value_and_returnOldValue_is_null(BaseClient client) { assertEquals(OK, ok); SetOptions options = SetOptions.builder().returnOldValue(true).build(); - String data = client.set("another", ANOTHER_VALUE, options).get(); + String data = client.set(UUID.randomUUID().toString(), ANOTHER_VALUE, options).get(); assertNull(data); } diff --git a/java/integTest/src/test/java/glide/TestUtilities.java b/java/integTest/src/test/java/glide/TestUtilities.java new file mode 100644 index 0000000000..49570a117e --- /dev/null +++ b/java/integTest/src/test/java/glide/TestUtilities.java @@ -0,0 +1,14 @@ +/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ +package glide; + +import glide.api.models.ClusterValue; +import lombok.experimental.UtilityClass; + +@UtilityClass +public class TestUtilities { + + /** Extract first value from {@link ClusterValue} assuming it contains a multi-value. */ + public static T getFirstEntryFromMultiValue(ClusterValue data) { + return data.getMultiValue().get(data.getMultiValue().keySet().toArray(String[]::new)[0]); + } +} diff --git a/java/integTest/src/test/java/glide/cluster/CommandTests.java b/java/integTest/src/test/java/glide/cluster/CommandTests.java index 38973aa581..4d719aa5b0 100644 --- a/java/integTest/src/test/java/glide/cluster/CommandTests.java +++ b/java/integTest/src/test/java/glide/cluster/CommandTests.java @@ -3,6 +3,7 @@ import static glide.TestConfiguration.CLUSTER_PORTS; import static glide.TestConfiguration.REDIS_VERSION; +import static glide.TestUtilities.getFirstEntryFromMultiValue; import static glide.api.models.commands.InfoOptions.Section.CLIENTS; import static glide.api.models.commands.InfoOptions.Section.CLUSTER; import static glide.api.models.commands.InfoOptions.Section.COMMANDSTATS; @@ -260,4 +261,64 @@ public void info_with_multi_node_route_and_options() { } } } + + @Test + @SneakyThrows + public void clientId() { + var id = clusterClient.clientId().get(); + assertTrue(id > 0); + } + + @Test + @SneakyThrows + public void clientId_with_single_node_route() { + var data = clusterClient.clientId(RANDOM).get(); + assertTrue(data.getSingleValue() > 0L); + } + + @Test + @SneakyThrows + public void clientId_with_multi_node_route() { + var data = clusterClient.clientId(ALL_NODES).get(); + data.getMultiValue().values().forEach(id -> assertTrue(id > 0)); + } + + @Test + @SneakyThrows + public void clientGetName() { + // TODO replace with the corresponding command once implemented + clusterClient.customCommand(new String[] {"client", "setname", "clientGetName"}).get(); + + var name = clusterClient.clientGetName().get(); + + assertEquals("clientGetName", name); + } + + @Test + @SneakyThrows + public void clientGetName_with_single_node_route() { + // TODO replace with the corresponding command once implemented + clusterClient + .customCommand( + new String[] {"client", "setname", "clientGetName_with_single_node_route"}, ALL_NODES) + .get(); + + var name = clusterClient.clientGetName(RANDOM).get(); + + assertEquals("clientGetName_with_single_node_route", name.getSingleValue()); + } + + @Test + @SneakyThrows + public void clientGetName_with_multi_node_route() { + // TODO replace with the corresponding command once implemented + clusterClient + .customCommand( + new String[] {"client", "setname", "clientGetName_with_multi_node_route"}, ALL_NODES) + .get(); + + var name = clusterClient.clientGetName(ALL_NODES).get(); + + assertEquals("clientGetName_with_multi_node_route", getFirstEntryFromMultiValue(name)); + } }