Skip to content

Commit 4bf438f

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-270 - Add support for 'CLIENT GETNAME'.
Support for getting client connection name has been added for 'jedis', 'jredis' and 'srp' drivers. Using 'jredis' will throw 'UnsupportedOperationException'. Original Pull Request: #51
1 parent e280a23 commit 4bf438f

File tree

14 files changed

+207
-12
lines changed

14 files changed

+207
-12
lines changed

docs/src/reference/docbook/appendix/appendix-command-reference.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
1+
#<?xml version="1.0" encoding="UTF-8"?>
22
<appendix xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="appendix-command-reference" xmlns:xi="http://www.w3.org/2001/XInclude">
33
<title>Spring Data Redis Supported Commands</title>
44

@@ -23,8 +23,8 @@
2323
<row><entry><code>BLPOP</code></entry><entry>X</entry></row>
2424
<row><entry><code>BRPOP</code></entry><entry>X</entry></row>
2525
<row><entry><code>BRPOPLPUSH</code></entry><entry>X</entry></row>
26-
<row><entry><code>CLIENT GETNAME</code></entry><entry>-</entry></row>
2726
<row><entry><code>CLIENT KILL</code></entry><entry>X</entry></row>
27+
<row><entry><code>CLIENT GETNAME</code></entry><entry>X</entry></row>
2828
<row><entry><code>CLIENT LIST</code></entry><entry>-</entry></row>
2929
<row><entry><code>CLIENT SETNAME</code></entry><entry>X</entry></row>
3030
<row><entry><code>CONFIG GET</code></entry><entry>X</entry></row>

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,15 +2191,6 @@ public Long time() {
21912191
return this.delegate.time();
21922192
}
21932193

2194-
/*
2195-
* (non-Javadoc)
2196-
* @see org.springframework.data.redis.connection.RedisServerCommands#killClient(byte[])
2197-
*/
2198-
@Override
2199-
public void killClient(String host, int port) {
2200-
this.delegate.killClient(host, port);
2201-
}
2202-
22032194
/**
22042195
* Specifies if pipelined and tx results should be deserialized to Strings. If false, results of
22052196
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the underlying connection
@@ -2257,4 +2248,20 @@ public void setClientName(String name) {
22572248
setClientName(this.serializer.serialize(name));
22582249
}
22592250

2251+
/*
2252+
* (non-Javadoc)
2253+
* @see org.springframework.data.redis.connection.RedisServerCommands#killClient(byte[])
2254+
*/
2255+
@Override
2256+
public void killClient(String host, int port) {
2257+
this.delegate.killClient(host, port);
2258+
}
2259+
2260+
/*
2261+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2262+
*/
2263+
@Override
2264+
public String getClientName() {
2265+
return this.delegate.getClientName();
2266+
}
22602267
}

src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,13 @@ public enum ShutdownOption {
177177
* @since 1.3
178178
*/
179179
void setClientName(byte[] name);
180+
181+
/**
182+
* Returns the name of the current connection.
183+
*
184+
* @see http://redis.io/commands/client-getname
185+
* @return
186+
* @since 1.3
187+
*/
188+
String getClientName();
180189
}

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,6 +2841,19 @@ public void setClientName(byte[] name) {
28412841
jedis.clientSetname(name);
28422842
}
28432843

2844+
/*
2845+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2846+
*/
2847+
@Override
2848+
public String getClientName() {
2849+
2850+
if (isPipelined() || isQueueing()) {
2851+
throw new UnsupportedOperationException();
2852+
}
2853+
2854+
return jedis.clientGetname();
2855+
}
2856+
28442857
/**
28452858
* Specifies if pipelined results should be converted to the expected data type. If false, results of
28462859
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Jedis driver

src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,4 +1195,13 @@ public void killClient(String host, int port) {
11951195
public void setClientName(byte[] name) {
11961196
throw new UnsupportedOperationException("'CLIENT SETNAME' is not supported by the JRedis driver.");
11971197
}
1198+
1199+
/*
1200+
* (non-Javadoc)
1201+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
1202+
*/
1203+
@Override
1204+
public String getClientName() {
1205+
throw new UnsupportedOperationException("The 'CLIENT GETNAME' command is not supported by the JRedis driver.");
1206+
}
11981207
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,6 +2935,30 @@ public void setClientName(byte[] name) {
29352935
getAsyncConnection().clientSetname(name);
29362936
}
29372937

