Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(scheduler): create tasks in parallel #2373

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

TBonnin
Copy link
Collaborator

@TBonnin TBonnin commented Jun 19, 2024

Describe your changes

Tasks are currently created by scheduler one by one. We can parallelize the tasks creation to speed up the scheduling transaction

Checklist before requesting a review (skip if just adding/editing APIs & templates)

  • I added tests, otherwise the reason is:
  • I added observability, otherwise the reason is:
  • I added analytics, otherwise the reason is:

} else {
taskIds.push(task.value.id);
taskIds.push(taskRes.value.value.id);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

a bit weird but the PromiseAllSettled result type is PromiseSettleResult<Result<Task>>[]

Copy link
Collaborator

@bodinsamuel bodinsamuel left a comment

Choose a reason for hiding this comment

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

Indeed. Looks good, just an open comment about the transaction

Comment on lines +122 to +126
for (const taskRes of res) {
if (taskRes.status === 'rejected') {
logger.error(`Failed to schedule task: ${taskRes.reason}`);
} else if (taskRes.value.isErr()) {
logger.error(`Failed to schedule task: ${stringifyError(taskRes.value.error)}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

you could have done all of that inside the .map (if that makes more sense)

Copy link
Collaborator Author

@TBonnin TBonnin Jun 19, 2024

Choose a reason for hiding this comment

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

you mean with a then? The goal is not to await for the result in the map

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant keeping the code exactly the same but inside the map, since you don't kill the transaction

schedules.value.map(async (schedule) => {
      const task = await tasks.create(trx, {
          scheduleId: schedule.id,
          startsAfter: new Date(),
          name: `${schedule.name}:${new Date().toISOString()}`,
          createdToStartedTimeoutSecs: schedule.createdToStartedTimeoutSecs,
          startedToCompletedTimeoutSecs: schedule.startedToCompletedTimeoutSecs,
          heartbeatTimeoutSecs: schedule.heartbeatTimeoutSecs
      });
      if (task.isErr()) 
      [...]
  });

Copy link
Collaborator Author

@TBonnin TBonnin Jun 19, 2024

Choose a reason for hiding this comment

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

I see. But you are awaiting while iterating through the list. no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

should not no, this is a common pattern await Promise.all(array.map(async))

logger.error(`Failed to create task for schedule: ${schedule.id}`);
})
);
const res = await Promise.allSettled(createTasks);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wait my comment was not sent 🤔
I was saying (before and after this pr) you are not killing the transaction if any tasks fails, is it on purpose?

Copy link
Collaborator Author

@TBonnin TBonnin Jun 19, 2024

Choose a reason for hiding this comment

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

yes it is on purpose. Failing to create a task should not prevent other tasks to be created.
The same scheduling logic will try to create the task again in the next iteration if needed

@TBonnin TBonnin removed the request for review from bastienbeurier June 19, 2024 16:58
@TBonnin TBonnin force-pushed the tbonnin/scheduling-parallel-tasks-creation branch from 4d2640f to b69417b Compare June 19, 2024 16:58
@TBonnin TBonnin merged commit 3516678 into master Jun 19, 2024
20 checks passed
@TBonnin TBonnin deleted the tbonnin/scheduling-parallel-tasks-creation branch June 19, 2024 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants