Search in sources :

Example 1 with GetFileResponseBuilder

use of org.apache.ignite.raft.jraft.rpc.GetFileResponseBuilder in project ignite-3 by apache.

the class FileService method handleGetFile.

/**
 * Handle GetFileRequest, run the response or set the response with done.
 */
public Message handleGetFile(final GetFileRequest request, final RpcRequestClosure done) {
    if (request.count() <= 0 || request.offset() < 0) {
        return // 
        RaftRpcFactory.DEFAULT.newResponse(msgFactory, RaftError.EREQUEST, "Invalid request: %s", request);
    }
    final FileReader reader = this.fileReaderMap.get(request.readerId());
    if (LOG.isDebugEnabled()) {
        LOG.info("handleGetFile id={}, name={}, offset={}, cnt={}", request.readerId(), request.filename(), request.offset(), request.count());
    }
    if (reader == null) {
        return // 
        RaftRpcFactory.DEFAULT.newResponse(msgFactory, RaftError.ENOENT, "Fail to find reader=%d", request.readerId());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("GetFile from {} path={} filename={} offset={} count={}", done.getRpcCtx().getRemoteAddress(), reader.getPath(), request.filename(), request.offset(), request.count());
    }
    final ByteBufferCollector dataBuffer = ByteBufferCollector.allocate();
    final GetFileResponseBuilder responseBuilder = msgFactory.getFileResponse();
    try {
        final int read = reader.readFile(dataBuffer, request.filename(), request.offset(), request.count());
        responseBuilder.readSize(read);
        responseBuilder.eof(read == FileReader.EOF);
        final ByteBuffer buf = dataBuffer.getBuffer();
        buf.flip();
        if (!buf.hasRemaining()) {
            // skip empty data
            responseBuilder.data(ByteString.EMPTY);
        } else {
            // TODO check hole https://issues.apache.org/jira/browse/IGNITE-14832
            responseBuilder.data(new ByteString(buf));
        }
        return responseBuilder.build();
    } catch (final RetryAgainException e) {
        return // 
        RaftRpcFactory.DEFAULT.newResponse(msgFactory, RaftError.EAGAIN, "Fail to read from path=%s filename=%s with error: %s", reader.getPath(), request.filename(), e.getMessage());
    } catch (final IOException e) {
        LOG.error("Fail to read file path={} filename={}", e, reader.getPath(), request.filename());
        return // 
        RaftRpcFactory.DEFAULT.newResponse(msgFactory, RaftError.EIO, "Fail to read from path=%s filename=%s", reader.getPath(), request.filename());
    }
}
Also used : ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) GetFileResponseBuilder(org.apache.ignite.raft.jraft.rpc.GetFileResponseBuilder) ByteString(org.apache.ignite.raft.jraft.util.ByteString) FileReader(org.apache.ignite.raft.jraft.storage.io.FileReader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) RetryAgainException(org.apache.ignite.raft.jraft.error.RetryAgainException)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 RetryAgainException (org.apache.ignite.raft.jraft.error.RetryAgainException)1 GetFileResponseBuilder (org.apache.ignite.raft.jraft.rpc.GetFileResponseBuilder)1 FileReader (org.apache.ignite.raft.jraft.storage.io.FileReader)1 ByteBufferCollector (org.apache.ignite.raft.jraft.util.ByteBufferCollector)1 ByteString (org.apache.ignite.raft.jraft.util.ByteString)1