Skip to content

Commit

Permalink
Remove fair version of KeyedLock (elastic#96411)
Browse files Browse the repository at this point in the history
No production code uses of the fair version exist and only one test case needed adjusting.
  • Loading branch information
original-brownbear committed May 29, 2023
1 parent 0e95bb4 commit dae0c6f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@
public final class KeyedLock<T> {

private final ConcurrentMap<T, KeyLock> map = ConcurrentCollections.newConcurrentMapWithAggressiveConcurrency();
private final boolean fair;

/**
* Creates a new lock
* @param fair Use fair locking, ie threads get the lock in the order they requested it
*/
public KeyedLock(boolean fair) {
this.fair = fair;
}

/**
* Creates a non-fair lock
*/
public KeyedLock() {
this(false);
}

/**
* 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
Expand Down Expand Up @@ -90,7 +74,7 @@ public Releasable tryAcquire(T key) {
}

private ReleasableLock tryCreateNewLock(T key) {
KeyLock newLock = new KeyLock(fair);
KeyLock newLock = new KeyLock();
newLock.lock();
KeyLock keyLock = map.putIfAbsent(key, newLock);
if (keyLock == null) {
Expand Down Expand Up @@ -140,8 +124,8 @@ public void close() {

@SuppressWarnings("serial")
private static final class KeyLock extends ReentrantLock {
KeyLock(boolean fair) {
super(fair);
KeyLock() {
super();
}

private final AtomicInteger count = new AtomicInteger(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class KeyedLockTests extends ESTestCase {
public void testIfMapEmptyAfterLotsOfAcquireAndReleases() throws InterruptedException {
ConcurrentHashMap<String, Integer> counter = new ConcurrentHashMap<>();
ConcurrentHashMap<String, AtomicInteger> safeCounter = new ConcurrentHashMap<>();
KeyedLock<String> connectionLock = new KeyedLock<>(randomBoolean());
KeyedLock<String> connectionLock = new KeyedLock<>();
String[] names = new String[randomIntBetween(1, 40)];
for (int i = 0; i < names.length; i++) {
names[i] = randomRealisticUnicodeOfLengthBetween(10, 20);
Expand Down

0 comments on commit dae0c6f

Please sign in to comment.