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): Implement soft-deletions for executions #7092

Merged
merged 44 commits into from
Sep 20, 2023

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Sep 4, 2023

Based on #7065 | Story: https://linear.app/n8n/issue/PAY-771

n8n on filesystem mode marks binary data to delete on manual execution deletion, on unsaved execution completion, and on every execution pruning cycle. We later prune binary data in a separate cycle via these marker files, based on the configured TTL. In the context of introducing an S3 client to manage binary data, the filesystem mode's mark-and-prune setup is too tightly coupled to the general binary data management client interface.

This PR...

  • Ensures the deletion of an execution causes the deletion of any binary data associated to it. This does away with the need for binary data TTL and simplifies the filesystem mode's mark-and-prune setup.
  • Refactors all execution deletions (including pruning) to cause soft deletions, hard-deletes soft-deleted executions based on the existing pruning config, and adjusts execution endpoints to filter out soft-deleted executions. This reduces DB load, and keeps binary data around long enough for users to access it when building workflows with unsaved executions.
  • Moves all execution pruning work from an execution lifecycle hook to execution.repository.ts. This keeps related logic in a single place.
  • Removes all marking logic from the binary data manager. This simplifies the interface that the S3 client will meet.
  • Adds basic sanity-check tests to pruning logic and execution deletion.

Out of scope:

  • Improving existing pruning logic.
  • Improving existing execution repository logic.
  • Adjusting dir structure for filesystem mode.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2023

Great PR! Please pay attention to the following items before merging:

Files matching packages/**:

  • If fixing bug, added test to cover scenario.
  • If addressing forum or Github issue, added link to description.

Files matching packages/**/*.ts:

  • Added unit tests to cover new or updated functionality.

Make sure to check off this list before asking for review.

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team node/improvement New feature or request labels Sep 4, 2023
@codecov
Copy link

codecov bot commented Sep 4, 2023

Codecov Report

Patch coverage: 53.01% and project coverage change: +0.04% 🎉

Comparison is base (9f797b9) 32.65% compared to head (7c3e58d) 32.70%.
Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7092      +/-   ##
==========================================
+ Coverage   32.65%   32.70%   +0.04%     
==========================================
  Files        3340     3340              
  Lines      199463   199435      -28     
  Branches    21845    21844       -1     
==========================================
+ Hits        65132    65219      +87     
+ Misses     133251   133125     -126     
- Partials     1080     1091      +11     
Files Changed Coverage Δ
packages/cli/src/GenericHelpers.ts 79.16% <0.00%> (ø)
packages/cli/src/Interfaces.ts 100.00% <ø> (ø)
...icApi/v1/handlers/executions/executions.service.ts 80.64% <ø> (-1.18%) ⬇️
packages/cli/src/Server.ts 0.00% <ø> (ø)
packages/cli/src/WorkflowHelpers.ts 51.64% <0.00%> (ø)
packages/cli/src/WorkflowRunner.ts 9.21% <0.00%> (ø)
packages/cli/src/commands/BaseCommand.ts 46.51% <0.00%> (-0.55%) ⬇️
packages/cli/src/commands/start.ts 0.00% <0.00%> (ø)
packages/cli/src/config/schema.ts 37.50% <ø> (ø)
...ages/cli/src/databases/entities/ExecutionEntity.ts 65.00% <ø> (ø)
... and 9 more

... and 6 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ivov ivov marked this pull request as ready for review September 13, 2023 13:31
@cypress
Copy link

cypress bot commented Sep 13, 2023

Passing run #2225 ↗︎

0 238 3 0 Flakiness 0

Details:

🌳 pay-771-implement-soft-deletion 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃...
Project: n8n Commit: 7c3e58db2f
Status: Passed Duration: 08:51 💡
Started: Sep 20, 2023 1:09 PM Ended: Sep 20, 2023 1:18 PM

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings.

@ivov ivov requested a review from krynble September 15, 2023 09:48
netroy
netroy previously approved these changes Sep 18, 2023
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.

besides the minor comments, LGTM

