Search in sources :

Example 1 with LockLevel

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;
}
Also used : LockLevel(org.commonjava.util.partyline.lock.LockLevel)

Example 2 with LockLevel

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;
}
Also used : LockLevel(org.commonjava.util.partyline.lock.LockLevel)

Example 3 with LockLevel

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;
    });
}
Also used : SignallingLocker(org.commonjava.cdi.util.weft.SignallingLocker) JoinableFilesystem(org.commonjava.util.partyline.spi.JoinableFilesystem) LockLevel(org.commonjava.util.partyline.lock.LockLevel) LoggerFactory(org.slf4j.LoggerFactory) StreamCallbacks(org.commonjava.util.partyline.callback.StreamCallbacks) Cache(org.infinispan.Cache) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta) RollbackException(javax.transaction.RollbackException) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) CacheEntryModified(org.infinispan.notifications.cachelistener.annotation.CacheEntryModified) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) UnlockStatus(org.commonjava.util.partyline.lock.UnlockStatus) Listener(org.infinispan.notifications.Listener) Logger(org.slf4j.Logger) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) PartylineException(org.commonjava.util.partyline.PartylineException) IOException(java.io.IOException) File(java.io.File) SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) NotSupportedException(javax.transaction.NotSupportedException) CacheEntryCreated(org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated) List(java.util.List) CacheEntryEvent(org.infinispan.notifications.cachelistener.event.CacheEntryEvent) SystemException(javax.transaction.SystemException) FileBlock(org.commonjava.util.partyline.impl.infinispan.model.FileBlock) HeuristicMixedException(javax.transaction.HeuristicMixedException) TransactionManager(javax.transaction.TransactionManager) Collections(java.util.Collections) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) LockLevel(org.commonjava.util.partyline.lock.LockLevel) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) PartylineException(org.commonjava.util.partyline.PartylineException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Aggregations

LockLevel (org.commonjava.util.partyline.lock.LockLevel)3 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 TransactionManager (javax.transaction.TransactionManager)1 SignallingLock (org.commonjava.cdi.util.weft.SignallingLock)1 SignallingLocker (org.commonjava.cdi.util.weft.SignallingLocker)1 PartylineException (org.commonjava.util.partyline.PartylineException)1 StreamCallbacks (org.commonjava.util.partyline.callback.StreamCallbacks)1 FileBlock (org.commonjava.util.partyline.impl.infinispan.model.FileBlock)1 FileMeta (org.commonjava.util.partyline.impl.infinispan.model.FileMeta)1 UnlockStatus (org.commonjava.util.partyline.lock.UnlockStatus)1