Search in sources :

Example 81 with LockResource

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

the class TieredBlockStore method abortBlockInternal.

/**
 * Aborts a temp block.
 *
 * @param sessionId the id of session
 * @param blockId the id of block
 * @throws BlockDoesNotExistException if block id can not be found in temporary blocks
 * @throws BlockAlreadyExistsException if block id already exists in committed blocks
 * @throws InvalidWorkerStateException if block id is not owned by session id
 */
private void abortBlockInternal(long sessionId, long blockId) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException {
    String path;
    TempBlockMeta tempBlockMeta;
    try (LockResource r = new LockResource(mMetadataReadLock)) {
        checkTempBlockOwnedBySession(sessionId, blockId);
        tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
        path = tempBlockMeta.getPath();
    }
    // The metadata lock is released during heavy IO. The temp block is private to one session, so
    // we do not lock it.
    Files.delete(Paths.get(path));
    try (LockResource r = new LockResource(mMetadataWriteLock)) {
        mMetaManager.abortTempBlockMeta(tempBlockMeta);
    } catch (BlockDoesNotExistException e) {
        // We shall never reach here
        throw Throwables.propagate(e);
    }
}
Also used : LockResource(alluxio.resource.LockResource) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException)

Example 82 with LockResource

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

the class TieredBlockStore method checkStorage.

@Override
public boolean checkStorage() {
    try (LockResource r = new LockResource(mMetadataWriteLock)) {
        List<StorageDir> dirsToRemove = new ArrayList<>();
        for (StorageTier tier : mMetaManager.getTiers()) {
            for (StorageDir dir : tier.getStorageDirs()) {
                String path = dir.getDirPath();
                if (!FileUtils.isStorageDirAccessible(path)) {
                    LOG.error("Storage check failed for path {}. The directory will be excluded.", path);
                    dirsToRemove.add(dir);
                }
            }
        }
        dirsToRemove.forEach(this::removeDir);
        return !dirsToRemove.isEmpty();
    }
}
Also used : LockResource(alluxio.resource.LockResource) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) StorageTier(alluxio.worker.block.meta.StorageTier) StorageDir(alluxio.worker.block.meta.StorageDir)

Example 83 with LockResource

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

the class TieredBlockStore method createBlockMetaInternal.

/**
 * Creates a temp block meta only if allocator finds available space. This method will not trigger
 * any eviction.
 *
 * @param sessionId session id
 * @param blockId block id
 * @param newBlock true if this temp block is created for a new block
 * @param options block allocation options
 * @return a temp block created if successful, or null if allocation failed (instead of throwing
 *         {@link WorkerOutOfSpaceException} because allocation failure could be an expected case)
 * @throws BlockAlreadyExistsException if there is already a block with the same block id
 */
private TempBlockMeta createBlockMetaInternal(long sessionId, long blockId, boolean newBlock, AllocateOptions options) throws BlockAlreadyExistsException, WorkerOutOfSpaceException, IOException {
    try (LockResource r = new LockResource(mMetadataWriteLock)) {
        // unnecessary to acquire block lock here since no sharing.
        if (newBlock) {
            checkTempBlockIdAvailable(blockId);
        }
        // Allocate space.
        StorageDirView dirView = allocateSpace(sessionId, options);
        // TODO(carson): Add tempBlock to corresponding storageDir and remove the use of
        // StorageDirView.createTempBlockMeta.
        TempBlockMeta tempBlock = dirView.createTempBlockMeta(sessionId, blockId, options.getSize());
        try {
            // Add allocated temp block to metadata manager. This should never fail if allocator
            // correctly assigns a StorageDir.
            mMetaManager.addTempBlockMeta(tempBlock);
        } catch (WorkerOutOfSpaceException | BlockAlreadyExistsException e) {
            // If we reach here, allocator is not working properly
            LOG.error("Unexpected failure: {} bytes allocated at {} by allocator, " + "but addTempBlockMeta failed", options.getSize(), options.getLocation());
            throw Throwables.propagate(e);
        }
        return tempBlock;
    }
}
Also used : BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) LockResource(alluxio.resource.LockResource) StorageDirView(alluxio.worker.block.meta.StorageDirView) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException)

Example 84 with LockResource

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

the class TieredBlockStore method removeBlockInternal.

@VisibleForTesting
BlockMeta removeBlockInternal(long sessionId, long blockId, BlockStoreLocation location, long timeoutMs) throws InvalidWorkerStateException, BlockDoesNotExistException, IOException {
    long lockId = mLockManager.tryLockBlock(sessionId, blockId, BlockLockType.WRITE, timeoutMs, TimeUnit.MILLISECONDS);
    if (lockId == BlockWorker.INVALID_LOCK_ID) {
        throw new DeadlineExceededException(String.format("Can not acquire lock to remove block %d for session %d after %d ms", blockId, sessionId, REMOVE_BLOCK_TIMEOUT_MS));
    }
    BlockMeta blockMeta;
    try (LockResource r = new LockResource(mMetadataReadLock)) {
        if (mMetaManager.hasTempBlockMeta(blockId)) {
            throw new InvalidWorkerStateException(ExceptionMessage.REMOVE_UNCOMMITTED_BLOCK, blockId);
        }
        blockMeta = mMetaManager.getBlockMeta(blockId);
        if (!blockMeta.getBlockLocation().belongsTo(location)) {
            throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_AT_LOCATION, blockId, location);
        }
    } catch (Exception e) {
        mLockManager.unlockBlock(lockId);
        throw e;
    }
    try (LockResource r = new LockResource(mMetadataWriteLock)) {
        removeBlockFileAndMeta(blockMeta);
    } finally {
        mLockManager.unlockBlock(lockId);
    }
    return blockMeta;
}
Also used : LockResource(alluxio.resource.LockResource) DeadlineExceededException(alluxio.exception.status.DeadlineExceededException) BlockMeta(alluxio.worker.block.meta.BlockMeta) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) 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) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 85 with LockResource

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

the class UnderFileSystemBlockStore method getBlockReader.

/**
 * Creates a block reader that reads from UFS and optionally caches the block to the Alluxio
 * block store.
 *
 * @param sessionId the client session ID that requested this read
 * @param blockId the ID of the block to read
 * @param offset the read offset within the block (NOT the file)
 * @param positionShort whether the client op is a positioned read to a small buffer
 * @param user the user that requests the block reader
 * @return the block reader instance
 * @throws BlockDoesNotExistException if the UFS block does not exist in the
 * {@link UnderFileSystemBlockStore}
 */
public BlockReader getBlockReader(final long sessionId, long blockId, long offset, boolean positionShort, String user) throws BlockDoesNotExistException, IOException {
    final BlockInfo blockInfo;
    try (LockResource lr = new LockResource(mLock)) {
        blockInfo = getBlockInfo(sessionId, blockId);
        BlockReader blockReader = blockInfo.getBlockReader();
        if (blockReader != null) {
            return blockReader;
        }
    }
    BlockReader reader = UnderFileSystemBlockReader.create(blockInfo.getMeta(), offset, positionShort, mLocalBlockStore, mUfsManager, mUfsInstreamCache, user);
    blockInfo.setBlockReader(reader);
    return reader;
}
Also used : LockResource(alluxio.resource.LockResource) BlockReader(alluxio.worker.block.io.BlockReader)

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