Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
@InterfaceAudience.Private
public final class HBackupFileSystem {
public static final Logger LOG = LoggerFactory.getLogger(HBackupFileSystem.class);
public static final String BACKUP_COMPLETE_FILE_NAME = ".backup.complete";

/**
* This is utility class.
Expand Down Expand Up @@ -93,6 +94,72 @@ public static Path getBackupPath(String backupRootDir, String backupId) {
return new Path(backupRootDir + Path.SEPARATOR + backupId);
}

/**
* Write a completion marker file to signal a backup has completed successfully. This is a
* best-effort operation: if the write fails, the backup itself is still considered complete (the
* system table is the source of truth). The marker file is intended for external consumers (e.g.,
* another cluster) that cannot access the originating cluster's BackupSystemTable. A backup can
* still be complete even if the completion marker file is missing, but there is no way to verify
* this without using the originating cluster's BackupSystemTable.
* <p>
* The marker is always written last and deleted first: any operation that removes or modifies
* data in a backup directory (failed-backup cleanup, backup delete, merge, removing a table from
* a backup image when a dependent backup is deleted) deletes the marker before touching the data.
* A backup whose image was modified this way may therefore remain COMPLETE in the system table
* without a marker.
* @param conf configuration
* @param backupRootDir backup root directory
* @param backupId backup id
*/
public static void writeBackupCompleteMarker(Configuration conf, String backupRootDir,
String backupId) {
Path backupDir = getBackupPath(backupRootDir, backupId);
Path markerPath = new Path(backupDir, BACKUP_COMPLETE_FILE_NAME);
try {
FileSystem fs = markerPath.getFileSystem(conf);
fs.create(markerPath, true).close();
LOG.info("Backup completion marker file written to: {}", markerPath);
} catch (IOException | RuntimeException e) {
LOG.warn("Failed to write {} marker file to backup directory: {}", BACKUP_COMPLETE_FILE_NAME,
backupDir, e);
}
}

/**
* Delete the completion marker file for the given backup, if it exists. Callers must invoke this
* before removing or modifying any data in the backup directory, so that the marker is never
* present on a backup whose data is incomplete.
* @param conf configuration
* @param backupRootDir backup root directory
* @param backupId backup id
* @throws IOException if the file system cannot be accessed or the marker could not be deleted
*/
public static void deleteBackupCompleteMarker(Configuration conf, String backupRootDir,
String backupId) throws IOException {
Path markerPath = new Path(getBackupPath(backupRootDir, backupId), BACKUP_COMPLETE_FILE_NAME);
FileSystem fs = markerPath.getFileSystem(conf);
if (fs.delete(markerPath, false)) {
LOG.debug("Removed backup completion marker: {}", markerPath);
} else if (fs.exists(markerPath)) {
throw new IOException("Unable to delete backup completion marker: " + markerPath);
}
}

/**
* Check whether a backup completion marker file exists for the given backup.
* @param conf configuration
* @param backupRootDir backup root directory
* @param backupId backup id
* @return true if the marker file exists
* @throws IOException if the file system cannot be accessed
*/
public static boolean isBackupComplete(Configuration conf, String backupRootDir, String backupId)
throws IOException {
Path markerPath = new Path(getBackupPath(backupRootDir, backupId), BACKUP_COMPLETE_FILE_NAME);
FileSystem fs = markerPath.getFileSystem(conf);
return fs.exists(markerPath);
}
Comment thread
kgeisz marked this conversation as resolved.
Resolved

/**
* Given the backup root dir, backup id and the table name, return the backup image location,
* which is also where the backup manifest file is. return value look like:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,17 @@ void removeTableFromBackupImage(BackupInfo info, TableName tn, BackupSystemTable
if (tables.isEmpty()) {
LOG.debug("Delete backup info " + info.getBackupId());

sysTable.deleteBackupInfo(info.getBackupId());
// Idempotent operation
// Clean up data before deleting the backup info, so a failure to remove the completion
// marker aborts before the metadata is gone. Idempotent operation.
BackupUtils.cleanupBackupData(info, conn.getConfiguration());
sysTable.deleteBackupInfo(info.getBackupId());
} else {
info.setTables(tables);
sysTable.updateBackupInfo(info);
// The backup directory no longer matches its manifest, so remove the completion marker
// before deleting the table's data. The backup stays COMPLETE in the system table.
HBackupFileSystem.deleteBackupCompleteMarker(conn.getConfiguration(),
info.getBackupRootDir(), info.getBackupId());
// Now, clean up directory for table (idempotent)
cleanupBackupDir(info, tn, conn.getConfiguration());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_WORKERS_DESC;
import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_YARN_QUEUE_NAME;
import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_YARN_QUEUE_NAME_DESC;
import static org.apache.hadoop.hbase.backup.HBackupFileSystem.writeBackupCompleteMarker;
import static org.apache.hadoop.hbase.backup.impl.BackupSystemTable.Order.NEW_TO_OLD;
import static org.apache.hadoop.hbase.backup.replication.ContinuousBackupReplicationEndpoint.ONE_DAY_IN_MILLISECONDS;
import static org.apache.hadoop.hbase.backup.util.BackupUtils.DATE_FORMAT;
Expand Down Expand Up @@ -1197,30 +1198,43 @@ public static void repairFailedBackupMergeIfAny(Connection conn, BackupSystemTab
return;
}
System.out.println("Found failed MERGE operation for: " + StringUtils.join(backupIds));
// Check if backup .tmp exists

BackupInfo bInfo = sysTable.readBackupInfo(backupIds[0]);
if (bInfo == null) {
throw new IOException(
"MERGE repair: failed due to null backup info for backup ID: " + backupIds[0]);
}

// Check if backup .tmp exists
String backupRoot = bInfo.getBackupRootDir();
FileSystem fs = FileSystem.get(new Path(backupRoot).toUri(), new Configuration());
String backupId = BackupUtils.findMostRecentBackupId(backupIds);
Path tmpPath = HBackupFileSystem.getBackupTmpDirPathForBackupId(backupRoot, backupId);
String mergedBackupId = BackupUtils.findMostRecentBackupId(backupIds);
Path tmpPath = HBackupFileSystem.getBackupTmpDirPathForBackupId(backupRoot, mergedBackupId);
Path destPath = HBackupFileSystem.getBackupPath(backupRoot, mergedBackupId);
if (fs.exists(tmpPath)) {
// Move data back
Path destPath = HBackupFileSystem.getBackupPath(backupRoot, backupId);
if (!fs.delete(destPath, true)) {
System.out.println("Failed to delete " + destPath);
}
boolean res = fs.rename(tmpPath, destPath);
if (!res) {
throw new IOException(
"MERGE repair: failed to rename from " + tmpPath + " to " + destPath);
"MERGE repair: failed to rename from " + tmpPath + " to " + destPath);
}
System.out
.println("MERGE repair: renamed from " + tmpPath + " to " + destPath + " res=" + res);
} else {
checkRemoveBackupImages(fs, backupRoot, backupIds);
checkRemoveBackupImages(conn.getConfiguration(), fs, backupRoot, backupIds);
}
// Restore table from snapshot
BackupSystemTable.restoreFromSnapshot(conn);

// The merged backup is now consistent. If the tmp directory was restored it already holds
// the original marker; otherwise the failed merge may not have written one. Writing is
// idempotent and best-effort. Write it before unlocking, so a concurrent delete or merge
// cannot run before the marker is written and leave a stale marker behind.
writeBackupCompleteMarker(conn.getConfiguration(), backupRoot, mergedBackupId);

// Unlock backup system
sysTable.finishBackupExclusiveOperation();
// Finish previous failed session
Expand All @@ -1229,15 +1243,18 @@ public static void repairFailedBackupMergeIfAny(Connection conn, BackupSystemTab
System.out.println("MERGE repair operation finished OK: " + StringUtils.join(backupIds));
}

private static void checkRemoveBackupImages(FileSystem fs, String backupRoot,
String[] backupIds) throws IOException {
private static void checkRemoveBackupImages(Configuration conf, FileSystem fs,
String backupRoot, String[] backupIds) throws IOException {
String mergedBackupId = BackupUtils.findMostRecentBackupId(backupIds);
for (String backupId : backupIds) {
if (backupId.equals(mergedBackupId)) {
continue;
}
Path path = HBackupFileSystem.getBackupPath(backupRoot, backupId);
if (fs.exists(path)) {
// Delete the marker first so a failed directory delete never leaves a merged-away
// backup looking complete
HBackupFileSystem.deleteBackupCompleteMarker(conf, backupRoot, backupId);
if (!fs.delete(path, true)) {
System.out.println("MERGE repair removing: " + path + " - FAILED");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ protected void failBackup(Connection conn, BackupInfo backupInfo, BackupManager

public static void cleanupAndRestoreBackupSystem(Connection conn, BackupInfo backupInfo,
Configuration conf) throws IOException {
// The .backup.complete marker should not exist for a failed backup, but remove it in case it
// does. This must happen before any destructive recovery step: restoring the backup system
// table from its snapshot removes the RUNNING backup record. If the marker cannot be removed,
// fail here while the RUNNING record, the system table snapshot, the backup data, and the
// exclusive lock are all still intact. Later, a "backup repair" can find and retry this backup.
HBackupFileSystem.deleteBackupCompleteMarker(conf, backupInfo.getBackupRootDir(),
backupInfo.getBackupId());

BackupType type = backupInfo.getType();
// if full backup, then delete HBase snapshots if there already are snapshots taken
// and also clean up export snapshot log files if exist
Expand Down Expand Up @@ -402,6 +410,11 @@ protected void completeBackup(final Connection conn, BackupInfo backupInfo, Back
BackupSystemTable.deleteSnapshot(conn);
backupManager.updateBackupInfo(backupInfo);

// Write the completion marker while still holding the exclusive lock, so a concurrent delete or
// merge cannot run before the marker is written and leave a stale marker behind
HBackupFileSystem.writeBackupCompleteMarker(conf, backupInfo.getBackupRootDir(),
backupInfo.getBackupId());

// Finish active session
backupManager.finishBackupSession();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void run(String[] backupIds) throws IOException {
// Update backup manifest
List<String> backupsToDelete = getBackupIdsToDelete(backupIds, mergedBackupId);
updateBackupManifest(tmpBackupDir.getParent().toString(), mergedBackupId, backupsToDelete);
// Copy meta files back from tmp to backup dir
// Copy meta files back from tmp to the backup dir
copyMetaData(fs, tmpBackupDir, backupDirPath);
// Delete tmp dir (Rename back during repair)
if (!fs.delete(tmpBackupDir, true)) {
Expand All @@ -180,6 +180,11 @@ public void run(String[] backupIds) throws IOException {
deleteBackupImages(backupsToDelete, conn, fs, backupRoot);
// Finish merge session
table.finishMergeOperation();
// Write the completion marker after the merge is finalized so external consumers never
// observe it on a partially-merged backup. The completion marker is written before the lock
// is released so a concurrent delete or merge cannot run before the marker is written and
// leave a stale marker behind.
HBackupFileSystem.writeBackupCompleteMarker(conf, backupRoot, mergedBackupId);
// Release lock
table.finishBackupExclusiveOperation();
} catch (RuntimeException e) {
Expand Down Expand Up @@ -208,9 +213,9 @@ public void run(String[] backupIds) throws IOException {
}

/**
* Copy meta data to of a backup session
* Copy metadata of a backup session
* @param fs file system
* @param tmpBackupDir temp backup directory, where meta is locaed
* @param tmpBackupDir temp backup directory, where meta is located
* @param backupDirPath new path for backup
* @throws IOException exception
*/
Expand All @@ -224,11 +229,15 @@ protected void copyMetaData(FileSystem fs, Path tmpBackupDir, Path backupDirPath
continue;
}
// Keep meta
String fileName = p.toString();
String fullPath = p.toString();
String baseName = p.getName();
// TABLEINFO_DIR is a directory that might have a name like ".tableinfo.0000000001.367", so
// we can't rely on an exact match. The other constants are files with those exact names, so
// an exact match is preferred.
if (
fileName.indexOf(FSTableDescriptors.TABLEINFO_DIR) > 0
|| fileName.indexOf(HRegionFileSystem.REGION_INFO_FILE) > 0
|| fileName.indexOf(BackupManifest.MANIFEST_FILE_NAME) > 0
fullPath.indexOf(FSTableDescriptors.TABLEINFO_DIR) > 0
|| HRegionFileSystem.REGION_INFO_FILE.equals(baseName)
|| BackupManifest.MANIFEST_FILE_NAME.equals(baseName)
) {
toKeep.add(p);
}
Expand Down Expand Up @@ -322,6 +331,12 @@ protected void updateBackupManifest(String backupRoot, String mergedBackupId,

protected void deleteBackupImages(List<String> backupIds, Connection conn, FileSystem fs,
String backupRoot) throws IOException {
// Delete completion markers first, so a merged-away backup never keeps a marker if its
// directory delete below fails
for (String backupId : backupIds) {
HBackupFileSystem.deleteBackupCompleteMarker(conf, backupRoot, backupId);
}

// Delete from backup system table
try (BackupSystemTable table = new BackupSystemTable(conn)) {
for (String backupId : backupIds) {
Expand All @@ -336,7 +351,7 @@ protected void deleteBackupImages(List<String> backupIds, Connection conn, FileS
Path backupDirPath = HBackupFileSystem.getBackupPath(backupRoot, backupId);

if (!fs.delete(backupDirPath, true)) {
LOG.warn("Could not delete " + backupDirPath);
LOG.warn("Could not delete {}", backupDirPath);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,28 @@ private static void cleanupHLogDir(BackupInfo backupInfo, Configuration conf) th
}
}

private static void cleanupTargetDir(BackupInfo backupInfo, Configuration conf) {
try {
// clean up the data at target directory
LOG.debug("Trying to cleanup up target dir : " + backupInfo.getBackupId());
String targetDir = backupInfo.getBackupRootDir();
if (targetDir == null) {
LOG.warn("No target directory specified for " + backupInfo.getBackupId());
return;
}
/**
* Delete the data of a backup from its target directory.
* @throws IOException if the backup completion marker could not be removed. In that case no data
* is deleted and the caller must not remove the backup's metadata.
*/
private static void cleanupTargetDir(BackupInfo backupInfo, Configuration conf)
throws IOException {
// clean up the data at target directory
LOG.debug("Trying to cleanup up target dir : " + backupInfo.getBackupId());
String targetDir = backupInfo.getBackupRootDir();
if (targetDir == null) {
LOG.warn("No target directory specified for " + backupInfo.getBackupId());
return;
}

// Delete the completion marker before any data so that a partial delete never leaves a marker
// on a backup with missing tables. If this fails, propagate the error before any data or
// metadata is removed: the marker, data, and system table record all remain, which is
// consistent, and the delete operation can be retried.
HBackupFileSystem.deleteBackupCompleteMarker(conf, targetDir, backupInfo.getBackupId());

try {
FileSystem outputFs = FileSystem.get(new Path(backupInfo.getBackupRootDir()).toUri(), conf);

for (TableName table : backupInfo.getTables()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FilterFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseTestingUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
Expand All @@ -31,6 +34,7 @@
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.replication.ReplicationPeerDescription;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.yetus.audience.InterfaceAudience;

@InterfaceAudience.Private
Expand Down Expand Up @@ -69,4 +73,33 @@ public static void verifyReplicationPeerSubscription(HBaseTestingUtil util, Tabl
"Table should be subscribed to the replication peer");
}
}

/**
* Returns a copy of the given configuration whose HDFS file system implementation is
* {@link MarkerDeletionFailHDFS}, with file system caching disabled so the override takes effect.
*/
static Configuration createMarkerDeletionFailConf(Configuration base) {
Configuration conf = new Configuration(base);
conf.setClass("fs.hdfs.impl", MarkerDeletionFailHDFS.class, FileSystem.class);
conf.setBoolean("fs.hdfs.impl.disable.cache", true);
return conf;
}

/**
* A {@link DistributedFileSystem} wrapper that refuses to delete the backup completion marker
* file ({@link HBackupFileSystem#BACKUP_COMPLETE_FILE_NAME}).
*/
public static class MarkerDeletionFailHDFS extends FilterFileSystem {
public MarkerDeletionFailHDFS() {
super(new DistributedFileSystem());
}

@Override
public boolean delete(Path f, boolean recursive) throws IOException {
if (f.getName().equals(HBackupFileSystem.BACKUP_COMPLETE_FILE_NAME)) {
return false;
}
return super.delete(f, recursive);
}
}
}
Loading