Skip to content

Commit b225076

Browse files
authored
[MRESOLVER-522] Improve congested file locks behaviour (#455) (#461)
Instead to immediately give up and sleep, they will sit a while to enter critical region. This is important for "hot" locks. Explanation: currently a "loser" will _immediately give up_ and will go to sleep 100ms if cannot enter critical region, even if winner exits critical region within next 5ms. The retry is needed to ensure that it is retried as much as given time/unit takes, that was before consumed by constant retries+sleeps. The logic still works, as if tryLock spends time/unit waiting on criticalRegion (which is possible only on VERY HIGHLY congested locks), there will be no retry happening. Backport of 089796b --- https://issues.apache.org/jira/browse/MRESOLVER-522
1 parent fc969c2 commit b225076

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/FileLockNamedLock.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ public FileLockNamedLock(final String name, final FileChannel fileChannel, final
8484

8585
@Override
8686
protected boolean doLockShared(final long time, final TimeUnit unit) throws InterruptedException {
87-
return retry(time, unit, RETRY_SLEEP_MILLIS, this::doLockShared, null, false);
87+
return retry(time, unit, RETRY_SLEEP_MILLIS, () -> doLockSharedPerform(time, unit), null, false);
8888
}
8989

9090
@Override
9191
protected boolean doLockExclusively(final long time, final TimeUnit unit) throws InterruptedException {
92-
return retry(time, unit, RETRY_SLEEP_MILLIS, this::doLockExclusively, null, false);
92+
return retry(time, unit, RETRY_SLEEP_MILLIS, () -> doLockExclusivelyPerform(time, unit), null, false);
9393
}
9494

95-
private Boolean doLockShared() {
96-
if (criticalRegion.tryLock()) {
95+
private Boolean doLockSharedPerform(final long time, final TimeUnit unit) throws InterruptedException {
96+
if (criticalRegion.tryLock(time, unit)) {
9797
try {
9898
Deque<Boolean> steps = threadSteps.computeIfAbsent(Thread.currentThread(), k -> new ArrayDeque<>());
9999
FileLock obtainedLock = fileLockRef.get();
@@ -127,8 +127,8 @@ private Boolean doLockShared() {
127127
return null;
128128
}
129129

130-
private Boolean doLockExclusively() {
131-
if (criticalRegion.tryLock()) {
130+
private Boolean doLockExclusivelyPerform(final long time, final TimeUnit unit) throws InterruptedException {
131+
if (criticalRegion.tryLock(time, unit)) {
132132
try {
133133
Deque<Boolean> steps = threadSteps.computeIfAbsent(Thread.currentThread(), k -> new ArrayDeque<>());
134134
FileLock obtainedLock = fileLockRef.get();

0 commit comments

Comments
 (0)