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): Introduce concurrency control for main mode #9453

Open
wants to merge 59 commits into
base: master
Choose a base branch
from

Conversation

ivov
Copy link
Contributor

@ivov ivov commented May 17, 2024

This PR introduces concurrency control for main mode. This limits how many production executions are allowed to run at the same time in main mode, preventing event loop overload and so improving reliability.

Concurrency control applies only in main mode and only to production executions (i.e. in webhook and trigger modes), not to executions in manual, retry, error, integrated, cli or internal modes.

Summary of changes

Queuing

  • Implement ConcurrencyControlService.
  • Reuse Adi's work in ConcurrencyQueue.
  • Block or let through executions at ActiveExecutions.
  • On releasing an enqueued execution, reset startedAt timestamp.
  • On cancelling an enqueued execution, remove from queue.
  • On retrying an enqueued execution, block retry attempt.
  • On deleting an enqueued execution via Public API, remove from queue.
  • On deleting an enqueued execution via Internal API, remove from queue.
  • On instance startup post crash, exclude enqueued executions from crash recovery.
  • On instance startup post crash, run all enqueued executions.
  • Serve executions prioritizing running over new
  • Ensure queue mode is unaffected by concurrency control.

Envs, logs, tests

  • Add new env var N8N_CONCURRENCY_PRODUCTION_CAP.
  • Set cap from env var for self-hosters. For cloud, set cap from license feature quota:maxConcurrentProductionExecutions, allowing for override with N8N_CLOUD_OVERRIDE_CONCURRENCY_PRODUCTION_CAP.
  • Add logs on throttling and releasing executions.
  • Add tests for ConcurrencyControlService and ConcurrencyQueue.
  • (pending) Add telemetry to monitor throttling frequency.

Discussion: https://www.notion.so/n8n/Concurrency-limits-for-main-mode-8b642df5e98a479cadd1cfc4950d27a1

Follow-up to: #8458

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels May 17, 2024
@ivov ivov marked this pull request as ready for review June 5, 2024 11:07
Copy link
Member

@netroy netroy left a comment

Choose a reason for hiding this comment

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

here are my first set of comments. Will review in more detail tomorrow.

jest.config.js Outdated Show resolved Hide resolved
packages/cli/src/ActiveExecutions.ts Outdated Show resolved Hide resolved
release({ mode }: { mode: ExecutionMode }) {
if (!this.isEnabled || this.isUncapped(mode)) return;

this.productionQueue.dequeue();
Copy link
Member

Choose a reason for hiding this comment

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

would it be better if dequeue explicitly removed a specific executionId from the queue, instead of simply saying that "one slot just opened up" 🤔 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I'm not seeing what you're seeing here - what is the benefit of this? remove() removes by execution ID but that's only needed when deleting an execution.

@@ -357,6 +357,12 @@ export class License {
return this.getFeatureValue(LICENSE_QUOTAS.TEAM_PROJECT_LIMIT) ?? 0;
}

getConcurrencyProductionCap() {
return (
this.getFeatureValue(LICENSE_QUOTAS.CONCURRENCY_PRODUCTION_CAP) ?? UNLIMITED_LICENSE_QUOTA
Copy link
Member

Choose a reason for hiding this comment

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

have we discussed that we want to use the license to determine concurrency limits? if yes, do we still want to use the executions.concurrency.productionCap config variable?
It feels like one or the other should suffice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From requirements:

Capture 2024-06-06 at 11 10 38@2x

Copy link
Contributor

Choose a reason for hiding this comment

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

So from reading this, I can only see mentions to creating environment variables, nothing license related.
For cloud where we want to impose some limits (to safeguard user instances) this can be done via environment variables, there's no need for any sort of licensing here, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could impose those limits using the ephemeral licenses but that's not necessary, we can simply use environment variables for this, unless we really want to centralize it all with licenses, but it seems to me we're mixing requirements

packages/cli/src/commands/start.ts Outdated Show resolved Hide resolved
packages/cli/src/services/frontend.service.ts Outdated Show resolved Hide resolved
packages/workflow/src/ExecutionStatus.ts Show resolved Hide resolved
@luizeof
Copy link
Contributor

luizeof commented Jun 6, 2024

@ivov It would be great if we could configure a worker to only run workflows that have certain tags.

This way we can configure a service for the "critical" tag, another for the "mailing" tag, etc...

@ivov
Copy link
Contributor Author

ivov commented Jun 7, 2024

@ivov It would be great if we could configure a worker to only run workflows that have certain tags.

This way we can configure a service for the "critical" tag, another for the "mailing" tag, etc...

Thanks for your feedback! fyi @jcn8n

packages/cli/src/ActiveExecutions.ts Outdated Show resolved Hide resolved
packages/cli/src/ActiveExecutions.ts Outdated Show resolved Hide resolved
@@ -357,6 +357,12 @@ export class License {
return this.getFeatureValue(LICENSE_QUOTAS.TEAM_PROJECT_LIMIT) ?? 0;
}

getConcurrencyProductionLimit() {
return (
this.getFeatureValue(LICENSE_QUOTAS.CONCURRENCY_PRODUCTION_LIMIT) ?? UNLIMITED_LICENSE_QUOTA
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be limiting concurrency via license.

That's a product discussion but I'd say we should toggle on or off the feature via license, but the number of concurrent executions may be handled by an environment variable right? Just making sure we have the right specs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's part of the reqs, please see here: #9453 (comment)

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants