Search in sources :

Example 91 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BlockLockManager method lockBlockInternal.

private long lockBlockInternal(long sessionId, long blockId, BlockLockType blockLockType, boolean blocking, @Nullable Long time, @Nullable TimeUnit unit) {
    ClientRWLock blockLock = getBlockLock(blockId);
    Lock lock = blockLockType == BlockLockType.READ ? blockLock.readLock() : blockLock.writeLock();
    // Make sure the session isn't already holding the block lock.
    if (blockLockType == BlockLockType.WRITE && sessionHoldsLock(sessionId, blockId)) {
        throw new IllegalStateException(String.format("Session %s attempted to take a write lock on block %s, but the session already" + " holds a lock on the block", sessionId, blockId));
    }
    if (blocking) {
        lock.lock();
    } else {
        Preconditions.checkNotNull(time, "time");
        Preconditions.checkNotNull(unit, "unit");
        try {
            if (!lock.tryLock(time, unit)) {
                LOG.warn("Failed to acquire lock for block {} after {} {}.  " + "session: {}, blockLockType: {}, lock reference count = {}", blockId, time, unit, sessionId, blockLockType, blockLock.getReferenceCount());
                return BlockWorker.INVALID_LOCK_ID;
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return BlockWorker.INVALID_LOCK_ID;
        }
    }
    try {
        long lockId = LOCK_ID_GEN.getAndIncrement();
        try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
            mLockIdToRecordMap.put(lockId, new LockRecord(sessionId, blockId, lock));
            Set<Long> sessionLockIds = mSessionIdToLockIdsMap.get(sessionId);
            if (sessionLockIds == null) {
                mSessionIdToLockIdsMap.put(sessionId, Sets.newHashSet(lockId));
            } else {
                sessionLockIds.add(lockId);
            }
        }
        return lockId;
    } catch (Throwable e) {
        // If an unexpected exception occurs, we should release the lock to be conservative.
        unlock(lock, blockId);
        throw e;
    }
}
Also used : LockResource(alluxio.resource.LockResource) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 92 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BlockLockManager method unlockBlock.

/**
 * Releases the lock with the specified session and block id.
 *
 * @param sessionId the session id
 * @param blockId the block id
 * @return whether the block has been successfully unlocked
 */
// TODO(bin): Temporary, remove me later.
public boolean unlockBlock(long sessionId, long blockId) {
    try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
        Set<Long> sessionLockIds = mSessionIdToLockIdsMap.get(sessionId);
        if (sessionLockIds == null) {
            return false;
        }
        for (long lockId : sessionLockIds) {
            LockRecord record = mLockIdToRecordMap.get(lockId);
            if (record == null) {
                // TODO(peis): Should this be a check failure?
                return false;
            }
            if (blockId == record.getBlockId()) {
                mLockIdToRecordMap.remove(lockId);
                sessionLockIds.remove(lockId);
                if (sessionLockIds.isEmpty()) {
                    mSessionIdToLockIdsMap.remove(sessionId);
                }
                Lock lock = record.getLock();
                unlock(lock, blockId);
                return true;
            }
        }
        return false;
    }
}
Also used : LockResource(alluxio.resource.LockResource) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 93 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BlockLockManager method releaseBlockLockIfUnused.

/**
 * Checks whether anyone is using the block lock for the given block id, returning the lock to
 * the lock pool if it is unused.
 *
 * @param blockId the block id for which to potentially release the block lock
 */
private void releaseBlockLockIfUnused(long blockId) {
    try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
        ClientRWLock lock = mLocks.get(blockId);
        if (lock == null) {
            // Someone else probably released the block lock already.
            return;
        }
        // If we were the last worker with a reference to the lock, clean it up.
        if (lock.dropReference() == 0) {
            mLocks.remove(blockId);
            mLockPool.release(lock);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource)

Example 94 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class TieredBlockStore method accessBlock.

@Override
public void accessBlock(long sessionId, long blockId) throws BlockDoesNotExistException {
    LOG.debug("accessBlock: sessionId={}, blockId={}", sessionId, blockId);
    try (LockResource r = new LockResource(mMetadataReadLock)) {
        BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
        for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
            synchronized (listener) {
                listener.onAccessBlock(sessionId, blockId);
                listener.onAccessBlock(sessionId, blockId, blockMeta.getBlockLocation());
            }
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) BlockMeta(alluxio.worker.block.meta.BlockMeta) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta)

Example 95 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class TieredBlockStore method cleanupSession.

@Override
public void cleanupSession(long sessionId) {
    LOG.debug("cleanupSession: sessionId={}", sessionId);
    // Release all locks the session is holding.
    mLockManager.cleanupSession(sessionId);
    // Collect a list of temp blocks the given session owns and abort all of them with best effort
    List<TempBlockMeta> tempBlocksToRemove;
    try (LockResource r = new LockResource(mMetadataReadLock)) {
        tempBlocksToRemove = mMetaManager.getSessionTempBlocks(sessionId);
    }
    for (TempBlockMeta tempBlockMeta : tempBlocksToRemove) {
        try {
            LOG.warn("Clean up expired temporary block {} from session {}.", tempBlockMeta.getBlockId(), sessionId);
            abortBlockInternal(sessionId, tempBlockMeta.getBlockId());
        } catch (Exception e) {
            LOG.error("Failed to cleanup tempBlock {}", tempBlockMeta.getBlockId(), e);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException) BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException) IOException(java.io.IOException) DeadlineExceededException(alluxio.exception.status.DeadlineExceededException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException)

Aggregations

LockResource (alluxio.resource.LockResource)116 IOException (java.io.IOException)14 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)13 HashMap (java.util.HashMap)12 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)11 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)11 Map (java.util.Map)11 AlluxioURI (alluxio.AlluxioURI)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)10 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)9 ArrayList (java.util.ArrayList)9 MasterWorkerInfo (alluxio.master.block.meta.MasterWorkerInfo)8 InvalidPathException (alluxio.exception.InvalidPathException)7 NotFoundException (alluxio.exception.status.NotFoundException)7 List (java.util.List)7 Lock (java.util.concurrent.locks.Lock)7 ConcurrentHashSet (alluxio.collections.ConcurrentHashSet)6 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)6 UnavailableException (alluxio.exception.status.UnavailableException)6