-
Notifications
You must be signed in to change notification settings - Fork 3.4k
HBASE-30049 RestoreSnapshotHelper creates StoreFileTracker with wrong… #8013
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
Open
bjomobo
wants to merge
1
commit into
apache:master
Choose a base branch
from
bjomobo:HBASE-30049-fix-restore-sft
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
271 changes: 271 additions & 0 deletions
271
...va/org/apache/hadoop/hbase/master/procedure/TestRestoreSnapshotProcedureFileBasedSFT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hbase.master.procedure; | ||
|
|
||
| import static org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory.TRACKER_IMPL; | ||
| import static org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory.Trackers.FILE; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileStatus; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
| import org.apache.hadoop.hbase.HBaseTestingUtil; | ||
| import org.apache.hadoop.hbase.TableName; | ||
| import org.apache.hadoop.hbase.client.Admin; | ||
| import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; | ||
| import org.apache.hadoop.hbase.client.Put; | ||
| import org.apache.hadoop.hbase.client.RegionInfo; | ||
| import org.apache.hadoop.hbase.client.Table; | ||
| import org.apache.hadoop.hbase.client.TableDescriptor; | ||
| import org.apache.hadoop.hbase.client.TableDescriptorBuilder; | ||
| import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils; | ||
| import org.apache.hadoop.hbase.testclassification.LargeTests; | ||
| import org.apache.hadoop.hbase.testclassification.MasterTests; | ||
| import org.apache.hadoop.hbase.util.Bytes; | ||
| import org.apache.hadoop.hbase.util.CommonFSUtils; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Integration test for RestoreSnapshotProcedure with FileBasedStoreFileTracker. Verifies the | ||
| * end-to-end restore snapshot flow works correctly when the FILE-based StoreFileTracker is in use. | ||
| * Uses HBaseTestingUtil to start an in-process mini HBase cluster to exercise the restoreRegion() | ||
| * code path in RestoreSnapshotHelper. | ||
| */ | ||
| @Category({ MasterTests.class, LargeTests.class }) | ||
| public class TestRestoreSnapshotProcedureFileBasedSFT { | ||
|
|
||
| @ClassRule | ||
| public static final HBaseClassTestRule CLASS_RULE = | ||
| HBaseClassTestRule.forClass(TestRestoreSnapshotProcedureFileBasedSFT.class); | ||
|
|
||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(TestRestoreSnapshotProcedureFileBasedSFT.class); | ||
|
|
||
| private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); | ||
| private static final byte[] CF = Bytes.toBytes("cf"); | ||
|
|
||
| /** The .filelist directory name used by FileBasedStoreFileTracker. */ | ||
| private static final String FILELIST_DIR = ".filelist"; | ||
|
|
||
| @BeforeClass | ||
| public static void setupCluster() throws Exception { | ||
| Configuration conf = UTIL.getConfiguration(); | ||
| // Do NOT set FILE tracker globally — the bug only manifests when | ||
| // global is Default and table-level config is FILE. | ||
| conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1); | ||
| UTIL.startMiniCluster(3); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDownCluster() throws Exception { | ||
| UTIL.shutdownMiniCluster(); | ||
| } | ||
|
|
||
| /** | ||
| * Test that reproduces the scenario where restore fails with FileNotFoundException: 1. Create | ||
| * table with FILE tracker, load data, snapshot 2. Load more data + flush (creates new HFiles not | ||
| * in snapshot) 3. Restore from snapshot 4. Verify all regions open and data matches the snapshot | ||
| * Before the fix, step 4 would fail with FileNotFoundException because the .filelist still | ||
| * referenced the post-snapshot HFiles that were archived during restore. | ||
| */ | ||
| @Test | ||
| public void testRestoreSnapshotWithFileTrackerAfterDataChange() throws Exception { | ||
| Admin admin = UTIL.getAdmin(); | ||
| TableName tableName = TableName.valueOf("testRestoreWithFileTracker"); | ||
| String snapshotName = "snapshot-before-change"; | ||
|
|
||
| try { | ||
| // Step 1: Create table, load initial data, take snapshot | ||
| TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName) | ||
| .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).setValue(TRACKER_IMPL, FILE.name()) | ||
| .build(); | ||
| UTIL.getAdmin().createTable(htd); | ||
| SnapshotTestingUtils.loadData(UTIL, tableName, 500, CF); | ||
|
|
||
| int snapshotRowCount; | ||
| try (Table table = UTIL.getConnection().getTable(tableName)) { | ||
| snapshotRowCount = UTIL.countRows(table); | ||
| } | ||
| LOG.info("Snapshot row count: {}", snapshotRowCount); | ||
| assertTrue("Should have loaded data", snapshotRowCount > 0); | ||
|
|
||
| admin.disableTable(tableName); | ||
| admin.snapshot(snapshotName, tableName); | ||
| admin.enableTable(tableName); | ||
|
|
||
| // Step 2: Load more data and flush to create new HFiles. | ||
| // These new HFiles will NOT be in the snapshot. | ||
| SnapshotTestingUtils.loadData(UTIL, tableName, 500, CF); | ||
| admin.flush(tableName); | ||
|
|
||
| int postFlushRowCount; | ||
| try (Table table = UTIL.getConnection().getTable(tableName)) { | ||
| postFlushRowCount = UTIL.countRows(table); | ||
| } | ||
| LOG.info("Post-flush row count: {} (snapshot had {})", postFlushRowCount, snapshotRowCount); | ||
| assertTrue("Should have more rows after loading more data", | ||
| postFlushRowCount > snapshotRowCount); | ||
|
|
||
| // Step 3: Disable and restore from the earlier snapshot. | ||
| // restoreRegion() must update .filelist to point to the snapshot's HFiles. | ||
| admin.disableTable(tableName); | ||
| admin.restoreSnapshot(snapshotName); | ||
|
|
||
| // Step 4: Enable table, triggers region opens. | ||
| admin.enableTable(tableName); | ||
|
|
||
| // Verify all regions are online | ||
| List<RegionInfo> regions = admin.getRegions(tableName); | ||
| LOG.info("Number of regions after restore: {}", regions.size()); | ||
| assertTrue("Table should have at least one region", regions.size() > 0); | ||
|
|
||
| // Verify data matches the snapshot (not the post-flush state) | ||
| SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshotRowCount); | ||
| LOG.info("Data verification passed: row count matches snapshot ({})", snapshotRowCount); | ||
|
|
||
| // Verify .filelist files exist on disk for each region | ||
| verifyFileListExists(tableName); | ||
|
|
||
| } finally { | ||
| if (admin.tableExists(tableName)) { | ||
| if (!admin.isTableDisabled(tableName)) { | ||
| admin.disableTable(tableName); | ||
| } | ||
| admin.deleteTable(tableName); | ||
| } | ||
| SnapshotTestingUtils.deleteAllSnapshots(admin); | ||
| SnapshotTestingUtils.deleteArchiveDirectory(UTIL); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test restore after compaction, compaction creates new HFiles that replace the originals. After | ||
| * restore, the .filelist must point to the snapshot's HFiles, not the compaction output. | ||
| */ | ||
| @Test | ||
| public void testRestoreSnapshotAfterCompaction() throws Exception { | ||
|
bjomobo marked this conversation as resolved.
|
||
| Admin admin = UTIL.getAdmin(); | ||
| TableName tableName = TableName.valueOf("testRestoreAfterCompaction"); | ||
| String snapshotName = "snapshot-before-compaction"; | ||
|
|
||
| try { | ||
| // Create table with multiple regions | ||
| TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName) | ||
| .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).setValue(TRACKER_IMPL, FILE.name()) | ||
| .build(); | ||
| UTIL.getAdmin().createTable(htd, Bytes.toBytes("row00000"), Bytes.toBytes("row99999"), 4); | ||
|
|
||
| // Load data across regions | ||
| try (Table table = UTIL.getConnection().getTable(tableName)) { | ||
| for (int i = 0; i < 200; i++) { | ||
| Put put = new Put(Bytes.toBytes(String.format("row%05d", i))); | ||
| put.addColumn(CF, Bytes.toBytes("q"), Bytes.toBytes("val" + i)); | ||
| table.put(put); | ||
| } | ||
| } | ||
| admin.flush(tableName); | ||
|
|
||
| int snapshotRowCount; | ||
| try (Table table = UTIL.getConnection().getTable(tableName)) { | ||
| snapshotRowCount = UTIL.countRows(table); | ||
| } | ||
|
|
||
| // Take snapshot | ||
| admin.disableTable(tableName); | ||
| admin.snapshot(snapshotName, tableName); | ||
| admin.enableTable(tableName); | ||
|
|
||
| // Load more data and trigger compaction, creates new HFiles | ||
| SnapshotTestingUtils.loadData(UTIL, tableName, 200, CF); | ||
| admin.flush(tableName); | ||
| admin.majorCompact(tableName); | ||
| // Wait for compaction to complete | ||
| Thread.sleep(5000); | ||
|
|
||
| // Restore from pre-compaction snapshot | ||
| admin.disableTable(tableName); | ||
| admin.restoreSnapshot(snapshotName); | ||
| admin.enableTable(tableName); | ||
|
|
||
| // Verify regions open and data is correct | ||
| SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshotRowCount); | ||
| verifyFileListExists(tableName); | ||
| LOG.info("Restore after compaction verified successfully"); | ||
|
|
||
| } finally { | ||
| if (admin.tableExists(tableName)) { | ||
| if (!admin.isTableDisabled(tableName)) { | ||
| admin.disableTable(tableName); | ||
| } | ||
| admin.deleteTable(tableName); | ||
| } | ||
| SnapshotTestingUtils.deleteAllSnapshots(admin); | ||
| SnapshotTestingUtils.deleteArchiveDirectory(UTIL); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verify that .filelist files exist for each region/family of the table. This confirms that | ||
| * FileBasedStoreFileTracker.doSetStoreFiles() was called (not the no-op | ||
| * DefaultStoreFileTracker.doSetStoreFiles()). | ||
| */ | ||
| private void verifyFileListExists(TableName tableName) throws IOException { | ||
| Configuration conf = UTIL.getConfiguration(); | ||
| FileSystem fs = FileSystem.get(conf); | ||
| Path rootDir = CommonFSUtils.getRootDir(conf); | ||
| Path tableDir = CommonFSUtils.getTableDir(rootDir, tableName); | ||
|
|
||
| int regionsWithFilelist = 0; | ||
| List<RegionInfo> regions = UTIL.getAdmin().getRegions(tableName); | ||
| for (RegionInfo ri : regions) { | ||
| Path regionDir = new Path(tableDir, ri.getEncodedName()); | ||
| Path familyDir = new Path(regionDir, Bytes.toString(CF)); | ||
| Path fileListDir = new Path(familyDir, FILELIST_DIR); | ||
|
|
||
| if (fs.exists(familyDir)) { | ||
| // Only check .filelist if the family directory has store files (non-directory entries | ||
| // excluding .filelist itself). Empty regions may not have .filelist written. | ||
| FileStatus[] storeFiles = | ||
| fs.listStatus(familyDir, path -> !path.getName().equals(FILELIST_DIR)); | ||
| if (storeFiles != null && storeFiles.length > 0) { | ||
| assertTrue("Expected .filelist directory for region " + ri.getEncodedName(), | ||
| fs.exists(fileListDir)); | ||
| FileStatus[] files = fs.listStatus(fileListDir); | ||
| assertTrue("Expected .filelist files for region " + ri.getEncodedName(), | ||
| files != null && files.length > 0); | ||
| regionsWithFilelist++; | ||
| LOG.info("Region {} has {} .filelist files", ri.getEncodedName(), files.length); | ||
| } else { | ||
| LOG.info("Region {} has no store files in family dir, skipping .filelist check", | ||
| ri.getEncodedName()); | ||
| } | ||
| } | ||
| } | ||
| assertTrue("Expected at least one region with .filelist", regionsWithFilelist > 0); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this test and TestRestoreSnapshotFileTrackerTableLevel.java have some overlapping test scenarios, can you check once, or am I missing something here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestRestoreSnapshotProcedureFileBasedSFTtests the end-to-end procedure path (single region, simple data change + compaction scenarios), whileTestRestoreSnapshotFileTrackerTableLeveluses pre-split regions (4 regions), proper compaction wait viaUTIL.waitFor(), and covers the multi-family restore path (add/remove column families). Happy to consolidate if you feel strongly, but I kept them separate since they exercise slightly different things