Skip to content

Commit b8328ab

Browse files
Jiabao-SunMartijnVisser
authored andcommitted
[FLINK-34192] Update to be compatible with updated SinkV2 interfaces
1 parent cfb275b commit b8328ab

File tree

4 files changed

+91
-80
lines changed

4 files changed

+91
-80
lines changed

.github/workflows/push_pr.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ jobs:
3030
include:
3131
- flink: 1.18.1
3232
jdk: '8, 11, 17'
33+
- flink: 1.19-SNAPSHOT
34+
jdk: '8, 11, 17, 21'
3335
uses: apache/flink-connector-shared-utils/.github/workflows/ci.yml@ci_utils
3436
with:
3537
flink_version: ${{ matrix.flink }}

flink-connector-kafka/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ under the License.
144144
<groupId>org.slf4j</groupId>
145145
<artifactId>slf4j-api</artifactId>
146146
</exclusion>
147+
<exclusion>
148+
<groupId>io.dropwizard.metrics</groupId>
149+
<artifactId>metrics-core</artifactId>
150+
</exclusion>
147151
</exclusions>
148152
<scope>test</scope>
149153
</dependency>

flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java

Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import org.apache.flink.metrics.Gauge;
2828
import org.apache.flink.metrics.MetricGroup;
2929
import org.apache.flink.metrics.groups.OperatorIOMetricGroup;
30+
import org.apache.flink.metrics.groups.OperatorMetricGroup;
3031
import org.apache.flink.metrics.groups.SinkWriterMetricGroup;
3132
import org.apache.flink.metrics.testutils.MetricListener;
3233
import org.apache.flink.runtime.metrics.groups.InternalSinkWriterMetricGroup;
34+
import org.apache.flink.runtime.metrics.groups.ProxyMetricGroup;
3335
import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
3436
import org.apache.flink.util.TestLoggerExtension;
3537
import org.apache.flink.util.UserCodeClassLoader;
@@ -58,7 +60,6 @@
5860
import java.nio.ByteBuffer;
5961
import java.util.ArrayList;
6062
import java.util.Collection;
61-
import java.util.Collections;
6263
import java.util.Comparator;
6364
import java.util.List;
6465
import java.util.Optional;
@@ -84,7 +85,7 @@ public class KafkaWriterITCase {
8485
private static final Network NETWORK = Network.newNetwork();
8586
private static final String KAFKA_METRIC_WITH_GROUP_NAME = "KafkaProducer.incoming-byte-total";
8687
private static final SinkWriter.Context SINK_WRITER_CONTEXT = new DummySinkWriterContext();
87-
private String topic;
88+
private static String topic;
8889

8990
private MetricListener metricListener;
9091
private TriggerTimeService timeService;
@@ -130,11 +131,8 @@ public void testNotRegisterMetrics(DeliveryGuarantee guarantee) throws Exception
130131

131132
@Test
132133
public void testIncreasingRecordBasedCounters() throws Exception {
133-
final OperatorIOMetricGroup operatorIOMetricGroup =
134-
UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup().getIOMetricGroup();
135-
final InternalSinkWriterMetricGroup metricGroup =
136-
InternalSinkWriterMetricGroup.mock(
137-
metricListener.getMetricGroup(), operatorIOMetricGroup);
134+
final SinkWriterMetricGroup metricGroup = createSinkWriterMetricGroup();
135+
138136
try (final KafkaWriter<Integer> writer =
139137
createWriterWithConfiguration(
140138
getKafkaClientConfiguration(), DeliveryGuarantee.NONE, metricGroup)) {
@@ -167,13 +165,9 @@ public void testIncreasingRecordBasedCounters() throws Exception {
167165

168166
@Test
169167
public void testCurrentSendTimeMetric() throws Exception {
170-
final InternalSinkWriterMetricGroup metricGroup =
171-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup());
172168
try (final KafkaWriter<Integer> writer =
173169
createWriterWithConfiguration(
174-
getKafkaClientConfiguration(),
175-
DeliveryGuarantee.AT_LEAST_ONCE,
176-
metricGroup)) {
170+
getKafkaClientConfiguration(), DeliveryGuarantee.AT_LEAST_ONCE)) {
177171
final Optional<Gauge<Long>> currentSendTime =
178172
metricListener.getGauge("currentSendTime");
179173
assertThat(currentSendTime.isPresent()).isTrue();
@@ -199,16 +193,12 @@ public void testCurrentSendTimeMetric() throws Exception {
199193
void testFlushAsyncErrorPropagationAndErrorCounter() throws Exception {
200194
Properties properties = getKafkaClientConfiguration();
201195

202-
SinkInitContext sinkInitContext =
203-
new SinkInitContext(
204-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup()),
205-
timeService,
206-
null);
196+
final SinkWriterMetricGroup metricGroup = createSinkWriterMetricGroup();
197+
207198
final KafkaWriter<Integer> writer =
208199
createWriterWithConfiguration(
209-
properties, DeliveryGuarantee.EXACTLY_ONCE, sinkInitContext);
210-
final Counter numRecordsOutErrors =
211-
sinkInitContext.metricGroup.getNumRecordsOutErrorsCounter();
200+
properties, DeliveryGuarantee.EXACTLY_ONCE, metricGroup);
201+
final Counter numRecordsOutErrors = metricGroup.getNumRecordsOutErrorsCounter();
212202
assertThat(numRecordsOutErrors.getCount()).isEqualTo(0L);
213203

214204
triggerProducerException(writer, properties);
@@ -228,16 +218,12 @@ void testFlushAsyncErrorPropagationAndErrorCounter() throws Exception {
228218
void testWriteAsyncErrorPropagationAndErrorCounter() throws Exception {
229219
Properties properties = getKafkaClientConfiguration();
230220

231-
SinkInitContext sinkInitContext =
232-
new SinkInitContext(
233-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup()),
234-
timeService,
235-
null);
221+
final SinkWriterMetricGroup metricGroup = createSinkWriterMetricGroup();
222+
236223
final KafkaWriter<Integer> writer =
237224
createWriterWithConfiguration(
238-
properties, DeliveryGuarantee.EXACTLY_ONCE, sinkInitContext);
239-
final Counter numRecordsOutErrors =
240-
sinkInitContext.metricGroup.getNumRecordsOutErrorsCounter();
225+
properties, DeliveryGuarantee.EXACTLY_ONCE, metricGroup);
226+
final Counter numRecordsOutErrors = metricGroup.getNumRecordsOutErrorsCounter();
241227
assertThat(numRecordsOutErrors.getCount()).isEqualTo(0L);
242228

243229
triggerProducerException(writer, properties);
@@ -259,10 +245,8 @@ void testMailboxAsyncErrorPropagationAndErrorCounter() throws Exception {
259245
Properties properties = getKafkaClientConfiguration();
260246

261247
SinkInitContext sinkInitContext =
262-
new SinkInitContext(
263-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup()),
264-
timeService,
265-
null);
248+
new SinkInitContext(createSinkWriterMetricGroup(), timeService, null);
249+
266250
final KafkaWriter<Integer> writer =
267251
createWriterWithConfiguration(
268252
properties, DeliveryGuarantee.EXACTLY_ONCE, sinkInitContext);
@@ -289,16 +273,12 @@ void testMailboxAsyncErrorPropagationAndErrorCounter() throws Exception {
289273
void testCloseAsyncErrorPropagationAndErrorCounter() throws Exception {
290274
Properties properties = getKafkaClientConfiguration();
291275

292-
SinkInitContext sinkInitContext =
293-
new SinkInitContext(
294-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup()),
295-
timeService,
296-
null);
276+
final SinkWriterMetricGroup metricGroup = createSinkWriterMetricGroup();
277+
297278
final KafkaWriter<Integer> writer =
298279
createWriterWithConfiguration(
299-
properties, DeliveryGuarantee.EXACTLY_ONCE, sinkInitContext);
300-
final Counter numRecordsOutErrors =
301-
sinkInitContext.metricGroup.getNumRecordsOutErrorsCounter();
280+
properties, DeliveryGuarantee.EXACTLY_ONCE, metricGroup);
281+
final Counter numRecordsOutErrors = metricGroup.getNumRecordsOutErrorsCounter();
302282
assertThat(numRecordsOutErrors.getCount()).isEqualTo(0L);
303283

304284
triggerProducerException(writer, properties);
@@ -334,7 +314,7 @@ public void testMetadataPublisher() throws Exception {
334314
createWriterWithConfiguration(
335315
getKafkaClientConfiguration(),
336316
DeliveryGuarantee.AT_LEAST_ONCE,
337-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup()),
317+
createSinkWriterMetricGroup(),
338318
meta -> metadataList.add(meta.toString()))) {
339319
List<String> expected = new ArrayList<>();
340320
for (int i = 0; i < 100; i++) {
@@ -518,45 +498,53 @@ private void assertKafkaMetricNotPresent(
518498
}
519499

520500
private KafkaWriter<Integer> createWriterWithConfiguration(
521-
Properties config, DeliveryGuarantee guarantee) {
522-
return createWriterWithConfiguration(
523-
config,
524-
guarantee,
525-
InternalSinkWriterMetricGroup.mock(metricListener.getMetricGroup()));
501+
Properties config, DeliveryGuarantee guarantee) throws IOException {
502+
return createWriterWithConfiguration(config, guarantee, createSinkWriterMetricGroup());
526503
}
527504

528505
private KafkaWriter<Integer> createWriterWithConfiguration(
529506
Properties config,
530507
DeliveryGuarantee guarantee,
531-
SinkWriterMetricGroup sinkWriterMetricGroup) {
508+
SinkWriterMetricGroup sinkWriterMetricGroup)
509+
throws IOException {
532510
return createWriterWithConfiguration(config, guarantee, sinkWriterMetricGroup, null);
533511
}
534512

535513
private KafkaWriter<Integer> createWriterWithConfiguration(
536514
Properties config,
537515
DeliveryGuarantee guarantee,
538516
SinkWriterMetricGroup sinkWriterMetricGroup,
539-
@Nullable Consumer<RecordMetadata> metadataConsumer) {
540-
return new KafkaWriter<>(
541-
guarantee,
542-
config,
543-
"test-prefix",
544-
new SinkInitContext(sinkWriterMetricGroup, timeService, metadataConsumer),
545-
new DummyRecordSerializer(),
546-
new DummySchemaContext(),
547-
Collections.emptyList());
517+
@Nullable Consumer<RecordMetadata> metadataConsumer)
518+
throws IOException {
519+
KafkaSink<Integer> kafkaSink =
520+
KafkaSink.<Integer>builder()
521+
.setKafkaProducerConfig(config)
522+
.setDeliveryGuarantee(guarantee)
523+
.setTransactionalIdPrefix("test-prefix")
524+
.setRecordSerializer(new DummyRecordSerializer())
525+
.build();
526+
return (KafkaWriter<Integer>)
527+
kafkaSink.createWriter(
528+
new SinkInitContext(sinkWriterMetricGroup, timeService, metadataConsumer));
548529
}
549530

550531
private KafkaWriter<Integer> createWriterWithConfiguration(
551-
Properties config, DeliveryGuarantee guarantee, SinkInitContext sinkInitContext) {
552-
return new KafkaWriter<>(
553-
guarantee,
554-
config,
555-
"test-prefix",
556-
sinkInitContext,
557-
new DummyRecordSerializer(),
558-
new DummySchemaContext(),
559-
Collections.emptyList());
532+
Properties config, DeliveryGuarantee guarantee, SinkInitContext sinkInitContext)
533+
throws IOException {
534+
KafkaSink<Integer> kafkaSink =
535+
KafkaSink.<Integer>builder()
536+
.setKafkaProducerConfig(config)
537+
.setDeliveryGuarantee(guarantee)
538+
.setTransactionalIdPrefix("test-prefix")
539+
.setRecordSerializer(new DummyRecordSerializer())
540+
.build();
541+
return (KafkaWriter<Integer>) kafkaSink.createWriter(sinkInitContext);
542+
}
543+
544+
private SinkWriterMetricGroup createSinkWriterMetricGroup() {
545+
DummyOperatorMetricGroup operatorMetricGroup =
546+
new DummyOperatorMetricGroup(metricListener.getMetricGroup());
547+
return InternalSinkWriterMetricGroup.wrap(operatorMetricGroup);
560548
}
561549

562550
private static Properties getKafkaClientConfiguration() {
@@ -632,7 +620,7 @@ public <MetaT> Optional<Consumer<MetaT>> metadataConsumer() {
632620
}
633621
}
634622

635-
private class DummyRecordSerializer implements KafkaRecordSerializationSchema<Integer> {
623+
private static class DummyRecordSerializer implements KafkaRecordSerializationSchema<Integer> {
636624
@Override
637625
public ProducerRecord<byte[], byte[]> serialize(
638626
Integer element, KafkaSinkContext context, Long timestamp) {
@@ -644,28 +632,33 @@ public ProducerRecord<byte[], byte[]> serialize(
644632
}
645633
}
646634

647-
private static class DummySchemaContext implements SerializationSchema.InitializationContext {
648-
635+
private static class DummySinkWriterContext implements SinkWriter.Context {
649636
@Override
650-
public MetricGroup getMetricGroup() {
651-
throw new UnsupportedOperationException("Not implemented.");
637+
public long currentWatermark() {
638+
return 0;
652639
}
653640

654641
@Override
655-
public UserCodeClassLoader getUserCodeClassLoader() {
656-
throw new UnsupportedOperationException("Not implemented.");
642+
public Long timestamp() {
643+
return null;
657644
}
658645
}
659646

660-
private static class DummySinkWriterContext implements SinkWriter.Context {
661-
@Override
662-
public long currentWatermark() {
663-
return 0;
647+
private static class DummyOperatorMetricGroup extends ProxyMetricGroup<MetricGroup>
648+
implements OperatorMetricGroup {
649+
650+
private final OperatorIOMetricGroup operatorIOMetricGroup;
651+
652+
public DummyOperatorMetricGroup(MetricGroup parentMetricGroup) {
653+
super(parentMetricGroup);
654+
this.operatorIOMetricGroup =
655+
UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup()
656+
.getIOMetricGroup();
664657
}
665658

666659
@Override
667-
public Long timestamp() {
668-
return null;
660+
public OperatorIOMetricGroup getIOMetricGroup() {
661+
return operatorIOMetricGroup;
669662
}
670663
}
671664

flink-connector-kafka/src/test/java/org/apache/flink/streaming/connectors/kafka/table/KafkaTableTestUtils.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Map;
4242
import java.util.concurrent.TimeUnit;
4343
import java.util.concurrent.TimeoutException;
44+
import java.util.stream.Collectors;
4445

4546
import static org.assertj.core.api.Assertions.assertThat;
4647
import static org.assertj.core.api.HamcrestCondition.matching;
@@ -98,8 +99,11 @@ public static void waitingExpectedResults(
9899
Collections.sort(expected);
99100
CommonTestUtils.waitUtil(
100101
() -> {
101-
List<String> actual = TestValuesTableFactory.getResults(sinkName);
102-
Collections.sort(actual);
102+
List<String> actual =
103+
TestValuesTableFactory.getResults(sinkName).stream()
104+
.map(KafkaTableTestUtils::rowToString)
105+
.sorted()
106+
.collect(Collectors.toList());
103107
return expected.equals(actual);
104108
},
105109
timeout,
@@ -124,4 +128,12 @@ public static void comparedWithKeyAndOrder(
124128
matching(TableTestMatchers.deepEqualTo(expectedData.get(key), false)));
125129
}
126130
}
131+
132+
private static String rowToString(Object o) {
133+
if (o instanceof Row) {
134+
return ((Row) o).toString();
135+
} else {
136+
return o.toString();
137+
}
138+
}
127139
}

0 commit comments

Comments
 (0)