Skip to content

Commit

Permalink
Data encoding should be executed on client thread only #1083
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Oct 2, 2017
1 parent c45fbde commit a41d7db
Show file tree
Hide file tree
Showing 33 changed files with 332 additions and 569 deletions.
Expand Up @@ -176,7 +176,7 @@ public RFuture<Boolean> removeAllAsync(Collection<?> c) {
} }


String channelName = RedissonSemaphore.getChannelName(getSemaphoreName()); String channelName = RedissonSemaphore.getChannelName(getSemaphoreName());
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES_6, return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local count = 0; " + "local count = 0; " +
"for i = 1, #ARGV, 1 do " "for i = 1, #ARGV, 1 do "
+ "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 1 then " + "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 1 then "
Expand All @@ -189,7 +189,7 @@ public RFuture<Boolean> removeAllAsync(Collection<?> c) {
+ "return 1;" + "return 1;"
+ "end;" + "end;"
+ "return 0 ", + "return 0 ",
Arrays.<Object>asList(getName(), getSemaphoreName(), channelName), c.toArray()); Arrays.<Object>asList(getName(), getSemaphoreName(), channelName), encode(c).toArray());
} }


@Override @Override
Expand Down
2 changes: 1 addition & 1 deletion redisson/src/main/java/org/redisson/RedissonBuckets.java
Expand Up @@ -71,7 +71,7 @@ public <V> Map<String, V> get(String... keys) {
return Collections.emptyMap(); return Collections.emptyMap();
} }


RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("MGET", new MapGetAllDecoder(Arrays.<Object>asList(keys), 0), ValueType.OBJECTS); RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("MGET", new MapGetAllDecoder(Arrays.<Object>asList(keys), 0));
RFuture<Map<String, V>> future = commandExecutor.readAsync(keys[0], new DelegateDecoderCodec(codec), command, keys); RFuture<Map<String, V>> future = commandExecutor.readAsync(keys[0], new DelegateDecoderCodec(codec), command, keys);
return commandExecutor.get(future); return commandExecutor.get(future);
} }
Expand Down
30 changes: 12 additions & 18 deletions redisson/src/main/java/org/redisson/RedissonDelayedQueue.java
Expand Up @@ -28,11 +28,7 @@
import org.redisson.api.RTopic; import org.redisson.api.RTopic;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec; import org.redisson.client.codec.LongCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommand.ValueType;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.convertor.BooleanReplayConvertor;
import org.redisson.client.protocol.convertor.VoidReplayConvertor;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;


import io.netty.util.internal.ThreadLocalRandom; import io.netty.util.internal.ThreadLocalRandom;
Expand All @@ -45,8 +41,6 @@
*/ */
public class RedissonDelayedQueue<V> extends RedissonExpirable implements RDelayedQueue<V> { public class RedissonDelayedQueue<V> extends RedissonExpirable implements RDelayedQueue<V> {


private static final RedisCommand<Void> EVAL_OFFER = new RedisCommand<Void>("EVAL", new VoidReplayConvertor(), 9);

private final QueueTransferService queueTransferService; private final QueueTransferService queueTransferService;


protected RedissonDelayedQueue(QueueTransferService queueTransferService, Codec codec, final CommandAsyncExecutor commandExecutor, String name) { protected RedissonDelayedQueue(QueueTransferService queueTransferService, Codec codec, final CommandAsyncExecutor commandExecutor, String name) {
Expand Down Expand Up @@ -108,7 +102,7 @@ public RFuture<Void> offerAsync(V e, long delay, TimeUnit timeUnit) {
long timeout = System.currentTimeMillis() + delayInMs; long timeout = System.currentTimeMillis() + delayInMs;


long randomId = ThreadLocalRandom.current().nextLong(); long randomId = ThreadLocalRandom.current().nextLong();
return commandExecutor.evalWriteAsync(getName(), codec, EVAL_OFFER, return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_VOID,
"local value = struct.pack('dLc0', tonumber(ARGV[2]), string.len(ARGV[3]), ARGV[3]);" "local value = struct.pack('dLc0', tonumber(ARGV[2]), string.len(ARGV[3]), ARGV[3]);"
+ "redis.call('zadd', KEYS[2], ARGV[1], value);" + "redis.call('zadd', KEYS[2], ARGV[1], value);"
+ "redis.call('rpush', KEYS[3], value);" + "redis.call('rpush', KEYS[3], value);"
Expand All @@ -120,7 +114,7 @@ public RFuture<Void> offerAsync(V e, long delay, TimeUnit timeUnit) {
+ "end;" + "end;"
, ,
Arrays.<Object>asList(getName(), getTimeoutSetName(), getQueueName(), getChannelName()), Arrays.<Object>asList(getName(), getTimeoutSetName(), getQueueName(), getChannelName()),
timeout, randomId, e); timeout, randomId, encode(e));
} }


@Override @Override
Expand Down Expand Up @@ -286,7 +280,7 @@ public RFuture<Boolean> removeAsync(Object o) {
} }


protected RFuture<Boolean> removeAsync(Object o, int count) { protected RFuture<Boolean> removeAsync(Object o, int count) {
return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 5), return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local s = redis.call('llen', KEYS[1]);" + "local s = redis.call('llen', KEYS[1]);" +
"for i = 0, s-1, 1 do " "for i = 0, s-1, 1 do "
+ "local v = redis.call('lindex', KEYS[1], i);" + "local v = redis.call('lindex', KEYS[1], i);"
Expand All @@ -298,7 +292,7 @@ protected RFuture<Boolean> removeAsync(Object o, int count) {
+ "end; " + "end; "
+ "end;" + + "end;" +
"return 0;", "return 0;",
Arrays.<Object>asList(getQueueName(), getTimeoutSetName()), o); Arrays.<Object>asList(getQueueName(), getTimeoutSetName()), encode(o));
} }


