alluxio.client.block.LocalBlockInStream Maven / Gradle / Ivy
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the “License”). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.block;
import alluxio.client.ClientContext;
import alluxio.exception.AlluxioException;
import alluxio.exception.ExceptionMessage;
import alluxio.util.io.BufferUtils;
import alluxio.util.network.NetworkAddressUtils;
import alluxio.wire.LockBlockResult;
import alluxio.worker.block.io.LocalFileBlockReader;
import com.google.common.io.Closer;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.annotation.concurrent.NotThreadSafe;
/**
* This class provides a streaming API to read a block in Alluxio. The data will be directly read
* from the local machine's storage.
*/
@NotThreadSafe
public final class LocalBlockInStream extends BufferedBlockInStream {
/** Helper to manage closables. */
private final Closer mCloser;
/** Client to communicate with the local worker. */
private final BlockWorkerClient mBlockWorkerClient;
/** The block store context which provides block worker clients. */
private final BlockStoreContext mContext;
/** The file reader to read a local block. */
private final LocalFileBlockReader mReader;
/**
* Creates a new local block input stream.
*
* @param blockId the block id
* @param blockSize the size of the block
* @throws IOException if I/O error occurs
*/
public LocalBlockInStream(long blockId, long blockSize) throws IOException {
super(blockId, blockSize);
mContext = BlockStoreContext.INSTANCE;
mCloser = Closer.create();
mBlockWorkerClient =
mContext.acquireWorkerClient(NetworkAddressUtils.getLocalHostName(ClientContext.getConf()));
try {
LockBlockResult result = mBlockWorkerClient.lockBlock(blockId);
if (result == null) {
throw new IOException(ExceptionMessage.BLOCK_NOT_LOCALLY_AVAILABLE.getMessage(mBlockId));
}
mReader = new LocalFileBlockReader(result.getBlockPath());
mCloser.register(mReader);
} catch (IOException e) {
mContext.releaseWorkerClient(mBlockWorkerClient);
throw e;
}
}
@Override
public void close() throws IOException {
if (mClosed) {
return;
}
try {
if (mBlockIsRead) {
mBlockWorkerClient.accessBlock(mBlockId);
ClientContext.getClientMetrics().incBlocksReadLocal(1);
}
mBlockWorkerClient.unlockBlock(mBlockId);
} catch (AlluxioException e) {
throw new IOException(e);
} finally {
mContext.releaseWorkerClient(mBlockWorkerClient);
mCloser.close();
if (mBuffer != null && mBuffer.isDirect()) {
BufferUtils.cleanDirectBuffer(mBuffer);
}
}
mClosed = true;
}
@Override
protected void bufferedRead(int len) throws IOException {
if (mBuffer.isDirect()) { // Buffer may not be direct on initialization
BufferUtils.cleanDirectBuffer(mBuffer);
}
mBuffer = mReader.read(getPosition(), len);
}
@Override
public int directRead(byte[] b, int off, int len) throws IOException {
ByteBuffer buf = mReader.read(getPosition(), len);
buf.get(b, off, len);
BufferUtils.cleanDirectBuffer(buf);
return len;
}
@Override
protected void incrementBytesReadMetric(int bytes) {
ClientContext.getClientMetrics().incBytesReadLocal(bytes);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy