HBASE-26956 ExportSnapshot tool supports removing TTL (#4538)

Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
XinSun 2022-06-21 09:19:08 +08:00 committed by Ddupg
parent b9d6d370f2
commit 135348192c
4 changed files with 74 additions and 12 deletions

View File

@ -152,6 +152,8 @@ public class ExportSnapshot extends AbstractHBaseTool implements Tool {
"Number of mappers to use during the copy (mapreduce.job.maps)."); "Number of mappers to use during the copy (mapreduce.job.maps).");
static final Option BANDWIDTH = static final Option BANDWIDTH =
new Option(null, "bandwidth", true, "Limit bandwidth to this value in MB/second."); new Option(null, "bandwidth", true, "Limit bandwidth to this value in MB/second.");
static final Option RESET_TTL =
new Option(null, "reset-ttl", false, "Do not copy TTL for the snapshot");
} }
// Export Map-Reduce Counters, to keep track of the progress // Export Map-Reduce Counters, to keep track of the progress
@ -917,6 +919,7 @@ public class ExportSnapshot extends AbstractHBaseTool implements Tool {
private int bandwidthMB = Integer.MAX_VALUE; private int bandwidthMB = Integer.MAX_VALUE;
private int filesMode = 0; private int filesMode = 0;
private int mappers = 0; private int mappers = 0;
private boolean resetTtl = false;
@Override @Override
protected void processOptions(CommandLine cmd) { protected void processOptions(CommandLine cmd) {
@ -938,6 +941,7 @@ public class ExportSnapshot extends AbstractHBaseTool implements Tool {
verifyChecksum = !cmd.hasOption(Options.NO_CHECKSUM_VERIFY.getLongOpt()); verifyChecksum = !cmd.hasOption(Options.NO_CHECKSUM_VERIFY.getLongOpt());
verifyTarget = !cmd.hasOption(Options.NO_TARGET_VERIFY.getLongOpt()); verifyTarget = !cmd.hasOption(Options.NO_TARGET_VERIFY.getLongOpt());
verifySource = !cmd.hasOption(Options.NO_SOURCE_VERIFY.getLongOpt()); verifySource = !cmd.hasOption(Options.NO_SOURCE_VERIFY.getLongOpt());
resetTtl = cmd.hasOption(Options.RESET_TTL.getLongOpt());
} }
/** /**
@ -1075,11 +1079,19 @@ public class ExportSnapshot extends AbstractHBaseTool implements Tool {
} }
} }
// Write a new .snapshotinfo if the target name is different from the source name // Write a new .snapshotinfo if the target name is different from the source name or we want to
if (!targetName.equals(snapshotName)) { // reset TTL for target snapshot.
SnapshotDescription snapshotDesc = SnapshotDescriptionUtils if (!targetName.equals(snapshotName) || resetTtl) {
.readSnapshotInfo(inputFs, snapshotDir).toBuilder().setName(targetName).build(); SnapshotDescription.Builder snapshotDescBuilder =
SnapshotDescriptionUtils.writeSnapshotInfo(snapshotDesc, initialOutputSnapshotDir, outputFs); SnapshotDescriptionUtils.readSnapshotInfo(inputFs, snapshotDir).toBuilder();
if (!targetName.equals(snapshotName)) {
snapshotDescBuilder.setName(targetName);
}
if (resetTtl) {
snapshotDescBuilder.setTtl(HConstants.DEFAULT_SNAPSHOT_TTL);
}
SnapshotDescriptionUtils.writeSnapshotInfo(snapshotDescBuilder.build(),
initialOutputSnapshotDir, outputFs);
if (filesUser != null || filesGroup != null) { if (filesUser != null || filesGroup != null) {
outputFs.setOwner( outputFs.setOwner(
new Path(initialOutputSnapshotDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE), filesUser, new Path(initialOutputSnapshotDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE), filesUser,
@ -1155,6 +1167,7 @@ public class ExportSnapshot extends AbstractHBaseTool implements Tool {
addOption(Options.CHMOD); addOption(Options.CHMOD);
addOption(Options.MAPPERS); addOption(Options.MAPPERS);
addOption(Options.BANDWIDTH); addOption(Options.BANDWIDTH);
addOption(Options.RESET_TTL);
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -24,9 +24,12 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileStatus;
@ -188,6 +191,38 @@ public class TestExportSnapshot {
testExportFileSystemState(tableName, snapshotName, targetName, tableNumFiles); testExportFileSystemState(tableName, snapshotName, targetName, tableNumFiles);
} }
@Test
public void testExportWithResetTtl() throws Exception {
String name = "testExportWithResetTtl";
TableName tableName = TableName.valueOf(name);
String snapshotNameStr = "snaptb-" + name;
byte[] snapshotName = Bytes.toBytes(snapshotNameStr);
Long ttl = 100000L;
try {
// create Table
createTable(tableName);
SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 50, FAMILY);
int tableNumFiles = admin.getRegions(tableName).size();
// take a snapshot with TTL
Map<String, Object> props = new HashMap<>();
props.put("TTL", ttl);
admin.snapshot(snapshotNameStr, tableName, props);
Optional<Long> ttlOpt =
admin.listSnapshots().stream().filter(s -> s.getName().equals(snapshotNameStr))
.map(org.apache.hadoop.hbase.client.SnapshotDescription::getTtl).findAny();
assertTrue(ttlOpt.isPresent());
assertEquals(ttl, ttlOpt.get());
testExportFileSystemState(tableName, snapshotName, snapshotName, tableNumFiles,
getHdfsDestinationDir(), false, true);
} catch (Exception e) {
throw e;
} finally {
TEST_UTIL.deleteTable(tableName);
}
}
private void testExportFileSystemState(final TableName tableName, final byte[] snapshotName, private void testExportFileSystemState(final TableName tableName, final byte[] snapshotName,
final byte[] targetName, int filesExpected) throws Exception { final byte[] targetName, int filesExpected) throws Exception {
testExportFileSystemState(tableName, snapshotName, targetName, filesExpected, testExportFileSystemState(tableName, snapshotName, targetName, filesExpected,
@ -196,8 +231,15 @@ public class TestExportSnapshot {
protected void testExportFileSystemState(final TableName tableName, final byte[] snapshotName, protected void testExportFileSystemState(final TableName tableName, final byte[] snapshotName,
final byte[] targetName, int filesExpected, Path copyDir, boolean overwrite) throws Exception { final byte[] targetName, int filesExpected, Path copyDir, boolean overwrite) throws Exception {
testExportFileSystemState(tableName, snapshotName, targetName, filesExpected, copyDir,
overwrite, false);
}
protected void testExportFileSystemState(final TableName tableName, final byte[] snapshotName,
final byte[] targetName, int filesExpected, Path copyDir, boolean overwrite, boolean resetTtl)
throws Exception {
testExportFileSystemState(TEST_UTIL.getConfiguration(), tableName, snapshotName, targetName, testExportFileSystemState(TEST_UTIL.getConfiguration(), tableName, snapshotName, targetName,
filesExpected, TEST_UTIL.getDefaultRootDirPath(), copyDir, overwrite, filesExpected, TEST_UTIL.getDefaultRootDirPath(), copyDir, overwrite, resetTtl,
getBypassRegionPredicate(), true); getBypassRegionPredicate(), true);
} }
@ -207,7 +249,8 @@ public class TestExportSnapshot {
protected static void testExportFileSystemState(final Configuration conf, protected static void testExportFileSystemState(final Configuration conf,
final TableName tableName, final byte[] snapshotName, final byte[] targetName, final TableName tableName, final byte[] snapshotName, final byte[] targetName,
final int filesExpected, final Path srcDir, Path rawTgtDir, final boolean overwrite, final int filesExpected, final Path srcDir, Path rawTgtDir, final boolean overwrite,
final RegionPredicate bypassregionPredicate, boolean success) throws Exception { final boolean resetTtl, final RegionPredicate bypassregionPredicate, boolean success)
throws Exception {
FileSystem tgtFs = rawTgtDir.getFileSystem(conf); FileSystem tgtFs = rawTgtDir.getFileSystem(conf);
FileSystem srcFs = srcDir.getFileSystem(conf); FileSystem srcFs = srcDir.getFileSystem(conf);
Path tgtDir = rawTgtDir.makeQualified(tgtFs.getUri(), tgtFs.getWorkingDirectory()); Path tgtDir = rawTgtDir.makeQualified(tgtFs.getUri(), tgtFs.getWorkingDirectory());
@ -225,6 +268,9 @@ public class TestExportSnapshot {
if (overwrite) { if (overwrite) {
opts.add("--overwrite"); opts.add("--overwrite");
} }
if (resetTtl) {
opts.add("--reset-ttl");
}
// Export Snapshot // Export Snapshot
int res = run(conf, new ExportSnapshot(), opts.toArray(new String[opts.size()])); int res = run(conf, new ExportSnapshot(), opts.toArray(new String[opts.size()]));
@ -253,7 +299,7 @@ public class TestExportSnapshot {
final Path targetDir = new Path(HConstants.SNAPSHOT_DIR_NAME, Bytes.toString(targetName)); final Path targetDir = new Path(HConstants.SNAPSHOT_DIR_NAME, Bytes.toString(targetName));
verifySnapshotDir(srcFs, new Path(srcDir, snapshotDir), tgtFs, new Path(tgtDir, targetDir)); verifySnapshotDir(srcFs, new Path(srcDir, snapshotDir), tgtFs, new Path(tgtDir, targetDir));
Set<String> snapshotFiles = verifySnapshot(conf, tgtFs, tgtDir, tableName, Set<String> snapshotFiles = verifySnapshot(conf, tgtFs, tgtDir, tableName,
Bytes.toString(targetName), bypassregionPredicate); Bytes.toString(targetName), resetTtl, bypassregionPredicate);
assertEquals(filesExpected, snapshotFiles.size()); assertEquals(filesExpected, snapshotFiles.size());
} }
@ -270,7 +316,7 @@ public class TestExportSnapshot {
*/ */
protected static Set<String> verifySnapshot(final Configuration conf, final FileSystem fs, protected static Set<String> verifySnapshot(final Configuration conf, final FileSystem fs,
final Path rootDir, final TableName tableName, final String snapshotName, final Path rootDir, final TableName tableName, final String snapshotName,
final RegionPredicate bypassregionPredicate) throws IOException { final boolean resetTtl, final RegionPredicate bypassregionPredicate) throws IOException {
final Path exportedSnapshot = final Path exportedSnapshot =
new Path(rootDir, new Path(HConstants.SNAPSHOT_DIR_NAME, snapshotName)); new Path(rootDir, new Path(HConstants.SNAPSHOT_DIR_NAME, snapshotName));
final Set<String> snapshotFiles = new HashSet<>(); final Set<String> snapshotFiles = new HashSet<>();
@ -303,6 +349,9 @@ public class TestExportSnapshot {
SnapshotDescription desc = SnapshotDescriptionUtils.readSnapshotInfo(fs, exportedSnapshot); SnapshotDescription desc = SnapshotDescriptionUtils.readSnapshotInfo(fs, exportedSnapshot);
assertTrue(desc.getName().equals(snapshotName)); assertTrue(desc.getName().equals(snapshotName));
assertTrue(desc.getTable().equals(tableName.getNameAsString())); assertTrue(desc.getTable().equals(tableName.getNameAsString()));
if (resetTtl) {
assertEquals(HConstants.DEFAULT_SNAPSHOT_TTL, desc.getTtl());
}
return snapshotFiles; return snapshotFiles;
} }

View File

@ -153,7 +153,7 @@ public class TestExportSnapshotAdjunct {
conf.setInt("mapreduce.map.maxattempts", 3); conf.setInt("mapreduce.map.maxattempts", 3);
TestExportSnapshot.testExportFileSystemState(conf, tableName, Bytes.toBytes(snapshotName), TestExportSnapshot.testExportFileSystemState(conf, tableName, Bytes.toBytes(snapshotName),
Bytes.toBytes(snapshotName), tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true, Bytes.toBytes(snapshotName), tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true,
null, true); false, null, true);
} }
/** /**
@ -170,6 +170,6 @@ public class TestExportSnapshotAdjunct {
conf.setInt("mapreduce.map.maxattempts", 3); conf.setInt("mapreduce.map.maxattempts", 3);
TestExportSnapshot.testExportFileSystemState(conf, tableName, Bytes.toBytes(snapshotName), TestExportSnapshot.testExportFileSystemState(conf, tableName, Bytes.toBytes(snapshotName),
Bytes.toBytes(snapshotName), tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true, Bytes.toBytes(snapshotName), tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true,
null, false); false, null, false);
} }
} }

View File

@ -109,7 +109,7 @@ public class TestExportSnapshotV1NoCluster {
TableName tableName = builder.getTableDescriptor().getTableName(); TableName tableName = builder.getTableDescriptor().getTableName();
TestExportSnapshot.testExportFileSystemState(testUtil.getConfiguration(), tableName, TestExportSnapshot.testExportFileSystemState(testUtil.getConfiguration(), tableName,
snapshotName, snapshotName, snapshotFilesCount, testDir, snapshotName, snapshotName, snapshotFilesCount, testDir,
getDestinationDir(fs, testUtil, testDir), false, null, true); getDestinationDir(fs, testUtil, testDir), false, false, null, true);
} }
static Path getDestinationDir(FileSystem fs, HBaseCommonTestingUtility hctu, Path testDir) static Path getDestinationDir(FileSystem fs, HBaseCommonTestingUtility hctu, Path testDir)