@Override @Override
Expand All @@ -307,7 +301,7 @@ public RFuture<Boolean> containsAllAsync(Collection<?> c) {
return newSucceededFuture(true); return newSucceededFuture(true);
} }


return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local s = redis.call('llen', KEYS[1]);" + "local s = redis.call('llen', KEYS[1]);" +
"for i = 0, s-1, 1 do " "for i = 0, s-1, 1 do "
+ "local v = redis.call('lindex', KEYS[1], i);" + "local v = redis.call('lindex', KEYS[1], i);"
Expand All @@ -320,7 +314,7 @@ public RFuture<Boolean> containsAllAsync(Collection<?> c) {
+ "end; " + "end; "
+ "end;" + + "end;" +
"return #ARGV == 0 and 1 or 0;", "return #ARGV == 0 and 1 or 0;",
Collections.<Object>singletonList(getQueueName()), c.toArray()); Collections.<Object>singletonList(getQueueName()), encode(c).toArray());
} }


@Override @Override
Expand All @@ -339,7 +333,7 @@ public RFuture<Boolean> removeAllAsync(Collection<?> c) {
return newSucceededFuture(false); return newSucceededFuture(false);
} }


return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 5, ValueType.OBJECTS), return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local result = 0;" + "local result = 0;" +
"local s = redis.call('llen', KEYS[1]);" + "local s = redis.call('llen', KEYS[1]);" +
"local i = 0;" + "local i = 0;" +
Expand All @@ -360,7 +354,7 @@ public RFuture<Boolean> removeAllAsync(Collection<?> c) {
+ "i = i + 1;" + "i = i + 1;"
+ "end; " + "end; "
+ "return result;", + "return result;",
Arrays.<Object>asList(getQueueName(), getTimeoutSetName()), c.toArray()); Arrays.<Object>asList(getQueueName(), getTimeoutSetName()), encode(c).toArray());
} }


@Override @Override
Expand All @@ -379,7 +373,7 @@ public RFuture<Boolean> retainAllAsync(Collection<?> c) {
return deleteAsync(); return deleteAsync();
} }


return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local changed = 0; " + "local changed = 0; " +
"local items = redis.call('lrange', KEYS[1], 0, -1); " "local items = redis.call('lrange', KEYS[1], 0, -1); "
+ "local i = 1; " + "local i = 1; "
Expand All @@ -399,7 +393,7 @@ public RFuture<Boolean> retainAllAsync(Collection<?> c) {
+ "i = i + 1; " + "i = i + 1; "
+ "end; " + "end; "
+ "return changed; ", + "return changed; ",
Collections.<Object>singletonList(getQueueName()), c.toArray()); Collections.<Object>singletonList(getQueueName()), encode(c).toArray());
} }


