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));
+ }
}