2938+
/*
2939+
* (non-Javadoc)
2940+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2941+
*/
2942+
@Override
2943+
public String getClientName() {
2944+
2945+
try {
2946+
2947+
if (isPipelined()) {
2948+
pipeline(new LettuceResult(getAsyncConnection().clientGetname(), LettuceConverters.bytesToString()));
2949+
return null;
2950+
}
2951+
if (isQueueing()) {
2952+
transaction(new LettuceTxResult(getConnection().clientGetname(), LettuceConverters.bytesToString()));
2953+
return null;
2954+
}
2955+
2956+
return LettuceConverters.toString(getConnection().clientGetname());
2957+
} catch (Exception ex) {
2958+
throw convertLettuceAccessException(ex);
2959+
}
2960+
}
2961+
29382962
/**
29392963
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
29402964
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Lettuce driver

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection.lettuce;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.Date;
2021
import java.util.LinkedHashSet;
2122
import java.util.List;
@@ -47,6 +48,7 @@ abstract public class LettuceConverters extends Converters {
4748

4849
private static final Converter<Date, Long> DATE_TO_LONG;
4950
private static final Converter<List<byte[]>, Set<byte[]>> BYTES_LIST_TO_BYTES_SET;
51+
private static final Converter<byte[], String> BYTES_TO_STRING;
5052
private static final Converter<Set<byte[]>, List<byte[]>> BYTES_SET_TO_BYTES_LIST;
5153
private static final Converter<KeyValue<byte[], byte[]>, List<byte[]>> KEY_VALUE_TO_BYTES_LIST;
5254
private static final Converter<List<ScoredValue<byte[]>>, Set<Tuple>> SCORED_VALUES_TO_TUPLE_SET;
@@ -65,6 +67,17 @@ public Set<byte[]> convert(List<byte[]> results) {
6567
return results != null ? new LinkedHashSet<byte[]>(results) : null;
6668
}
6769

70+
};
71+
BYTES_TO_STRING = new Converter<byte[], String>() {
72+
73+
@Override
74+
public String convert(byte[] source) {
75+
if (source == null || Arrays.equals(source, new byte[0])) {
76+
return null;
77+
}
78+
return new String(source);
79+
}
80+
6881
};
6982
BYTES_SET_TO_BYTES_LIST = new Converter<Set<byte[]>, List<byte[]>>() {
7083
public List<byte[]> convert(Set<byte[]> results) {
@@ -110,6 +123,10 @@ public static Converter<List<byte[]>, Set<byte[]>> bytesListToBytesSet() {
110123
return BYTES_LIST_TO_BYTES_SET;
111124
}
112125

126+
public static Converter<byte[], String> bytesToString() {
127+
return BYTES_TO_STRING;
128+
}
129+
113130
public static Converter<KeyValue<byte[], byte[]>, List<byte[]>> keyValueToBytesList() {
114131
return KEY_VALUE_TO_BYTES_LIST;
115132
}
@@ -158,6 +175,10 @@ public static DataAccessException toDataAccessException(Exception ex) {
158175
return EXCEPTION_CONVERTER.convert(ex);
159176
}
160177

178+
public static String toString(byte[] source) {
179+
return BYTES_TO_STRING.convert(source);
180+
}
181+
161182
public static ScriptOutputType toScriptOutputType(ReturnType returnType) {
162183
switch (returnType) {
163184
case BOOLEAN:

src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,26 @@ public void setClientName(byte[] name) {
22602260
}
22612261
}
22622262

2263+
/*
2264+
* (non-Javadoc)
2265+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2266+
*/
2267+
@Override
2268+
public String getClientName() {
2269+
2270+
try {
2271+
2272+
if (isPipelined()) {
2273+
pipeline(new SrpGenericResult(pipeline.client_getname(), SrpConverters.replyToString()));
2274+
return null;
2275+
}
2276+
2277+
return SrpConverters.toString(client.client_getname());
2278+
} catch (Exception ex) {
2279+
throw convertSrpAccessException(ex);
2280+
}
2281+
}
2282+
22632283
private List<Object> closeTransaction() {
22642284
List<Object> results = Collections.emptyList();
22652285
if (txTracker != null) {

src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
3636
import org.springframework.data.redis.connection.convert.Converters;
3737
import org.springframework.util.Assert;
38-
import org.springframework.util.NumberUtils;
3938

4039
import redis.client.RedisException;
4140
import redis.reply.IntegerReply;
@@ -61,6 +60,7 @@ abstract public class SrpConverters extends Converters {
6160
private static final Converter<Reply[], Map<byte[], byte[]>> REPLIES_TO_BYTES_MAP;
6261
private static final Converter<Reply[], List<Boolean>> REPLIES_TO_BOOLEAN_LIST;
6362
private static final Converter<Reply[], List<String>> REPLIES_TO_STRING_LIST;
63+
private static final Converter<Reply, String> REPLY_TO_STRING;
6464
private static final Converter<byte[], Properties> BYTES_TO_PROPERTIES;
6565
private static final Converter<byte[], String> BYTES_TO_STRING;
6666
private static final Converter<byte[], Double> BYTES_TO_DOUBLE;
@@ -171,6 +171,16 @@ public List<String> convert(Reply[] source) {
171171
return results;
172172
}
173173
};
174+
REPLY_TO_STRING = new Converter<Reply, String>() {
175+
176+
@Override
177+
public String convert(Reply source) {
178+
if (source == null) {
179+
return null;
180+
}
181+
return SrpConverters.toString((byte[]) source.data());
182+
}
183+
};
174184
}
175185

176186
public static Converter<Reply[], List<byte[]>> repliesToBytesList() {
@@ -201,6 +211,10 @@ public static Converter<byte[], String> bytesToString() {
201211
return BYTES_TO_STRING;
202212
}
203213

214+
public static Converter<Reply, String> replyToString() {
215+
return REPLY_TO_STRING;
216+
}
217+
204218
public static Converter<Reply[], List<Boolean>> repliesToBooleanList() {
205219
return REPLIES_TO_BOOLEAN_LIST;
206220
}
@@ -237,6 +251,10 @@ public static Map<byte[], byte[]> toBytesMap(Reply[] source) {
237251
return REPLIES_TO_BYTES_MAP.convert(source);
238252
}
239253

254+
public static String toString(Reply source) {
255+
return REPLY_TO_STRING.convert(source);
256+
}
257+
240258
public static String toString(byte[] source) {
241259
return BYTES_TO_STRING.convert(source);
242260
}
@@ -293,4 +311,5 @@ public static byte[] toBytes(Position source) {
293311
public static List<String> toStringList(String source) {
294312
return Collections.singletonList(source);
295313
}
314+
296315
}

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,16 @@ public void settingClientNameShouldDelegateToNativeConnection() {
17201720
verify(nativeConnection, times(1)).setClientName(eq("foo".getBytes()));
17211721
}
17221722

1723+
/**
1724+
* @see DATAREDIS-270
1725+
*/
1726+
@Test
1727+
public void testGetClientNameIsDelegatedCorrectlyToNativeConnection() {
1728+
1729+
actual.add(connection.getClientName());
1730+
verify(nativeConnection, times(1)).getClientName();
1731+
}
1732+
17231733
protected List<Object> getResults() {
17241734
return actual;
17251735
}

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ public void killClientShouldDelegateCallCorrectly() {
123123
connection.killClient("127.0.0.1", 1001);
124124
verifyNativeConnectionInvocation().clientKill(eq("127.0.0.1:1001"));
125125
}
126+
127+
/**
128+
* @see DATAREDIS-270
129+
*/
130+
@Test
131+
public void getClientNameShouldSendRequestCorrectly() {
132+
133+
connection.getClientName();
134+
verifyNativeConnectionInvocation().clientGetname();
135+
}
136+
126137
}
127138

128139
public static class JedisConnectionPipelineUnitTests extends JedisConnectionUnitTests {
@@ -151,10 +162,23 @@ public void shutdownSaveShouldBeSentCorrectlyUsingLuaScript() {
151162
super.shutdownSaveShouldBeSentCorrectlyUsingLuaScript();
152163
}
153164

165+
/**
166+
* @see DATAREDIS-267
167+
*/
154168
@Test(expected = UnsupportedOperationException.class)
155169
public void killClientShouldDelegateCallCorrectly() {
156170
super.killClientShouldDelegateCallCorrectly();
157171
}
172+
173+
/**
174+
* @see DATAREDIS-270
175+
*/
176+
@Override
177+
@Test(expected = UnsupportedOperationException.class)
178+
public void getClientNameShouldSendRequestCorrectly() {
179+
super.getClientNameShouldSendRequestCorrectly();
180+
}
181+
158182
}
159183

160184
/**

src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionUnitTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ public void shutdownNosaveShouldThrowUnsupportedOperationException() {
5656
public void shutdownWithNullShouldThrowUnsupportedOperationException() {
5757
connection.shutdown(null);
5858
}
59+
60+
/**
61+
* @see DATAREDIS-270
62+
*/
63+
@Test(expected = UnsupportedOperationException.class)
64+
public void getClientNameShouldSendRequestCorrectly() {
65+
connection.getClientName();
66+
}
67+
5968
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTestSuite.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ public void killClientShouldDelegateCallCorrectly() {
9393
verifyNativeConnectionInvocation().clientKill(eq(ipPort));
9494
}
9595

96+
/**
97+
* @see DATAREDIS-270
98+
*/
99+
@Test
100+
public void getClientNameShouldSendRequestCorrectly() {
101+
102+
connection.getClientName();
103+
verifyNativeConnectionInvocation().clientGetname();
104+
}
105+
96106
}
97107

