-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[KAFKA-6730] Simplify State Store Recovery #5013
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9b41afa
[KAFKA-6730] Simplify State Store Recovery
a129b4e
Making change
4883a43
ensuring updatedEndOffsets is called once
804a850
Fixing post initialization
3963257
Merge branch 'trunk' into KAFKA-6730
ConcurrencyPractitioner 78d81c3
Adding removal of partitions
8207dbe
Removing conditional
303fbab
Patching
4a33d9c
Strengthening conditional
dcb5f0c
Reverting previous unforeseen error
e742d31
Moving partition addition
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ | |
| import org.apache.kafka.common.utils.LogContext; | ||
| import org.apache.kafka.streams.KeyValue; | ||
| import org.apache.kafka.streams.errors.StreamsException; | ||
| import org.apache.kafka.streams.errors.TaskMigratedException; | ||
| import org.apache.kafka.streams.processor.StateRestoreListener; | ||
| import org.slf4j.Logger; | ||
|
|
||
|
|
@@ -50,6 +49,7 @@ public class StoreChangelogReader implements ChangelogReader { | |
| private final Map<TopicPartition, StateRestorer> stateRestorers = new HashMap<>(); | ||
| private final Map<TopicPartition, StateRestorer> needsRestoring = new HashMap<>(); | ||
| private final Map<TopicPartition, StateRestorer> needsInitializing = new HashMap<>(); | ||
| private Map<TopicPartition, Long> updatedEndOffsets = new HashMap<>(); | ||
|
|
||
| public StoreChangelogReader(final Consumer<byte[], byte[]> restoreConsumer, | ||
| final StateRestoreListener userStateRestoreListener, | ||
|
|
@@ -66,11 +66,11 @@ public void register(final StateRestorer restorer) { | |
| needsInitializing.put(restorer.partition(), restorer); | ||
| } | ||
|
|
||
| /** | ||
| * @throws TaskMigratedException if another thread wrote to the changelog topic that is currently restored | ||
| */ | ||
| public Collection<TopicPartition> restore(final RestoringTasks active) { | ||
| if (!needsInitializing.isEmpty()) { | ||
| final Set<TopicPartition> remainingPartitions = new HashSet<>(needsRestoring.keySet()); | ||
| remainingPartitions.removeAll(updatedEndOffsets.keySet()); | ||
| updatedEndOffsets.putAll(restoreConsumer.endOffsets(remainingPartitions)); | ||
| initialize(); | ||
| } | ||
|
|
||
|
|
@@ -79,11 +79,23 @@ public Collection<TopicPartition> restore(final RestoringTasks active) { | |
| return completed(); | ||
| } | ||
|
|
||
| final Set<TopicPartition> restoringPartitions = new HashSet<>(needsRestoring.keySet()); | ||
| try { | ||
| final ConsumerRecords<byte[], byte[]> allRecords = restoreConsumer.poll(10); | ||
| for (final TopicPartition partition : restoringPartitions) { | ||
| restorePartition(allRecords, partition, active.restoringTaskFor(partition)); | ||
| final ConsumerRecords<byte[], byte[]> records = restoreConsumer.poll(10); | ||
| final Iterator<TopicPartition> iterator = needsRestoring.keySet().iterator(); | ||
| final HashSet<TopicPartition> completedPartitions = new HashSet<>(); | ||
| while (iterator.hasNext()) { | ||
| final TopicPartition partition = iterator.next(); | ||
| final StateRestorer restorer = stateRestorers.get(partition); | ||
| final long pos = processNext(records.records(partition), restorer, updatedEndOffsets.get(partition)); | ||
| restorer.setRestoredOffset(pos); | ||
| if (restorer.hasCompleted(pos, updatedEndOffsets.get(partition))) { | ||
| restorer.restoreDone(); | ||
| updatedEndOffsets.remove(partition); | ||
| completedPartitions.add(partition); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more thing -- missed this before: can't we remove Maybe we need to change the iterator to iterate over the |
||
| } | ||
| } | ||
| for (TopicPartition partition : completedPartitions) { | ||
| needsRestoring.remove(partition); | ||
| } | ||
| } catch (final InvalidOffsetException recoverableException) { | ||
| log.warn("Restoring StreamTasks failed. Deleting StreamTasks stores to recreate from scratch.", recoverableException); | ||
|
|
@@ -240,41 +252,6 @@ public void reset() { | |
| needsInitializing.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * @throws TaskMigratedException if another thread wrote to the changelog topic that is currently restored | ||
| */ | ||
| private void restorePartition(final ConsumerRecords<byte[], byte[]> allRecords, | ||
| final TopicPartition topicPartition, | ||
| final Task task) { | ||
| final StateRestorer restorer = stateRestorers.get(topicPartition); | ||
| final Long endOffset = endOffsets.get(topicPartition); | ||
| final long pos = processNext(allRecords.records(topicPartition), restorer, endOffset); | ||
| restorer.setRestoredOffset(pos); | ||
| if (restorer.hasCompleted(pos, endOffset)) { | ||
| if (pos > endOffset) { | ||
| throw new TaskMigratedException(task, topicPartition, endOffset, pos); | ||
| } | ||
|
|
||
| // need to check for changelog topic | ||
| if (restorer.offsetLimit() == Long.MAX_VALUE) { | ||
| final Long updatedEndOffset = restoreConsumer.endOffsets(Collections.singletonList(topicPartition)).get(topicPartition); | ||
| if (!restorer.hasCompleted(pos, updatedEndOffset)) { | ||
| throw new TaskMigratedException(task, topicPartition, updatedEndOffset, pos); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| log.debug("Completed restoring state from changelog {} with {} records ranging from offset {} to {}", | ||
| topicPartition, | ||
| restorer.restoredNumRecords(), | ||
| restorer.startingOffset(), | ||
| restorer.restoredOffset()); | ||
|
|
||
| restorer.restoreDone(); | ||
| needsRestoring.remove(topicPartition); | ||
| } | ||
| } | ||
|
|
||
| private long processNext(final List<ConsumerRecord<byte[], byte[]>> records, | ||
| final StateRestorer restorer, | ||
| final Long endOffset) { | ||
|
|
@@ -326,7 +303,6 @@ private boolean hasPartition(final TopicPartition topicPartition) { | |
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
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
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
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.
we can also remove the entry in
updatedEndOffsetsif this is true.