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(core): Batch workflow activation to speed up startup #13191

Merged
merged 2 commits into from
Feb 11, 2025
Merged

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Feb 11, 2025

Summary

During startup we activate workflows one by one, which can be slow for instances with many workflows to activate. This PR introduces support for batching workflow activations via N8N_WORKFLOW_ACTIVATION_BATCH_SIZE so users can choose how much of this work to parallelize based on their use cases.

Related Linear tickets, Github issues, and Community forum posts

n/a

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

@@ -533,7 +543,6 @@ export class ActiveWorkflowManager {
}

if (shouldDisplayActivationMessage) {
this.logger.info(` - ${dbWorkflow.display()}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Move next to its pair to prevent logs out of order:

 ================================
   Start Active Workflows:
 ================================
   - "My workflow 70" (ID: rriRxvVT6Y1YcUnq))
   - "My workflow 71" (ID: UNlbMpRODEH9OkW2))
     => Started
     => Started

Copy link

codecov bot commented Feb 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@ivov ivov marked this pull request as ready for review February 11, 2025 11:19
@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Feb 11, 2025
Copy link
Collaborator

@tomi tomi left a comment

Choose a reason for hiding this comment

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

Couple comments

Comment on lines 429 to 466
const activationPromises = batch.map(async (dbWorkflow) => {
try {
const wasActivated = await this.add(dbWorkflow.id, activationMode, dbWorkflow, {
shouldPublish: false,
});
this.logger.info(' => Started');
}
} catch (error) {
this.errorReporter.error(error);
this.logger.info(
' => ERROR: Workflow could not be activated on first try, keep on trying if not an auth issue',
);
if (wasActivated) {
this.logger.info(` - ${dbWorkflow.display()})`);
this.logger.info(' => Started');
this.logger.debug(`Successfully started workflow ${dbWorkflow.display()}`, {
workflowName: dbWorkflow.name,
workflowId: dbWorkflow.id,
});
}
} catch (error) {
this.errorReporter.error(error);
this.logger.info(
` => ERROR: Workflow ${dbWorkflow.display()} could not be activated on first try, keep on trying if not an auth issue`,
);

this.logger.info(` ${error.message}`);
this.logger.error(
`Issue on initial workflow activation try of ${dbWorkflow.display()} (startup)`,
{
workflowName: dbWorkflow.name,
workflowId: dbWorkflow.id,
},
);
this.logger.info(` ${error.message}`);
this.logger.error(
`Issue on initial workflow activation try of ${dbWorkflow.display()} (startup)`,
{
workflowName: dbWorkflow.name,
workflowId: dbWorkflow.id,
},
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.executeErrorWorkflow(error, dbWorkflow, 'internal');
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.executeErrorWorkflow(error, dbWorkflow, 'internal');

// do not keep trying to activate on authorization error
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
if (error.message.includes('Authorization')) continue;
// do not keep trying to activate on authorization error
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
if (error.message.includes('Authorization')) return;

this.addQueuedWorkflowActivation('init', dbWorkflow);
}
this.addQueuedWorkflowActivation('init', dbWorkflow);
}
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we extract this into a separate method to make this more readable? Either the creation of a batch or activation of a single WF

Comment on lines 426 to 427
for (let i = 0; i < dbWorkflows.length; i += activationBatchSize) {
const batch = dbWorkflows.slice(i, i + activationBatchSize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using e.g. lodash chunk would be clearer than implementing the chunking ourselves

@ivov ivov requested a review from tomi February 11, 2025 12:34
Copy link
Collaborator

@tomi tomi left a comment

Choose a reason for hiding this comment

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

Nice work 👏 🚀

Copy link
Contributor

⚠️ Some Cypress E2E specs are failing, please fix them before merging

Copy link

cypress bot commented Feb 11, 2025

n8n    Run #9232

Run Properties:  status check passed Passed #9232  •  git commit 001f1ab81e: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review batch-activation
Run status status check passed Passed #9232
Run duration 04m 36s
Commit git commit 001f1ab81e: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Committer Iván Ovejero
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 1
Tests that did not run due to a developer annotating a test with .skip  Pending 5
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 436
View all changes introduced in this branch ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

@ivov ivov merged commit 17acf70 into master Feb 11, 2025
37 checks passed
@ivov ivov deleted the batch-activation branch February 11, 2025 14:12
@github-actions github-actions bot mentioned this pull request Feb 13, 2025
@janober
Copy link
Member

janober commented Feb 13, 2025

Got released with n8n@1.79.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants