Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -763,7 +763,7 @@ public boolean checkStateUpdater(final long now,
if (stateUpdater.restoresActiveTasks()) {
handleRestoredTasksFromStateUpdater(now, offsetResetter);
}
return !stateUpdater.restoresActiveTasks();
return !stateUpdater.restoresActiveTasks() && !tasks.pendingTasksToRecycleExist();

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 am not familiar with this code. The method name is checkStateUpdater -- but check for what? Semantics are not clear to me. Could we find a better name or add a comment?

@cadonna cadonna Aug 3, 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.

I will consider to rename this method in a different PR since I am thinking about to split this method in three methods -- one for adding tasks to the state updater, one for getting tasks from the state updater, and one for checking whether the stream thread can change state to RUNNING.
That should make the code clearer and also testing might become simpler.

}

private void recycleTaskFromStateUpdater(final Task task,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public void addPendingTaskToRecycle(final TaskId taskId, final Set<TopicPartitio
pendingUpdateActions.put(taskId, PendingUpdateAction.createRecycleTask(inputPartitions));
}

@Override
public boolean pendingTasksToRecycleExist() {

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.

super nit: This name sounds like its should return a list of tasks that are pending to recycle. Maybe add an is at the the front or something?

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 would call is hasPendingTasksToRecycle

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 idea!

return pendingUpdateActions.values().stream().anyMatch(action -> action.getAction() == Action.RECYCLE);
}

@Override
public Set<TopicPartition> removePendingTaskToUpdateInputPartitions(final TaskId taskId) {
if (containsTaskIdWithAction(taskId, Action.UPDATE_INPUT_PARTITIONS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface TasksRegistry {

Set<TopicPartition> removePendingTaskToRecycle(final TaskId taskId);

boolean pendingTasksToRecycleExist();

void addPendingTaskToRecycle(final TaskId taskId, final Set<TopicPartition> inputPartitions);

Set<TopicPartition> removePendingTaskToUpdateInputPartitions(final TaskId taskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ public void shouldSuspendRevokedTaskRemovedFromStateUpdater() {
Mockito.verify(statefulTask).suspend();
Mockito.verify(tasks).addTask(statefulTask);
}

@Test
public void shouldHandleMultipleRemovedTasksFromStateUpdater() {
final StreamTask taskToRecycle0 = statefulTask(taskId00, taskId00ChangelogPartitions)
Expand Down Expand Up @@ -947,6 +948,35 @@ public void shouldHandleMultipleRemovedTasksFromStateUpdater() {
Mockito.verify(stateUpdater).add(taskToUpdateInputPartitions);
}

@Test
public void shouldReturnFalseFromCheckStateUpdaterIfActiveTasksAreRestoring() {
when(stateUpdater.restoresActiveTasks()).thenReturn(true);
final TasksRegistry tasks = mock(TasksRegistry.class);
final TaskManager taskManager = setUpTaskManager(ProcessingMode.AT_LEAST_ONCE, tasks, true);

assertFalse(taskManager.checkStateUpdater(time.milliseconds(), noOpResetter));
}

@Test
public void shouldReturnFalseFromCheckStateUpdaterIfActiveTasksAreNotRestoringButPendingTasksToRecycle() {
when(stateUpdater.restoresActiveTasks()).thenReturn(false);
final TasksRegistry tasks = mock(TasksRegistry.class);
when(tasks.pendingTasksToRecycleExist()).thenReturn(true);
final TaskManager taskManager = setUpTaskManager(ProcessingMode.AT_LEAST_ONCE, tasks, true);

assertFalse(taskManager.checkStateUpdater(time.milliseconds(), noOpResetter));
}

@Test
public void shouldReturnTrueFromCheckStateUpdaterIfActiveTasksAreNotRestoringAndNoPendingTasksToRecycle() {
when(stateUpdater.restoresActiveTasks()).thenReturn(false);
final TasksRegistry tasks = mock(TasksRegistry.class);
when(tasks.pendingTasksToRecycleExist()).thenReturn(false);
final TaskManager taskManager = setUpTaskManager(ProcessingMode.AT_LEAST_ONCE, tasks, true);

assertTrue(taskManager.checkStateUpdater(time.milliseconds(), noOpResetter));
}

@Test
public void shouldAddActiveTaskWithRevokedInputPartitionsInStateUpdaterToPendingTasksToSuspend() {
final StreamTask task = statefulTask(taskId00, taskId00ChangelogPartitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public class TasksTest {
private final static TopicPartition TOPIC_PARTITION_B_1 = new TopicPartition("topicB", 1);
private final static TaskId TASK_0_0 = new TaskId(0, 0);
private final static TaskId TASK_0_1 = new TaskId(0, 1);
private final static TaskId TASK_0_2 = new TaskId(0, 2);
private final static TaskId TASK_1_0 = new TaskId(1, 0);
private final static TaskId TASK_1_1 = new TaskId(1, 1);
private final static TaskId TASK_1_2 = new TaskId(1, 2);

private final Tasks tasks = new Tasks(new LogContext());

Expand Down Expand Up @@ -122,6 +125,28 @@ public void shouldAddAndRemovePendingTaskToRecycle() {
assertNull(tasks.removePendingTaskToRecycle(TASK_0_0));
}

@Test
public void shouldVerifyIfPendingTaskToRecycleExist() {
assertFalse(tasks.pendingTasksToRecycleExist());
tasks.addPendingTaskToRecycle(TASK_0_0, mkSet(TOPIC_PARTITION_A_0));
assertTrue(tasks.pendingTasksToRecycleExist());

tasks.addPendingTaskToRecycle(TASK_1_0, mkSet(TOPIC_PARTITION_A_1));
assertTrue(tasks.pendingTasksToRecycleExist());

tasks.addPendingTaskToCloseClean(TASK_0_1);
tasks.addPendingTaskToCloseDirty(TASK_0_2);
tasks.addPendingTaskToUpdateInputPartitions(TASK_1_1, mkSet(TOPIC_PARTITION_B_0));
tasks.addPendingActiveTaskToSuspend(TASK_1_2);
assertTrue(tasks.pendingTasksToRecycleExist());

tasks.removePendingTaskToRecycle(TASK_0_0);
assertTrue(tasks.pendingTasksToRecycleExist());

tasks.removePendingTaskToRecycle(TASK_1_0);
assertFalse(tasks.pendingTasksToRecycleExist());
}

@Test
public void shouldAddAndRemovePendingTaskToUpdateInputPartitions() {
final Set<TopicPartition> expectedInputPartitions = mkSet(TOPIC_PARTITION_A_0);
Expand Down