Skip to content

Commit 132a967

Browse files
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 3542798 commit 132a967

13 files changed

+130
-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>-</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
@@ -2230,4 +2230,22 @@ private List<Object> convertResults(List<Object> results, Queue<Converter> conve
22302230
return convertedResults;
22312231
}
22322232

2233+
/*
2234+
* (non-Javadoc)
2235+
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(java.lang.String)
2236+
*/
2237+
@Override
2238+
public void setClientName(byte[] name) {
2239+
this.delegate.setClientName(name);
2240+
}
2241+
2242+
/*
2243+
* (non-Javadoc)
2244+
* @see org.springframework.data.redis.connection.StringRedisConnection#setClientName(java.lang.String)
2245+
*/
2246+
@Override
2247+
public void setClientName(String name) {
2248+
setClientName(this.serializer.serialize(name));
2249+
}
2250+
22332251
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,11 @@ public enum ShutdownOption {
161161
* @since 1.1
162162
*/
163163
Long time();
164+
165+
/**
166+
* Assign given name to current connection.
167+
*
168+
* @since 1.3
169+
*/
170+
void setClientName(byte[] name);
164171
}

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,20 @@ public Long time() {
28082808
return Converters.toTimeMillis(serverTimeInformation.get(0), serverTimeInformation.get(1));
28092809
}
28102810

2811+
/*
2812+
* (non-Javadoc)
2813+
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(java.lang.String)
2814+
*/
2815+
@Override
2816+
public void setClientName(byte[] name) {
2817+
2818+
if (isPipelined() || isQueueing()) {
2819+
throw new UnsupportedOperationException("'CLIENT SETNAME' is not suppored in transacton / pipeline mode.");
2820+
}
2821+
2822+
jedis.clientSetname(name);
2823+
}
2824+
28112825
/**
28122826
* Specifies if pipelined results should be converted to the expected data type. If false, results of
28132827
* {@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
@@ -1185,4 +1185,9 @@ public <T> T evalSha(String scriptSha1, ReturnType returnType, int numKeys, byte
11851185
public Long time() {
11861186
throw new UnsupportedOperationException("The 'TIME' command is not supported by the JRedis driver.");
11871187
}
1188+
1189+
@Override
1190+
public void setClientName(byte[] name) {
1191+
throw new UnsupportedOperationException("'CLIENT SETNAME' is not supported by the JRedis driver.");
1192+
}
11881193
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,6 +2903,20 @@ public Long time() {
29032903
return Converters.toTimeMillis(new String(result.get(0)), new String(result.get(1)));
29042904
}
29052905

2906+
@Override
2907+
public void setClientName(byte[] name) {
2908+
2909+
if (isQueueing()) {
2910+
pipeline(new LettuceStatusResult(getAsyncConnection().clientSetname(name)));
2911+
return;
2912+
}
2913+
if (isQueueing()) {
2914+
transaction(new LettuceTxResult(getConnection().clientSetname(name)));
2915+
return;
2916+
}
2917+
getAsyncConnection().clientSetname(name);
2918+
}
2919+
29062920
/**
29072921
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
29082922
* {@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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,24 @@ public Long time() {
22222222
return SrpConverters.toTimeAsLong(reply.data());
22232223
}
22242224

2225+
/*
2226+
* (non-Javadoc)
2227+
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(byte[])
2228+
*/
2229+
@Override
2230+
public void setClientName(byte[] name) {
2231+
try {
2232+
if (isPipelined()) {
2233+
pipeline(new SrpStatusResult(pipeline.client_setname(name)));
2234+
return;
2235+
}
2236+
2237+
this.client.client_setname(name);
2238+
} catch (Exception ex) {
2239+
throw convertSrpAccessException(ex);
2240+
}
2241+
}
2242+
22252243
private List<Object> closeTransaction() {
22262244
List<Object> results = Collections.emptyList();
22272245
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)