Skip to content

KAFKA-14299: Fix pause and resume with state updater#13025

Merged
guozhangwang merged 12 commits into
apache:trunkfrom
lucasbru:pause_resume
Feb 21, 2023
Merged

KAFKA-14299: Fix pause and resume with state updater#13025
guozhangwang merged 12 commits into
apache:trunkfrom
lucasbru:pause_resume

Conversation

@lucasbru

@lucasbru lucasbru commented Dec 20, 2022

Copy link
Copy Markdown
Member

Fixes required to make the PauseResumeIntegrationTest pass. It was not
enabled and it does not pass for the state updater code path.

  1. Make sure no progress is made on paused topologies
    The state updater restored one round of polls from the restore
    consumer before realizing that a newly added task was already
    in paused state when being added.

  2. Wake up state updater when tasks are being resumed
    If a task is resumed, it may be necessary to wake up the state
    updater from waiting on the tasksAndActions condition.

  3. Make sure that allTasks methods also return the tasks
    that are currently being restored.

  4. Enable PauseResumeIntegrationTest and upgrade it to
    JUnit5.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@lucasbru

Copy link
Copy Markdown
Member Author

@cadonna Could you have a look?

@cadonna cadonna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, @lucasbru !

Here my feedback!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have this if on line 264 and adding it to the pausedTasks right away instead of calling pauseTask() which does a couple of steps like writing a checkpoint which is not needed at when the task is just added to the state updater?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. But I also had to make more changes to make resuming reliable. I have to wake up the state updater thread when a topology is resumed, to make sure that the state updater does in fact resume the task

This wasn't enabled and didn't pass for state
updater.
The state updater restored one round of polls from the restore
consumer before realizing that a newly added task was already
in paused state when being added.

This is tested by the existing `PauseResumeIntegrationTest`.

@cadonna cadonna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasbru Thanks for the updates!

Here my feedback!

