Search in sources :

Example 1 with RetryAgainException

use of org.apache.ignite.raft.jraft.error.RetryAgainException in project ignite-3 by apache.

the class SnapshotFileReader method readFile.

@Override
public int readFile(final ByteBufferCollector metaBufferCollector, final String fileName, final long offset, final long maxCount) throws IOException, RetryAgainException {
    // read the whole meta file.
    if (fileName.equals(Snapshot.JRAFT_SNAPSHOT_META_FILE)) {
        final ByteBuffer metaBuf = this.metaTable.saveToByteBufferAsRemote();
        // because bufRef will flip the buffer before using, so we must set the meta buffer position to it's limit.
        metaBuf.position(metaBuf.limit());
        metaBufferCollector.setBuffer(metaBuf);
        return EOF;
    }
    final LocalFileMeta fileMeta = this.metaTable.getFileMeta(fileName);
    if (fileMeta == null) {
        throw new FileNotFoundException("LocalFileMeta not found for " + fileName);
    }
    // go through throttle
    long newMaxCount = maxCount;
    if (this.snapshotThrottle != null) {
        newMaxCount = this.snapshotThrottle.throttledByThroughput(maxCount);
        if (newMaxCount < maxCount) {
            // throughput is throttled to 0, try again.
            if (newMaxCount == 0) {
                throw new RetryAgainException("readFile throttled by throughput");
            }
        }
    }
    return readFileWithMeta(metaBufferCollector, fileName, fileMeta, offset, newMaxCount);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) LocalFileMeta(org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter.LocalFileMeta) ByteBuffer(java.nio.ByteBuffer) RetryAgainException(org.apache.ignite.raft.jraft.error.RetryAgainException)

Example 2 with RetryAgainException

use of org.apache.ignite.raft.jraft.error.RetryAgainException 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

ByteBuffer (java.nio.ByteBuffer)2 RetryAgainException (org.apache.ignite.raft.jraft.error.RetryAgainException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 LocalFileMeta (org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter.LocalFileMeta)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