24
24
import org .apache .kafka .clients .producer .RecordMetadata ;
25
25
import org .apache .kafka .clients .producer .internals .TransactionManager ;
26
26
import org .apache .kafka .clients .producer .internals .TransactionalRequestResult ;
27
+ import org .apache .kafka .common .KafkaException ;
27
28
import org .apache .kafka .common .errors .ProducerFencedException ;
28
29
import org .slf4j .Logger ;
29
30
import org .slf4j .LoggerFactory ;
@@ -55,6 +56,7 @@ class FlinkKafkaInternalProducer<K, V> extends KafkaProducer<K, V> {
55
56
private volatile boolean inTransaction ;
56
57
private volatile boolean hasRecordsInTransaction ;
57
58
private volatile boolean closed ;
59
+ private volatile boolean flushGated ;
58
60
59
61
public FlinkKafkaInternalProducer (Properties properties , @ Nullable String transactionalId ) {
60
62
super (withTransactionalId (properties , transactionalId ));
@@ -74,6 +76,10 @@ private static Properties withTransactionalId(
74
76
75
77
@ Override
76
78
public Future <RecordMetadata > send (ProducerRecord <K , V > record , Callback callback ) {
79
+ if (flushGated ) {
80
+ throw new KafkaException (
81
+ "unable to write more records, current producer has been flush gated" );
82
+ }
77
83
if (inTransaction ) {
78
84
hasRecordsInTransaction = true ;
79
85
}
@@ -82,10 +88,12 @@ public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callbac
82
88
83
89
@ Override
84
90
public void flush () {
91
+ flushGated = true ;
85
92
super .flush ();
86
93
if (inTransaction ) {
87
94
flushNewPartitions ();
88
95
}
96
+ flushGated = false ;
89
97
}
90
98
91
99
@ Override
@@ -109,7 +117,9 @@ public void commitTransaction() throws ProducerFencedException {
109
117
checkState (inTransaction , "Transaction was not started" );
110
118
inTransaction = false ;
111
119
hasRecordsInTransaction = false ;
120
+ flushGated = true ;
112
121
super .commitTransaction ();
122
+ flushGated = false ;
113
123
}
114
124
115
125
public boolean isInTransaction () {
@@ -120,6 +130,10 @@ public boolean hasRecordsInTransaction() {
120
130
return hasRecordsInTransaction ;
121
131
}
122
132
133
+ public void setFlushGate (boolean closed ) {
134
+ flushGated = closed ;
135
+ }
136
+
123
137
@ Override
124
138
public void close () {
125
139
closed = true ;
@@ -398,6 +412,8 @@ public String toString() {
398
412
+ inTransaction
399
413
+ ", closed="
400
414
+ closed
415
+ + ", flushGated="
416
+ + flushGated
401
417
+ '}' ;
402
418
}
403
419
}
0 commit comments