Skip to content

Commit 66e30fe

Browse files
authored
Merge pull request #10 from crossoverJie/fix-1.0.5
Fix 1.0.5
2 parents 96fe6a2 + d8c7280 commit 66e30fe

File tree

3 files changed

+220
-44
lines changed

3 files changed

+220
-44
lines changed

src/main/java/com/crossoverjie/distributed/lock/RedisLock.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.crossoverjie.distributed.lock;
22

33
import com.crossoverjie.distributed.constant.RedisToolsConstant;
4-
import com.crossoverjie.distributed.limit.RedisLimit;
54
import com.crossoverjie.distributed.util.ScriptUtil;
65
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
@@ -10,9 +9,7 @@
109
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
1110
import redis.clients.jedis.Jedis;
1211
import redis.clients.jedis.JedisCluster;
13-
import redis.clients.jedis.JedisCommands;
1412

15-
import java.io.IOException;
1613
import java.util.Collections;
1714

1815
/**
@@ -93,11 +90,7 @@ public boolean tryLock(String key, String request) {
9390
((Jedis) connection).close();
9491
}else {
9592
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
96-
try {
97-
((JedisCluster) connection).close();
98-
} catch (IOException e) {
99-
logger.error("IOException",e);
100-
}
93+
10194
}
10295

10396
if (LOCK_MSG.equals(result)) {
@@ -120,14 +113,11 @@ public void lock(String key, String request) throws InterruptedException {
120113
for (; ;) {
121114
if (connection instanceof Jedis){
122115
result = ((Jedis)connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
123-
((Jedis) connection).close();
116+
if (LOCK_MSG.equals(result)){
117+
((Jedis) connection).close();
118+
}
124119
}else {
125120
result = ((JedisCluster)connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
126-
try {
127-
((JedisCluster) connection).close();
128-
} catch (IOException e) {
129-
logger.error("IOException",e);
130-
}
131121
}
132122

133123
if (LOCK_MSG.equals(result)) {
@@ -156,14 +146,11 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
156146
while (blockTime >= 0) {
157147
if (connection instanceof Jedis){
158148
result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME) ;
159-
((Jedis) connection).close();
149+
if (LOCK_MSG.equals(result)){
150+
((Jedis) connection).close();
151+
}
160152
}else {
161153
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME) ;
162-
try {
163-
((JedisCluster) connection).close();
164-
} catch (IOException e) {
165-
logger.error("IOException",e);
166-
}
167154
}
168155
if (LOCK_MSG.equals(result)) {
169156
return true;
@@ -195,11 +182,6 @@ public boolean tryLock(String key, String request, int expireTime) {
195182
((Jedis) connection).close();
196183
}else {
197184
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
198-
try {
199-
((JedisCluster) connection).close();
200-
} catch (IOException e) {
201-
logger.error("IOException",e);
202-
}
203185
}
204186

205187
if (LOCK_MSG.equals(result)) {
@@ -229,11 +211,7 @@ public boolean unlock(String key, String request) {
229211
((Jedis) connection).close();
230212
} else if (connection instanceof JedisCluster) {
231213
result = ((JedisCluster) connection).eval(script, Collections.singletonList(lockPrefix + key), Collections.singletonList(request));
232-
try {
233-
((JedisCluster) connection).close();
234-
} catch (IOException e) {
235-
logger.error("IOException",e);
236-
}
214+
237215
} else {
238216
//throw new RuntimeException("instance is error") ;
239217
return false;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.crossoverjie.distributed.lock;
2+
3+
import com.crossoverjie.distributed.constant.RedisToolsConstant;
4+
import com.google.common.util.concurrent.ThreadFactoryBuilder;
5+
import org.junit.Before;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.data.redis.connection.RedisClusterConfiguration;
9+
import org.springframework.data.redis.connection.RedisNode;
10+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
11+
import redis.clients.jedis.HostAndPort;
12+
import redis.clients.jedis.JedisCluster;
13+
import redis.clients.jedis.JedisPool;
14+
import redis.clients.jedis.JedisPoolConfig;
15+
16+
import java.util.concurrent.*;
17+
18+
/**
19+
* real test by Redis cluster
20+
*/
21+
public class RealRedisClusterLockTest {
22+
23+
private static Logger logger = LoggerFactory.getLogger(RealRedisClusterLockTest.class);
24+
private static ExecutorService executorServicePool;
25+
26+
27+
private static RedisLock redisLock;
28+
29+
private static JedisPool jedisPool;
30+
31+
32+
public static void main(String[] args) throws InterruptedException {
33+
RealRedisClusterLockTest redisLockTest = new RealRedisClusterLockTest();
34+
redisLockTest.init();
35+
initThread();
36+
37+
for (int i = 0; i < 50; i++) {
38+
executorServicePool.execute(new Worker(i));
39+
}
40+
41+
executorServicePool.shutdown();
42+
while (!executorServicePool.awaitTermination(1, TimeUnit.SECONDS)) {
43+
logger.info("worker running");
44+
}
45+
logger.info("worker over");
46+
47+
}
48+
49+
@Before
50+
public void setBefore() {
51+
init();
52+
53+
}
54+
55+
private void init() {
56+
57+
JedisPoolConfig config = new JedisPoolConfig();
58+
config.setMaxIdle(100);
59+
config.setMaxTotal(100);
60+
config.setMaxWaitMillis(10000);
61+
config.setTestOnBorrow(true);
62+
config.setTestOnReturn(true);
63+
64+
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
65+
redisClusterConfiguration.addClusterNode(new RedisNode("10.19.13.51", 7000));
66+
67+
//集群
68+
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration) ;
69+
70+
jedisConnectionFactory.setTimeout(100000);
71+
jedisConnectionFactory.afterPropertiesSet();
72+
//jedisConnectionFactory.setShardInfo(new JedisShardInfo("47.98.194.60", 6379));
73+
//JedisCluster jedisCluster = new JedisCluster(hostAndPort);
74+
75+
HostAndPort hostAndPort = new HostAndPort("10.19.13.51", 7000);
76+
JedisCluster jedisCluster = new JedisCluster(hostAndPort);
77+
redisLock = new RedisLock.Builder(jedisConnectionFactory, RedisToolsConstant.CLUSTER)
78+
.lockPrefix("lock_")
79+
.sleepTime(100)
80+
.build();
81+
82+
}
83+
84+
public static void initThread() {
85+
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
86+
.setNameFormat("current-thread-%d").build();
87+
executorServicePool = new ThreadPoolExecutor(350, 350, 0L, TimeUnit.MILLISECONDS,
88+
new ArrayBlockingQueue<Runnable>(200), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
89+
90+
}
91+
92+
93+
private static class Worker implements Runnable {
94+
95+
private int index;
96+
97+
public Worker(int index) {
98+
this.index = index;
99+
}
100+
101+
@Override
102+
public void run() {
103+
//测试非阻塞锁
104+
boolean limit = redisLock.tryLock("abc", "12345");
105+
if (limit) {
106+
logger.info("加锁成功=========");
107+
boolean unlock = redisLock.unlock("abc", "12345");
108+
logger.info("解锁结果===[{}]",unlock);
109+
} else {
110+
logger.info("加锁失败");
111+
112+
}
113+
114+
//测试非阻塞锁 + 超时时间
115+
//boolean limit = redisLock.tryLock("abc", "12345",1000);
116+
//if (limit) {
117+
// logger.info("加锁成功=========");
118+
// boolean unlock = redisLock.unlock("abc", "12345");
119+
// logger.info("解锁结果===[{}]",unlock);
120+
//} else {
121+
// logger.info("加锁失败");
122+
//
123+
//}
124+
125+
126+
//测试阻塞锁
127+
//try {
128+
// redisLock.lock("abc", "12345");
129+
// logger.info("加锁成功=========");
130+
//} catch (InterruptedException e) {
131+
// e.printStackTrace();
132+
//}
133+
//redisLock.unlock("abc","12345") ;
134+
135+
136+
137+
//测试阻塞锁 + 阻塞时间
138+
//try {
139+
// boolean limit = redisLock.lock("abc", "12345", 100);
140+
// if (limit) {
141+
// logger.info("加锁成功=========");
142+
// boolean unlock = redisLock.unlock("abc", "12345");
143+
// logger.info("解锁结果===[{}]",unlock);
144+
// } else {
145+
// logger.info("加锁失败");
146+
//
147+
// }
148+
//} catch (InterruptedException e) {
149+
// e.printStackTrace();
150+
//}
151+
}
152+
}
153+
154+
155+
}

