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;
}
}
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);
}
}
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);
}
}
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);
}
}
}
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!");
}
}
Aggregations