From d75a83d68c0979f9b2e9f8b5256367fbdf6ed82a Mon Sep 17 00:00:00 2001 From: Nikita Date: Sat, 4 Jul 2015 11:18:59 +0300 Subject: [PATCH] minor optimization --- .../java/org/redisson/RedissonAtomicLong.java | 16 +++------ .../org/redisson/RedissonBlockingQueue.java | 14 +++----- .../org/redisson/RedissonCountDownLatch.java | 21 ++++-------- src/main/java/org/redisson/RedissonList.java | 32 ++++++----------- src/main/java/org/redisson/RedissonLock.java | 34 +++++++------------ 5 files changed, 39 insertions(+), 78 deletions(-) diff --git a/src/main/java/org/redisson/RedissonAtomicLong.java b/src/main/java/org/redisson/RedissonAtomicLong.java index 08967591d..d0960d712 100644 --- a/src/main/java/org/redisson/RedissonAtomicLong.java +++ b/src/main/java/org/redisson/RedissonAtomicLong.java @@ -54,12 +54,10 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong @Override public boolean compareAndSet(final long expect, final long update) { - ArrayList keys = new ArrayList(); - keys.add(getName()); return new RedissonScript(connectionManager).evalR( "if redis.call('get', KEYS[1]) == ARGV[1] then redis.call('set', KEYS[1], ARGV[2]); return true else return false end", RScript.ReturnType.BOOLEAN, - keys, Collections.EMPTY_LIST, Arrays.asList(expect, update)); + Collections.singletonList(getName()), Collections.EMPTY_LIST, Arrays.asList(expect, update)); } @Override @@ -79,22 +77,18 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong @Override public long getAndAdd(final long delta) { - ArrayList keys = new ArrayList(); - keys.add(getName()); return new RedissonScript(connectionManager).evalR( "local v = redis.call('get', KEYS[1]) or 0; redis.call('set', KEYS[1], v + ARGV[1]); return tonumber(v)", RScript.ReturnType.INTEGER, - keys, Collections.EMPTY_LIST, Collections.singletonList(delta)); + Collections.singletonList(getName()), Collections.EMPTY_LIST, Collections.singletonList(delta)); } @Override public long getAndSet(final long newValue) { - ArrayList keys = new ArrayList(); - keys.add(getName()); return new RedissonScript(connectionManager).evalR( "local v = redis.call('get', KEYS[1]) or 0; redis.call('set', KEYS[1], ARGV[1]); return tonumber(v)", RScript.ReturnType.INTEGER, - keys, Collections.EMPTY_LIST, Collections.singletonList(newValue)); + Collections.singletonList(getName()), Collections.EMPTY_LIST, Collections.singletonList(newValue)); } @Override @@ -118,12 +112,10 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong @Override public void set(final long newValue) { - ArrayList keys = new ArrayList(); - keys.add(getName()); new RedissonScript(connectionManager).evalR( "redis.call('set', KEYS[1], ARGV[1])", RScript.ReturnType.STATUS, - keys, Collections.EMPTY_LIST, Collections.singletonList(newValue)); + Collections.singletonList(getName()), Collections.EMPTY_LIST, Collections.singletonList(newValue)); } public String toString() { diff --git a/src/main/java/org/redisson/RedissonBlockingQueue.java b/src/main/java/org/redisson/RedissonBlockingQueue.java index 870f6b091..24843ba86 100644 --- a/src/main/java/org/redisson/RedissonBlockingQueue.java +++ b/src/main/java/org/redisson/RedissonBlockingQueue.java @@ -36,7 +36,7 @@ import org.redisson.core.RScript; * {@link LinkedBlockingQueue} where items are added as soon as * blpop returns. All {@link BlockingQueue} methods are actually * delegated to this intermediary queue. - * + * * @author pdeschen@gmail.com * @author Nikita Koksharov */ @@ -81,7 +81,7 @@ public class RedissonBlockingQueue extends RedissonQueue implements RBlock throws InterruptedException { return pollLastAndOfferFirstTo(queue.getName(), timeout, unit); } - + @Override public V pollLastAndOfferFirstTo(final String queueName, final long timeout, final TimeUnit unit) throws InterruptedException { return connectionManager.write(getName(), new SyncInterruptedOperation() { @@ -91,7 +91,7 @@ public class RedissonBlockingQueue extends RedissonQueue implements RBlock } }); } - + @Override public int remainingCapacity() { return Integer.MAX_VALUE; @@ -102,14 +102,12 @@ public class RedissonBlockingQueue extends RedissonQueue implements RBlock if (c == null) { throw new NullPointerException(); } - ArrayList keys = new ArrayList(); - keys.add(getName()); List list = new RedissonScript(connectionManager).eval( "local vals = redis.call('lrange', KEYS[1], 0, -1); " + "redis.call('ltrim', KEYS[1], -1, 0); " + "return vals", RScript.ReturnType.MAPVALUELIST, - keys); + Collections.singletonList(getName())); c.addAll(list); return list.size(); } @@ -123,15 +121,13 @@ public class RedissonBlockingQueue extends RedissonQueue implements RBlock throw new NullPointerException(); } - ArrayList keys = new ArrayList(); - keys.add(getName()); List list = new RedissonScript(connectionManager).evalR( "local elemNum = math.min(ARGV[1], redis.call('llen', KEYS[1])) - 1;" + "local vals = redis.call('lrange', KEYS[1], 0, elemNum); " + "redis.call('ltrim', KEYS[1], elemNum + 1, -1); " + "return vals", RScript.ReturnType.MAPVALUELIST, - keys, Collections.emptyList(), Collections.singletonList(maxElements)); + Collections.singletonList(getName()), Collections.emptyList(), Collections.singletonList(maxElements)); c.addAll(list); return list.size(); } diff --git a/src/main/java/org/redisson/RedissonCountDownLatch.java b/src/main/java/org/redisson/RedissonCountDownLatch.java index c748e5f10..3214d652e 100644 --- a/src/main/java/org/redisson/RedissonCountDownLatch.java +++ b/src/main/java/org/redisson/RedissonCountDownLatch.java @@ -193,15 +193,14 @@ public class RedissonCountDownLatch extends RedissonObject implements RCountDown if (getCount() <= 0) { return; } - ArrayList keys = new ArrayList(); - keys.add(getName()); + new RedissonScript(connectionManager).evalR( "local v = redis.call('decr', KEYS[1]);" + "if v <= 0 then redis.call('del', KEYS[1]) end;" + "if v == 0 then redis.call('publish', ARGV[2], ARGV[1]) end;" + "return 'OK'", RScript.ReturnType.STATUS, - keys, Collections.singletonList(zeroCountMessage), Collections.singletonList(getChannelName())); + Collections.singletonList(getName()), Collections.singletonList(zeroCountMessage), Collections.singletonList(getChannelName())); } private String getEntryName() { @@ -218,13 +217,11 @@ public class RedissonCountDownLatch extends RedissonObject implements RCountDown } private long getCountInner() { - ArrayList keys = new ArrayList(); - keys.add(getName()); + Long val = new RedissonScript(connectionManager).eval( "return redis.call('get', KEYS[1])", RScript.ReturnType.INTEGER, - keys); - + Collections.singletonList(getName())); if (val == null) { return 0; @@ -233,23 +230,19 @@ public class RedissonCountDownLatch extends RedissonObject implements RCountDown } @Override - public boolean trySetCount(final long count) { - ArrayList keys = new ArrayList(); - keys.add(getName()); + public boolean trySetCount(long count) { return new RedissonScript(connectionManager).evalR( "if redis.call('exists', KEYS[1]) == 0 then redis.call('set', KEYS[1], ARGV[2]); redis.call('publish', ARGV[3], ARGV[1]); return true else return false end", RScript.ReturnType.BOOLEAN, - keys, Collections.singletonList(newCountMessage), Arrays.asList(count, getChannelName())); + Collections.singletonList(getName()), Collections.singletonList(newCountMessage), Arrays.asList(count, getChannelName())); } @Override public boolean delete() { - ArrayList keys = new ArrayList(); - keys.add(getName()); Boolean deleted = new RedissonScript(connectionManager).evalR( "if redis.call('del', KEYS[1]) == 1 then redis.call('publish', ARGV[2], ARGV[1]); return true else return false end", RScript.ReturnType.BOOLEAN, - keys, Collections.singletonList(newCountMessage), Collections.singletonList(getChannelName())); + Collections.singletonList(getName()), Collections.singletonList(newCountMessage), Collections.singletonList(getChannelName())); if (!deleted) { throw new IllegalStateException(); } diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index 5d69cb590..036839d4f 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -95,7 +95,7 @@ public class RedissonList extends RedissonExpirable implements RList { public boolean add(V e) { return addAll(Collections.singleton(e)); } - + @Override public Future addAsync(V e) { return addAllAsync(Collections.singleton(e)); @@ -147,12 +147,12 @@ public class RedissonList extends RedissonExpirable implements RList { public boolean addAll(Collection c) { return connectionManager.get(addAllAsync(c)); } - + @Override public Future addAllAsync(final Collection c) { if (c.isEmpty()) { return connectionManager.getGroup().next().newSucceededFuture(false); - } + } return connectionManager.writeAsync(getName(), new AsyncOperation() { @Override public void execute(final Promise promise, RedisAsyncConnection async) { @@ -188,9 +188,6 @@ public class RedissonList extends RedissonExpirable implements RList { // insert into middle of list - ArrayList keys = new ArrayList(); - keys.add(getName()); - return "OK".equals(new RedissonScript(connectionManager).evalR( "local ind = table.remove(ARGV); " + // index is last parameter "local tail = redis.call('lrange', KEYS[1], ind, -1); " + @@ -199,7 +196,7 @@ public class RedissonList extends RedissonExpirable implements RList { "for i, v in ipairs(tail) do redis.call('rpush', KEYS[1], v) end;" + "return 'OK'", RScript.ReturnType.STATUS, - keys, new ArrayList(coll), Collections.singletonList(index))); + Collections.singletonList(getName()), new ArrayList(coll), Collections.singletonList(index))); } else { // append to list return addAll(coll); @@ -260,7 +257,7 @@ public class RedissonList extends RedissonExpirable implements RList { } }); } - + @Override public V get(int index) { checkIndex(index); @@ -296,15 +293,14 @@ public class RedissonList extends RedissonExpirable implements RList { public V set(final int index, final V element) { checkIndex(index); - ArrayList keys = new ArrayList(); - keys.add(getName()); - return new RedissonScript(connectionManager).evalR( "local v = redis.call('lindex', KEYS[1], ARGV[2]); " + "redis.call('lset', KEYS[1], ARGV[2], ARGV[1]); " + "return v", RScript.ReturnType.VALUE, - keys, Collections.singletonList(element), Collections.singletonList(index) + Collections.singletonList(getName()), + Collections.singletonList(element), + Collections.singletonList(index) ); } @@ -338,8 +334,6 @@ public class RedissonList extends RedissonExpirable implements RList { }); } // else - ArrayList keys = new ArrayList(); - keys.add(getName()); return new RedissonScript(connectionManager).evalR( "local v = redis.call('lindex', KEYS[1], ARGV[1]); " + "local tail = redis.call('lrange', KEYS[1], ARGV[1]);" + @@ -347,7 +341,7 @@ public class RedissonList extends RedissonExpirable implements RList { "for i, v in ipairs(tail) do redis.call('rpush', KEYS[1], v) end;" + "return v", RScript.ReturnType.VALUE, - keys, Collections.emptyList(), Collections.singletonList(index)); + Collections.singletonList(getName()), Collections.emptyList(), Collections.singletonList(index)); } @Override @@ -356,14 +350,12 @@ public class RedissonList extends RedissonExpirable implements RList { return -1; } - ArrayList keys = new ArrayList(); - keys.add(getName()); Long index = new RedissonScript(connectionManager).eval( "local s = redis.call('llen', KEYS[1]);" + "for i = 0, s, 1 do if ARGV[1] == redis.call('lindex', KEYS[1], i) then return i end end;" + "return -1", RScript.ReturnType.INTEGER, - keys, o); + Collections.singletonList(getName()), o); return index.intValue(); } @@ -373,14 +365,12 @@ public class RedissonList extends RedissonExpirable implements RList { return -1; } - ArrayList keys = new ArrayList(); - keys.add(getName()); Long index = new RedissonScript(connectionManager).eval( "local s = redis.call('llen', KEYS[1]);" + "for i = s, 0, -1 do if ARGV[1] == redis.call('lindex', KEYS[1], i) then return i end end;" + "return -1", RScript.ReturnType.INTEGER, - keys, o); + Collections.singletonList(getName()), o); return index.intValue(); } diff --git a/src/main/java/org/redisson/RedissonLock.java b/src/main/java/org/redisson/RedissonLock.java index fcf7ffff5..73f6bd1e7 100644 --- a/src/main/java/org/redisson/RedissonLock.java +++ b/src/main/java/org/redisson/RedissonLock.java @@ -221,6 +221,7 @@ public class RedissonLock extends RedissonExpirable implements RLock { private Long tryLockInner() { Long ttlRemaining = tryLockInner(LOCK_EXPIRATION_INTERVAL_SECONDS, TimeUnit.SECONDS); + // lock acquired if (ttlRemaining == null) { newRefreshTask(); } @@ -229,8 +230,9 @@ public class RedissonLock extends RedissonExpirable implements RLock { private void newRefreshTask() { if (refreshTaskMap.containsKey(getName())) { - return; + return; } + Timeout task = connectionManager.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { @@ -239,6 +241,7 @@ public class RedissonLock extends RedissonExpirable implements RLock { newRefreshTask(); // reschedule itself } }, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS); + if (refreshTaskMap.putIfAbsent(getName(), task) != null) { task.cancel(); } @@ -248,22 +251,17 @@ public class RedissonLock extends RedissonExpirable implements RLock { * Stop refresh timer * @return true if timer was stopped successfully */ - private boolean stopRefreshTask() { - boolean returnValue =false; - Timeout task = refreshTaskMap.get(getName()); + private void stopRefreshTask() { + Timeout task = refreshTaskMap.remove(getName()); if (task != null) { - returnValue = task.cancel(); - refreshTaskMap.remove(getName()); + task.cancel(); } - return returnValue; } private Long tryLockInner(final long leaseTime, final TimeUnit unit) { internalLockLeaseTime = unit.toMillis(leaseTime); - ArrayList keys = new ArrayList(); - keys.add(getName()); return new RedissonScript(connectionManager) .evalR("local v = redis.call('get', KEYS[1]); " + "if (v == false) then " + @@ -278,7 +276,7 @@ public class RedissonLock extends RedissonExpirable implements RLock { " return redis.call('pttl', KEYS[1]); " + "end", RScript.ReturnType.INTEGER, - keys, Collections.singletonList(id.toString() + "-" + Thread.currentThread().getId()), Collections.singletonList(internalLockLeaseTime)); + Collections.singletonList(getName()), Collections.singletonList(id.toString() + "-" + Thread.currentThread().getId()), Collections.singletonList(internalLockLeaseTime)); } public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException { @@ -340,8 +338,6 @@ public class RedissonLock extends RedissonExpirable implements RLock { @Override public void unlock() { - ArrayList keys = new ArrayList(); - keys.add(getName()); String opStatus = new RedissonScript(connectionManager) .evalR("local v = redis.call('get', KEYS[1]); " + "if (v == false) then " + @@ -363,7 +359,7 @@ public class RedissonLock extends RedissonExpirable implements RLock { " return nil; " + "end", RScript.ReturnType.STATUS, - keys, Arrays.asList(id.toString() + "-" + Thread.currentThread().getId(), unlockMessage), Arrays.asList(internalLockLeaseTime, getChannelName())); + Collections.singletonList(getName()), Arrays.asList(id.toString() + "-" + Thread.currentThread().getId(), unlockMessage), Arrays.asList(internalLockLeaseTime, getChannelName())); if ("OK".equals(opStatus)) { stopRefreshTask(); } else if ("FALSE".equals(opStatus)) { @@ -383,13 +379,11 @@ public class RedissonLock extends RedissonExpirable implements RLock { @Override public void forceUnlock() { - ArrayList keys = new ArrayList(); - keys.add(getName()); stopRefreshTask(); new RedissonScript(connectionManager) .evalR("redis.call('del', KEYS[1]); redis.call('publish', ARGV[2], ARGV[1]); return 'OK'", RScript.ReturnType.STATUS, - keys, Collections.singletonList(unlockMessage), Collections.singletonList(getChannelName())); + Collections.singletonList(getName()), Collections.singletonList(unlockMessage), Collections.singletonList(getChannelName())); } @Override @@ -404,8 +398,6 @@ public class RedissonLock extends RedissonExpirable implements RLock { @Override public boolean isHeldByCurrentThread() { - ArrayList keys = new ArrayList(); - keys.add(getName()); String opStatus = new RedissonScript(connectionManager) .eval("local v = redis.call('get', KEYS[1]); " + "if (v == false) then " + @@ -419,14 +411,12 @@ public class RedissonLock extends RedissonExpirable implements RLock { " end;" + "end", RScript.ReturnType.STATUS, - keys, id.toString() + "-" + Thread.currentThread().getId()); + Collections.singletonList(getName()), id.toString() + "-" + Thread.currentThread().getId()); return "OK".equals(opStatus); } @Override public int getHoldCount() { - ArrayList keys = new ArrayList(); - keys.add(getName()); Long opStatus = new RedissonScript(connectionManager) .eval("local v = redis.call('get', KEYS[1]); " + "if (v == false) then " + @@ -436,7 +426,7 @@ public class RedissonLock extends RedissonExpirable implements RLock { " return o['c']; " + "end", RScript.ReturnType.INTEGER, - keys, id.toString() + "-" + Thread.currentThread().getId()); + Collections.singletonList(getName()), id.toString() + "-" + Thread.currentThread().getId()); return opStatus.intValue(); }