src/test/java/com/crossoverjie/distributed/lock/RedisLockTest.java renamed to src/test/java/com/crossoverjie/distributed/lock/RealRedisLockTest.java

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package com.crossoverjie.distributed.lock;
22

33
import com.crossoverjie.distributed.constant.RedisToolsConstant;
4-
import com.crossoverjie.distributed.limit.RedisLimit;
54
import com.google.common.util.concurrent.ThreadFactoryBuilder;
65
import org.junit.Before;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
98
import org.springframework.data.redis.connection.RedisClusterConfiguration;
109
import org.springframework.data.redis.connection.RedisNode;
1110
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
12-
import redis.clients.jedis.*;
11+
import redis.clients.jedis.HostAndPort;
12+
import redis.clients.jedis.JedisCluster;
13+
import redis.clients.jedis.JedisPool;
14+
import redis.clients.jedis.JedisPoolConfig;
1315

1416
import java.util.concurrent.*;
1517

16-
public class RedisLockTest {
18+
public class RealRedisLockTest {
1719

18-
private static Logger logger = LoggerFactory.getLogger(RedisLockTest.class);
20+
private static Logger logger = LoggerFactory.getLogger(RealRedisLockTest.class);
1921
private static ExecutorService executorServicePool;
2022

2123

@@ -25,11 +27,11 @@ public class RedisLockTest {
2527

2628

2729
public static void main(String[] args) throws InterruptedException {
28-
RedisLockTest redisLockTest = new RedisLockTest();
30+
RealRedisLockTest redisLockTest = new RealRedisLockTest();
2931
redisLockTest.init();
3032
initThread();
3133

32-
for (int i = 0; i < 100; i++) {
34+
for (int i = 0; i < 50; i++) {
3335
executorServicePool.execute(new Worker(i));
3436
}
3537

@@ -50,8 +52,8 @@ public void setBefore() {
5052
private void init() {
5153

5254
JedisPoolConfig config = new JedisPoolConfig();
53-
config.setMaxIdle(50);
54-
config.setMaxTotal(50);
55+
config.setMaxIdle(60);
56+
config.setMaxTotal(60);
5557
config.setMaxWaitMillis(10000);
5658
config.setTestOnBorrow(true);
5759
config.setTestOnReturn(true);
@@ -100,14 +102,55 @@ public Worker(int index) {
100102

101103
@Override
102104
public void run() {
103-
boolean limit = redisLock.tryLock("abc", "12345");
104-
if (limit) {
105+
//测试非阻塞锁
106+
//boolean limit = redisLock.tryLock("abc", "12345");
107+
//if (limit) {
108+
// logger.info("加锁成功=========");
109+
// boolean unlock = redisLock.unlock("abc", "12345");
110+
// logger.info("解锁结果===[{}]",unlock);
111+
//} else {
112+
// logger.info("加锁失败");
113+
//
114+
//}
115+
116+
//测试非阻塞锁 + 超时时间
117+
//boolean limit = redisLock.tryLock("abc", "12345",1000);
118+
//if (limit) {
119+
// logger.info("加锁成功=========");
120+
// boolean unlock = redisLock.unlock("abc", "12345");
121+
// logger.info("解锁结果===[{}]",unlock);
122+
//} else {
123+
// logger.info("加锁失败");
124+
//
125+
//}
126+
127+
128+
129+
//测试阻塞锁
130+
try {
131+
redisLock.lock("abc", "12345");
105132
logger.info("加锁成功=========");
106-
} else {
107-
logger.info("加锁失败");
108-
133+
redisLock.unlock("abc","12345") ;
134+
} catch (InterruptedException e) {
135+
e.printStackTrace();
109136
}
110-
redisLock.unlock("abc","12345") ;
137+
138+
139+
140+
//测试阻塞锁 + 阻塞时间
141+
//try {
142+
// boolean limit = redisLock.lock("abc", "12345", 100);
143+
// if (limit) {
144+
// logger.info("加锁成功=========");
145+
// boolean unlock = redisLock.unlock("abc", "12345");
146+
// logger.info("解锁结果===[{}]",unlock);
147+
// } else {
148+
// logger.info("加锁失败");
149+
//
150+
// }
151+
//} catch (InterruptedException e) {
152+
// e.printStackTrace();
153+
//}
111154
}
112155
}
113156

0 commit comments

Comments
 (0)