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

refactor(core): Separate listeners in scaling service (no-changelog) #10487

Merged
merged 6 commits into from
Aug 26, 2024

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Aug 21, 2024

The scaling service currently registers all listeners on all instance types - a leftover from the original implementation. Registering all listeners on all instance types is inefficient and makes the service harder to understand.

This PR separates listeners in the scaling service into...

  • main instance listener: respond to webhook
  • worker listeners: abort job, error on max stalled count, exit on Lua scripts init error
  • shared listener: log Redis reconnection attempts and exit on timeout

It also expands test coverage in the scaling service.

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Aug 21, 2024
@ivov ivov changed the title refactor(core): Separate Bull queue listeners for main and workers (no-changelog) refactor(core): Separate listeners in scaling service (no-changelog) Aug 26, 2024
Copy link
Contributor

@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 about naming/types. Otherwise good stuff 👌

packages/cli/src/scaling/scaling.service.ts Show resolved Hide resolved
Comment on lines 32 to 37
export type JobMessage = RepondToWebhookMessage | AbortJobMessage;

export type RepondToWebhookMessage = {
export type MessageToMain = {
kind: 'respond-to-webhook';
executionId: string;
response: IExecuteResponsePromiseData;
};

export type AbortJobMessage = {
export type MessageToWorker = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these would be clearer if the types were still called RespondToWebhookMessage and AbortJobMessage, as that's what they semantically are. If we want we can then create aliaes for MessagesToMain and MessagesToWorker which alias these types

* Register listeners on a `main` process for Bull queue events.
*/
private registerMainListeners() {
this.queue.on('global:progress', (_jobId: JobId, msg: MessageToMain) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the type for msg is not correct, or otherwise we wouldn't need the if (msg.kind === ... ). Now I don't know if there are other events that might be emitted, but we could either

  1. Type the msg as unknown and have a type guard that checks based on kind if the message is a RespondToWebhookMessage (i.e. there might be other messages as well)
  2. Type the msg as RespondToWebhookMessage and assert(msg.kind === ...) (i.e. there are no other message types)

packages/core/src/InstanceSettings.ts Show resolved Hide resolved
* - `leader` after bootup in single-main setup,
* - `leader` or `follower` after bootup in multi-main setup.
*
* A non-main instance type (e.g. `worker`) is always `unset`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it instead be worker when the instance acts as a worker? Then it would be more "complete"

Copy link
Contributor Author

@ivov ivov Aug 26, 2024

Choose a reason for hiding this comment

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

So the role an instance has (unset, leader, follower) is only ever relevant for a main - and the role is separate from what kind of instance it is (main, worker, webhook, etc.) which is instanceType.

Ideally we should throw whenever attempting to access instanceRole (or its derived getters) from a non-main instance as this should never happen, but that'd be a bit outside the scope of this PR.

@ivov ivov requested a review from tomi August 26, 2024 08:48
Copy link
Contributor

@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.

🚀

Copy link

cypress bot commented Aug 26, 2024

n8n    Run #6606

Run Properties:  status check passed Passed #6606  •  git commit 94ecd0b98d: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review separate-bull-queue-listeners-for-main-and-workers
Run status status check passed Passed #6606
Run duration 04m 41s
Commit git commit 94ecd0b98d: 🌳 🖥️ 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 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 419
View all changes introduced in this branch ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

@ivov ivov merged commit 352aa2a into master Aug 26, 2024
44 of 48 checks passed
@ivov ivov deleted the separate-bull-queue-listeners-for-main-and-workers branch August 26, 2024 10:35
MiloradFilipovic added a commit that referenced this pull request Aug 27, 2024
* master:
  refactor(core): Use `@/databases/` instead of `@db/` (no-changelog) (#10573)
  ci: Fix destroy benchmark env workflow (no-changelog) (#10572)
  feat: Add benchmarking of pooled sqlite (no-changelog) (#10550)
  refactor(editor): User journey link to n8n.io (#10331)
  fix(Wait Node): Prevent waiting until invalid date (#10523)
  refactor(core): Standardize filename casing for controllers and databases (no-changelog) (#10564)
  refactor(core): Allow custom types on getCredentials (no-changelog) (#10567)
  fix(editor): Scale output item selector input width with value (#10555)
  refactor(core): Delete InternalHooks (no-changelog) (#10561)
  fix(core): Make boolean config value parsing backward-compatible (#10560)
  fix(Google Sheets Trigger Node): Show sheet name is too long error (#10542)
  fix(editor): Ensure `Datatable` component renders `All` option (#10525)
  fix(core): Stop explicit redis client disconnect on shutdown (#10551)
  ci: Use correct branch for benchmark docker build workflow (no-changelog) (#10552)
  refactor(core): Separate listeners in scaling service (no-changelog) (#10487)
@janober
Copy link
Member

janober commented Aug 28, 2024

Got released with [email protected]

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