Search in sources :

Example 1 with SharedFileChannel

use of org.apache.cassandra.net.SharedDefaultFileRegion.SharedFileChannel in project cassandra by apache.

the class AsyncStreamingOutputPlus method writeFileToChannelZeroCopyThrottled.

private long writeFileToChannelZeroCopyThrottled(FileChannel file, RateLimiter limiter, int batchSize, int lowWaterMark, int highWaterMark) throws IOException {
    final long length = file.size();
    long bytesTransferred = 0;
    final SharedFileChannel sharedFile = SharedDefaultFileRegion.share(file);
    try {
        int toWrite;
        while (bytesTransferred < length) {
            toWrite = (int) min(batchSize, length - bytesTransferred);
            limiter.acquire(toWrite);
            ChannelPromise promise = beginFlush(toWrite, lowWaterMark, highWaterMark);
            SharedDefaultFileRegion fileRegion = new SharedDefaultFileRegion(sharedFile, bytesTransferred, toWrite);
            channel.writeAndFlush(fileRegion, promise);
            if (logger.isTraceEnabled())
                logger.trace("Writing {} bytes at position {} of {}", toWrite, bytesTransferred, length);
            bytesTransferred += toWrite;
        }
        return bytesTransferred;
    } finally {
        sharedFile.release();
    }
}
Also used : SharedFileChannel(org.apache.cassandra.net.SharedDefaultFileRegion.SharedFileChannel) ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)1 SharedFileChannel (org.apache.cassandra.net.SharedDefaultFileRegion.SharedFileChannel)1