@@ -49,7 +50,7 @@ export class ExecutionEntity {
@Column({ type: datetimeColumnType, nullable: true })
stoppedAt: Date;

@Column(datetimeColumnType)
@DeleteDateColumn({ type: datetimeColumnType, nullable: true })
Copy link
Member

Choose a reason for hiding this comment

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

are we really benefiting from this decorator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DeleteDateColumn is what enables softDelete to work, which is used in many places:

Capture 2023-09-18 at 14 48 19@2x

@@ -49,7 +50,7 @@ export class ExecutionEntity {
@Column({ type: datetimeColumnType, nullable: true })
stoppedAt: Date;

@Column(datetimeColumnType)
@DeleteDateColumn({ type: datetimeColumnType, nullable: true })
deletedAt: Date;
Copy link
Member

Choose a reason for hiding this comment

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

should this be

Suggested change
deletedAt: Date;
deletedAt?: Date;

Copy link
Contributor Author

@ivov ivov Sep 18, 2023

Choose a reason for hiding this comment

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

Not 100% on this, but I logged on startup webhook entities (which have webhookId and pathLength which are nullable and marked with ?) and they show up as null when missing:

> n8n@1.7.0 start /Users/ivov/Development/n8n
> run-script-os


> n8n@1.7.0 start:default
> cd packages/cli/bin && ./n8n

n8n ready on 0.0.0.0, port 5678
Initializing n8n process
Version: 1.7.0
 ================================
   Start Active Workflows:
 ================================
   - My workflow 21 (ID: HDemWLl4i60bkGsm)
allWebhooks [
  WebhookEntity {
    workflowId: 'HDemWLl4i60bkGsm',
    webhookPath: '9347adb3-289c-4508-8705-531575ac3e60',
    method: 'GET',
    node: 'Webhook',
    webhookId: null,
    pathLength: null
  }
]

So if anything no properties that are nullable columns should have ? right? (Always present, but may be null)

}

setPruningInterval() {
setInterval(async () => this.pruneBySoftDeleting(), 1 * TIME.HOUR);
Copy link
Member

@netroy netroy Sep 18, 2023

Choose a reason for hiding this comment

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

I think we should add a global AbortController/AbortSignal for the shutdown process, so that we can clean up timers like these properly at shutdown.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clearing the timers on shutdown at abe6d9d, to keep scope small.

@github-actions
Copy link
Contributor

✅ All Cypress E2E specs passed

Comment on lines 91 to 95
this.setHardDeletionInterval();
}

setPruningInterval() {
setInterval(async () => this.pruneBySoftDeleting(), 1 * TIME.HOUR);
Copy link
Member

Choose a reason for hiding this comment

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

These timers are going to run on every container. Shouldn't we add some checks to run these only on the main container?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah this is a great point, we have something kinda similar that was merged by @flipswitchingmonkey yesterday for things that must run only on the main process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, please see 2682b3d

Copy link
Contributor

@krynble krynble left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions
Copy link
Contributor

✅ All Cypress E2E specs passed

@ivov ivov merged commit cd08c8e into master Sep 20, 2023
53 checks passed
@ivov ivov deleted the pay-771-implement-soft-deletion branch September 20, 2023 13:21
MiloradFilipovic added a commit that referenced this pull request Sep 20, 2023
* master:
  refactor(core): Implement soft-deletions for executions (#7092)
  docs(Google Sheets Node): Operations naming update (no-changelog) (#7211)
  fix(core): Make parsing of content-type and content-disposition headers more flexible (#7217)
  fix(HTML Node): Add pairedItem support for 'Convert to HTML Table' operation (#7196)
  fix(HTTP Request Node): Decrease default timeout to 5min (#7177)
  feat(Linear Node): Add support for OAuth2 (#7201)
  refactor: Ignore large-scale revisions (no-changelog) (#7210)
  fix(core): Resolve domains to IPv4 first (#7206)
  ci: Fix tests failing on MySQL (no-changelog) (#7208)
  feat(Set Node): Overhaul (#6348)
  fix: Attempt license renewal when n8n starts (no-changelog) (#7204)
ivov added a commit that referenced this pull request Sep 22, 2023
…g) (#7164)

Depends on: #7092 | Story:
[PAY-768](https://linear.app/n8n/issue/PAY-768)

This PR: 
- Generalizes the `IBinaryDataManager` interface.
- Adjusts `Filesystem.ts` to satisfy the interface.
- Sets up an S3 client stub to be filled in in the next PR.
- Turns `BinaryDataManager` into an injectable service.
- Adjusts the config schema and adds new validators.

Note that the PR looks large but all the main changes are in
`packages/core/src/binaryData`.

Out of scope:
- `BinaryDataManager` (now `BinaryDataService`) and `Filesystem.ts` (now
`fs.client.ts`) were slightly refactored for maintainability, but fully
overhauling them is **not** the focus of this PR, which is meant to
clear the way for the S3 implementation. Future improvements for these
two should include setting up a backwards-compatible dir structure that
makes it easier to locate binary data files to delete, removing
duplication, simplifying cloning methods, using integers for binary data
size instead of `prettyBytes()`, writing tests for existing binary data
logic, etc.

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
@janober
Copy link
Member

janober commented Sep 28, 2023

Got released with n8n@1.9.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 node/improvement New feature or request Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants