Search in sources :

Example 1 with InvalidWorkerStateException

use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.

the class UnderFileSystemBlockReader method updateBlockWriter.

/**
   * Updates the block writer given an offset to read. If the offset is beyond the current
   * position of the block writer, the block writer will be aborted.
   *
   * @param offset the read offset
   */
private void updateBlockWriter(long offset) throws IOException {
    try {
        if (mBlockWriter != null && offset > mBlockWriter.getPosition()) {
            mBlockWriter.close();
            mBlockWriter = null;
            mLocalBlockStore.abortBlock(mBlockMeta.getSessionId(), mBlockMeta.getBlockId());
        }
    } catch (BlockDoesNotExistException e) {
        // This can only happen when the session is expired.
        LOG.warn("Block {} does not exist when being aborted.", mBlockMeta.getBlockId());
    } catch (BlockAlreadyExistsException | InvalidWorkerStateException | IOException e) {
        // reader does not commit the block if it fails to abort the block.
        throw CommonUtils.castToIOException(e);
    }
    try {
        if (mBlockWriter == null && offset == 0 && !mNoCache) {
            BlockStoreLocation loc = BlockStoreLocation.anyDirInTier(mStorageTierAssoc.getAlias(0));
            String blockPath = mLocalBlockStore.createBlock(mBlockMeta.getSessionId(), mBlockMeta.getBlockId(), loc, mInitialBlockSize).getPath();
            mBlockWriter = new LocalFileBlockWriter(blockPath);
        }
    } catch (IOException | BlockAlreadyExistsException | WorkerOutOfSpaceException e) {
        // This can happen when there are concurrent UFS readers who are all trying to cache to block.
        LOG.debug("Failed to update block writer for UFS block [blockId: {}, ufsPath: {}, offset: {}]", mBlockMeta.getBlockId(), mBlockMeta.getUnderFileSystemPath(), offset, e);
        mBlockWriter = null;
    }
}
Also used : LocalFileBlockWriter(alluxio.worker.block.io.LocalFileBlockWriter) BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Example 2 with InvalidWorkerStateException

use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.

the class FileDataManager method persistFile.

/**
   * Persists the blocks of a file into the under file system.
   *
   * @param fileId the id of the file
   * @param blockIds the list of block ids
   * @throws AlluxioException if an unexpected Alluxio exception is thrown
   * @throws IOException if the file persistence fails
   */
