refactoring

pull/544/head
Nikita 9 years ago
parent c7d2afbaf4
commit 222563b376

@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@ -196,18 +197,26 @@ public class RedissonMultiLock implements Lock {
}
protected boolean sync(Map<RLock, Future<Boolean>> tryLockFutures) {
for (Future<Boolean> future : tryLockFutures.values()) {
List<RLock> lockedLocks = new ArrayList<RLock>(tryLockFutures.size());
RuntimeException latestException = null;
for (Entry<RLock, Future<Boolean>> entry : tryLockFutures.entrySet()) {
try {
if (!future.syncUninterruptibly().getNow()) {
unlockInner(tryLockFutures.keySet());
return false;
if (entry.getValue().syncUninterruptibly().getNow()) {
lockedLocks.add(entry.getKey());
}
} catch (RuntimeException e) {
unlockInner(tryLockFutures.keySet());
throw e;
latestException = e;
}
}
if (lockedLocks.size() < tryLockFutures.size()) {
unlockInner(lockedLocks);
if (latestException != null) {
throw latestException;
}
return false;
}
return true;
}

@ -47,7 +47,7 @@ public class RedissonRedLock extends RedissonMultiLock {
}
protected boolean sync(Map<RLock, Future<Boolean>> tryLockFutures) {
Queue<RLock> lockedLocks = new ConcurrentLinkedQueue<RLock>();
List<RLock> lockedLocks = new ArrayList<RLock>(tryLockFutures.size());
RuntimeException latestException = null;
for (Entry<RLock, Future<Boolean>> entry : tryLockFutures.entrySet()) {
try {
@ -61,7 +61,6 @@ public class RedissonRedLock extends RedissonMultiLock {
if (lockedLocks.size() < minLocksAmount(locks)) {
unlockInner(lockedLocks);
lockedLocks.clear();
if (latestException != null) {
throw latestException;
}

@ -4,12 +4,14 @@ import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.core.RLock;
import org.redisson.core.RedissonMultiLock;
import org.redisson.core.RedissonRedLock;
import io.netty.channel.nio.NioEventLoopGroup;
import org.redisson.RedisRunner.RedisProcess;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
@ -30,6 +32,7 @@ public class RedissonRedLockTest {
Thread t1 = new Thread() {
public void run() {
lock2.lock();
lock3.lock();
};
};
@ -41,7 +44,49 @@ public class RedissonRedLockTest {
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
lock.lock();
System.out.println("123");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
lock.unlock();
};
};
t.start();
t.join(1000);
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
Assert.assertFalse(lock.tryLock());
assertThat(redis1.stop()).isEqualTo(0);
assertThat(redis2.stop()).isEqualTo(0);
}
@Test
public void testLockSuccess() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance(6320);
RedisProcess redis2 = redisTestMultilockInstance(6321);
RedissonClient client1 = createClient("127.0.0.1:6320");
RedissonClient client2 = createClient("127.0.0.1:6321");
RLock lock1 = client1.getLock("lock1");
RLock lock2 = client1.getLock("lock2");
RLock lock3 = client2.getLock("lock3");
Thread t1 = new Thread() {
public void run() {
lock3.lock();
};
};
t1.start();
t1.join();
Thread t = new Thread() {
public void run() {
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
lock.lock();
try {
Thread.sleep(3000);
@ -56,7 +101,6 @@ public class RedissonRedLockTest {
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
lock.lock();
System.out.println("1234");
lock.unlock();
assertThat(redis1.stop()).isEqualTo(0);

Loading…
Cancel
Save