log.info("Stateless active task " + task.id() + " was added to the restored tasks of the state updater");
log.info("Stateless active task " + taskId + " was added to the restored tasks of the state updater");
} else if (topologyMetadata.isPaused(taskId.topologyName())) {
pausedTasks.put(taskId, task);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent, we should also verify here if the task already exists similar to the else-branch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this complexity is necessary, since we do not make strict ordering guarantees for paused topologies -- i.e. it's okay to still processing those tasks for a while after the pause() call is triggered. Is it really a correctness or concurrency issue?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PauseResumeIntegrationTest is testing precisely this (that no progress is made when a task is added in paused state). I also wasn't sure how important this property is for KSQL, but I suppose it is important enough that Jim wrote a unit test for it. I also think it makes sense to have the property, making a little progress on a paused task is unintuitive IMHO.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w.r.t. Bruno's comment: done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While working on another PR I realized in checkAllUpdatingTaskStates we only try to pause tasks when the commit interval has elapsed, i.e. when we pause a named topology, its corresponding tasks may only be paused after a while, while when we resume, tasks are resumed immediately.

So I think we should move the pausing logic out of the checkAllUpdatingTaskStates as well like resuming, which would leave checkAllUpdatingTaskStates to just become maybeCheckpointUpdatingTasks. WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, almost missed this comment. I think this would work, but it would mean cycling through the list of all tasks in every single iteration of main state updater loop. I thought the pause code was only run after commitMs to reduce this overhead, so I thought it makes sense. It's different for resume, because we get a resume signal from the stream thread.

private final Condition restoredActiveTasksCondition = restoredActiveTasksLock.newCondition();
private final BlockingQueue<ExceptionAndTasks> exceptionsAndFailedTasks = new LinkedBlockingQueue<>();
private final BlockingQueue<Task> removedTasks = new LinkedBlockingQueue<>();
private final AtomicBoolean needsResumeCheck = new AtomicBoolean(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I find the name a bit confusing. What do you think about isTopologyResumed or 'topologyResumed'?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +1543 to +1549
if (stateUpdater != null) {
final Map<TaskId, Task> ret = stateUpdater.getTasks().stream().collect(Collectors.toMap(Task::id, x -> x));
ret.putAll(tasks.allTasksPerId());
return ret;
} else {
return tasks.allTasksPerId();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find a test for this change. Could you add one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@lucasbru lucasbru changed the title KAFKA-14299: Make sure no progress is made on paused topologies KAFKA-14299: Fix pause and resume with state updater Jan 31, 2023
@lucasbru

Copy link
Copy Markdown
Member Author

@guozhangwang also wanted to have a look (note that I didn't address Brunos comments yet)

@guozhangwang

Copy link
Copy Markdown
Contributor

ack, will take a look asap.

@Override
public boolean commitNeeded() {
throw new UnsupportedOperationException("This task is read-only");
return task.commitNeeded();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change these two functions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StreamThread.maybeCommit uses TaskManager.allTasks that excluded tasks in the state updater before, but includes tasks in the state updater now. However, commitNeeded should be false for all those tasks right? I was scratching my head here a bit, because StreamThread.maybeCommit was processing RESTORING tasks in the old code path, but not in the state updater code path.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. That makes sense.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gives me a bit of a headache to forward the call to commitNeeded() since it changes the state of a stream task by updating the consumed offsets.
I am also wondering why Streams also commits restoring tasks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the actual culprit is like @lucasbru said, StreamThread.maybeCommit uses TaskManager.allTasks, while the callee TaskManager.allTasks is used in many different places.. For example for IQ, it was used and in that case it should return all tasks, whereas in this caller, TaskManager.allTasks should just return all processing tasks when updater is enabled, i.e. only the ones in TaskRegistry.

I am also wondering why Streams also commits restoring tasks.

For that, the rationale is to allow the restoration progress to be recorded as well in case it was paused due to another rebalance (though it's prepareCommit would always return empty map, so the committing process for those restoring tasks would be reduced to writing checkpoints). But somehow in the middle of history we lost the logic to ever change commitNeeded flag for restoring tasks, so they would always not be triggered. Hence a new JIRA is proposed to fix it, and we fixed it in state updater.

I thought about fixing it in a probably better way, which involves more changes: 1) introduce a TaskManager.allProcessingTasks, 2) depending on the updater enabled flag, let maybeCommit call either this one or allTasks. When I thought about that, I admit I was thinking this change is a bit too much, and since tasks in updater the task should not make any side-effects and return commitNeeded false this maybe just okay.

But thinking about this a bit more, I think it's indeed worrisome to let ReadyOnlyTask.commitNeeded to have any potential side effects if we ever have bugs..

@lucasbru lucasbru Feb 15, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. introduce a TaskManager.allProcessingTasks, 2) depending on the updater enabled flag, let maybeCommit call either this one or allTasks

let's do :this:
I will update the PR tomorrow first thing

log.info("Stateless active task " + task.id() + " was added to the restored tasks of the state updater");
log.info("Stateless active task " + taskId + " was added to the restored tasks of the state updater");
} else if (topologyMetadata.isPaused(taskId.topologyName())) {
pausedTasks.put(taskId, task);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this complexity is necessary, since we do not make strict ordering guarantees for paused topologies -- i.e. it's okay to still processing those tasks for a while after the pause() call is triggered. Is it really a correctness or concurrency issue?

return tasks.allTasksPerId();
if (stateUpdater != null) {
final Map<TaskId, Task> ret = stateUpdater.getTasks().stream().collect(Collectors.toMap(Task::id, x -> x));
ret.putAll(tasks.allTasksPerId());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the func name slightly in another PR, so if that PR is merged we need to do a slight rebase/conflict resolution, just FYI.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, once you merge the other one we can rebase this one.

@guozhangwang

Copy link
Copy Markdown
Contributor

I do not have further comments. @cadonna do you want to take a final look? If not I can go ahead and merge it.

@cadonna cadonna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasbru Thanks for the updates!

I have one major comment in ReadOnlyTask.

@Override
public boolean commitNeeded() {
throw new UnsupportedOperationException("This task is read-only");
return task.commitNeeded();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gives me a bit of a headache to forward the call to commitNeeded() since it changes the state of a stream task by updating the consumed offsets.
I am also wondering why Streams also commits restoring tasks.

@Override
public Map<TopicPartition, Long> changelogOffsets() {
throw new UnsupportedOperationException("This task is read-only");
return task.changelogOffsets();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the code correctly, this forwarding is fine.

@guozhangwang

Copy link
Copy Markdown
Contributor

@lucasbru seems I forgot to send those pending comments from last week, sorry... anyways there's only one comment that I had here: #13025 (comment) and I think it's okay to no address in this PR actually, as we can do it in a different PR (I still need to update the logic a bit while I add more metrics recordings anyways). just FYI.


StreamTask activeTask(final TaskManager taskManager, final TopicPartition partition) {
final Stream<Task> standbys = taskManager.allTasks().values().stream().filter(Task::isActive);
final Stream<Task> standbys = taskManager.allOwnedTasks().values().stream().filter(Task::isActive);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding: why we need to change the callee here as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we don't

add("changelogPartitions");
add("commitRequested");
add("isActive");
add("commitNeeded");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know :)

@guozhangwang

Copy link
Copy Markdown
Contributor

@lucasbru the pause/ resume integration test fails again for J11/S13, could you take a look into it?

@lucasbru

Copy link
Copy Markdown
Member Author

@lucasbru the pause/ resume integration test fails again for J11/S13, could you take a look into it?

Ah yes, that was coming from the rebase. should be fixed now

@guozhangwang
guozhangwang merged commit 0fc029c into apache:trunk Feb 21, 2023
@guozhangwang

Copy link
Copy Markdown
Contributor

LGTM! Merged to trunk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants