Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
public final class NamedLockFactoryAdapter {
public static final String TIME_KEY = "aether.syncContext.named.time";

public static final String EXCLUSIVE_TIME_KEY = "aether.syncContext.named.exclusiveTime";

public static final long DEFAULT_TIME = 900L;

public static final String TIME_UNIT_KEY = "aether.syncContext.named.time.unit";
Expand Down Expand Up @@ -111,6 +113,8 @@ private static class AdaptedLockSyncContext implements SyncContext {

private final long time;

private final long exclusiveTime;

private final TimeUnit timeUnit;

private final int retry;
Expand All @@ -128,7 +132,8 @@ private AdaptedLockSyncContext(
this.shared = shared;
this.lockNaming = lockNaming;
this.namedLockFactory = namedLockFactory;
this.time = getTime(session);
this.time = getTime(session, DEFAULT_TIME, TIME_KEY);
this.exclusiveTime = getTime(session, DEFAULT_TIME, EXCLUSIVE_TIME_KEY);
this.timeUnit = getTimeUnit(session);
this.retry = getRetry(session);
this.retryWait = getRetryWait(session);
Expand All @@ -137,6 +142,9 @@ private AdaptedLockSyncContext(
if (time < 0L) {
throw new IllegalArgumentException(TIME_KEY + " value cannot be negative");
}
if (exclusiveTime < 0L) {
throw new IllegalArgumentException(EXCLUSIVE_TIME_KEY + " value cannot be negative");
}
if (retry < 0L) {
throw new IllegalArgumentException(RETRY_KEY + " value cannot be negative");
}
Expand All @@ -145,8 +153,8 @@ private AdaptedLockSyncContext(
}
}

private long getTime(final RepositorySystemSession session) {
return ConfigUtils.getLong(session, DEFAULT_TIME, TIME_KEY);
private long getTime(final RepositorySystemSession session, long defaultValue, String... keys) {
return ConfigUtils.getLong(session, defaultValue, keys);
}

private TimeUnit getTimeUnit(final RepositorySystemSession session) {
Expand All @@ -168,40 +176,38 @@ public void acquire(Collection<? extends Artifact> artifacts, Collection<? exten
return;
}

final String lockKindString = shared ? "shared" : "exclusive";
final String lockTimeoutString = (shared ? time : exclusiveTime) + " " + timeUnit;

final int attempts = retry + 1;
final ArrayList<IllegalStateException> illegalStateExceptions = new ArrayList<>();
for (int attempt = 1; attempt <= attempts; attempt++) {
LOGGER.trace(
"Attempt {}: Need {} {} lock(s) for {}", attempt, keys.size(), shared ? "read" : "write", keys);
LOGGER.trace("Attempt {}: Need {} {} lock(s) for {}", attempt, keys.size(), lockKindString, keys);
int acquiredLockCount = 0;
try {
if (attempt > 1) {
Thread.sleep(retryWait);
}
for (String key : keys) {
NamedLock namedLock = namedLockFactory.getLock(key);
LOGGER.trace("Acquiring {} lock for '{}'", shared ? "read" : "write", key);
LOGGER.trace("Acquiring {} lock for '{}'", lockKindString, key);

boolean locked;
if (shared) {
locked = namedLock.lockShared(time, timeUnit);
} else {
locked = namedLock.lockExclusively(time, timeUnit);
locked = namedLock.lockExclusively(exclusiveTime, timeUnit);
}

if (!locked) {
String timeStr = time + " " + timeUnit;
LOGGER.trace(
"Failed to acquire {} lock for '{}' in {}",
shared ? "read" : "write",
key,
timeStr);
"Failed to acquire {} lock for '{}' in {}", lockKindString, key, lockTimeoutString);

namedLock.close();
closeAll();
illegalStateExceptions.add(new IllegalStateException(
"Attempt " + attempt + ": Could not acquire " + (shared ? "read" : "write")
+ " lock for '" + namedLock.name() + "' in " + timeStr));
"Attempt " + attempt + ": Could not acquire " + lockKindString + " lock for '"
+ namedLock.name() + "' in " + lockTimeoutString));
break;
} else {
locks.push(namedLock);
Expand All @@ -218,10 +224,10 @@ public void acquire(Collection<? extends Artifact> artifacts, Collection<? exten
}
}
if (!illegalStateExceptions.isEmpty()) {
String message = "Could not acquire " + (shared ? "shared" : "exclusive") + " lock for "
+ lockSubjects(artifacts, metadatas) + " in " + time + " " + timeUnit
String message = "Could not acquire " + lockKindString + " lock for "
+ lockSubjects(artifacts, metadatas) + " in " + lockTimeoutString
+ "; consider using '" + TIME_KEY
+ "' property to increase lock timeout to a value that fits your environment";
+ "' property to increase shared lock timeout to a value that fits your environment";
FailedToAcquireLockException ex = new FailedToAcquireLockException(shared, message);
illegalStateExceptions.forEach(ex::addSuppressed);
throw namedLockFactory.onFailure(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void before() throws IOException {
when(session.getLocalRepository()).thenReturn(localRepository);
HashMap<String, Object> config = new HashMap<>();
config.put(NamedLockFactoryAdapter.TIME_KEY, String.valueOf(ADAPTER_TIME));
config.put(NamedLockFactoryAdapter.EXCLUSIVE_TIME_KEY, String.valueOf(ADAPTER_TIME));
config.put(NamedLockFactoryAdapter.TIME_UNIT_KEY, ADAPTER_TIME_UNIT.name());
when(session.getConfigProperties()).thenReturn(config);
}
Expand Down
Loading