hbase icon indicating copy to clipboard operation
hbase copied to clipboard

HBASE-29435 Add MapreduceRestoreSnapshotHelper.java to guard against accidental data loss

Open SwaraliJoshi opened this issue 6 months ago • 0 comments

Problem

When a MapReduce snapshot-scanning job is misconfigured with restoreDir pointing to (or under) the production HBase data directory (hbase.rootdir/data/), RestoreSnapshotHelper.copySnapshotForScanner() creates HFileLink reference files directly inside the live data directory. When the MR job ends and cleanup triggers HFileArchiver, production HFiles get moved into the archive, where HFileCleaner (default TTL: 5 minutes) permanently deletes them — causing silent, irreversible data loss.

The existing same-filesystem and sub-directory checks in RestoreSnapshotHelper did not catch the exact-match case (restoreDir == rootDir), and the resulting error message gave no hint about the data-loss risk.

JIRA HBASE-29435

Current Impact (Without Fix)

A single misconfigured restoreDir parameter (e.g., set to hbase.rootdir or hbase.rootdir/data/...) can cause permanent production data loss. The failure mode is silent — no exception, no warning; data simply disappears after the HFileCleaner TTL elapses. Every MapReduce snapshot-scanning path is exposed: TableSnapshotInputFormat, MultiTableSnapshotInputFormat, and VerifyReplication (peer cluster). Stricter validation cannot simply be added to RestoreSnapshotHelper itself — Master-side procedures (RestoreSnapshotProcedure, CloneSnapshotProcedure) legitimately invoke that helper against the production hbase.rootdir, and tightening it would break those flows.

Approach

An earlier revision of this PR forked the entire RestoreSnapshotHelper (~830 lines) into the MapReduce module. Following review feedback (thanks @gvprathyusha6 and @sairampola), it has been reworked so that no security-critical core restore/clone logic is duplicated:

  1. Pluggable archiver, no forked restore logic. A new RestoreSnapshotArchiver interface (in hbase-server) abstracts the only operations that differ between the server-side and MapReduce restore paths — region and family archiving. RestoreSnapshotHelper now routes its two archive call sites through an injectable RestoreSnapshotArchiver, defaulting to one backed by HFileArchiver. Master-side RestoreSnapshotProcedure / CloneSnapshotProcedure keep using the default and are behaviorally unchanged.

  2. Thin MapReduce wrapper. MapreduceRestoreSnapshotHelper is now a small wrapper with no copied logic. It performs the MR-specific safety guard and then delegates to RestoreSnapshotHelper.copySnapshotForScanner(..., archiver), injecting the MR-local MapreduceHFileArchiver.

  3. Tighter path validation. The guard rejects both the sub-directory case and the previously-uncovered exact-match case (restoreDir == rootDir), logs at ERROR for ops visibility, then throws IllegalArgumentException with a clear data-loss message and remediation — before any filesystem work:

String rootPath = rootDir.toUri().getPath();
String restorePath = restoreDir.toUri().getPath();
if (restorePath.equals(rootPath) || restorePath.startsWith(rootPath + "/")) {
  String message = "BLOCKED: MapReduce restore directory cannot be the HBase root directory "
    + "or a sub directory of it. This could lead to accidental archival and permanent "
    + "data loss if the path falls under " + rootDir + "/data/. Use a temporary directory "
    + "outside of hbase.rootdir for MR snapshot scanning. RootDir: " + rootDir
    + ", restoreDir: " + restoreDir;
  LOG.error(message);
  throw new IllegalArgumentException(message);
}
  1. All in-module call sites migrated to the MR-local helper: TableSnapshotInputFormatImpl.setInput(), MultiTableSnapshotInputFormatImpl, and VerifyReplication.restoreSnapshotForPeerCluster(). CompactionTool is intentionally left untouched — it operates on production data by design — and now carries a comment documenting that it is exempt from the HBASE-29435 guard.

Note: every shipped caller restores into a fresh, unique (UUID) directory, so the operation always takes the non-destructive clone path. The guard is a fail-fast safety net for misconfigured or custom callers that pass a pre-populated / production directory.

Impact After Fix

  • Misconfigured restoreDir (whether equal to hbase.rootdir or any sub-path of it) immediately throws IllegalArgumentException with a clear, HBASE-29435-tagged error message.
  • The job fails fast before any files are created or modified in production directories — zero risk of accidental archival or data loss from MR snapshot scanning paths.
  • LOG.error(message) provides clear ops visibility — blocked attempts surface in MR job logs without requiring stack-trace parsing.
  • No impact on legitimate operations: Master's RestoreSnapshotProcedure and CloneSnapshotProcedure continue to use the original RestoreSnapshotHelper and remain unchanged.
  • No performance overhead — two string comparisons at job-setup time.
  • No core restore/clone logic is duplicated; the ~1,400-line fork is avoided. Master-side restore/clone procedures use the unchanged default archiver.
Entry Point Coverage
TableSnapshotInputFormatImpl.setInput() Single-table snapshot scans, mapred API wrappers, ClientSideRegionScanner
MultiTableSnapshotInputFormatImpl.setInput() Multi-table snapshot scans
VerifyReplication.restoreSnapshotForPeerCluster() Replication verification with peer snapshots

Files Changed

File Change
snapshot/RestoreSnapshotArchiver.java (new, hbase-server) Pluggable archiver strategy; DEFAULT backed by HFileArchiver
snapshot/RestoreSnapshotHelper.java (hbase-server) Routes archiving through the injectable archiver; adds a copySnapshotForScanner(..., archiver) overload and an archiver-accepting constructor
util/MapreduceHFileArchiver.java (hbase-mapreduce) MR-local archiver; now implements RestoreSnapshotArchiver; added ASF license header + @InterfaceAudience.Private; removed dead archiveExecutor field
mapreduce/MapreduceRestoreSnapshotHelper.java (hbase-mapreduce) Rewritten as a thin guard + delegate (no copied core logic)
mapreduce/TableSnapshotInputFormatImpl.java Routes restore through the MR helper
mapreduce/MultiTableSnapshotInputFormatImpl.java Routes restore through the MR helper
mapreduce/replication/VerifyReplication.java Routes peer-cluster restore through the MR helper
regionserver/CompactionTool.java Comment documenting its intentional exemption from the guard
snapshot/TestRestoreSnapshotArchiver.java (new test) Verifies the injected archiver is used on region removal and not used on the fresh clone path; covers the default archiver
mapreduce/TestMapreduceRestoreSnapshotHelper.java (test) Adds guard throw-path tests (exact-match + sub-dir) plus clone/TTL delegation; removed unused imports/helpers
util/TestMapreduceHFileArchiver.java (new test) FS-level tests confirming the fork implements the interface and archives region/family correctly

Testing

All passing (run via mvn test on hbase-server and hbase-mapreduce):

Test Result
TestRestoreSnapshotArchiver 4/4
TestRestoreSnapshotHelper 6/6
TestMobRestoreSnapshotHelper 6/6
TestMapreduceRestoreSnapshotHelper 4/4
TestMapreduceHFileArchiver 4/4

JaCoCo coverage on the new/changed code: RestoreSnapshotArchiver 100% line/method; MapreduceRestoreSnapshotHelper 100% line/branch/method; MapreduceHFileArchiver 97% method coverage (happy-path archiving fully covered); the RestoreSnapshotHelper paths touched by this change are exercised.

SwaraliJoshi avatar May 18 '26 06:05 SwaraliJoshi