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