-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8755: Fix state restore for standby tasks with optimized topology #7238
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
Changes from 2 commits
e468587
3d92400
1f8a06b
390606c
7a18866
0b14fbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,26 +171,6 @@ public String toString(final String indent) { | |
| return sb.toString(); | ||
| } | ||
|
|
||
| protected void updateOffsetLimits() { | ||
| for (final TopicPartition partition : partitions) { | ||
| try { | ||
| final OffsetAndMetadata metadata = consumer.committed(partition); // TODO: batch API? | ||
| final long offset = metadata != null ? metadata.offset() : 0L; | ||
| stateMgr.putOffsetLimit(partition, offset); | ||
|
|
||
| if (log.isTraceEnabled()) { | ||
| log.trace("Updating store offset limits {} for changelog {}", offset, partition); | ||
| } | ||
| } catch (final AuthorizationException e) { | ||
| throw new ProcessorStateException(String.format("task [%s] AuthorizationException when initializing offsets for %s", id, partition), e); | ||
| } catch (final WakeupException e) { | ||
| throw e; | ||
| } catch (final KafkaException e) { | ||
| throw new ProcessorStateException(String.format("task [%s] Failed to initialize offsets for %s", id, partition), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Flush all state stores owned by this task | ||
| */ | ||
|
|
@@ -219,9 +199,6 @@ void registerStateStores() { | |
| } | ||
| log.trace("Initializing state stores"); | ||
|
|
||
| // set initial offset limits | ||
| updateOffsetLimits(); | ||
|
|
||
| for (final StateStore store : topology.stateStores()) { | ||
| log.trace("Initializing store {}", store.name()); | ||
| processorContext.uninitialize(); | ||
|
|
@@ -272,4 +249,18 @@ public boolean hasStateStores() { | |
| public Collection<TopicPartition> changelogPartitions() { | ||
| return stateMgr.changelogPartitions(); | ||
| } | ||
|
|
||
| protected long getConsumerCommittedOffset(final TopicPartition partition) { | ||
| try { | ||
| final OffsetAndMetadata metadata = consumer.committed(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. @guozhangwang With regard to you comment about exceptions for EOS -- it seem only relevant to catch and swallow a
Contributor
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. I think TimeoutException is the one, yes, but more importantly we should see if our timeout values are set reasonably in Streams :) |
||
| return metadata != null ? metadata.offset() : 0L; | ||
| } catch (final AuthorizationException e) { | ||
| throw new ProcessorStateException(String.format("task [%s] AuthorizationException when initializing offsets for %s", id, partition), e); | ||
| } catch (final WakeupException e) { | ||
| throw e; | ||
| } catch (final KafkaException e) { | ||
| throw new ProcessorStateException(String.format("task [%s] Failed to initialize offsets for %s", id, partition), e); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,4 +24,14 @@ class AssignedStandbyTasks extends AssignedTasks<StandbyTask> { | |
| super(logContext, "standby task"); | ||
| } | ||
|
|
||
| @Override | ||
| int commit() { | ||
| final int committed = super.commit(); | ||
| // TODO: this contortion would not be necessary if we got rid of the two-step | ||
| // task.commitNeeded and task.commit and instead just had task.commitIfNeeded. Currently | ||
| // we only call commit if commitNeeded is true, which means that we need a way to indicate | ||
| // that we are eligible for updating the offset limit outside of commit. | ||
| running.forEach((id, task) -> task.allowUpdateOfOffsetLimit()); | ||
|
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. Seems we should add
Contributor
Author
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. We could add a test here, but it is super implementation specific. This is not really the best home for the code, just the most convenient at the moment. All we would test, I think, is that we called allowUpdateOfOffsetLimit? The integration test covers this in a more comprehensive way and doesn't break when we move this code. What do you think?
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.
Yes, the test should be
"and doesn't break" -- I guess it should break? The main purpose of the unit test is, that is would be hard to debug the integration test and figure out the root cause why it fails.
Contributor
Author
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. I added back the StandbyTask tests that will cover this with a much more narrow test - specifically to cover when to update the offset limit. If this gets moved into StandbyTask that test should not fail, provided it was done right. If it's broken in any way that test will flag it and it should be easy to look at the test failure and the changed code to determine the cause. Do you think StandbyTaskTest sufficiently addresses your concern? |
||
| return committed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,14 @@ | |
| */ | ||
| package org.apache.kafka.streams.processor.internals; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.apache.kafka.clients.consumer.Consumer; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.common.TopicPartition; | ||
|
|
@@ -25,20 +33,14 @@ | |
| import org.apache.kafka.streams.processor.TaskId; | ||
| import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A StandbyTask | ||
| */ | ||
| public class StandbyTask extends AbstractTask { | ||
|
cpettitt-confluent marked this conversation as resolved.
|
||
|
|
||
| private Map<TopicPartition, Long> checkpointedOffsets = new HashMap<>(); | ||
| private final Sensor closeTaskSensor; | ||
| private final Map<TopicPartition, Long> offsetLimits = new HashMap<>(); | ||
| private final Set<TopicPartition> updateableOffsetLimits = new HashSet<>(); | ||
|
|
||
| /** | ||
| * Create {@link StandbyTask} with its assigned partitions | ||
|
|
@@ -63,6 +65,14 @@ public class StandbyTask extends AbstractTask { | |
|
|
||
| closeTaskSensor = metrics.threadLevelSensor("task-closed", Sensor.RecordingLevel.INFO); | ||
| processorContext = new StandbyContextImpl(id, config, stateMgr, metrics); | ||
|
|
||
| final Set<String> changelogTopicNames = new HashSet<>(topology.storeToChangelogTopic().values()); | ||
| partitions.stream() | ||
| .filter(tp -> changelogTopicNames.contains(tp.topic())) | ||
| .forEach(tp -> { | ||
| offsetLimits.put(tp, 0L); | ||
| updateableOffsetLimits.add(tp); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -88,7 +98,7 @@ public void initializeTopology() { | |
| @Override | ||
| public void resume() { | ||
| log.debug("Resuming"); | ||
| updateOffsetLimits(); | ||
| allowUpdateOfOffsetLimit(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -102,9 +112,7 @@ public void resume() { | |
| public void commit() { | ||
| log.trace("Committing"); | ||
| flushAndCheckpointState(); | ||
| // reinitialize offset limits | ||
| updateOffsetLimits(); | ||
|
|
||
| allowUpdateOfOffsetLimit(); | ||
| commitNeeded = false; | ||
| } | ||
|
|
||
|
|
@@ -166,12 +174,22 @@ public void closeSuspended(final boolean clean, | |
| public List<ConsumerRecord<byte[], byte[]>> update(final TopicPartition partition, | ||
| final List<ConsumerRecord<byte[], byte[]>> records) { | ||
| log.trace("Updating standby replicas of its state store for partition [{}]", partition); | ||
| final long limit = stateMgr.offsetLimit(partition); | ||
| long limit = offsetLimits.getOrDefault(partition, Long.MAX_VALUE); | ||
|
cpettitt-confluent marked this conversation as resolved.
|
||
|
|
||
| long lastOffset = -1L; | ||
| final List<ConsumerRecord<byte[], byte[]>> restoreRecords = new ArrayList<>(records.size()); | ||
| final List<ConsumerRecord<byte[], byte[]>> remainingRecords = new ArrayList<>(); | ||
|
|
||
| // Check if we are unable to process one or more records due to an offset limit (e.g. when | ||
|
cpettitt-confluent marked this conversation as resolved.
Outdated
|
||
| // our partition is both a source and changelog). If we are limited then try to refresh | ||
| // the offset limit if possible. | ||
| if (!records.isEmpty() && | ||
|
Contributor
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. I think we only need to refresh offset limit if Also since this should only happen when
Contributor
Author
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.
@guozhangwang Can do. This says something a little different than the current code. This says if there were any records that could not be played then refresh the consumer committed offset. The current code says refresh the consumer committed offset only if we could not play any of the records. In other words, we may refresh more frequently. Sound good?
Makes sense, I will treat this case as a bug and throw.
Contributor
Author
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. I ended up not using
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. To simplify this, should we just check if I also think, this code may still do two passed, as we have no guarantee that |
||
| records.get(records.size() - 1).offset() >= limit && | ||
| updateableOffsetLimits.remove(partition)) { | ||
|
cpettitt-confluent marked this conversation as resolved.
Outdated
|
||
|
|
||
| limit = updateOffsetLimits(partition); | ||
| } | ||
|
|
||
| for (final ConsumerRecord<byte[], byte[]> record : records) { | ||
| if (record.offset() < limit) { | ||
| restoreRecords.add(record); | ||
|
|
@@ -181,9 +199,8 @@ public List<ConsumerRecord<byte[], byte[]>> update(final TopicPartition partitio | |
| } | ||
| } | ||
|
|
||
| stateMgr.updateStandbyStates(partition, restoreRecords, lastOffset); | ||
|
|
||
| if (!restoreRecords.isEmpty()) { | ||
| stateMgr.updateStandbyStates(partition, restoreRecords, lastOffset); | ||
|
cpettitt-confluent marked this conversation as resolved.
|
||
| commitNeeded = true; | ||
| } | ||
|
|
||
|
|
@@ -194,4 +211,21 @@ Map<TopicPartition, Long> checkpointedOffsets() { | |
| return checkpointedOffsets; | ||
| } | ||
|
|
||
| private long updateOffsetLimits(final TopicPartition partition) { | ||
| if (!offsetLimits.containsKey(partition)) { | ||
| throw new IllegalArgumentException("Topic is not both a source and a changelog: " + partition); | ||
| } | ||
|
|
||
| final long newLimit = getConsumerCommittedOffset(partition); | ||
| final long previousLimit = offsetLimits.put(partition, newLimit); | ||
| if (previousLimit > newLimit) { | ||
| throw new IllegalStateException("Offset limit should monotonically increase, but was reduced. " + | ||
| "New limit: " + newLimit + ". Previous limit: " + previousLimit); | ||
| } | ||
| return newLimit; | ||
| } | ||
|
|
||
| void allowUpdateOfOffsetLimit() { | ||
| updateableOffsetLimits.addAll(offsetLimits.keySet()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,39 +141,44 @@ private void initialize(final RestoringTasks active) { | |
|
|
||
| // try to fetch end offsets for the initializable restorers and remove any partitions | ||
| // where we already have all of the data | ||
| final Map<TopicPartition, Long> queriedEndOffsets; | ||
| try { | ||
| endOffsets.putAll(restoreConsumer.endOffsets(initializable)); | ||
| queriedEndOffsets = restoreConsumer.endOffsets(initializable); | ||
| } catch (final TimeoutException e) { | ||
| // if timeout exception gets thrown we just give up this time and retry in the next run loop | ||
| log.debug("Could not fetch end offset for {}; will fall back to partition by partition fetching", initializable); | ||
| return; | ||
| } | ||
|
|
||
| queriedEndOffsets.forEach((tp, endOffset) -> { | ||
| if (endOffset != null) { | ||
| final StateRestorer restorer = stateRestorers.get(tp); | ||
| final long offsetLimit = restorer.offsetLimit(); | ||
| endOffsets.put(tp, Math.min(endOffset, offsetLimit)); | ||
|
cpettitt-confluent marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| log.info("End offset cannot be found form the returned metadata; removing this partition from the current run loop"); | ||
| initializable.remove(tp); | ||
| } | ||
| }); | ||
|
|
||
| final Iterator<TopicPartition> iter = initializable.iterator(); | ||
| while (iter.hasNext()) { | ||
| final TopicPartition topicPartition = iter.next(); | ||
| final Long endOffset = endOffsets.get(topicPartition); | ||
|
|
||
| // offset should not be null; but since the consumer API does not guarantee it | ||
| // we add this check just in case | ||
| if (endOffset != null) { | ||
| final StateRestorer restorer = stateRestorers.get(topicPartition); | ||
| if (restorer.checkpoint() >= endOffset) { | ||
| restorer.setRestoredOffset(restorer.checkpoint()); | ||
| iter.remove(); | ||
| completedRestorers.add(topicPartition); | ||
| } else if (restorer.offsetLimit() == 0 || endOffset == 0) { | ||
| restorer.setRestoredOffset(0); | ||
| iter.remove(); | ||
| completedRestorers.add(topicPartition); | ||
| } else { | ||
| restorer.setEndingOffset(endOffset); | ||
| } | ||
| needsInitializing.remove(topicPartition); | ||
| } else { | ||
| log.info("End offset cannot be found form the returned metadata; removing this partition from the current run loop"); | ||
| final StateRestorer restorer = stateRestorers.get(topicPartition); | ||
| if (restorer.checkpoint() >= endOffset) { | ||
|
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. Thinking about this, the checkpoint should never be larger than the
Contributor
Author
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. Totally agree from a correctness perspective. Do you think we have enough test coverage to ensure this change is safe?
Contributor
Author
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. Looking at this one a bit more, I think it might be a good idea to defer this change. It's pretty orthogonal to the changes I'm doing (this code was just moved in this patch). I can create a ticket if you like?
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. Maybe. Would be good to get input from @guozhangwang about it -- maybe |
||
| restorer.setRestoredOffset(restorer.checkpoint()); | ||
| iter.remove(); | ||
| completedRestorers.add(topicPartition); | ||
| } else if (restorer.offsetLimit() == 0 || endOffset == 0) { | ||
|
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. This conditions seems to be redundant as we compute Actually, I am wondering if we can unify the first and second branch:
Contributor
Author
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. Sounds great! |
||
| restorer.setRestoredOffset(0); | ||
| iter.remove(); | ||
| completedRestorers.add(topicPartition); | ||
| } else { | ||
| restorer.setEndingOffset(endOffset); | ||
| } | ||
| needsInitializing.remove(topicPartition); | ||
| } | ||
|
|
||
| // set up restorer for those initializable | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,19 @@ | |
| */ | ||
| package org.apache.kafka.streams.processor.internals; | ||
|
|
||
| import static java.lang.String.format; | ||
| import static java.util.Collections.singleton; | ||
| import static org.apache.kafka.streams.kstream.internals.metrics.Sensors.recordLatenessSensor; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.apache.kafka.clients.consumer.CommitFailedException; | ||
| import org.apache.kafka.clients.consumer.Consumer; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
|
@@ -48,18 +61,6 @@ | |
| import org.apache.kafka.streams.processor.internals.metrics.ThreadMetrics; | ||
| import org.apache.kafka.streams.state.internals.ThreadCache; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static java.lang.String.format; | ||
| import static java.util.Collections.singleton; | ||
| import static org.apache.kafka.streams.kstream.internals.metrics.Sensors.recordLatenessSensor; | ||
|
|
||
| /** | ||
| * A StreamTask is associated with a {@link PartitionGroup}, and is assigned to a StreamThread for processing. | ||
| */ | ||
|
|
@@ -236,6 +237,22 @@ public StreamTask(final TaskId id, | |
| @Override | ||
| public boolean initializeStateStores() { | ||
| log.trace("Initializing state stores"); | ||
|
|
||
| // Currently there is no easy way to tell the ProcessorStateManager to only restore up to | ||
| // a specific offset. In most cases this is fine. However, in optimized topologies we can | ||
| // have a source topic that also serves as a changelog, and in this case we want our active | ||
| // stream task to only play records up to the last consumer committed offset. Here we find | ||
| // partitions of topics that are both sources and changelogs and set the consumer committed | ||
| // offset via stateMgr as there is not a more direct route. | ||
| final Set<String> changelogTopicNames = new HashSet<>(topology.storeToChangelogTopic().values()); | ||
|
Contributor
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.
Contributor
Author
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. This code should be functionally equivalent to the previous code. Previously we initialized the offset limits in
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. The only exception that is non-fatal should be
Contributor
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. Yeah this is not a comment for this PR but more or less an FYI for folks who are working on KIP-447 :) I also left some thoughts on the voting thread regarding this purpose. |
||
| partitions.stream() | ||
| .filter(tp -> changelogTopicNames.contains(tp.topic())) | ||
| .forEach(tp -> { | ||
| final long offset = getConsumerCommittedOffset(tp); | ||
| stateMgr.putOffsetLimit(tp, offset); | ||
| log.trace("Updating store offset limits {} for changelog {}", offset, tp); | ||
| }); | ||
|
|
||
| registerStateStores(); | ||
|
|
||
| return changelogPartitions().isEmpty(); | ||
|
|
@@ -460,7 +477,6 @@ void commit(final boolean startNewTransaction) { | |
| final TopicPartition partition = entry.getKey(); | ||
| final long offset = entry.getValue() + 1; | ||
| consumedOffsetsAndMetadata.put(partition, new OffsetAndMetadata(offset)); | ||
| stateMgr.putOffsetLimit(partition, offset); | ||
| } | ||
|
|
||
| try { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.