Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,7 @@ public void resume() {
} else {
topologyMetadata.resumeTopology(UNNAMED_TOPOLOGY);
}
threads.forEach(StreamThread::signalResume);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void run() {

private void runOnce() throws InterruptedException {
performActionsOnTasks();
resumeTasks();
restoreTasks();
checkAllUpdatingTaskStates(time.milliseconds());
waitIfAllChangelogsCompletelyRead();
Expand All @@ -140,6 +141,16 @@ private void performActionsOnTasks() {
}
}

private void resumeTasks() {
if (isTopologyResumed.compareAndSet(true, false)) {
for (final Task task : pausedTasks.values()) {
if (!topologyMetadata.isPaused(task.id().topologyName())) {
resumeTask(task);
}
}
}
}

private void restoreTasks() {
try {
changelogReader.restore(updatingTasks);
Expand Down Expand Up @@ -229,7 +240,7 @@ private void waitIfAllChangelogsCompletelyRead() throws InterruptedException {
if (isRunning.get() && changelogReader.allChangelogsCompleted()) {
tasksAndActionsLock.lock();
try {
while (tasksAndActions.isEmpty()) {
while (tasksAndActions.isEmpty() && !isTopologyResumed.get()) {
tasksAndActionsCondition.await();
}
} finally {
Expand Down Expand Up @@ -258,21 +269,39 @@ private List<TaskAndAction> getTasksAndActions() {
}

private void addTask(final Task task) {
final TaskId taskId = task.id();

Task existingTask = pausedTasks.get(taskId);
if (existingTask != null) {
throw new IllegalStateException(
(existingTask.isActive() ? "Active" : "Standby") + " task " + taskId + " already exist in paused tasks, " +
"should not try to add another " + (task.isActive() ? "active" : "standby") + " task with the same id. "
+ BUG_ERROR_MESSAGE);
}
existingTask = updatingTasks.get(taskId);
if (existingTask != null) {
throw new IllegalStateException(
(existingTask.isActive() ? "Active" : "Standby") + " task " + taskId + " already exist in updating tasks, " +
"should not try to add another " + (task.isActive() ? "active" : "standby") + " task with the same id. "
+ BUG_ERROR_MESSAGE);
}

if (isStateless(task)) {
addToRestoredTasks((StreamTask) task);
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.

changelogReader.register(task.changelogPartitions(), task.stateManager());
log.debug((task.isActive() ? "Active" : "Standby")
+ " task " + taskId + " was directly added to the paused tasks.");
} else {
final Task existingTask = updatingTasks.putIfAbsent(task.id(), task);
if (existingTask != null) {
throw new IllegalStateException((existingTask.isActive() ? "Active" : "Standby") + " task " + task.id() + " already exist, " +
"should not try to add another " + (task.isActive() ? "active" : "standby") + " task with the same id. " + BUG_ERROR_MESSAGE);
}
updatingTasks.put(taskId, task);
changelogReader.register(task.changelogPartitions(), task.stateManager());
if (task.isActive()) {
log.info("Stateful active task " + task.id() + " was added to the state updater");
log.info("Stateful active task " + taskId + " was added to the state updater");
changelogReader.enforceRestoreActive();
} else {
log.info("Standby task " + task.id() + " was added to the state updater");
log.info("Standby task " + taskId + " was added to the state updater");
if (updatingTasks.size() == 1) {
changelogReader.transitToUpdateStandby();
}
Expand Down Expand Up @@ -388,12 +417,6 @@ private void checkAllUpdatingTaskStates(final long now) {
}
}

for (final Task task : pausedTasks.values()) {
if (!topologyMetadata.isPaused(task.id().topologyName())) {
resumeTask(task);
}
}

lastCommitMs = now;
}
}
Expand All @@ -411,6 +434,7 @@ private void checkAllUpdatingTaskStates(final long now) {
private final Condition restoredActiveTasksCondition = restoredActiveTasksLock.newCondition();
private final BlockingQueue<ExceptionAndTasks> exceptionsAndFailedTasks = new LinkedBlockingQueue<>();
private final BlockingQueue<Task> removedTasks = new LinkedBlockingQueue<>();
private final AtomicBoolean isTopologyResumed = new AtomicBoolean(false);

private final long commitIntervalMs;
private long lastCommitMs;
Expand Down Expand Up @@ -506,6 +530,17 @@ public void remove(final TaskId taskId) {
}
}

@Override
public void signalResume() {
tasksAndActionsLock.lock();
try {
isTopologyResumed.set(true);
tasksAndActionsCondition.signalAll();
} finally {
tasksAndActionsLock.unlock();
}
}

@Override
public Set<StreamTask> drainRestoredActiveTasks(final Duration timeout) {
final long timeoutMs = timeout.toMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public StateStore getStore(final String name) {

@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.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public int hashCode() {
*/
void remove(final TaskId taskId);

/**
* Wakes up the state updater if it is currently dormant, to check if a paused task should be resumed.
*/
void signalResume();

/**
* Drains the restored active tasks from the state updater.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,11 @@ public boolean isThreadAlive() {
return isAlive();
}

// Call method when a topology is resumed
public void signalResume() {
taskManager.signalResume();
}

/**
* Try to commit all active tasks owned by this thread.
*
Expand All @@ -1113,7 +1118,7 @@ int maybeCommit() {
}

committed = taskManager.commit(
taskManager.allTasks()
taskManager.allOwnedTasks()
.values()
.stream()
.filter(t -> t.state() == Task.State.RUNNING || t.state() == Task.State.RESTORING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,12 @@ private void removeLostActiveTasksFromStateUpdater() {
}
}

public void signalResume() {
if (stateUpdater != null) {
stateUpdater.signalResume();
}
}

/**
* Compute the offset total summed across all stores in a task. Includes offset sum for any tasks we own the
* lock for, which includes assigned and unassigned tasks we locked in {@link #tryToLockAllNonEmptyTaskDirectories()}.
Expand Down Expand Up @@ -1532,15 +1538,38 @@ Set<TaskId> standbyTaskIds() {
}

Map<TaskId, Task> allTasks() {
// not bothering with an unmodifiable map, since the tasks themselves are mutable, but
// if any outside code modifies the map or the tasks, it would be a severe transgression.
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.

return ret;
} else {
return tasks.allTasksPerId();
}
Comment on lines +1543 to +1549

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

}

/**
* Returns tasks owned by the stream thread. With state updater disabled, these are all tasks. With
* state updater enabled, this does not return any tasks currently owned by the state updater.
* @return
*/
Map<TaskId, Task> allOwnedTasks() {
// not bothering with an unmodifiable map, since the tasks themselves are mutable, but
// if any outside code modifies the map or the tasks, it would be a severe transgression.
return tasks.allTasksPerId();
}

Set<Task> readOnlyAllTasks() {
// need to make sure the returned set is unmodifiable as it could be accessed
// by other thread than the StreamThread owning this task manager;
return Collections.unmodifiableSet(tasks.allTasks());
// not bothering with an unmodifiable map, since the tasks themselves are mutable, but
// if any outside code modifies the map or the tasks, it would be a severe transgression.
if (stateUpdater != null) {
final HashSet<Task> ret = new HashSet<>(stateUpdater.getTasks());
ret.addAll(tasks.allTasks());
return Collections.unmodifiableSet(ret);
} else {
return Collections.unmodifiableSet(tasks.allTasks());
}
}

Map<TaskId, Task> notPausedTasks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.kafka.streams.processor.TaskId;
import org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier;
import org.apache.kafka.streams.processor.internals.InternalTopologyBuilder;
import org.apache.kafka.streams.processor.internals.StreamThread;
import org.apache.kafka.streams.processor.internals.Task;
import org.apache.kafka.streams.processor.internals.TopologyMetadata;
import org.slf4j.Logger;
Expand Down Expand Up @@ -276,6 +277,7 @@ public boolean isNamedTopologyPaused(final String topologyName) {
*/
public void resumeNamedTopology(final String topologyName) {
topologyMetadata.resumeTopology(topologyName);
threads.forEach(StreamThread::signalResume);
}

/**
Expand Down
Loading