forked from baidu/dlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistributedReentrantLockTest.java
272 lines (224 loc) · 8.32 KB
/
DistributedReentrantLockTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.baidu.fsg.dlock;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baidu.fsg.dlock.domain.DLockConfig;
import com.baidu.fsg.dlock.jedis.JedisClient;
import com.baidu.fsg.dlock.processor.impl.RedisLockProcessor;
import com.baidu.fsg.dlock.utils.ReflectionUtils;
/**
* Test for {@link DistributedReentrantLock}.<p>
*
* <B>Note:</B>No redis mock is provided, to be continue for mock test.<br>
* If you can't connect to BDRP, try to connect VPN first.
*
* @author yutianbao
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:dlock/spring-dlock.xml" })
public class DistributedReentrantLockTest {
@Resource
private RedisLockProcessor lockProcessor;
@Resource
private JedisClient jedisClient;
/**
* DistributedReentrantLock instance
*/
private Lock lockOnServer1;
/**
* DistributedReentrantLock. Simulate as another server
*/
private Lock lockOnServer2;
/**
* DistributedReentrantLock. Used for single server test
*/
private Lock lockSingleServer;
/**
* CountDownLatch used for multi servers
*/
private static CountDownLatch cdLatch = new CountDownLatch(2);
@Before
public void setup() {
DLockConfig singleServerLockConfig = new DLockConfig("USER_LOCK", "778899", 1000, TimeUnit.MILLISECONDS);
lockSingleServer = new DistributedReentrantLock(singleServerLockConfig, lockProcessor);
// The retry thread's execute interval is depended on The lease duration (Retry interval = lease ms * 0.75)
DLockConfig multiServerLockConfig = new DLockConfig("USER_LOCK", "778899", 500, TimeUnit.MILLISECONDS);
lockOnServer1 = new DistributedReentrantLock(multiServerLockConfig, lockProcessor);
lockOnServer2 = new DistributedReentrantLock(multiServerLockConfig, lockProcessor);
// Delete unique key of the last round test
jedisClient.del(singleServerLockConfig.getLockUniqueKey());
jedisClient.del(multiServerLockConfig.getLockUniqueKey());
}
/**
* Case1: Test for reentrant feature
*/
@Test
public void testReentrant() throws Exception {
// Reentrant caller
reentrantGateOne(lockSingleServer);
// Assert no holder for this lock
checkHoldCnt(lockSingleServer);
}
/**
* Case2: Test for one server - multi threads
*/
@Test
public void testSingleServer() throws Exception {
// Thread max work elapse is 2s, greater than the lease duration (1s)
// It means when the thread hold the lock, lease must be expanded.
launchSingleServer(50, "S1", lockSingleServer, 2000);
// joinThreads(threads);
checkHoldCnt(lockSingleServer);
}
/**
* Case3: Test for multi servers - multi threads
*/
@Test
public void testMultiServer() throws InterruptedException {
try {
// Launch servers "S1", "S2"
new ServerThread(50, "S1", lockOnServer1, 2000).start();
new ServerThread(50, "S2", lockOnServer2, 2000).start();
// Check
cdLatch.await();
checkHoldCnt(lockOnServer1);
checkHoldCnt(lockOnServer2);
} catch (Exception e) {
Assert.fail();
}
}
/**
* Launch threads on a single server
*
* @param totalThread
* @param serverName
* @param lock
* @param maxWorkElapsed
*/
private static void launchSingleServer(int totalThread, String serverName, Lock lock, int maxWorkElapsed) {
List<Thread> threads = new ArrayList<>(totalThread);
for (int i = 0; i < totalThread; i++) {
String tName = serverName + "-t" + StringUtils.leftPad(i + "", 2, "0");
threads.add(i, new RedisTestThread(tName, lock, maxWorkElapsed));
}
threads.forEach(t -> t.start());
threads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
}
});
System.out.println("**** All Done **** " + serverName);
}
/**
* Check hold cnt of lock
*/
private void checkHoldCnt(Lock lock) throws Exception {
AtomicInteger holdCnt = (AtomicInteger) ReflectionUtils.getProperty(lock, "holdCount");
Assert.assertEquals(0, holdCnt.get());
}
/**
* Server thread
*/
static class ServerThread extends Thread {
int totalThread;
String serverName;
Lock lock;
int maxWorkElapsed;
ServerThread(int totalThread, String serverName, Lock lock, int maxWorkElapsed) {
super(serverName);
this.totalThread = totalThread;
this.serverName = serverName;
this.lock = lock;
this.maxWorkElapsed = maxWorkElapsed;
setDaemon(true);
}
@Override
public void run() {
launchSingleServer(totalThread, serverName, lock, maxWorkElapsed);
cdLatch.countDown();
}
}
/**
* Test thread
*/
static class RedisTestThread extends Thread {
private Lock redisLock;
private int maxWorkElapse;
RedisTestThread(String name, Lock redisLock, int maxWorkElapse) {
super(name);
this.redisLock = redisLock;
this.maxWorkElapse = maxWorkElapse;
setDaemon(true);
}
@Override
public void run() {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start <= 60 * 1000L) {
try {
long t = System.currentTimeMillis();
redisLock.lock();
System.out.println("*********************** Lock block ***********************");
System.out.println(getName() + " >>>>> get lock time:" + (System.currentTimeMillis() - t));
doWork();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
redisLock.unlock();
}
break;
}
System.out.println(getName() + " > Done!");
System.out.println();
}
private void doWork() throws InterruptedException {
int sleepTime = new Random().nextInt(maxWorkElapse);
sleep(sleepTime);
System.out.println(getName() + " >>> worked done for:" + sleepTime);
}
}
/**
* Reentrant test tool methods
*/
private void reentrantGateOne(Lock lock) {
try {
System.out.println("method1 ready to lock.");
lock.lock();
System.out.println("method1 lock success. ++");
reentrantGateTwo(lock);
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("method1 ready to release lock.");
lock.unlock();
System.out.println("method1 unlocked success. --");
}
}
private void reentrantGateTwo(Lock lock) {
try {
System.out.println(">>>>method2 ready to lock.");
lock.lock();
System.out.println(">>>>method2 lock success. ++");
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(">>>>method2 ready to release lock.");
lock.unlock();
System.out.println(">>>>method2 unlocked success. --");
}
}
}