Skip to content

Conversation

@swamirishi
Copy link
Contributor

What changes were proposed in this pull request?

Currently both background task and shutdown acquire a synchronized lock which can lead to a deadlock on shutdown which means the threadPool shutdown takes time unnecessarily. The synchronized block on task was taken to ensure there are no parallel tasks that run together.
This patch removes the unnecessary synchronized calls and converts the ThreadPoolExecutor to a fork join pool. Instead of hacking around with the wait logic present at the start of a periodic task to avoid deadlock in case of a single threaded background service we can use a forkJoin pool to fork on to background and task and join all the forked task to implement the wait.

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-14020

How was this patch tested?

Existing tests should be sufficient

@swamirishi swamirishi changed the title HDDS-14020. Use forkjoin pool instead of thread pool on background service HDDS-14020. Use forkjoin pool instead of a ScheduledThreadPoolExecutor on background service Nov 28, 2025
@swamirishi swamirishi force-pushed the HDDS-14020_FJP_Change branch 2 times, most recently from 2be9d46 to ee29c5f Compare November 28, 2025 07:11
@swamirishi
Copy link
Contributor Author

@SaketaChalamchala do you wanna review this?

@swamirishi swamirishi force-pushed the HDDS-14020_FJP_Change branch from ee29c5f to 4d431fd Compare November 28, 2025 07:41
@adoroszlai adoroszlai changed the title HDDS-14020. Use forkjoin pool instead of a ScheduledThreadPoolExecutor on background service HDDS-14020. Use ForkJoinPool instead of a ScheduledThreadPoolExecutor in BackgroundService Nov 28, 2025
@swamirishi swamirishi force-pushed the HDDS-14020_FJP_Change branch 4 times, most recently from 9212b98 to 579eac8 Compare November 28, 2025 22:20
…rvice

Change-Id: I4c1a051e8574d32375cbebeb10546563f4a4f817
@swamirishi swamirishi force-pushed the HDDS-14020_FJP_Change branch from 579eac8 to 14d25f1 Compare November 29, 2025 00:39
Copy link
Contributor

@sumitagrawl sumitagrawl left a comment

Choose a reason for hiding this comment

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

@swamirishi given few comments, may need change in such a way to avoid changes all other places for change in execution way.

if (tasks.isEmpty()) {
// No task found, or some problems to init tasks
// return and retry in next interval.
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

this can cause continuous loop as directly jump to while condition without waitForNextInterval()

* A task thread to run by {@link BackgroundService}.
*/
public interface BackgroundTask extends Callable<BackgroundTaskResult> {
public abstract class BackgroundTask extends RecursiveTask<BackgroundTask.BackgroundTaskForkResult>
Copy link
Contributor

Choose a reason for hiding this comment

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

need not change BackgroundTask, but encapsulate in inside ForkJoinTask as transient and can be scheduled. This avoid changing all other place due to change in framework. Externally, its just Runnable task.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The external task has to be a recursive task otherwise forkjoin pool cannot work

Copy link
Contributor

Choose a reason for hiding this comment

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

We can do like below, please check... BackgroundTaskForkJoin will not be exposed.

public class BackgroundTaskForkJoin extends RecursiveTask<BackgroundTaskForkJoin.BackgroundTaskForkResult> {
  private static final long serialVersionUID = 1L;
  private final transient BackgroundTask backgroundTask;
  
  public BackgroundTaskForkJoin(BackgroundTask backgroundTask) {
    this.backgroundTask = backgroundTask;
  }
  
  public static class BackgroundTaskForkResult {
    ...
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this one is closed. I've raised #9686 that addressed the comment above.

}
}
}
future.complete(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

unable to get use of future here

while (!tasksInFlight.isEmpty()) {
BackgroundTask taskInFlight = tasksInFlight.poll();
// Join the tasks forked before and wait for the result one by one.
BackgroundTask.BackgroundTaskForkResult result = taskInFlight.join();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the task is cancellable? if crosses certain time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can implement something if we want to. But this is not supported even today.

@jojochuang jojochuang self-requested a review December 1, 2025 17:34
@jojochuang
Copy link
Contributor

@rnblough

Change-Id: Ie2118356f902443a93fe666d890ad4d59e9dd467
@github-actions
Copy link

github-actions bot commented Jan 6, 2026

This PR has been marked as stale due to 21 days of inactivity. Please comment or remove the stale label to keep it open. Otherwise, it will be automatically closed in 7 days.

@github-actions github-actions bot added the stale label Jan 6, 2026
@github-actions
Copy link

Thank you for your contribution. This PR is being closed due to inactivity. If needed, feel free to reopen it.

@github-actions github-actions bot closed this Jan 14, 2026
@smengcl
Copy link
Contributor

smengcl commented Jan 14, 2026

@sumitagrawl Would you like to take another look?

smengcl added a commit to smengcl/hadoop-ozone that referenced this pull request Jan 28, 2026
smengcl added a commit to smengcl/hadoop-ozone that referenced this pull request Jan 28, 2026
smengcl added a commit to smengcl/hadoop-ozone that referenced this pull request Jan 28, 2026
smengcl added a commit to smengcl/hadoop-ozone that referenced this pull request Jan 28, 2026
smengcl added a commit to smengcl/hadoop-ozone that referenced this pull request Jan 28, 2026
…omment: apache#9390 (comment)

Refactor BackgroundTask to use wrapper pattern for ForkJoinPool integration

This commit introduces BackgroundTaskForkJoin as a wrapper class to
integrate BackgroundTask with ForkJoinPool, avoiding the need to change
all service implementations from 'implements' to 'extends'.

Key changes:
- Reverted BackgroundTask from abstract class back to interface
- Created BackgroundTaskForkJoin wrapper extending RecursiveTask
- Updated BackgroundService to wrap tasks before forking
- Reverted all service task classes to 'implements BackgroundTask'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants