use of org.commonjava.util.partyline.lock.LockLevel in project partyline by Commonjava.
the class Partyline method waitForReadUnlock.
/**
* Wait the specified timeout milliseconds for read access on the specified file to become available. Return false
* if the timeout elapses without the file becoming available for reads. If a {@link RandomAccessJF} is available for
* the file, don't wait (immediately return true).
*
* @see #isReadLocked(File)
*/
public boolean waitForReadUnlock(final File file, final long timeout) throws InterruptedException {
long to = timeout < 1 ? DEFAULT_TIMEOUT : timeout;
logger.trace(">>>WAIT (read unlock): {} with timeout: {}", file, to);
long end = System.currentTimeMillis() + to;
boolean result = false;
while (System.currentTimeMillis() < end) {
LockLevel lockLevel = locks.getLockLevel(file);
if (lockLevel != LockLevel.delete) {
result = true;
break;
}
synchronized (locks) {
locks.wait(100);
}
lockLevel = locks.getLockLevel(file);
if (lockLevel != LockLevel.delete) {
result = true;
break;
}
}
logger.trace("<<<WAIT (read unlock) result: {}", result);
return result;
}
use of org.commonjava.util.partyline.lock.LockLevel in project partyline by Commonjava.
the class Partyline method waitForWriteUnlock.
/**
* Wait the specified timeout milliseconds for write access on the specified file to become available. Return false
* if the timeout elapses without the file becoming available for writes.
*
* @see #isWriteLocked(File)
*/
// FIXME: How do we prevent new incoming lock requests while we wait?
public boolean waitForWriteUnlock(final File file, long timeout) throws InterruptedException {
long to = timeout < 1 ? DEFAULT_TIMEOUT : timeout;
logger.trace(">>>WAIT (write unlock): {} with timeout: {}", file, to);
long end = System.currentTimeMillis() + to;
boolean result = false;
while (System.currentTimeMillis() < end) {
LockLevel lockLevel = locks.getLockLevel(file);
if (lockLevel == null) {
result = true;
break;
}
synchronized (locks) {
locks.wait(100);
}
lockLevel = locks.getLockLevel(file);
if (lockLevel == null) {
result = true;
break;
}
}
logger.trace("<<<WAIT (write unlock) result: {}", result);
return result;
}
use of org.commonjava.util.partyline.lock.LockLevel in project partyline by Commonjava.
the class InfinispanJFS method getMetadata.
FileMeta getMetadata(final File target, final LocalLockOwner owner) throws IOException {
String path = target.getAbsolutePath();
return lockManager.lockAnd(path, (key, opLock) -> {
FileMeta meta = null;
TransactionManager transactionManager = metadataCache.getAdvancedCache().getTransactionManager();
try {
transactionManager.begin();
meta = metadataCache.computeIfAbsent(path, (p) -> new FileMeta(p, target.isDirectory(), this.blockSize));
LockLevel currentLockLevel = meta.getLockLevel(this.nodeKey);
// Only update the cache if the lock level changed
if (currentLockLevel == null || currentLockLevel != owner.getLockLevel()) {
meta.setLock(this.nodeKey, owner.getLockLevel());
metadataCache.put(path, meta);
}
transactionManager.commit();
} catch (Exception e) {
logger.error("Failed to execute in transaction. Rolling back. Path: " + path, e);
try {
transactionManager.rollback();
} catch (SystemException e1) {
logger.error("SystemException during transaction rollback involving path: " + path, e1);
}
}
return meta;
});
}
Aggregations