-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Kafka Connect: Fix coordinator potentially committing files from prior commit in certain rebalance scenarios #17713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ajreid21
wants to merge
4
commits into
apache:main
Choose a base branch
from
ajreid21:kafka-connect-control-topic-replay-dedup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−15
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
53720bb
Kafka Connect: Ignore replayed control topic records
ajreid21 58dc592
Kafka Connect: Clarify control offset rewind comment
ajreid21 e390f70
Kafka Connect: Apply test formatting
ajreid21 b987b9c
Kafka Connect: Fix replay test checkstyle
ajreid21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...afka-connect/src/test/java/org/apache/iceberg/connect/channel/TestControlTopicReplay.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.iceberg.connect.channel; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DataFiles; | ||
| import org.apache.iceberg.FileFormat; | ||
| import org.apache.iceberg.FileScanTask; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.Snapshot; | ||
| import org.apache.iceberg.connect.IcebergSinkConfig; | ||
| import org.apache.iceberg.connect.events.AvroUtil; | ||
| import org.apache.iceberg.connect.events.DataComplete; | ||
| import org.apache.iceberg.connect.events.DataWritten; | ||
| import org.apache.iceberg.connect.events.Event; | ||
| import org.apache.iceberg.connect.events.StartCommit; | ||
| import org.apache.iceberg.connect.events.TableReference; | ||
| import org.apache.iceberg.connect.events.TopicPartitionOffset; | ||
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.types.Types.StructType; | ||
| import org.apache.kafka.clients.admin.MemberAssignment; | ||
| import org.apache.kafka.clients.admin.MemberDescription; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.connect.sink.SinkTaskContext; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class TestControlTopicReplay extends ChannelTestBase { | ||
|
|
||
| @Test | ||
| void handlesReplayedControlRecordsOnce() { | ||
| TrackingChannel channel = | ||
| new TrackingChannel(config, clientFactory, mock(SinkTaskContext.class)); | ||
| channel.start(); | ||
| initConsumer(); | ||
|
|
||
| addStartCommit(0L); | ||
| addStartCommit(1L); | ||
| channel.process(); | ||
|
|
||
| assertThat(channel.receivedCount()).isEqualTo(2); | ||
| assertThat(channel.nextOffset(0)).isEqualTo(2L); | ||
|
|
||
| consumer.seek(new TopicPartition(CTL_TOPIC_NAME, 0), 0L); | ||
| addStartCommit(0L); | ||
| addStartCommit(1L); | ||
| channel.process(); | ||
|
|
||
| assertThat(channel.receivedCount()).isEqualTo(2); | ||
| assertThat(channel.nextOffset(0)).isEqualTo(2L); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotCommitReplayedDataFilesTwice() throws IOException { | ||
| when(config.commitIntervalMs()).thenReturn(0); | ||
| when(config.commitTimeoutMs()).thenReturn(Integer.MAX_VALUE); | ||
|
|
||
| MemberAssignment assignment = | ||
| new MemberAssignment( | ||
| ImmutableSet.of( | ||
| new TopicPartition(SRC_TOPIC_NAME, 0), | ||
| new TopicPartition(SRC_TOPIC_NAME, 1), | ||
| new TopicPartition(SRC_TOPIC_NAME, 2))); | ||
| MemberDescription member = | ||
| new MemberDescription(null, Optional.empty(), null, null, assignment); | ||
| Coordinator coordinator = | ||
| new Coordinator( | ||
| catalog, config, ImmutableList.of(member), clientFactory, mock(SinkTaskContext.class)); | ||
| coordinator.start(); | ||
| initConsumer(); | ||
| coordinator.process(); | ||
|
|
||
| UUID commitId = | ||
| ((StartCommit) AvroUtil.decode(producer.history().get(0).value()).payload()).commitId(); | ||
| DataFile file1 = dataFile("path/to/file-1.parquet"); | ||
| DataFile file2 = dataFile("path/to/file-2.parquet"); | ||
|
|
||
| addDataWritten(0L, commitId, file1); | ||
| addDataComplete(1L, commitId, 0); | ||
| addDataWritten(2L, commitId, file2); | ||
| addDataComplete(3L, commitId, 1); | ||
| coordinator.process(); | ||
|
|
||
| consumer.seek(new TopicPartition(CTL_TOPIC_NAME, 0), 0L); | ||
| addDataWritten(0L, commitId, file1); | ||
| addDataComplete(1L, commitId, 0); | ||
| addDataWritten(2L, commitId, file2); | ||
| addDataComplete(3L, commitId, 1); | ||
| coordinator.process(); | ||
|
|
||
| addDataComplete(4L, commitId, 2); | ||
| coordinator.process(); | ||
|
|
||
| // Start another cycle and force a partial commit. Before the replay guard, the replayed tail | ||
| // remains buffered and this cycle commits file2 a second time. | ||
| when(config.commitTimeoutMs()).thenReturn(-1); | ||
| coordinator.process(); | ||
|
|
||
| table.refresh(); | ||
| List<Snapshot> snapshots = ImmutableList.copyOf(table.snapshots()); | ||
| assertThat(snapshots).hasSize(1); | ||
| assertThat(snapshots.get(0).summary()).containsEntry(OFFSETS_SNAPSHOT_PROP, "{\"0\":5}"); | ||
|
|
||
| List<String> locations = Lists.newArrayList(); | ||
| try (CloseableIterable<FileScanTask> tasks = table.newScan().planFiles()) { | ||
| tasks.forEach(task -> locations.add(task.file().location())); | ||
| } | ||
|
|
||
| assertThat(locations) | ||
| .containsExactlyInAnyOrder(file1.location().toString(), file2.location().toString()); | ||
| } | ||
|
|
||
| private void addStartCommit(long offset) { | ||
| Event event = new Event(config.connectGroupId(), new StartCommit(UUID.randomUUID())); | ||
| addControlRecord(offset, event); | ||
| } | ||
|
|
||
| private void addDataWritten(long offset, UUID commitId, DataFile file) { | ||
| Event event = | ||
| new Event( | ||
| config.connectGroupId(), | ||
| new DataWritten( | ||
| StructType.of(), | ||
| commitId, | ||
| TableReference.of("catalog", TABLE_IDENTIFIER, table.uuid()), | ||
| ImmutableList.of(file), | ||
| ImmutableList.of())); | ||
| addControlRecord(offset, event); | ||
| } | ||
|
|
||
| private void addDataComplete(long offset, UUID commitId, int sourcePartition) { | ||
| Event event = | ||
| new Event( | ||
| config.connectGroupId(), | ||
| new DataComplete( | ||
| commitId, | ||
| ImmutableList.of( | ||
| new TopicPartitionOffset( | ||
| SRC_TOPIC_NAME, sourcePartition, 1L, EventTestUtil.now())))); | ||
| addControlRecord(offset, event); | ||
| } | ||
|
|
||
| private void addControlRecord(long offset, Event event) { | ||
| consumer.addRecord( | ||
| new ConsumerRecord<>(CTL_TOPIC_NAME, 0, offset, "key", AvroUtil.encode(event))); | ||
| } | ||
|
|
||
| private DataFile dataFile(String location) { | ||
| return DataFiles.builder(PartitionSpec.unpartitioned()) | ||
| .withPath(location) | ||
| .withFormat(FileFormat.PARQUET) | ||
| .withFileSizeInBytes(100L) | ||
| .withRecordCount(1L) | ||
| .build(); | ||
| } | ||
|
|
||
| private static class TrackingChannel extends Channel { | ||
| private int receivedCount = 0; | ||
|
|
||
| private TrackingChannel( | ||
| IcebergSinkConfig config, KafkaClientFactory clientFactory, SinkTaskContext context) { | ||
| super("tracking", "tracking-group", config, clientFactory, context); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean receive(Envelope envelope) { | ||
| receivedCount += 1; | ||
| return true; | ||
| } | ||
|
|
||
| private void process() { | ||
| consumeAvailable(Duration.ZERO); | ||
| } | ||
|
|
||
| private int receivedCount() { | ||
| return receivedCount; | ||
| } | ||
|
|
||
| private Long nextOffset(int partition) { | ||
| return controlTopicOffsets().get(partition); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that there might be some unresolved cross-PR consolidation here. This exact replay-dedup guard is duplicated, byte-equivalent, in the earlier open #17376 and #17450 (same file, same method). Should we pick one vehicle for the fix and reconcile the others to avoid merge conflicts? The focused TestControlTopicReplay regression coverage added here is worth preserving, whichever PR lands. This is just a consolidation decision, not a code defect. Otherwise, looks good - thank you @ajreid21!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created this PR to target this specific case as it is a much simpler case (and fix) to reason about. This PR is not intended to try and fix all the potential coordinator replay and zombie issues -- these larger issues require much more discussion and larger PRs (like the 2 referenced) that may take many review cycles to get in.
I think it still makes sense to get this (ultimately, ~5 line code addition) in in the meantime to help prevent this particular issue from happening.