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): Map out pubsub messages (no-changelog) #10566

Merged
merged 4 commits into from
Aug 28, 2024

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Aug 27, 2024

In scaling mode, we have two pubsub channels for mains, workers, and webhooks to communicate: the command channel (main to workers, main to mains, main to webhooks) and the response channel (worker to mains).

This pubsub setup has two big areas for improvement:

  • It is made up of an intricate hierarchy of classes: OrchestrationHandlerService, OrchestrationHandlerMainService, OrchestrationHandlerWebhookService, OrchestrationHandlerWorkerService, OrchestrationWebhookService, OrchestrationWorkerService, OrchestrationService, RedisService, RedisServicePubSubSubscriber, RedisServicePubSubPublisher, RedisServiceBaseReceiver, RedisServiceBaseSender, RedisServiceBase, RedisClientService. Our use case does not require this much complexity, so we should aggressively simplify all this to make it easier to reason about.
  • All messages are handled together in a large switch statement in a single function per instance type per channel: main/handleCommandMessageMain.ts, main/handleWorkerResponseMessageMain.ts, worker/handleCommandMessageWorker.ts, webhook/handleCommandMessageWebhook.ts We should break all this down into individually unit-testable class methods, e.g. following the model in telemetry-event-relay.ts and log-streaming-event-relay.ts. This will also decouple channel subscription from message handling.

To start improving this, specifically the handler side, we can...

  1. set up an event emitter to fire an event for every message received,
  2. handle each message event in a method in the relevant class, and
  3. unit test each handler method.

This initial PR maps out all pubsub messages and payloads for all interactions between mains, workers, and webhooks. Next steps will be...

  • normalize all command names following the new pubsub message map,
  • create one handler class per instance type: main.message-handler.ts, worker.message-handler.ts, and webhook.message-handler.ts,
  • for each event received via channel, emit an event using EventService and handle it in the relevant handler class, in a unit-tested method, and
  • once all events are handled like this, tear down the legacy handler setup and do a single emit call taking the event name from the incoming message.

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Aug 27, 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.

Overall the approach looks nice. Are we certain that we don't need await when we publish any of the defined messages? Asking because EventEmitter interface is sync (by design) and we can't wait for the subscribers to process the events

@ivov
Copy link
Contributor Author

ivov commented Aug 28, 2024

Overall the approach looks nice. Are we certain that we don't need await when we publish any of the defined messages? Asking because EventEmitter interface is sync (by design) and we can't wait for the subscribers to process the events

Correct, we do not await the result of messages pushed into a channel. Publishers send them and they're eventually handled by subscribers.

Copy link

cypress bot commented Aug 28, 2024

n8n    Run #6638

Run Properties:  status check passed Passed #6638  •  git commit 9203ac5948: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review map-out-pubsub-commands
Run status status check passed Passed #6638
Run duration 04m 40s
Commit git commit 9203ac5948: 🌳 🖥️ 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 acfd60a into master Aug 28, 2024
40 checks passed
@ivov ivov deleted the map-out-pubsub-commands branch August 28, 2024 08:48
MiloradFilipovic added a commit that referenced this pull request Aug 28, 2024
* master:
  ci: Fix provenance generation during NPM publish (no-changelog) (#10586)
  feat(core): Expose queue metrics for Prometheus (#10559)
  refactor(core): Map out pubsub messages (no-changelog) (#10566)
  fix: Fix scenario prefix not being passed (no-changelog) (#10575)
  refactor(core): Convert `verbose` to `debug` logs (#10574)
@janober
Copy link
Member

janober commented Aug 28, 2024

Got released with n8n@1.57.0

MiloradFilipovic added a commit that referenced this pull request Aug 28, 2024
* master: (24 commits)
  feat(core): Switch to MJML for email templates (#10518)
  fix(Gmail Trigger Node): Don't return date instances, but date strings instead (#10582)
  fix(core): Deduplicate sentry events using error stacktraces instead (no-changelog) (#10590)
  feat(editor): Implement new app layout (#10548)
  refactor(core): Standardize filename casing for services and Public API (no-changelog) (#10579)
  🚀 Release 1.57.0 (#10587)
  fix(editor): Add tooltips to workflow history button (#10570)
  ci: Fix provenance generation during NPM publish (no-changelog) (#10586)
  feat(core): Expose queue metrics for Prometheus (#10559)
  refactor(core): Map out pubsub messages (no-changelog) (#10566)
  fix: Fix scenario prefix not being passed (no-changelog) (#10575)
  refactor(core): Convert `verbose` to `debug` logs (#10574)
  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)
  ...

# Conflicts:
#	packages/editor-ui/src/stores/assistant.store.ts
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