Skip to content

Commit 64b4de3

Browse files
rmdmattinglyhgromerHernan Gelaf-Romer
authored
HBASE-28897 Force CF compatibility during incremental backup (#6340) (#6357)
Signed-off-by: Ray Mattingly <[email protected]> Co-authored-by: Hernan Romer <[email protected]> Co-authored-by: Hernan Gelaf-Romer <[email protected]>
1 parent ad8b2a8 commit 64b4de3

File tree

3 files changed

+189
-8
lines changed

3 files changed

+189
-8
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.impl;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.hadoop.hbase.TableName;
24+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
25+
import org.apache.yetus.audience.InterfaceAudience;
26+
27+
@InterfaceAudience.Public
28+
public final class ColumnFamilyMismatchException extends BackupException {
29+
private final List<TableName> mismatchedTables;
30+
31+
private ColumnFamilyMismatchException(String msg, List<TableName> mismatchedTables) {
32+
super(msg);
33+
this.mismatchedTables = mismatchedTables;
34+
}
35+
36+
public static final class ColumnFamilyMismatchExceptionBuilder {
37+
private final List<TableName> mismatchedTables = new ArrayList<>();
38+
private final StringBuilder msg = new StringBuilder();
39+
40+
public ColumnFamilyMismatchExceptionBuilder addMismatchedTable(TableName tableName,
41+
ColumnFamilyDescriptor[] currentCfs, ColumnFamilyDescriptor[] backupCfs) {
42+
this.mismatchedTables.add(tableName);
43+
44+
String currentCfsParsed = StringUtils.join(currentCfs, ',');
45+
String backupCfsParsed = StringUtils.join(backupCfs, ',');
46+
msg.append("\nMismatch in column family descriptor for table: ").append(tableName)
47+
.append("\n");
48+
msg.append("Current families: ").append(currentCfsParsed).append("\n");
49+
msg.append("Backup families: ").append(backupCfsParsed);
50+
51+
return this;
52+
}
53+
54+
public ColumnFamilyMismatchException build() {
55+
return new ColumnFamilyMismatchException(msg.toString(), mismatchedTables);
56+
}
57+
}
58+
59+
public List<TableName> getMismatchedTables() {
60+
return mismatchedTables;
61+
}
62+
63+
public static ColumnFamilyMismatchExceptionBuilder newBuilder() {
64+
return new ColumnFamilyMismatchExceptionBuilder();
65+
}
66+
}

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

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URI;
2424
import java.net.URISyntaxException;
2525
import java.util.ArrayList;
26+
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Set;
@@ -32,15 +33,20 @@
3233
import org.apache.hadoop.fs.Path;
3334
import org.apache.hadoop.hbase.TableName;
3435
import org.apache.hadoop.hbase.backup.BackupCopyJob;
36+
import org.apache.hadoop.hbase.backup.BackupInfo;
3537
import org.apache.hadoop.hbase.backup.BackupInfo.BackupPhase;
3638
import org.apache.hadoop.hbase.backup.BackupRequest;
3739
import org.apache.hadoop.hbase.backup.BackupRestoreFactory;
3840
import org.apache.hadoop.hbase.backup.BackupType;
41+
import org.apache.hadoop.hbase.backup.HBackupFileSystem;
3942
import org.apache.hadoop.hbase.backup.mapreduce.MapReduceBackupCopyJob;
4043
import org.apache.hadoop.hbase.backup.util.BackupUtils;
4144
import org.apache.hadoop.hbase.client.Admin;
45+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
4246
import org.apache.hadoop.hbase.client.Connection;
4347
import org.apache.hadoop.hbase.mapreduce.WALPlayer;
48+
import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
49+
import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
4450
import org.apache.hadoop.hbase.util.Bytes;
4551
import org.apache.hadoop.hbase.util.CommonFSUtils;
4652
import org.apache.hadoop.hbase.util.HFileArchiveUtil;
@@ -51,6 +57,8 @@
5157
import org.slf4j.Logger;
5258
import org.slf4j.LoggerFactory;
5359

60+
import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
61+
5462
/**
5563
* Incremental backup implementation. See the {@link #execute() execute} method.
5664
*/
@@ -260,8 +268,11 @@ private void updateFileLists(List<String> activeFiles, List<String> archiveFiles
260268
}
261269

262270
@Override
263-
public void execute() throws IOException {
271+
public void execute() throws IOException, ColumnFamilyMismatchException {
264272
try {
273+
Map<TableName, String> tablesToFullBackupIds = getFullBackupIds();
274+
verifyCfCompatibility(backupInfo.getTables(), tablesToFullBackupIds);
275+
265276
// case PREPARE_INCREMENTAL:
266277
beginBackup(backupManager, backupInfo);
267278
backupInfo.setPhase(BackupPhase.PREPARE_INCREMENTAL);
@@ -434,4 +445,86 @@ protected Path getBulkOutputDir() {
434445
path = new Path(path, backupId);
435446
return path;
436447
}
448+
449+
private Map<TableName, String> getFullBackupIds() throws IOException {
450+
// Ancestors are stored from newest to oldest, so we can iterate backwards
451+
// in order to populate our backupId map with the most recent full backup
452+
// for a given table
453+
List<BackupManifest.BackupImage> images = getAncestors(backupInfo);
454+
Map<TableName, String> results = new HashMap<>();
455+
for (int i = images.size() - 1; i >= 0; i--) {
456+
BackupManifest.BackupImage image = images.get(i);
457+
if (image.getType() != BackupType.FULL) {
458+
continue;
459+
}
460+
461+
for (TableName tn : image.getTableNames()) {
462+
results.put(tn, image.getBackupId());
463+
}
464+
}
465+
return results;
466+
}
467+
468+
/**
469+
* Verifies that the current table descriptor CFs matches the descriptor CFs of the last full
470+
* backup for the tables. This ensures CF compatibility across incremental backups. If a mismatch
471+
* is detected, a full table backup should be taken, rather than an incremental one
472+
*/
473+
private void verifyCfCompatibility(Set<TableName> tables,
474+
Map<TableName, String> tablesToFullBackupId) throws IOException, ColumnFamilyMismatchException {
475+
ColumnFamilyMismatchException.ColumnFamilyMismatchExceptionBuilder exBuilder =
476+
ColumnFamilyMismatchException.newBuilder();
477+
try (Admin admin = conn.getAdmin(); BackupAdminImpl backupAdmin = new BackupAdminImpl(conn)) {
478+
for (TableName tn : tables) {
479+
String backupId = tablesToFullBackupId.get(tn);
480+
BackupInfo fullBackupInfo = backupAdmin.getBackupInfo(backupId);
481+
482+
ColumnFamilyDescriptor[] currentCfs = admin.getDescriptor(tn).getColumnFamilies();
483+
String snapshotName = fullBackupInfo.getSnapshotName(tn);
484+
Path root = HBackupFileSystem.getTableBackupPath(tn,
485+
new Path(fullBackupInfo.getBackupRootDir()), fullBackupInfo.getBackupId());
486+
Path manifestDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, root);
487+
488+
FileSystem fs;
489+
try {
490+
fs = FileSystem.get(new URI(fullBackupInfo.getBackupRootDir()), conf);
491+
} catch (URISyntaxException e) {
492+
throw new IOException("Unable to get fs", e);
493+
}
494+
495+
SnapshotProtos.SnapshotDescription snapshotDescription =
496+
SnapshotDescriptionUtils.readSnapshotInfo(fs, manifestDir);
497+
SnapshotManifest manifest =
498+
SnapshotManifest.open(conf, fs, manifestDir, snapshotDescription);
499+
500+
ColumnFamilyDescriptor[] backupCfs = manifest.getTableDescriptor().getColumnFamilies();
501+
if (!areCfsCompatible(currentCfs, backupCfs)) {
502+
exBuilder.addMismatchedTable(tn, currentCfs, backupCfs);
503+
}
504+
}
505+
}
506+
507+
ColumnFamilyMismatchException ex = exBuilder.build();
508+
if (!ex.getMismatchedTables().isEmpty()) {
509+
throw ex;
510+
}
511+
}
512+
513+
private static boolean areCfsCompatible(ColumnFamilyDescriptor[] currentCfs,
514+
ColumnFamilyDescriptor[] backupCfs) {
515+
if (currentCfs.length != backupCfs.length) {
516+
return false;
517+
}
518+
519+
for (int i = 0; i < backupCfs.length; i++) {
520+
String currentCf = currentCfs[i].getNameAsString();
521+
String backupCf = backupCfs[i].getNameAsString();
522+
523+
if (!currentCf.equals(backupCf)) {
524+
return false;
525+
}
526+
}
527+
528+
return true;
529+
}
437530
}

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package org.apache.hadoop.hbase.backup;
1919

2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.assertTrue;
2223

24+
import java.io.IOException;
2325
import java.util.ArrayList;
2426
import java.util.Collection;
2527
import java.util.HashSet;
@@ -31,6 +33,7 @@
3133
import org.apache.hadoop.hbase.TableName;
3234
import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
3335
import org.apache.hadoop.hbase.backup.impl.BackupManifest;
36+
import org.apache.hadoop.hbase.backup.impl.ColumnFamilyMismatchException;
3437
import org.apache.hadoop.hbase.backup.util.BackupUtils;
3538
import org.apache.hadoop.hbase.client.Admin;
3639
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
@@ -53,6 +56,7 @@
5356
import org.slf4j.Logger;
5457
import org.slf4j.LoggerFactory;
5558

59+
import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
5660
import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
5761
import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
5862

@@ -102,9 +106,7 @@ public void TestIncBackupRestore() throws Exception {
102106
insertIntoTable(conn, table1, mobName, 3, NB_ROWS_FAM3).close();
103107
Admin admin = conn.getAdmin();
104108
BackupAdminImpl client = new BackupAdminImpl(conn);
105-
BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
106-
String backupIdFull = client.backupTables(request);
107-
assertTrue(checkSucceeded(backupIdFull));
109+
String backupIdFull = takeFullBackup(tables, client);
108110

109111
// #2 - insert some data to table
110112
Table t1 = insertIntoTable(conn, table1, famName, 1, ADD_ROWS);
@@ -141,16 +143,14 @@ public void TestIncBackupRestore() throws Exception {
141143
// exception will be thrown.
142144
LOG.debug("region is not splittable, because " + e);
143145
}
144-
while (!admin.isTableAvailable(table1)) {
145-
Thread.sleep(100);
146-
}
146+
TEST_UTIL.waitTableAvailable(table1);
147147
long endSplitTime = EnvironmentEdgeManager.currentTime();
148148
// split finished
149149
LOG.debug("split finished in =" + (endSplitTime - startSplitTime));
150150

151151
// #3 - incremental backup for multiple tables
152152
tables = Lists.newArrayList(table1, table2);
153-
request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
153+
BackupRequest request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
154154
String backupIdIncMultiple = client.backupTables(request);
155155
assertTrue(checkSucceeded(backupIdIncMultiple));
156156
BackupManifest manifest =
@@ -165,6 +165,13 @@ public void TestIncBackupRestore() throws Exception {
165165
.build();
166166
TEST_UTIL.getAdmin().modifyTable(newTable1Desc);
167167

168+
// check that an incremental backup fails because the CFs don't match
169+
final List<TableName> tablesCopy = tables;
170+
IOException ex = assertThrows(IOException.class, () -> client
171+
.backupTables(createBackupRequest(BackupType.INCREMENTAL, tablesCopy, BACKUP_ROOT_DIR)));
172+
checkThrowsCFMismatch(ex, List.of(table1));
173+
takeFullBackup(tables, client);
174+
168175
int NB_ROWS_FAM2 = 7;
169176
Table t3 = insertIntoTable(conn, table1, fam2Name, 2, NB_ROWS_FAM2);
170177
t3.close();
@@ -227,4 +234,19 @@ public void TestIncBackupRestore() throws Exception {
227234
admin.close();
228235
}
229236
}
237+
238+
private void checkThrowsCFMismatch(IOException ex, List<TableName> tables) {
239+
Throwable cause = Throwables.getRootCause(ex);
240+
assertEquals(cause.getClass(), ColumnFamilyMismatchException.class);
241+
ColumnFamilyMismatchException e = (ColumnFamilyMismatchException) cause;
242+
assertEquals(tables, e.getMismatchedTables());
243+
}
244+
245+
private String takeFullBackup(List<TableName> tables, BackupAdminImpl backupAdmin)
246+
throws IOException {
247+
BackupRequest req = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
248+
String backupId = backupAdmin.backupTables(req);
249+
checkSucceeded(backupId);
250+
return backupId;
251+
}
230252
}

0 commit comments

Comments
 (0)