Skip to content

Commit 78080c3

Browse files
author
Ray Mattingly
committed
HBASE-28882 Backup restores are broken if the backup has moved locations
1 parent bd1bee0 commit 78080c3

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/HBackupFileSystem.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.backup;
1919

20+
import com.google.errorprone.annotations.RestrictedApi;
2021
import java.io.IOException;
2122
import org.apache.hadoop.conf.Configuration;
2223
import org.apache.hadoop.fs.FileSystem;
@@ -27,6 +28,8 @@
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
2930

31+
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
32+
3033
/**
3134
* View to an on-disk Backup Image FileSytem Provides the set of methods necessary to interact with
3235
* the on-disk Backup Image data.
@@ -97,10 +100,18 @@ public static Path getTableBackupPath(TableName tableName, Path backupRootPath,
97100

98101
private static Path getManifestPath(Configuration conf, Path backupRootPath, String backupId)
99102
throws IOException {
103+
return getManifestPath(conf, backupRootPath, backupId, true);
104+
}
105+
106+
/* Visible for testing only */
107+
@RestrictedApi(explanation = "Should only be called in tests", link = "",
108+
allowedOnPath = ".*/src/test/.*")
109+
protected static Path getManifestPath(Configuration conf, Path backupRootPath, String backupId,
110+
boolean throwIfNotFound) throws IOException {
100111
FileSystem fs = backupRootPath.getFileSystem(conf);
101112
Path manifestPath = new Path(getBackupPath(backupRootPath.toString(), backupId) + Path.SEPARATOR
102113
+ BackupManifest.MANIFEST_FILE_NAME);
103-
if (!fs.exists(manifestPath)) {
114+
if (throwIfNotFound && !fs.exists(manifestPath)) {
104115
String errorMsg = "Could not find backup manifest " + BackupManifest.MANIFEST_FILE_NAME
105116
+ " for " + backupId + ". File " + manifestPath + " does not exists. Did " + backupId
106117
+ " correspond to previously taken backup ?";
@@ -109,6 +120,16 @@ private static Path getManifestPath(Configuration conf, Path backupRootPath, Str
109120
return manifestPath;
110121
}
111122

123+
/**
124+
* The inverse of {@link #getManifestPath(Configuration, Path, String)}
125+
*/
126+
public static Path getRootDirFromManifestPath(Path manifestPath, String backupId) {
127+
Preconditions.checkArgument(manifestPath.getParent().toString().endsWith(backupId),
128+
String.format("Manifest path parent %s must end in backupId %s", manifestPath, backupId));
129+
return new Path(
130+
manifestPath.toString().substring(0, manifestPath.toString().indexOf("/" + backupId)));
131+
}
132+
112133
public static BackupManifest getManifest(Configuration conf, Path backupRootPath, String backupId)
113134
throws IOException {
114135
BackupManifest manifest =

hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupManifest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.backup.impl;
1919

20+
import com.google.errorprone.annotations.RestrictedApi;
2021
import java.io.IOException;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
@@ -439,7 +440,7 @@ public BackupManifest(FileSystem fs, Path backupPath) throws BackupException {
439440
} catch (Exception e) {
440441
throw new BackupException(e);
441442
}
442-
this.backupImage = BackupImage.fromProto(proto);
443+
this.backupImage = hydrateRootDir(BackupImage.fromProto(proto), backupPath);
443444
LOG.debug("Loaded manifest instance from manifest file: "
444445
+ BackupUtils.getPath(subFile.getPath()));
445446
return;
@@ -452,6 +453,25 @@ public BackupManifest(FileSystem fs, Path backupPath) throws BackupException {
452453
}
453454
}
454455

456+
/* Visible for testing only */
457+
@RestrictedApi(explanation = "Should only be called in tests", link = "",
458+
allowedOnPath = ".*/src/test/.*")
459+
public static BackupImage hydrateRootDir(BackupImage backupImage, Path backupPath)
460+
throws IOException {
461+
String providedRootDir =
462+
HBackupFileSystem.getRootDirFromManifestPath(backupPath, backupImage.backupId).toString();
463+
if (!providedRootDir.startsWith(backupImage.getRootDir())) {
464+
LOG.info(
465+
"Provided backup root ({}) is different from the BackupImage's root ({}). We will prefer the provided root for this Backup and its ancestors.",
466+
providedRootDir, backupImage.getRootDir());
467+
backupImage.setRootDir(providedRootDir);
468+
for (BackupImage ancestor : backupImage.getAncestors()) {
469+
ancestor.setRootDir(providedRootDir);
470+
}
471+
}
472+
return backupImage;
473+
}
474+
455475
public BackupType getType() {
456476
return backupImage.getType();
457477
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.backup;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
import java.io.IOException;
23+
import org.apache.hadoop.conf.Configuration;
24+
import org.apache.hadoop.fs.Path;
25+
import org.apache.hadoop.hbase.HBaseClassTestRule;
26+
import org.apache.hadoop.hbase.testclassification.SmallTests;
27+
import org.junit.ClassRule;
28+
import org.junit.Test;
29+
import org.junit.experimental.categories.Category;
30+
31+
@Category(SmallTests.class)
32+
public class TestHBackupFileSystem {
33+
34+
@ClassRule
35+
public static final HBaseClassTestRule CLASS_RULE =
36+
HBaseClassTestRule.forClass(TestHBackupFileSystem.class);
37+
38+
private static final Path ROOT_DIR = new Path("/backup/root");
39+
private static final String BACKUP_ID = "123";
40+
41+
@Test
42+
public void testRootDirManifestPathConversion() throws IOException {
43+
Path manifestPath =
44+
HBackupFileSystem.getManifestPath(new Configuration(), ROOT_DIR, BACKUP_ID, false);
45+
Path convertedRootDir = HBackupFileSystem.getRootDirFromManifestPath(manifestPath, BACKUP_ID);
46+
assertEquals(ROOT_DIR, convertedRootDir);
47+
}
48+
}

hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestIncrementalBackup.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertTrue;
2222

23+
import java.io.IOException;
2324
import java.util.ArrayList;
2425
import java.util.Collection;
2526
import java.util.HashSet;
@@ -104,6 +105,7 @@ public void TestIncBackupRestore() throws Exception {
104105
BackupAdminImpl client = new BackupAdminImpl(conn);
105106
BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
106107
String backupIdFull = client.backupTables(request);
108+
validateRootPathCanBeOverridden(BACKUP_ROOT_DIR, backupIdFull);
107109
assertTrue(checkSucceeded(backupIdFull));
108110

109111
// #2 - insert some data to table
@@ -156,6 +158,7 @@ public void TestIncBackupRestore() throws Exception {
156158
BackupManifest manifest =
157159
HBackupFileSystem.getManifest(conf1, new Path(BACKUP_ROOT_DIR), backupIdIncMultiple);
158160
assertEquals(Sets.newHashSet(table1, table2), new HashSet<>(manifest.getTableList()));
161+
validateRootPathCanBeOverridden(BACKUP_ROOT_DIR, backupIdIncMultiple);
159162

160163
// add column family f2 to table1
161164
// drop column family f3
@@ -176,6 +179,7 @@ public void TestIncBackupRestore() throws Exception {
176179
request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
177180
String backupIdIncMultiple2 = client.backupTables(request);
178181
assertTrue(checkSucceeded(backupIdIncMultiple2));
182+
validateRootPathCanBeOverridden(BACKUP_ROOT_DIR, backupIdIncMultiple2);
179183

180184
// #5 - restore full backup for all tables
181185
TableName[] tablesRestoreFull = new TableName[] { table1, table2 };
@@ -227,4 +231,21 @@ public void TestIncBackupRestore() throws Exception {
227231
admin.close();
228232
}
229233
}
234+
235+
/**
236+
* Check that backup manifest can be produced for a different root. Users may want to move
237+
* existing backups to a different location.
238+
*/
239+
private void validateRootPathCanBeOverridden(String originalPath, String backupId)
240+
throws IOException {
241+
String anotherRootDir = "/some/other/root/dir";
242+
Path anotherPath = new Path(anotherRootDir, backupId);
243+
BackupManifest.BackupImage differentLocationImage = BackupManifest.hydrateRootDir(
244+
HBackupFileSystem.getManifest(conf1, new Path(originalPath), backupId).getBackupImage(),
245+
anotherPath);
246+
assertEquals(differentLocationImage.getRootDir(), anotherRootDir);
247+
for (BackupManifest.BackupImage ancestor : differentLocationImage.getAncestors()) {
248+
assertEquals(ancestor.getRootDir(), anotherRootDir);
249+
}
250+
}
230251
}

0 commit comments

Comments
 (0)