public void persistFile(long fileId, List<Long> blockIds) throws AlluxioException, IOException {
    Map<Long, Long> blockIdToLockId;
    synchronized (mLock) {
        blockIdToLockId = mPersistingInProgressFiles.get(fileId);
        if (blockIdToLockId == null || !blockIdToLockId.keySet().equals(new HashSet<>(blockIds))) {
            throw new IOException("Not all the blocks of file " + fileId + " are locked");
        }
    }
    String dstPath = prepareUfsFilePath(fileId);
    UnderFileSystem ufs = UnderFileSystem.Factory.get(dstPath);
    FileInfo fileInfo = mBlockWorker.getFileInfo(fileId);
    OutputStream outputStream = ufs.create(dstPath, CreateOptions.defaults().setOwner(fileInfo.getOwner()).setGroup(fileInfo.getGroup()).setMode(new Mode((short) fileInfo.getMode())));
    final WritableByteChannel outputChannel = Channels.newChannel(outputStream);
    List<Throwable> errors = new ArrayList<>();
    try {
        for (long blockId : blockIds) {
            long lockId = blockIdToLockId.get(blockId);
            if (Configuration.getBoolean(PropertyKey.WORKER_FILE_PERSIST_RATE_LIMIT_ENABLED)) {
                BlockMeta blockMeta = mBlockWorker.getBlockMeta(Sessions.CHECKPOINT_SESSION_ID, blockId, lockId);
                mPersistenceRateLimiter.acquire((int) blockMeta.getBlockSize());
            }
            // obtain block reader
            BlockReader reader = mBlockWorker.readBlockRemote(Sessions.CHECKPOINT_SESSION_ID, blockId, lockId);
            // write content out
            ReadableByteChannel inputChannel = reader.getChannel();
            BufferUtils.fastCopy(inputChannel, outputChannel);
            reader.close();
        }
    } catch (BlockDoesNotExistException | InvalidWorkerStateException e) {
        errors.add(e);
    } finally {
        // make sure all the locks are released
        for (long lockId : blockIdToLockId.values()) {
            try {
                mBlockWorker.unlockBlock(lockId);
            } catch (BlockDoesNotExistException e) {
                errors.add(e);
            }
        }
        // Process any errors
        if (!errors.isEmpty()) {
            StringBuilder errorStr = new StringBuilder();
            errorStr.append("the blocks of file").append(fileId).append(" are failed to persist\n");
            for (Throwable e : errors) {
                errorStr.append(e).append('\n');
            }
            throw new IOException(errorStr.toString());
        }
    }
    outputStream.flush();
    outputChannel.close();
    outputStream.close();
    synchronized (mLock) {
        mPersistingInProgressFiles.remove(fileId);
        mPersistedFiles.add(fileId);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) OutputStream(java.io.OutputStream) Mode(alluxio.security.authorization.Mode) BlockReader(alluxio.worker.block.io.BlockReader) WritableByteChannel(java.nio.channels.WritableByteChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInfo(alluxio.wire.FileInfo) UnderFileSystem(alluxio.underfs.UnderFileSystem) BlockMeta(alluxio.worker.block.meta.BlockMeta) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Example 3 with InvalidWorkerStateException

use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.

the class FileDataManagerTest method errorHandling.

/**
   * Tests that the correct error message is provided when persisting a file fails.
   */
@Test
public void errorHandling() throws Exception {
    long fileId = 1;
    List<Long> blockIds = Lists.newArrayList(1L, 2L);
    FileInfo fileInfo = new FileInfo();
    fileInfo.setPath("test");
    Mockito.when(mBlockWorker.getFileInfo(fileId)).thenReturn(fileInfo);
    for (long blockId : blockIds) {
        Mockito.when(mBlockWorker.lockBlock(Sessions.CHECKPOINT_SESSION_ID, blockId)).thenReturn(blockId);
        Mockito.doThrow(new InvalidWorkerStateException("invalid worker")).when(mBlockWorker).readBlockRemote(Sessions.CHECKPOINT_SESSION_ID, blockId, blockId);
    }
    String ufsRoot = Configuration.get(PropertyKey.UNDERFS_ADDRESS);
    Mockito.when(mUfs.isDirectory(ufsRoot)).thenReturn(true);
    OutputStream outputStream = Mockito.mock(OutputStream.class);
    // mock BufferUtils
    PowerMockito.mockStatic(BufferUtils.class);
    String dstPath = PathUtils.concatPath(ufsRoot, fileInfo.getPath());
    fileInfo.setUfsPath(dstPath);
    Mockito.when(mUfs.create(dstPath)).thenReturn(outputStream);
    Mockito.when(mUfs.create(Mockito.anyString(), Mockito.any(CreateOptions.class))).thenReturn(outputStream);
    Mockito.when(mMockFileSystem.getStatus(Mockito.any(AlluxioURI.class))).thenReturn(new URIStatus(fileInfo));
    mManager.lockBlocks(fileId, blockIds);
    try {
        mManager.persistFile(fileId, blockIds);
        Assert.fail("the persist should fail");
    } catch (IOException e) {
        assertEquals("the blocks of file1 are failed to persist\n" + "alluxio.exception.InvalidWorkerStateException: invalid worker\n", e.getMessage());
        // verify the locks are all unlocked
        Mockito.verify(mBlockWorker).unlockBlock(1L);
        Mockito.verify(mBlockWorker).unlockBlock(2L);
    }
}
Also used : FileInfo(alluxio.wire.FileInfo) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus) CreateOptions(alluxio.underfs.options.CreateOptions) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException) AlluxioURI(alluxio.AlluxioURI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with InvalidWorkerStateException

use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.

the class TieredBlockStore method requestSpace.

@Override
public void requestSpace(long sessionId, long blockId, long additionalBytes) throws BlockDoesNotExistException, WorkerOutOfSpaceException, IOException {
    LOG.debug("requestSpace: sessionId={}, blockId={}, additionalBytes={}", sessionId, blockId, additionalBytes);
    if (additionalBytes <= 0) {
        return;
    }
    // block lock here since no sharing
    try (LockResource r = new LockResource(mMetadataWriteLock)) {
        TempBlockMeta tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
        StorageDirView allocationDir = allocateSpace(sessionId, AllocateOptions.forRequestSpace(additionalBytes, tempBlockMeta.getBlockLocation()));
        if (!allocationDir.toBlockStoreLocation().equals(tempBlockMeta.getBlockLocation())) {
            // If reached here, allocateSpace() failed to enforce 'forceLocation' flag.
            throw new IllegalStateException(String.format("Allocation error: location enforcement failed for location: %s", allocationDir.toBlockStoreLocation()));
        }
        // Increase the size of this temp block
        try {
            mMetaManager.resizeTempBlockMeta(tempBlockMeta, tempBlockMeta.getBlockSize() + additionalBytes);
        } catch (InvalidWorkerStateException e) {
            // we shall never reach here
            throw Throwables.propagate(e);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) TempBlockMeta(alluxio.worker.block.meta.TempBlockMeta) StorageDirView(alluxio.worker.block.meta.StorageDirView) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Example 5 with InvalidWorkerStateException

use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.

the class DefaultStorageDir method resizeTempBlockMeta.

@Override
public void resizeTempBlockMeta(TempBlockMeta tempBlockMeta, long newSize) throws InvalidWorkerStateException {
    long oldSize = tempBlockMeta.getBlockSize();
    if (newSize > oldSize) {
        reserveSpace(newSize - oldSize, false);
        tempBlockMeta.setBlockSize(newSize);
    } else if (newSize < oldSize) {
        throw new InvalidWorkerStateException("Shrinking block, not supported!");
    }
}
Also used : InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Aggregations

InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)14 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)8 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)8 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)7 IOException (java.io.IOException)7 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)6 LockResource (alluxio.resource.LockResource)5 BlockMeta (alluxio.worker.block.meta.BlockMeta)5 DeadlineExceededException (alluxio.exception.status.DeadlineExceededException)2 FileInfo (alluxio.wire.FileInfo)2 OutputStream (java.io.OutputStream)2 AlluxioURI (alluxio.AlluxioURI)1 RpcUtils (alluxio.RpcUtils)1 URIStatus (alluxio.client.file.URIStatus)1 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 AlluxioException (alluxio.exception.AlluxioException)1 UnavailableException (alluxio.exception.status.UnavailableException)1 CreateLocalBlockResponse (alluxio.grpc.CreateLocalBlockResponse)1 Mode (alluxio.security.authorization.Mode)1 UnderFileSystem (alluxio.underfs.UnderFileSystem)1