lucasbru commented on code in PR #23991:
URL: https://github.com/apache/kafka/pull/23991#discussion_r4562259974
##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/StoreChangelogReaderTest.java:
##########
@@ -388,7 +388,10 @@ public void
shouldInitializeChangelogAndCheckForCompletion(final Task.TaskType t
setupStore();
@SuppressWarnings("unchecked")
final Map<TaskId, Task> mockTasks = mock(Map.class);
- when(mockTasks.get(null)).thenReturn(mock(Task.class));
+ // the reader starts out restoring actives, so it never restores the
standby changelog
Review Comment:
Thanks for filing KAFKA-21488.
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StoreChangelogReader.java:
##########
@@ -583,50 +583,33 @@ private ConsumerRecords<byte[], byte[]>
pollRecordsFromRestoreConsumer(final Map
return polledRecords;
}
- private void pauseResumePartitions(final Map<TaskId, Task> tasks,
- final Set<TopicPartition>
restoringChangelogs) {
- if (state == ChangelogReaderState.ACTIVE_RESTORING) {
- updatePartitionsByType(tasks, restoringChangelogs,
TaskType.ACTIVE);
- } else {
- updatePartitionsByType(tasks, restoringChangelogs,
TaskType.STANDBY);
- // Paused active tasks keep their changelogs registered and
restoring, so pause them explicitly while
- // updating standbys.
- final Set<TopicPartition> activeRestoringChangelogs =
activeRestoringChangelogs();
- if (!activeRestoringChangelogs.isEmpty()) {
- restoreConsumer.pause(activeRestoringChangelogs);
- }
- }
- }
-
- private void updatePartitionsByType(final Map<TaskId, Task> tasks,
- final Set<TopicPartition>
restoringChangelogs,
- final TaskType taskType) {
- final Collection<TopicPartition> toResume =
- restoringChangelogs.stream().filter(t -> shouldResume(tasks, t,
taskType)).collect(Collectors.toList());
- final Collection<TopicPartition> toPause =
- restoringChangelogs.stream().filter(t -> shouldPause(tasks, t,
taskType)).collect(Collectors.toList());
- restoreConsumer.resume(toResume);
+ private void pauseResumePartitions(
+ final Set<TopicPartition> restoringChangelogs,
+ final Set<TopicPartition> fetchableChangelogs
+ ) {
+ final Set<TopicPartition> toPause = new HashSet<>(restoringChangelogs);
+ toPause.removeAll(fetchableChangelogs);
+ restoreConsumer.resume(fetchableChangelogs);
restoreConsumer.pause(toPause);
}
- private boolean shouldResume(final Map<TaskId, Task> tasks, final
TopicPartition partition, final TaskType taskType) {
- final ProcessorStateManager manager =
changelogs.get(partition).stateManager;
- final TaskId taskId = manager.taskId();
- final Task task = tasks.get(taskId);
- if (manager.taskType() == taskType) {
- return task != null;
- }
- return false;
- }
-
- private boolean shouldPause(final Map<TaskId, Task> tasks, final
TopicPartition partition, final TaskType taskType) {
- final ProcessorStateManager manager =
changelogs.get(partition).stateManager;
- final TaskId taskId = manager.taskId();
- final Task task = tasks.get(taskId);
- if (manager.taskType() == taskType) {
- return task == null;
- }
- return false;
+ /**
+ * The restoring changelogs the restore consumer fetches from in this
state: those of active tasks while
+ * {@link ChangelogReaderState#ACTIVE_RESTORING}, those of standby tasks
while
+ * {@link ChangelogReaderState#STANDBY_UPDATING}, and in both cases only
for tasks in {@code tasks}, which
+ * excludes paused tasks.
+ */
+ private Set<TopicPartition> fetchableChangelogs(
Review Comment:
Could the PR description mention this part too? Standby changelogs are now
explicitly paused and skipped while actives restore, which is a behavior change
of its own, and the description currently only covers the end offset. It'll end
up as the commit message.
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StoreChangelogReader.java:
##########
@@ -479,17 +479,17 @@ public Set<TopicPartition> completedChangelogs() {
.collect(Collectors.toSet());
}
- // Report a conservative value (higher than the real value) as fallback.
- // Only "source topic optimized" changelogs have a logical end offset from
`ChangelogMetadata` (the committed offset on the source topic).
- // If not known (not set yet, or no "source topic optimization"), fall
back to physical end offset.
+ // The end offset of each changelog as the next offset to fetch: the
restore end offset if known (for an active task,
Review Comment:
Thanks! Small things: could the javadoc say it's the next offset to fetch,
e.g. "the end offset of the partition (one past its last offset)"? Now that
`StateStoreMetadata.endOffset` is documented as the last offset, the same name
meaning two different things is easy to trip over. Also, for an active task on
a source-topic changelog it's the min of the end offset and the committed
offset (`initializeChangelogs`), not just the committed offset.
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StoreChangelogReader.java:
##########
@@ -800,7 +801,11 @@ private void maybeAdvanceStandbyRestoredOffset(final
ProcessorStateManager state
// the next offset to fetch, not last-applied
final OptionalLong lag = restoreConsumer.currentLag(partition);
if (lag.isPresent() && lag.getAsLong() == 0L) {
- stateManager.advanceRestoredOffsetTo(storeMetadata,
restoreConsumer.position(partition, Duration.ZERO));
+ final long position = restoreConsumer.position(partition,
Duration.ZERO);
+ stateManager.advanceRestoredOffsetTo(storeMetadata,
position);
+ // the end offset is otherwise only learned from a
restored batch, which never comes when
+ // there is nothing to restore; like there, it is the last
offset, -1 for an empty changelog
+ storeMetadata.setEndOffset(position - 1L);
Review Comment:
I don't think it's that rare, though: with this PR a standby at the end of
the changelog sets `endOffset = position - 1` on every loop, so any standby or
warm-up that is caught up when we switch to restoring actives freezes at
exactly lag 0. That's the hot case, which is the one we'd act on.
+1 on the follow-up for standbys. Refreshing the end offset of the paused
changelogs when we report would also take care of the warm-up case without the
downside you mentioned: a warm-up that's still within `acceptable.recovery.lag`
would still get promoted, only one that actually fell behind wouldn't. Want to
file a ticket for it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]