Skip to content

Commit 5ff68b2

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-513 - Fix RedisServerCommands.time() failure when in pipeline mode.
We fixed a glitch in Jedis/Lettuce RedisConneciton TIME command when used in pipeline or transaction mode. Original pull request: #199.
1 parent d9db5d9 commit 5ff68b2

File tree

5 files changed

+71
-21
lines changed

5 files changed

+71
-21
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -786,8 +786,8 @@ public Boolean expire(byte[] key, long seconds) {
786786

787787
/*
788788
* @see DATAREDIS-286 to avoid overflow in Jedis
789-
*
790-
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
789+
*
790+
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
791791
* fix for: https://github.com/xetorthio/jedis/pull/575
792792
*/
793793
if (seconds > Integer.MAX_VALUE) {
@@ -973,8 +973,8 @@ public Boolean pExpire(byte[] key, long millis) {
973973

974974
/*
975975
* @see DATAREDIS-286 to avoid overflow in Jedis
976-
*
977-
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
976+
*
977+
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
978978
* fix for: https://github.com/xetorthio/jedis/pull/575
979979
*/
980980
if (millis > Integer.MAX_VALUE) {
@@ -3090,13 +3090,21 @@ public <T> T evalSha(byte[] scriptSha1, ReturnType returnType, int numKeys, byte
30903090
@Override
30913091
public Long time() {
30923092

3093-
List<String> serverTimeInformation = this.jedis.time();
3093+
try {
30943094

3095-
Assert.notEmpty(serverTimeInformation, "Received invalid result from server. Expected 2 items in collection.");
3096-
Assert.isTrue(serverTimeInformation.size() == 2,
3097-
"Received invalid nr of arguments from redis server. Expected 2 received " + serverTimeInformation.size());
3095+
if (isPipelined()) {
3096+
pipeline(new JedisResult(pipeline.time(), JedisConverters.toTimeConverter()));
3097+
return null;
3098+
}
30983099

3099-
return Converters.toTimeMillis(serverTimeInformation.get(0), serverTimeInformation.get(1));
3100+
if (isQueueing()) {
3101+
transaction(new JedisResult(transaction.time(), JedisConverters.toTimeConverter()));
3102+
return null;
3103+
}
3104+
return JedisConverters.toTimeConverter().convert(jedis.time());
3105+
} catch (Exception ex) {
3106+
throw convertJedisAccessException(ex);
3107+
}
31003108
}
31013109

31023110
/*

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ abstract public class JedisConverters extends Converters {
6767
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_CLIENT_INFO_CONVERTER = new StringToRedisClientInfoConverter();
6868
private static final Converter<redis.clients.jedis.Tuple, Tuple> TUPLE_CONVERTER;
6969
private static final ListConverter<redis.clients.jedis.Tuple, Tuple> TUPLE_LIST_TO_TUPLE_LIST_CONVERTER;
70+
private static final Converter<List<String>, Long> STRING_LIST_TO_TIME_CONVERTER;
7071

7172
public static final byte[] PLUS_BYTES;
7273
public static final byte[] MINUS_BYTES;
@@ -96,6 +97,19 @@ public Tuple convert(redis.clients.jedis.Tuple source) {
9697
MINUS_BYTES = toBytes("-");
9798
POSITIVE_INFINITY_BYTES = toBytes("+inf");
9899
NEGATIVE_INFINITY_BYTES = toBytes("-inf");
100+
101+
STRING_LIST_TO_TIME_CONVERTER = new Converter<List<String>, Long>() {
102+
103+
@Override
104+
public Long convert(List<String> source) {
105+
106+
Assert.notEmpty(source, "Received invalid result from server. Expected 2 items in collection.");
107+
Assert.isTrue(source.size() == 2,
108+
"Received invalid nr of arguments from redis server. Expected 2 received " + source.size());
109+
110+
return toTimeMillis(source.get(0), source.get(1));
111+
}
112+
};
99113
}
100114

101115
public static Converter<String, byte[]> stringToBytes() {
@@ -320,4 +334,8 @@ private static byte[] boundaryToBytes(Boundary boundary, byte[] inclPrefix, byte
320334
return buffer.array();
321335

322336
}
337+
338+
static Converter<List<String>, Long> toTimeConverter() {
339+
return STRING_LIST_TO_TIME_CONVERTER;
340+
}
323341
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,16 +3060,19 @@ public void subscribe(MessageListener listener, byte[]... channels) {
30603060
*/
30613061
@Override
30623062
public Long time() {
3063-
try {
3064-
3065-
List<byte[]> result = getConnection().time();
30663063

3067-
Assert.notEmpty(result, "Received invalid result from server. Expected 2 items in collection.");
3068-
Assert.isTrue(result.size() == 2, "Received invalid nr of arguments from redis server. Expected 2 received "
3069-
+ result.size());
3064+
try {
30703065

3071-
return Converters.toTimeMillis(new String(result.get(0)), new String(result.get(1)));
3066+
if (isPipelined()) {
3067+
pipeline(new LettuceResult(getAsyncConnection().time(), LettuceConverters.toTimeConverter()));
3068+
return null;
3069+
}
3070+
if (isQueueing()) {
3071+
transaction(new LettuceTxResult(getConnection().time(), LettuceConverters.toTimeConverter()));
3072+
return null;
3073+
}
30723074

3075+
return LettuceConverters.toTimeConverter().convert(getConnection().time());
30733076
} catch (Exception ex) {
30743077
throw convertLettuceAccessException(ex);
30753078
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ abstract public class LettuceConverters extends Converters {
7878
private static final Converter<List<byte[]>, Map<byte[], byte[]>> BYTES_LIST_TO_MAP;
7979
private static final Converter<List<byte[]>, List<Tuple>> BYTES_LIST_TO_TUPLE_LIST_CONVERTER;
8080
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
81+
private static final Converter<List<byte[]>, Long> BYTES_LIST_TO_TIME_CONVERTER;
8182

8283
public static final byte[] PLUS_BYTES;
8384
public static final byte[] MINUS_BYTES;
@@ -203,6 +204,19 @@ public List<Tuple> convert(List<byte[]> source) {
203204
MINUS_BYTES = toBytes("-");
204205
POSITIVE_INFINITY_BYTES = toBytes("+inf");
205206
NEGATIVE_INFINITY_BYTES = toBytes("-inf");
207+
208+
BYTES_LIST_TO_TIME_CONVERTER = new Converter<List<byte[]>, Long>() {
209+
210+
@Override
211+
public Long convert(List<byte[]> source) {
212+
213+
Assert.notEmpty(source, "Received invalid result from server. Expected 2 items in collection.");
214+
Assert.isTrue(source.size() == 2,
215+
"Received invalid nr of arguments from redis server. Expected 2 received " + source.size());
216+
217+
return toTimeMillis(LettuceConverters.toString(source.get(0)), LettuceConverters.toString(source.get(1)));
218+
}
219+
};
206220
}
207221

208222
public static List<Tuple> toTuple(List<byte[]> list) {
@@ -523,4 +537,7 @@ private static String boundaryToBytes(Boundary boundary, byte[] inclPrefix, byte
523537
return toString(buffer.array());
524538
}
525539

540+
static Converter<List<byte[]>, Long> toTimeConverter() {
541+
return BYTES_LIST_TO_TIME_CONVERTER;
542+
}
526543
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.junit.Assume.*;
2121
import static org.springframework.data.redis.SpinBarrier.*;
@@ -1919,13 +1919,17 @@ public void testLastSave() {
19191919

19201920
/**
19211921
* @see DATAREDIS-206
1922+
* @see DATAREDIS-513
19221923
*/
19231924
@Test
19241925
public void testGetTimeShouldRequestServerTime() {
19251926

1926-
Long time = connectionFactory.getConnection().time();
1927-
assertThat(time, notNullValue());
1928-
assertThat(time > 0, equalTo(true));
1927+
actual.add(connection.time());
1928+
1929+
List<Object> results = getResults();
1930+
assertThat(results, is(not(empty())));
1931+
assertThat(results.get(0), notNullValue());
1932+
assertThat((Long) results.get(0) > 0, equalTo(true));
19291933
}
19301934

19311935
/**

0 commit comments

Comments
 (0)