98108
public static class LettucePipelineConnectionUnitTests extends LettuceConnectionUnitTests {

src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionUnitTestSuite.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public void killClientShouldDelegateCallCorrectly() {
8989
verifyNativeConnectionInvocation().client_kill(eq(ipPort));
9090
}
9191

92+
/**
93+
* @see DATAREDIS-270
94+
*/
95+
@Test
96+
public void getClientNameShouldSendRequestCorrectly() {
97+
98+
connection.getClientName();
99+
verifyNativeConnectionInvocation().client_getname();
100+
}
101+
92102
}
93103

94104
public static class SrpConnectionPiplineUnitTests extends AbstractConnectionUnitTestBase<Pipeline> {
@@ -135,6 +145,16 @@ public void shutdownWithNosaveIsCalledCorrectly() {
135145
verifyNativeConnectionInvocation().shutdown("NOSAVE".getBytes(Charsets.UTF_8), null);
136146
}
137147

148+
/**
149+
* @see DATAREDIS-270
150+
*/
151+
@Test
152+
public void getClientNameShouldSendRequestCorrectly() {
153+
154+
connection.getClientName();
155+
verifyNativeConnectionInvocation().client_getname();
156+
}
157+
138158
}
139159

140160
}

0 commit comments

Comments
 (0)