KAFKA-14299: Fix pause and resume with state updater#13025
Conversation
|
@cadonna Could you have a look? |
bf4b07e to
89b95be
Compare
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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`.
| 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); |
There was a problem hiding this comment.
To be consistent, we should also verify here if the task already exists similar to the else-branch.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
w.r.t. Bruno's comment: done
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
nit: I find the name a bit confusing. What do you think about isTopologyResumed or 'topologyResumed'?
| 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(); | ||
| } |
There was a problem hiding this comment.
I could not find a test for this change. Could you add one?
|
@guozhangwang also wanted to have a look (note that I didn't address Brunos comments yet) |
|
ack, will take a look asap. |
| @Override | ||
| public boolean commitNeeded() { | ||
| throw new UnsupportedOperationException("This task is read-only"); | ||
| return task.commitNeeded(); |
There was a problem hiding this comment.
Why do we need to change these two functions?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ack. That makes sense.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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..
There was a problem hiding this comment.
- 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); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes, once you merge the other one we can rebase this one.
|
I do not have further comments. @cadonna do you want to take a final look? If not I can go ahead and merge it. |
| @Override | ||
| public boolean commitNeeded() { | ||
| throw new UnsupportedOperationException("This task is read-only"); | ||
| return task.commitNeeded(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
If I understand the code correctly, this forwarding is fine.
|
@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); |
There was a problem hiding this comment.
For my understanding: why we need to change the callee here as well?
| add("changelogPartitions"); | ||
| add("commitRequested"); | ||
| add("isActive"); | ||
| add("commitNeeded"); |
|
@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 |
|
LGTM! Merged to trunk. |
Fixes required to make the
PauseResumeIntegrationTestpass. It was notenabled and it does not pass for the state updater code path.
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.
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
tasksAndActionscondition.Make sure that
allTasksmethods also return the tasksthat are currently being restored.
Enable
PauseResumeIntegrationTestand upgrade it toJUnit5.
Committer Checklist (excluded from commit message)