Class Handover
- java.lang.Object
-
- org.apache.flink.cdc.debezium.internal.Handover
-
- All Implemented Interfaces:
Closeable,AutoCloseable
@ThreadSafe @Internal public class Handover extends Object implements Closeable
The Handover is a utility to hand over data (a buffer of records) and exception from a producer thread to a consumer thread. It effectively behaves like a "size one blocking queue", with some extras around exception reporting, closing, and waking up thread withoutinterruptingthreads.This class is used in the Flink Debezium Engine Consumer to hand over data and exceptions between the thread that runs the DebeziumEngine class and the main thread.
The Handover can also be "closed", signalling from one thread to the other that it the thread has terminated.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHandover.ClosedExceptionAn exception thrown by the Handover in thepollNext()orproduce(List)method, after the Handover was closed viaclose().
-
Constructor Summary
Constructors Constructor Description Handover()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the handover.ThrowablegetError()Return the error, if its set.ObjectgetLock()booleanhasError()Return whether there is an error.List<io.debezium.engine.ChangeEvent<org.apache.kafka.connect.source.SourceRecord,org.apache.kafka.connect.source.SourceRecord>>pollNext()Polls the next element from the Handover, possibly blocking until the next element is available.voidproduce(List<io.debezium.engine.ChangeEvent<org.apache.kafka.connect.source.SourceRecord,org.apache.kafka.connect.source.SourceRecord>> element)Hands over an element from the producer.voidreportError(Throwable t)Reports an exception.
-
-
-
Method Detail
-
pollNext
public List<io.debezium.engine.ChangeEvent<org.apache.kafka.connect.source.SourceRecord,org.apache.kafka.connect.source.SourceRecord>> pollNext() throws Exception
Polls the next element from the Handover, possibly blocking until the next element is available. This method behaves similar to polling from a blocking queue.If an exception was handed in by the producer (
reportError(Throwable)), then that exception is thrown rather than an element being returned.- Returns:
- The next element (buffer of records, never null).
- Throws:
Handover.ClosedException- Thrown if the Handover wasclosed.Exception- Rethrows exceptions from thereportError(Throwable)method.
-
produce
public void produce(List<io.debezium.engine.ChangeEvent<org.apache.kafka.connect.source.SourceRecord,org.apache.kafka.connect.source.SourceRecord>> element) throws InterruptedException
Hands over an element from the producer. If the Handover already has an element that was not yet picked up by the consumer thread, this call blocks until the consumer picks up that previous element.This behavior is similar to a "size one" blocking queue.
- Parameters:
element- The next element to hand over.- Throws:
InterruptedException- Thrown, if the thread is interrupted while blocking for the Handover to be empty.
-
reportError
public void reportError(Throwable t)
Reports an exception. The consumer will throw the given exception immediately, if it is currently blocked in thepollNext()method, or the next time it calls that method.After this method has been called, no call to either
produce( List)orpollNext()will ever return regularly any more, but will always return exceptionally.If another exception was already reported, this method does nothing.
For the producer, the Handover will appear as if it was
closed.- Parameters:
t- The exception to report.
-
hasError
public boolean hasError()
Return whether there is an error.- Returns:
- whether there is an error
-
getError
@Nullable public Throwable getError()
Return the error, if its set.- Returns:
- the error, if its set
-
close
public void close()
Closes the handover. Both theproduce(List)method and thepollNext()will throw aHandover.ClosedExceptionon any currently blocking and future invocations.If an exception was previously reported via the
reportError(Throwable)method, that exception will not be overridden. The consumer thread will throw that exception upon callingpollNext(), rather than theClosedException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
getLock
@VisibleForTesting public Object getLock()
-
-