@Override @Override
Expand Down Expand Up @@ -458,7 +452,7 @@ public RFuture<V> pollLastAndOfferFirstToAsync(String queueName) {


@Override @Override
public RFuture<Boolean> containsAsync(Object o) { public RFuture<Boolean> containsAsync(Object o) {
return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 4), return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local s = redis.call('llen', KEYS[1]);" + "local s = redis.call('llen', KEYS[1]);" +
"for i = 0, s-1, 1 do " "for i = 0, s-1, 1 do "
+ "local v = redis.call('lindex', KEYS[1], i);" + "local v = redis.call('lindex', KEYS[1], i);"
Expand All @@ -468,7 +462,7 @@ public RFuture<Boolean> containsAsync(Object o) {
+ "end; " + "end; "
+ "end;" + + "end;" +
"return 0;", "return 0;",
Collections.<Object>singletonList(getQueueName()), o); Collections.<Object>singletonList(getQueueName()), encode(o));
} }


@Override @Override
Expand Down
9 changes: 3 additions & 6 deletions redisson/src/main/java/org/redisson/RedissonDeque.java
Expand Up @@ -23,9 +23,7 @@
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommand.ValueType;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.convertor.VoidReplayConvertor;
import org.redisson.client.protocol.decoder.ListFirstObjectDecoder; import org.redisson.client.protocol.decoder.ListFirstObjectDecoder;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;


Expand All @@ -38,7 +36,6 @@
*/ */
public class RedissonDeque<V> extends RedissonQueue<V> implements RDeque<V> { public class RedissonDeque<V> extends RedissonQueue<V> implements RDeque<V> {


private static final RedisCommand<Void> RPUSH_VOID = new RedisCommand<Void>("RPUSH", new VoidReplayConvertor(), 2, ValueType.OBJECTS);
private static final RedisCommand<Object> LRANGE_SINGLE = new RedisCommand<Object>("LRANGE", new ListFirstObjectDecoder()); private static final RedisCommand<Object> LRANGE_SINGLE = new RedisCommand<Object>("LRANGE", new ListFirstObjectDecoder());




Expand All @@ -57,7 +54,7 @@ public void addFirst(V e) {


@Override @Override
public RFuture<Void> addFirstAsync(V e) { public RFuture<Void> addFirstAsync(V e) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.LPUSH_VOID, getName(), e); return commandExecutor.writeAsync(getName(), codec, RedisCommands.LPUSH_VOID, getName(), encode(e));
} }


@Override @Override
Expand All @@ -67,7 +64,7 @@ public void addLast(V e) {


@Override @Override
public RFuture<Void> addLastAsync(V e) { public RFuture<Void> addLastAsync(V e) {
return commandExecutor.writeAsync(getName(), codec, RPUSH_VOID, getName(), e); return commandExecutor.writeAsync(getName(), codec, RedisCommands.RPUSH_VOID, getName(), encode(e));
} }




Expand Down Expand Up @@ -128,7 +125,7 @@ public boolean offerFirst(V e) {


@Override @Override
public RFuture<Boolean> offerFirstAsync(V e) { public RFuture<Boolean> offerFirstAsync(V e) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.LPUSH_BOOLEAN, getName(), e); return commandExecutor.writeAsync(getName(), codec, RedisCommands.LPUSH_BOOLEAN, getName(), encode(e));
} }


@Override @Override
Expand Down
4 changes: 2 additions & 2 deletions redisson/src/main/java/org/redisson/RedissonHyperLogLog.java
Expand Up @@ -69,14 +69,14 @@ public void mergeWith(String... otherLogNames) {


@Override @Override
public RFuture<Boolean> addAsync(V obj) { public RFuture<Boolean> addAsync(V obj) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.PFADD, getName(), obj); return commandExecutor.writeAsync(getName(), codec, RedisCommands.PFADD, getName(), encode(obj));
} }


@Override @Override
public RFuture<Boolean> addAllAsync(Collection<V> objects) { public RFuture<Boolean> addAllAsync(Collection<V> objects) {
List<Object> args = new ArrayList<Object>(objects.size() + 1); List<Object> args = new ArrayList<Object>(objects.size() + 1);
args.add(getName()); args.add(getName());
args.addAll(objects); encode(args, objects);
return commandExecutor.writeAsync(getName(), codec, RedisCommands.PFADD, getName(), args.toArray()); return commandExecutor.writeAsync(getName(), codec, RedisCommands.PFADD, getName(), args.toArray());
} }


Expand Down

0 comments on commit a41d7db

Please sign in to comment.