Package org.apache.cassandra.net
Class AsyncChannelOutputPlus
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.cassandra.io.util.DataOutputStreamPlus
-
- org.apache.cassandra.io.util.BufferedDataOutputStreamPlus
-
- org.apache.cassandra.net.AsyncChannelOutputPlus
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataOutput
,java.io.Flushable
,java.lang.AutoCloseable
,DataOutputPlus
- Direct Known Subclasses:
AsyncMessageOutputPlus
,AsyncStreamingOutputPlus
public abstract class AsyncChannelOutputPlus extends BufferedDataOutputStreamPlus
ADataOutputStreamPlus
that writes ASYNCHRONOUSLY to a Netty Channel. The close() and flush() methods synchronously wait for pending writes, and will propagate any exceptions encountered in writing them to the wire. The correctness of this class depends on the ChannelPromise we create against a Channel always being completed, which appears to be a guarantee provided by Netty so long as the event loop is running. There are two logical threads accessing the state in this class: the eventLoop of the channel, and the writer (the writer thread may change, so long as only one utilises the class at any time). Each thread has exclusive write access to certain state in the class, with the other thread only viewing the state, simplifying concurrency considerations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AsyncChannelOutputPlus.FlushException
-
Field Summary
-
Fields inherited from class org.apache.cassandra.io.util.BufferedDataOutputStreamPlus
buffer
-
-
Constructor Summary
Constructors Constructor Description AsyncChannelOutputPlus(io.netty.channel.Channel channel)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected io.netty.channel.ChannelPromise
beginFlush(long byteCount, long lowWaterMark, long highWaterMark)
Create a ChannelPromise for a flush of the given size.void
close()
Flush any remaining writes, and release any buffers.abstract void
discard()
Discard any buffered data, and the buffers that contain it.protected abstract void
doFlush(int count)
void
flush()
Perform an asynchronous flush, then waits until all outstanding flushes have completedlong
flushed()
long
flushedToNetwork()
protected java.nio.channels.WritableByteChannel
newDefaultChannel()
protected void
parkUntilFlushed(long wakeUpWhenFlushed, long signalWhenFlushed)
Utility method for waitUntilFlushed, which actually parks the current thread until the necessary number of bytes have been flushed This may only be invoked by the writer thread, never by the eventLoop.abstract long
position()
Returns the current position of the underlying target like a file-pointer or the position withing a buffer.protected void
releaseSpace(long bytesFlushed)
Update our flushed count, and signal any waiters.-
Methods inherited from class org.apache.cassandra.io.util.BufferedDataOutputStreamPlus
allocate, order, write, write, write, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeMostSignificantBytes, writeShort, writeUTF
-
Methods inherited from class org.apache.cassandra.io.util.DataOutputStreamPlus
retrieveTemporaryBuffer
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.cassandra.io.util.DataOutputPlus
bytesLeftInPage, hasPosition, maxBytesInPage, paddedPosition, padToPageBoundary, write, writeUnsignedVInt, writeUnsignedVInt, writeUnsignedVInt32, writeVInt, writeVInt, writeVInt32
-
-
-
-
Method Detail
-
beginFlush
protected io.netty.channel.ChannelPromise beginFlush(long byteCount, long lowWaterMark, long highWaterMark) throws java.io.IOException
Create a ChannelPromise for a flush of the given size.This method will not return until the write is permitted by the provided watermarks and in flight bytes, and on its completion will mark the requested bytes flushed.
If this method returns normally, the ChannelPromise MUST be writtenAndFlushed, or else completed exceptionally.
- Throws:
java.io.IOException
-
parkUntilFlushed
protected void parkUntilFlushed(long wakeUpWhenFlushed, long signalWhenFlushed)
Utility method for waitUntilFlushed, which actually parks the current thread until the necessary number of bytes have been flushed This may only be invoked by the writer thread, never by the eventLoop.
-
releaseSpace
protected void releaseSpace(long bytesFlushed)
Update our flushed count, and signal any waiters. This may only be invoked by the eventLoop, never by the writer thread.
-
doFlush
protected abstract void doFlush(int count) throws java.io.IOException
- Overrides:
doFlush
in classBufferedDataOutputStreamPlus
- Throws:
java.io.IOException
-
position
public abstract long position()
Description copied from interface:DataOutputPlus
Returns the current position of the underlying target like a file-pointer or the position withing a buffer. Not every implementation may support this functionality. Whether or not this functionality is supported can be checked via theDataOutputPlus.hasPosition()
.
-
flushed
public long flushed()
-
flushedToNetwork
public long flushedToNetwork()
-
flush
public void flush() throws java.io.IOException
Perform an asynchronous flush, then waits until all outstanding flushes have completed- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classBufferedDataOutputStreamPlus
- Throws:
java.io.IOException
- if any flush fails
-
close
public void close() throws java.io.IOException
Flush any remaining writes, and release any buffers. The channel is not closed, as it is assumed to be managed externally. WARNING: This method requires mutual exclusivity with all other producer methods to run safely. It should only be invoked by the owning thread, never the eventLoop; the eventLoop should propagate errors toflushFailed
, which will propagate them to the producer thread no later than its final invocation toclose()
orflush()
(that must not be followed by any further writes).- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classBufferedDataOutputStreamPlus
- Throws:
java.io.IOException
-
discard
public abstract void discard()
Discard any buffered data, and the buffers that contain it. May be invoked instead ofclose()
if we terminate exceptionally.
-
newDefaultChannel
protected java.nio.channels.WritableByteChannel newDefaultChannel()
- Overrides:
newDefaultChannel
in classDataOutputStreamPlus
-
-