use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.
the class TieredBlockStore method moveBlockInternal.
/**
* Moves a block to new location only if allocator finds available space in newLocation. This
* method will not trigger any eviction. Returns {@link MoveBlockResult}.
*
* @param sessionId session id
* @param blockId block id
* @param oldLocation the source location of the block
* @param moveOptions the allocate options for the move
* @return the resulting information about the move operation
* @throws BlockDoesNotExistException if block is not found
* @throws BlockAlreadyExistsException if a block with same id already exists in new location
* @throws InvalidWorkerStateException if the block to move is a temp block
*/
private MoveBlockResult moveBlockInternal(long sessionId, long blockId, BlockStoreLocation oldLocation, AllocateOptions moveOptions) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException {
long lockId = mLockManager.lockBlock(sessionId, blockId, BlockLockType.WRITE);
try {
long blockSize;
String srcFilePath;
String dstFilePath;
BlockMeta srcBlockMeta;
BlockStoreLocation srcLocation;
BlockStoreLocation dstLocation;
try (LockResource r = new LockResource(mMetadataReadLock)) {
if (mMetaManager.hasTempBlockMeta(blockId)) {
throw new InvalidWorkerStateException(ExceptionMessage.MOVE_UNCOMMITTED_BLOCK, blockId);
}
srcBlockMeta = mMetaManager.getBlockMeta(blockId);
srcLocation = srcBlockMeta.getBlockLocation();
srcFilePath = srcBlockMeta.getPath();
blockSize = srcBlockMeta.getBlockSize();
// Update moveOptions with the block size.
moveOptions.setSize(blockSize);
}
if (!srcLocation.belongsTo(oldLocation)) {
throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_AT_LOCATION, blockId, oldLocation);
}
if (srcLocation.belongsTo(moveOptions.getLocation())) {
return new MoveBlockResult(true, blockSize, srcLocation, srcLocation);
}
TempBlockMeta dstTempBlock;
try {
dstTempBlock = createBlockMetaInternal(sessionId, blockId, false, moveOptions);
} catch (Exception e) {
return new MoveBlockResult(false, blockSize, null, null);
}
// When `newLocation` is some specific location, the `newLocation` and the `dstLocation` are
// just the same; while for `newLocation` with a wildcard significance, the `dstLocation`
// is a specific one with specific tier and dir which belongs to newLocation.
dstLocation = dstTempBlock.getBlockLocation();
// internally from the newLocation and return success with specific block location.
if (dstLocation.belongsTo(srcLocation)) {
mMetaManager.abortTempBlockMeta(dstTempBlock);
return new MoveBlockResult(true, blockSize, srcLocation, dstLocation);
}
dstFilePath = dstTempBlock.getCommitPath();
// Heavy IO is guarded by block lock but not metadata lock. This may throw IOException.
FileUtils.move(srcFilePath, dstFilePath);
try (LockResource r = new LockResource(mMetadataWriteLock)) {
// If this metadata update fails, we panic for now.
// TODO(bin): Implement rollback scheme to recover from IO failures.
mMetaManager.moveBlockMeta(srcBlockMeta, dstTempBlock);
} catch (BlockAlreadyExistsException | BlockDoesNotExistException | WorkerOutOfSpaceException e) {
// we shall never reach here
throw Throwables.propagate(e);
}
return new MoveBlockResult(true, blockSize, srcLocation, dstLocation);
} finally {
mLockManager.unlockBlock(lockId);
}
}
use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.
the class ShortCircuitBlockWriteHandler method onNext.
/**
* Handles request to create local block. No exceptions should be thrown.
*
* @param request a create request
*/
@Override
public void onNext(CreateLocalBlockRequest request) {
final String methodName = request.getOnlyReserveSpace() ? "ReserveSpace" : "CreateBlock";
RpcUtils.streamingRPCAndLog(LOG, new RpcUtils.StreamingRpcCallable<CreateLocalBlockResponse>() {
@Override
public CreateLocalBlockResponse call() throws Exception {
if (request.getOnlyReserveSpace()) {
mBlockWorker.requestSpace(mSessionId, request.getBlockId(), request.getSpaceToReserve());
return CreateLocalBlockResponse.newBuilder().build();
} else {
Preconditions.checkState(mRequest == null);
mRequest = request;
if (mSessionId == INVALID_SESSION_ID) {
mSessionId = IdUtils.createSessionId();
String path = mBlockWorker.createBlock(mSessionId, request.getBlockId(), request.getTier(), request.getMediumType(), request.getSpaceToReserve());
CreateLocalBlockResponse response = CreateLocalBlockResponse.newBuilder().setPath(path).build();
return response;
} else {
LOG.warn("Create block {} without closing the previous session {}.", request.getBlockId(), mSessionId);
throw new InvalidWorkerStateException(ExceptionMessage.SESSION_NOT_CLOSED.getMessage(mSessionId));
}
}
}
@Override
public void exceptionCaught(Throwable throwable) {
if (mSessionId != INVALID_SESSION_ID) {
// In case the client is a UfsFallbackDataWriter, DO NOT clean the temp blocks.
if (throwable instanceof alluxio.exception.WorkerOutOfSpaceException && request.hasCleanupOnFailure() && !request.getCleanupOnFailure()) {
mResponseObserver.onError(GrpcExceptionUtils.fromThrowable(throwable));
return;
}
mBlockWorker.cleanupSession(mSessionId);
mSessionId = INVALID_SESSION_ID;
}
mResponseObserver.onError(GrpcExceptionUtils.fromThrowable(throwable));
}
}, methodName, true, false, mResponseObserver, "Session=%d, Request=%s", mSessionId, request);
}
use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.
the class TieredBlockStore method freeSpaceInternal.
/**
* Tries to get an eviction plan to free a certain amount of space in the given location, and
* carries out this plan with the best effort.
*
* @param sessionId the session Id
* @param availableBytes amount of space in bytes to free
* @param location location of space
* @throws WorkerOutOfSpaceException if it is impossible to achieve the free requirement
* @throws IOException if I/O errors occur when removing or moving block files
*/
private void freeSpaceInternal(long sessionId, long availableBytes, BlockStoreLocation location) throws WorkerOutOfSpaceException, IOException {
EvictionPlan plan;
try (LockResource r = new LockResource(mMetadataReadLock)) {
plan = mEvictor.freeSpaceWithView(availableBytes, location, getUpdatedView());
// Absent plan means failed to evict enough space.
if (plan == null) {
throw new WorkerOutOfSpaceException(ExceptionMessage.NO_EVICTION_PLAN_TO_FREE_SPACE);
}
}
// 1. remove blocks to make room.
for (Pair<Long, BlockStoreLocation> blockInfo : plan.toEvict()) {
try {
removeBlockInternal(sessionId, blockInfo.getFirst(), blockInfo.getSecond());
} catch (InvalidWorkerStateException e) {
// Evictor is not working properly
LOG.error("Failed to evict blockId {}, this is temp block", blockInfo.getFirst());
continue;
} catch (BlockDoesNotExistException e) {
LOG.info("Failed to evict blockId {}, it could be already deleted", blockInfo.getFirst());
continue;
}
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onRemoveBlockByWorker(sessionId, blockInfo.getFirst());
}
}
}
// 2. transfer blocks among tiers.
// 2.1. group blocks move plan by the destination tier.
Map<String, Set<BlockTransferInfo>> blocksGroupedByDestTier = new HashMap<>();
for (BlockTransferInfo entry : plan.toMove()) {
String alias = entry.getDstLocation().tierAlias();
if (!blocksGroupedByDestTier.containsKey(alias)) {
blocksGroupedByDestTier.put(alias, new HashSet<BlockTransferInfo>());
}
blocksGroupedByDestTier.get(alias).add(entry);
}
// 2.2. move blocks in the order of their dst tiers, from bottom to top
for (int tierOrdinal = mStorageTierAssoc.size() - 1; tierOrdinal >= 0; --tierOrdinal) {
Set<BlockTransferInfo> toMove = blocksGroupedByDestTier.get(mStorageTierAssoc.getAlias(tierOrdinal));
if (toMove == null) {
toMove = new HashSet<>();
}
for (BlockTransferInfo entry : toMove) {
long blockId = entry.getBlockId();
BlockStoreLocation oldLocation = entry.getSrcLocation();
BlockStoreLocation newLocation = entry.getDstLocation();
MoveBlockResult moveResult;
try {
moveResult = moveBlockInternal(sessionId, blockId, oldLocation, newLocation);
} catch (InvalidWorkerStateException e) {
// Evictor is not working properly
LOG.error("Failed to evict blockId {}, this is temp block", blockId);
continue;
} catch (BlockAlreadyExistsException e) {
continue;
} catch (BlockDoesNotExistException e) {
LOG.info("Failed to move blockId {}, it could be already deleted", blockId);
continue;
}
if (moveResult.getSuccess()) {
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onMoveBlockByWorker(sessionId, blockId, moveResult.getSrcLocation(), newLocation);
}
}
}
}
}
}
use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.
the class TieredBlockStore method moveBlockInternal.
/**
* Moves a block to new location only if allocator finds available space in newLocation. This
* method will not trigger any eviction. Returns {@link MoveBlockResult}.
*
* @param sessionId session Id
* @param blockId block Id
* @param oldLocation the source location of the block
* @param newLocation new location to move this block
* @return the resulting information about the move operation
* @throws BlockDoesNotExistException if block is not found
* @throws BlockAlreadyExistsException if a block with same Id already exists in new location
* @throws InvalidWorkerStateException if the block to move is a temp block
* @throws IOException if I/O errors occur when moving block file
*/
private MoveBlockResult moveBlockInternal(long sessionId, long blockId, BlockStoreLocation oldLocation, BlockStoreLocation newLocation) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException {
long lockId = mLockManager.lockBlock(sessionId, blockId, BlockLockType.WRITE);
try {
long blockSize;
String srcFilePath;
String dstFilePath;
BlockMeta srcBlockMeta;
BlockStoreLocation srcLocation;
BlockStoreLocation dstLocation;
try (LockResource r = new LockResource(mMetadataReadLock)) {
if (mMetaManager.hasTempBlockMeta(blockId)) {
throw new InvalidWorkerStateException(ExceptionMessage.MOVE_UNCOMMITTED_BLOCK, blockId);
}
srcBlockMeta = mMetaManager.getBlockMeta(blockId);
srcLocation = srcBlockMeta.getBlockLocation();
srcFilePath = srcBlockMeta.getPath();
blockSize = srcBlockMeta.getBlockSize();
}
if (!srcLocation.belongsTo(oldLocation)) {
throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_AT_LOCATION, blockId, oldLocation);
}
TempBlockMeta dstTempBlock = createBlockMetaInternal(sessionId, blockId, newLocation, blockSize, false);
if (dstTempBlock == null) {
return new MoveBlockResult(false, blockSize, null, null);
}
// When `newLocation` is some specific location, the `newLocation` and the `dstLocation` are
// just the same; while for `newLocation` with a wildcard significance, the `dstLocation`
// is a specific one with specific tier and dir which belongs to newLocation.
dstLocation = dstTempBlock.getBlockLocation();
// internally from the newLocation and return success with specific block location.
if (dstLocation.belongsTo(srcLocation)) {
mMetaManager.abortTempBlockMeta(dstTempBlock);
return new MoveBlockResult(true, blockSize, srcLocation, dstLocation);
}
dstFilePath = dstTempBlock.getCommitPath();
// Heavy IO is guarded by block lock but not metadata lock. This may throw IOException.
FileUtils.move(srcFilePath, dstFilePath);
try (LockResource r = new LockResource(mMetadataWriteLock)) {
// If this metadata update fails, we panic for now.
// TODO(bin): Implement rollback scheme to recover from IO failures.
mMetaManager.moveBlockMeta(srcBlockMeta, dstTempBlock);
} catch (BlockAlreadyExistsException | BlockDoesNotExistException | WorkerOutOfSpaceException e) {
// we shall never reach here
throw Throwables.propagate(e);
}
return new MoveBlockResult(true, blockSize, srcLocation, dstLocation);
} finally {
mLockManager.unlockBlock(lockId);
}
}
use of alluxio.exception.InvalidWorkerStateException in project alluxio by Alluxio.
the class StorageDir method resizeTempBlockMeta.
/**
* Changes the size of a temp block.
*
* @param tempBlockMeta the metadata of the temp block to resize
* @param newSize the new size after change in bytes
* @throws InvalidWorkerStateException when newSize is smaller than oldSize
*/
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