Skip to content

3.0: fix: restore database delete skip for already deleted relations - #24749

Merged
XuPeng-SH merged 1 commit into
matrixorigin:3.0-devfrom
gouhongshen:codex/restore-skip-deleted-relations-3.0
Jun 1, 2026
Merged

XuPeng-SH merged 1 commit into
matrixorigin:3.0-devfrom
gouhongshen:codex/restore-skip-deleted-relations-3.0

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

This reverts commit b0544ba.

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24711

What this PR does / why we need it:

Restore the 3.0-dev fix from #24715 after #24726 reverted it.

The original fix is still needed for restore-account database delete: when a relation was already deleted in the current transaction, Engine.Delete(database) should skip that relation only when tableOps.existAndDeleted proves it was deleted, while preserving other errors.

Tested with:

source $HOME/.zshrc && moenv && go test ./pkg/vm/engine/disttae

@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Restore database delete skip for already deleted relations

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Restore database delete skip for already deleted relations
• Add accountId field to txnDatabase struct
• Filter out relations already deleted in transaction
• Add helper functions to detect deleted relations
• Add comprehensive unit tests for deletion filtering
Diagram
flowchart LR
  A["Engine.Delete"] --> B["filterDeleteDatabaseRelations"]
  B --> C["isDeleteDatabaseRelationDeletedInTxn"]
  C --> D["tableOps.existAndDeleted"]
  D --> E["Skip already deleted relations"]

Loading

Grey Divider

File Changes

1. pkg/vm/engine/disttae/engine.go 🐞 Bug fix +30/-1

Add relation deletion filtering logic

• Added accountId field initialization in Create method for txnDatabase
• Added accountId field initialization in Database method for txnDatabase
• Implemented filterDeleteDatabaseRelations function to skip relations already deleted in
 transaction
• Implemented isDeleteDatabaseRelationDeletedInTxn helper function with system table account
 handling
• Modified Delete method to filter relations before deletion

pkg/vm/engine/disttae/engine.go


2. pkg/vm/engine/disttae/engine_test.go 🧪 Tests +88/-0

Add unit tests for deletion filtering

• Added TestFilterDeleteDatabaseRelationsSkipsAlreadyDeletedRelation test
• Added TestIsDeleteDatabaseRelationDeletedInTxn test with multiple scenarios
• Added TestIsDeleteDatabaseRelationDeletedInTxnUsesSystemAccount test for system table handling
• Tests verify correct filtering of deleted relations and system account logic

pkg/vm/engine/disttae/engine_test.go


3. pkg/vm/engine/disttae/types.go ✨ Enhancement +1/-0

Add accountId field to txnDatabase

• Added accountId field to txnDatabase struct

pkg/vm/engine/disttae/types.go


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0)

Grey Divider


Remediation recommended

1. Per-table Info log spam 🐞 Bug ◔ Observability
Description
filterDeleteDatabaseRelations emits an Info log for every skipped table during database deletion,
which can generate large log volume (and extra overhead) when many relations are already marked
deleted in the same txn. This can materially reduce signal-to-noise in logs during restore/cleanup
flows that hit this path.
Code

pkg/vm/engine/disttae/engine.go[R706-715]

Evidence
Engine.Delete iterates over all relations and applies the filter before calling Delete per relation;
the filter logs at Info for every skipped relation, making log volume proportional to the number of
skipped tables.

pkg/vm/engine/disttae/engine.go[624-652]
pkg/vm/engine/disttae/engine.go[706-719]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`filterDeleteDatabaseRelations` logs at `Info` level inside a loop (once per skipped table). When deleting a database with many already-deleted relations, this can produce excessive logs and overhead.

### Issue Context
This path is called from `Engine.Delete` before iterating and deleting each relation.

### Fix Focus Areas
- pkg/vm/engine/disttae/engine.go[706-721]

### Suggested fix
- Change the per-table log to `Debug` (or wrap with `common.DoIfDebugEnabled`).
- Optionally aggregate: count skipped relations and emit a single log line after the loop (e.g., `skipped=N`, `total=M`) instead of one per table.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Advisory comments

2. Fragile unchecked type cast 🐞 Bug ⚙ Maintainability
Description
Engine.Delete relies on an unchecked type assertion to *txnDatabase, which will panic if
Engine.Database ever returns a different engine.Database implementation. While currently safe given
today’s Engine.Database implementation, adding a guard (or documenting the invariant) improves
resilience to future refactors.
Code

pkg/vm/engine/disttae/engine.go[R646-647]

Evidence
The new code introduces the unchecked assertion; Engine.Database’s current implementation returns
*txnDatabase, which is why this is currently safe but still a fragile assumption.

pkg/vm/engine/disttae/engine.go[635-648]
pkg/vm/engine/disttae/engine.go[354-454]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`toDelDB.(*txnDatabase)` is an unchecked assertion; if the return type of `Engine.Database` changes in the future, this will panic.

### Issue Context
Today `Engine.Database` returns `*txnDatabase`, so this is defensive hardening / future-proofing.

### Fix Focus Areas
- pkg/vm/engine/disttae/engine.go[635-648]

### Suggested fix
Replace the assertion with a checked assertion:
```go
txnDB, ok := toDelDB.(*txnDatabase)
if !ok {
   return moerr.NewInternalErrorf(ctx, "unexpected database type %T", toDelDB)
}
```
Alternatively, add a short comment documenting the invariant that `Engine.Database` always returns `*txnDatabase`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants