Skip to content

Commit e280a23

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-269 - Add support for 'CLIENT SETNAME'.
Setting client name is possible for 'jedis', 'lettuce' and 'srp'. Using 'jredis' throws 'UnspupportedOperationException'. Original Pull Request: #54
1 parent 6a6d03e commit e280a23

13 files changed

+131
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<row><entry><code>CLIENT GETNAME</code></entry><entry>-</entry></row>
2727
<row><entry><code>CLIENT KILL</code></entry><entry>X</entry></row>
2828
<row><entry><code>CLIENT LIST</code></entry><entry>-</entry></row>
29-
<row><entry><code>CLIENT SETNAME</code></entry><entry>-</entry></row>
29+
<row><entry><code>CLIENT SETNAME</code></entry><entry>X</entry></row>
3030
<row><entry><code>CONFIG GET</code></entry><entry>X</entry></row>
3131
<row><entry><code>CONFIG RESETSTAT</code></entry><entry>X</entry></row>
3232
<row><entry><code>CONFIG REWRITE</code></entry><entry>-</entry></row>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,4 +2239,22 @@ private List<Object> convertResults(List<Object> results, Queue<Converter> conve
22392239
return convertedResults;
22402240
}
22412241

2242+
/*
2243+
* (non-Javadoc)
2244+
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(java.lang.String)
2245+
*/
2246+
@Override
2247+
public void setClientName(byte[] name) {
2248+
this.delegate.setClientName(name);
2249+
}
2250+
2251+
/*
2252+
* (non-Javadoc)
2253+
* @see org.springframework.data.redis.connection.StringRedisConnection#setClientName(java.lang.String)
2254+
*/
2255+
@Override
2256+
public void setClientName(String name) {
2257+
setClientName(this.serializer.serialize(name));
2258+
}
2259+
22422260
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,10 @@ public enum ShutdownOption {
171171
*/
172172
void killClient(String host, int port);
173173

174+
/**
175+
* Assign given name to current connection.
176+
*
177+
* @since 1.3
178+
*/
179+
void setClientName(byte[] name);
174180
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,13 @@ public interface StringTuple extends Tuple {
292292
<T> T eval(String script, ReturnType returnType, int numKeys, String... keysAndArgs);
293293

294294
<T> T evalSha(String scriptSha1, ReturnType returnType, int numKeys, String... keysAndArgs);
295+
296+
/**
297+
* Assign given {@code name} to connection using registered {@link RedisSerializer} for name conversion.
298+
*
299+
* @param name
300+
* @see #setClientName(byte[])
301+
* @sice 1.3
302+
*/
303+
void setClientName(String name);
295304
}

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
@@ -2828,6 +2828,19 @@ public void killClient(String host, int port) {
28282828
}
28292829
}
28302830

2831+
/*
2832+
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(java.lang.String)
2833+
*/
2834+
@Override
2835+
public void setClientName(byte[] name) {
2836+
2837+
if (isPipelined() || isQueueing()) {
2838+
throw new UnsupportedOperationException("'CLIENT SETNAME' is not suppored in transacton / pipeline mode.");
2839+
}
2840+
2841+
jedis.clientSetname(name);
2842+
}
2843+
28312844
/**
28322845
* Specifies if pipelined results should be converted to the expected data type. If false, results of
28332846
* {@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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,4 +1190,9 @@ public Long time() {
11901190
public void killClient(String host, int port) {
11911191
throw new UnsupportedOperationException("The 'CLIENT KILL' command is not supported by the JRedis driver.");
11921192
}
1193+
1194+
@Override
1195+
public void setClientName(byte[] name) {
1196+
throw new UnsupportedOperationException("'CLIENT SETNAME' is not supported by the JRedis driver.");
1197+
}
11931198
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,6 +2920,21 @@ public void killClient(String host, int port) {
29202920
}
29212921
}
29222922

2923+
@Override
2924+
public void setClientName(byte[] name) {
2925+
2926+
if (isQueueing()) {
2927+
pipeline(new LettuceStatusResult(getAsyncConnection().clientSetname(name)));
2928+
return;
2929+
}
2930+
if (isQueueing()) {
2931+
transaction(new LettuceTxResult(getConnection().clientSetname(name)));
2932+
return;
2933+
}
2934+
2935+
getAsyncConnection().clientSetname(name);
2936+
}
2937+
29232938
/**
29242939
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
29252940
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Lettuce driver

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
@@ -2240,6 +2240,26 @@ public void killClient(String host, int port) {
22402240
}
22412241
}
22422242

2243+
/*
2244+
* (non-Javadoc)
2245+
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(byte[])
2246+
*/
2247+
@Override
2248+
public void setClientName(byte[] name) {
2249+
2250+
try {
2251+
2252+
if (isPipelined()) {
2253+
pipeline(new SrpStatusResult(pipeline.client_setname(name)));
2254+
return;
2255+
}
2256+
2257+
this.client.client_setname(name);
2258+
} catch (Exception ex) {
2259+
throw convertSrpAccessException(ex);
2260+
}
2261+
}
2262+
22432263
private List<Object> closeTransaction() {
22442264
List<Object> results = Collections.emptyList();
22452265
if (txTracker != null) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,14 @@ public void testGetTimeShouldRequestServerTime() {
18841884
assertThat(time > 0, equalTo(true));
18851885
}
18861886

1887+
/**
1888+
* @see DATAREDIS-269
1889+
*/
1890+
@Test
1891+
public void clientSetNameWorksCorrectly() {
1892+
connection.setClientName("foo".getBytes());
1893+
}
1894+
18871895
protected void verifyResults(List<Object> expected) {
18881896
assertEquals(expected, getResults());
18891897
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,16 @@ public void testShutdownInDelegatedCorrectlyToNativeConnection() {
17101710
verify(nativeConnection, times(1)).shutdown(eq(ShutdownOption.NOSAVE));
17111711
}
17121712

1713+
/**
1714+
* @see DATAREDIS-269
1715+
*/
1716+
@Test
1717+
public void settingClientNameShouldDelegateToNativeConnection() {
1718+
1719+
connection.setClientName("foo");
1720+
verify(nativeConnection, times(1)).setClientName(eq("foo".getBytes()));
1721+
}
1722+
17131723
protected List<Object> getResults() {
17141724
return actual;
17151725
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,12 @@ public void testInfoBySection() throws Exception {
246246
public void testZAddMultiple() {
247247
super.testZAddMultiple();
248248
}
249+
250+
/**
251+
* @see DATAREDIS-269
252+
*/
253+
@Test(expected = UnsupportedOperationException.class)
254+
public void clientSetNameWorksCorrectly() {
255+
super.clientSetNameWorksCorrectly();
256+
}
249257
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,13 @@ public void testRestoreBadData() {
191191
public void testRestoreExistingKey() {
192192
super.testRestoreExistingKey();
193193
}
194+
195+
/**
196+
* @see DATAREDIS-269
197+
*/
198+
@Test(expected = UnsupportedOperationException.class)
199+
public void clientSetNameWorksCorrectly() {
200+
super.clientSetNameWorksCorrectly();
201+
}
202+
194203
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,4 +809,13 @@ public void testExecuteShouldConvertArrayReplyCorrectly() {
809809
public void testPsetEx() throws Exception {
810810
super.testPsetEx();
811811
}
812+
813+
/**
814+
* @see DATAREDIS-269
815+
*/
816+
@Override
817+
@Test(expected = UnsupportedOperationException.class)
818+
public void clientSetNameWorksCorrectly() {
819+
super.clientSetNameWorksCorrectly();
820+
}
812821
}

0 commit comments

Comments
 (0)