Class DuplicatingCheckpointOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.flink.core.fs.FSDataOutputStream
-
- org.apache.flink.runtime.state.CheckpointStateOutputStream
-
- org.apache.flink.runtime.state.DuplicatingCheckpointOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public class DuplicatingCheckpointOutputStream extends CheckpointStateOutputStream
A CheckpointStateOutputStream that wraps a primary and a secondary CheckpointStateOutputStream and duplicates all writes into both streams. This stream applies buffering to reduce the amount of dual-method calling. Furthermore, exceptions that happen in interactions with the secondary stream are not exposed, until the user callscloseAndGetSecondaryHandle()
. In contrast to that, exceptions from interactions with the primary stream are immediately returned to the user. This class is used to write state for local recovery as a local (secondary) copy of the (primary) state snapshot that is written to a (slower but highly-available) remote filesystem.
-
-
Constructor Summary
Constructors Constructor Description DuplicatingCheckpointOutputStream(CheckpointStateOutputStream primaryOutputStream, CheckpointStateOutputStream secondaryOutputStream)
DuplicatingCheckpointOutputStream(CheckpointStateOutputStream primaryOutputStream, CheckpointStateOutputStream secondaryOutputStream, int bufferSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
This method should close the stream, if has not been closed before.StreamStateHandle
closeAndGetHandle()
Closes the stream and gets a state handle that can create an input stream producing the data written to this stream.StreamStateHandle
closeAndGetPrimaryHandle()
Returns the state handle from theprimaryOutputStream
.StreamStateHandle
closeAndGetSecondaryHandle()
Returns the state handle from thesecondaryOutputStream
.void
flush()
Flushes the stream, writing any data currently buffered in stream implementation to the proper output stream.long
getPos()
Gets the position of the stream (non-negative), defined as the number of bytes from the beginning of the file to the current writing position.Exception
getSecondaryStreamException()
void
sync()
Flushes the data all the way to the persistent non-volatile storage (for example disks).void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
DuplicatingCheckpointOutputStream
public DuplicatingCheckpointOutputStream(CheckpointStateOutputStream primaryOutputStream, CheckpointStateOutputStream secondaryOutputStream) throws IOException
- Throws:
IOException
-
DuplicatingCheckpointOutputStream
public DuplicatingCheckpointOutputStream(CheckpointStateOutputStream primaryOutputStream, CheckpointStateOutputStream secondaryOutputStream, int bufferSize) throws IOException
- Throws:
IOException
-
-
Method Detail
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
getPos
public long getPos() throws IOException
Description copied from class:FSDataOutputStream
Gets the position of the stream (non-negative), defined as the number of bytes from the beginning of the file to the current writing position. The position corresponds to the zero-based index of the next byte that will be written.This method must report accurately report the current position of the stream. Various components of the high-availability and recovery logic rely on the accurate
- Specified by:
getPos
in classFSDataOutputStream
- Returns:
- The current position in the stream, defined as the number of bytes from the beginning of the file to the current writing position.
- Throws:
IOException
- Thrown if an I/O error occurs while obtaining the position from the stream implementation.
-
flush
public void flush() throws IOException
Description copied from class:FSDataOutputStream
Flushes the stream, writing any data currently buffered in stream implementation to the proper output stream. After this method has been called, the stream implementation must not hold onto any buffered data any more.A completed flush does not mean that the data is necessarily persistent. Data persistence can is only assumed after calls to
FSDataOutputStream.close()
orFSDataOutputStream.sync()
.Implementation note: This overrides the method defined in
OutputStream
as abstract to force implementations of theFSDataOutputStream
to implement this method directly.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in classFSDataOutputStream
- Throws:
IOException
- Thrown if an I/O error occurs while flushing the stream.
-
sync
public void sync() throws IOException
Description copied from class:FSDataOutputStream
Flushes the data all the way to the persistent non-volatile storage (for example disks). The method behaves similar to the fsync function, forcing all data to be persistent on the devices.- Specified by:
sync
in classFSDataOutputStream
- Throws:
IOException
- Thrown if an I/O error occurs
-
close
public void close() throws IOException
Description copied from class:CheckpointStateOutputStream
This method should close the stream, if has not been closed before. If this method actually closes the stream, it should delete/release the resource behind the stream, such as the file that the stream writes to.The above implies that this method is intended to be the "unsuccessful close", such as when cancelling the stream writing, or when an exception occurs. Closing the stream for the successful case must go through
CheckpointStateOutputStream.closeAndGetHandle()
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classCheckpointStateOutputStream
- Throws:
IOException
- Thrown, if the stream cannot be closed.
-
closeAndGetHandle
@Nullable public StreamStateHandle closeAndGetHandle() throws IOException
Description copied from class:CheckpointStateOutputStream
Closes the stream and gets a state handle that can create an input stream producing the data written to this stream.This closing must be called (also when the caller is not interested in the handle) to successfully close the stream and retain the produced resource. In contrast, the
CheckpointStateOutputStream.close()
method removes the target resource when called.- Specified by:
closeAndGetHandle
in classCheckpointStateOutputStream
- Returns:
- A state handle that can create an input stream producing the data written to this stream.
- Throws:
IOException
- Thrown, if the stream cannot be closed.
-
closeAndGetPrimaryHandle
public StreamStateHandle closeAndGetPrimaryHandle() throws IOException
Returns the state handle from theprimaryOutputStream
.- Throws:
IOException
-
closeAndGetSecondaryHandle
public StreamStateHandle closeAndGetSecondaryHandle() throws IOException
Returns the state handle from thesecondaryOutputStream
. Also reports suppressed exceptions from earlier interactions with that stream.- Throws:
IOException
-
getSecondaryStreamException
public Exception getSecondaryStreamException()
-
-