Skip to content

fix(toolkit): keep source DB intact when DbMove copy fails - #6946

Merged
kuny0707 merged 9 commits into
tronprotocol:release_v4.8.3from
halibobo1205:feature/dbmove-data-safety
Oct 13, 2026
Merged

kuny0707 merged 9 commits into
tronprotocol:release_v4.8.3from
halibobo1205:feature/dbmove-data-safety

Conversation

@halibobo1205

Copy link
Copy Markdown
Collaborator

Closes #6940

What does this PR do?

Makes db mv a fail-safe two-phase migration: validate first (exit 2, nothing written), then copy every database (recursive, link-aware, fail-fast), and only after all copies succeed replace sources with symlinks. Any copy failure keeps all sources, removes the created destinations, and exits 1 — the same command can simply be re-run. Finalization failures keep the complete copy and print recovery instructions. move db done. / exit 0 only on full success. Also documents the stop-the-node precondition in plugins/README.md.

Why are these changes required?

The previous implementation ignored per-file copy errors, then deleted the source and symlinked it to an incomplete destination while still reporting success — any I/O fault (disk full, permissions, bad sector) meant silent, unrecoverable data loss (#6940).

This PR has been tested by:

  • Unit Tests: DbMoveTest grown to 15 cases (failure/rollback/retry, recursive copy, symlink and permission faults, exit codes).
  • Manual Testing: simulated copy failures on real node data — sources kept, destinations rolled back, exit 1; retry completed the migration.

Follow up

None.

Extra details

Design writeup with sequence diagram and non-goals: see #6940.

Prevent the prior flow from deleting source databases after logging and ignoring per-file copy errors.

Preserve every source until all configured databases copy successfully. Roll back partial destinations, handle recursive database contents, and return a non-zero status on failure.

Finalize symlinks only after the copy phase completes, report completion once, and cover rollback and direct retry behavior.

private boolean replaceSourceWithLink(Property p) {
try {
if (!FileUtils.deleteDir(p.original.toFile())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great fix for the copy-failure case. One adjacent gap worth flagging while you're in here: FileUtils.deleteDir (plugins utils/FileUtils.java:73-84) still recurses with java.io.File, and isDirectory() follows symlinks — so a directory symlink gets traversed and its target's contents are deleted.

This composes with this PR: after db mv, the original path is a symlink to the moved DB (DbMove.java:176). If a later db lite merge deletes the archive dbs under that original path (DbLite.backupArchiveDbs calls FileUtils.deleteDir per db), it wipes the already-migrated DB on the new disk — two legitimate operations, chained data loss, no attacker needed.

Suggested fix: replace the recursion with Files.walkFileTree + NOFOLLOW_LINKS (delete the link itself, never follow). The copy phase already uses NOFOLLOW_LINKS (:154/:198), so this would make the delete side consistent. The same pattern also exists in common/.../FileUtil.java:114-126 and the toolkit copy in tron-docker's tools/toolkit — happy to split that into a separate PR if you prefer.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for flagging this. The symlink traversal issue is valid, but it predates this PR: db mv already created symlinks, and this change does not modify FileUtils.deleteDir or the db lite merge workflow.

I’ll keep this PR focused on preventing source deletion after copy failures and reporting migration failures correctly. The deletion behavior and lite merge’s handling of relocated databases should be addressed in a separate PR, with regression coverage for the combined workflow.

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed 01638e7. The overlapping-destination data loss, config type validation, and progress-bar comments are addressed and verified locally. One remaining [SHOULD] is in the config-validation thread: InvalidPathException still returns exit 1 instead of 2.

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed 43b0ae0. The code findings from my previous review have been addressed and verified locally. I have no further code change requests.

The Debian/JDK 8 build still fails in :framework:test, where Gradle Test Executor 7 exits with code 134; the cause is not yet confirmed. Coverage Gate depends on this job and was skipped as a result. Please rerun the failed job and make sure Coverage Gate passes before merging.

@kuny0707
kuny0707 merged commit c9bb7d8 into tronprotocol:release_v4.8.3 Oct 13, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:DB Database

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]Toolkit db mv may delete the source database after a copy failure, causing data loss

6 participants