Skip to content

Commit dae0c6f

Browse files
Remove fair version of KeyedLock (#96411)
No production code uses of the fair version exist and only one test case needed adjusting.
1 parent 0e95bb4 commit dae0c6f

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

server/src/main/java/org/elasticsearch/common/util/concurrent/KeyedLock.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@
2626
public final class KeyedLock<T> {
2727

2828
private final ConcurrentMap<T, KeyLock> map = ConcurrentCollections.newConcurrentMapWithAggressiveConcurrency();
29-
private final boolean fair;
30-
31-
/**
32-
* Creates a new lock
33-
* @param fair Use fair locking, ie threads get the lock in the order they requested it
34-
*/
35-
public KeyedLock(boolean fair) {
36-
this.fair = fair;
37-
}
38-
39-
/**
40-
* Creates a non-fair lock
41-
*/
42-
public KeyedLock() {
43-
this(false);
44-
}
4529

4630
/**
4731
* Acquires a lock for the given key. The key is compared by it's equals method not by object identity. The lock can be acquired
@@ -90,7 +74,7 @@ public Releasable tryAcquire(T key) {
9074
}
9175

9276
private ReleasableLock tryCreateNewLock(T key) {
93-
KeyLock newLock = new KeyLock(fair);
77+
KeyLock newLock = new KeyLock();
9478
newLock.lock();
9579
KeyLock keyLock = map.putIfAbsent(key, newLock);
9680
if (keyLock == null) {
@@ -140,8 +124,8 @@ public void close() {
140124

141125
@SuppressWarnings("serial")
142126
private static final class KeyLock extends ReentrantLock {
143-
KeyLock(boolean fair) {
144-
super(fair);
127+
KeyLock() {
128+
super();
145129
}
146130

147131
private final AtomicInteger count = new AtomicInteger(1);

server/src/test/java/org/elasticsearch/common/util/concurrent/KeyedLockTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class KeyedLockTests extends ESTestCase {
2929
public void testIfMapEmptyAfterLotsOfAcquireAndReleases() throws InterruptedException {
3030
ConcurrentHashMap<String, Integer> counter = new ConcurrentHashMap<>();
3131
ConcurrentHashMap<String, AtomicInteger> safeCounter = new ConcurrentHashMap<>();
32-
KeyedLock<String> connectionLock = new KeyedLock<>(randomBoolean());
32+
KeyedLock<String> connectionLock = new KeyedLock<>();
3333
String[] names = new String[randomIntBetween(1, 40)];
3434
for (int i = 0; i < names.length; i++) {
3535
names[i] = randomRealisticUnicodeOfLengthBetween(10, 20);

0 commit comments

Comments
 (0)