Search in sources :

Example 86 with LockResource

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

the class UnderFileSystemBlockStore method cleanupSession.

/**
 * Cleans up all the block information(e.g. block reader/writer) that belongs to this session.
 *
 * @param sessionId the session ID
 */
@Override
public void cleanupSession(long sessionId) {
    Set<Long> blockIds;
    try (LockResource lr = new LockResource(mLock)) {
        blockIds = mSessionIdToBlockIds.get(sessionId);
        if (blockIds == null) {
            return;
        }
    }
    // message), and is expected very rare to trigger.
    for (Long blockId : blockIds) {
        try {
            // Note that we don't need to explicitly call abortBlock to cleanup the temp block
            // in Local block store because they will be cleanup by the session cleaner in the
            // Local block store.
            closeReaderOrWriter(sessionId, blockId);
            releaseAccess(sessionId, blockId);
        } catch (Exception e) {
            LOG.warn("Failed to cleanup UFS block {}, session {}.", blockId, sessionId);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) IOException(java.io.IOException) BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException)

Example 87 with LockResource

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

the class UnderFileSystemBlockStore method closeReaderOrWriter.

/**
 * Closes the block reader or writer and checks whether it is necessary to commit the block
 * to Local block store.
 *
 * During UFS block read, this is triggered when the block is unlocked.
 * During UFS block write, this is triggered when the UFS block is committed.
 *
 * @param sessionId the session ID
 * @param blockId the block ID
 */
public void closeReaderOrWriter(long sessionId, long blockId) throws IOException {
    BlockInfo blockInfo;
    try (LockResource lr = new LockResource(mLock)) {
        blockInfo = mBlocks.get(new Key(sessionId, blockId));
        if (blockInfo == null) {
            LOG.warn("Key (block ID: {}, session ID {}) is not found when cleaning up the UFS block.", blockId, sessionId);
            return;
        }
    }
    blockInfo.closeReaderOrWriter();
}
Also used : LockResource(alluxio.resource.LockResource)

Example 88 with LockResource

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

the class BlockLockManager method unlockBlockNoException.

/**
 * Releases the lock with the specified lock id.
 *
 * @param lockId the id of the lock to release
 * @return whether the lock corresponding the lock ID has been successfully unlocked
 */
public boolean unlockBlockNoException(long lockId) {
    Lock lock;
    LockRecord record;
    try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
        record = mLockIdToRecordMap.get(lockId);
        if (record == null) {
            return false;
        }
        long sessionId = record.getSessionId();
        lock = record.getLock();
        mLockIdToRecordMap.remove(lockId);
        Set<Long> sessionLockIds = mSessionIdToLockIdsMap.get(sessionId);
        sessionLockIds.remove(lockId);
        if (sessionLockIds.isEmpty()) {
            mSessionIdToLockIdsMap.remove(sessionId);
        }
    }
    unlock(lock, record.getBlockId());
    return true;
}
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 89 with LockResource

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

the class BlockLockManager method validate.

/**
 * Checks the internal state of the manager to make sure invariants hold.
 *
 * This method is intended for testing purposes. A runtime exception will be thrown if invalid
 * state is encountered.
 */
public void validate() {
    try (LockResource r = new LockResource(mSharedMapsLock.readLock())) {
        // Compute block lock reference counts based off of lock records
        ConcurrentMap<Long, AtomicInteger> blockLockReferenceCounts = new ConcurrentHashMap<>();
        for (LockRecord record : mLockIdToRecordMap.values()) {
            blockLockReferenceCounts.putIfAbsent(record.getBlockId(), new AtomicInteger(0));
            blockLockReferenceCounts.get(record.getBlockId()).incrementAndGet();
        }
        // Check that the reference count for each block lock matches the lock record counts.
        for (Entry<Long, ClientRWLock> entry : mLocks.entrySet()) {
            long blockId = entry.getKey();
            ClientRWLock lock = entry.getValue();
            Integer recordCount = blockLockReferenceCounts.get(blockId).get();
            Integer referenceCount = lock.getReferenceCount();
            if (!Objects.equal(recordCount, referenceCount)) {
                throw new IllegalStateException("There are " + recordCount + " lock records for block" + " id " + blockId + ", but the reference count is " + referenceCount);
            }
        }
        // contains that session id.
        for (Entry<Long, Set<Long>> entry : mSessionIdToLockIdsMap.entrySet()) {
            for (Long lockId : entry.getValue()) {
                LockRecord record = mLockIdToRecordMap.get(lockId);
                if (record.getSessionId() != entry.getKey()) {
                    throw new IllegalStateException("The session id map contains lock id " + lockId + "under session id " + entry.getKey() + ", but the record for that lock id (" + record + ")" + " doesn't contain that session id");
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LockResource(alluxio.resource.LockResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 90 with LockResource

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

the class BlockLockManager method getBlockLock.

/**
 * Returns the block lock for the given block id, acquiring such a lock if it doesn't exist yet.
 *
 * If all locks have been allocated, this method will block until one can be acquired.
 *
 * @param blockId the block id to get the lock for
 * @return the block lock
 */
private ClientRWLock getBlockLock(long blockId) {
    // block lock from the lock pool.
    while (true) {
        ClientRWLock blockLock;
        // Check whether a lock has already been allocated for the block id.
        try (LockResource r = new LockResource(mSharedMapsLock.readLock())) {
            blockLock = mLocks.get(blockId);
            if (blockLock != null) {
                blockLock.addReference();
                return blockLock;
            }
        }
        // Since a block lock hasn't already been allocated, try to acquire a new one from the pool.
        // Acquire the lock outside the synchronized section because #acquire might need to block.
        // We shouldn't wait indefinitely in acquire because the another lock for this block could be
        // allocated to another thread, in which case we could just use that lock.
        blockLock = mLockPool.acquire(1, TimeUnit.SECONDS);
        if (blockLock != null) {
            try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
                // Check if someone else acquired a block lock for blockId while we were acquiring one.
                if (mLocks.containsKey(blockId)) {
                    mLockPool.release(blockLock);
                    blockLock = mLocks.get(blockId);
                } else {
                    mLocks.put(blockId, blockLock);
                }
                blockLock.addReference();
                return blockLock;
            }
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource)

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