Skip to content

Commit 34ff3a6

Browse files
author
Hernan Gelaf-Romer
committed
Force CF compatibility during incremental backup
1 parent ef19929 commit 34ff3a6

File tree

3 files changed

+139
-7
lines changed

3 files changed

+139
-7
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 org.apache.commons.lang3.StringUtils;
21+
import org.apache.hadoop.hbase.TableName;
22+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
23+
import org.apache.yetus.audience.InterfaceAudience;
24+
25+
@InterfaceAudience.Public
26+
public final class ColumnFamilyMismatchException extends BackupException {
27+
private ColumnFamilyMismatchException(String msg) {
28+
super(msg);
29+
}
30+
31+
public static ColumnFamilyMismatchException create(TableName tn,
32+
ColumnFamilyDescriptor[] currentCfs, ColumnFamilyDescriptor[] backupCfs) {
33+
String currentCfsParsed = StringUtils.join(currentCfs, ',');
34+
String backupCfsParsed = StringUtils.join(backupCfs, ',');
35+
36+
String msg = "Mismatch in column family descriptors for table: " + tn
37+
+ "\nCurrent families: %s\nBackup families: %s".formatted(currentCfsParsed, backupCfsParsed);
38+
return new ColumnFamilyMismatchException(msg);
39+
}
40+
}

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

Lines changed: 77 additions & 0 deletions
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
*/
@@ -262,6 +270,16 @@ private void updateFileLists(List<String> activeFiles, List<String> archiveFiles
262270
@Override
263271
public void execute() throws IOException {
264272
try {
273+
Map<TableName, String> tablesToFullBackupIds = getFullBackupIds();
274+
275+
try (BackupAdminImpl backupAdmin = new BackupAdminImpl(conn)) {
276+
for (TableName tn : backupInfo.getTables()) {
277+
String fullBackupId = tablesToFullBackupIds.get(tn);
278+
BackupInfo fullBackupInfo = backupAdmin.getBackupInfo(fullBackupId);
279+
verifyHtd(tn, fullBackupInfo);
280+
}
281+
}
282+
265283
// case PREPARE_INCREMENTAL:
266284
beginBackup(backupManager, backupInfo);
267285
backupInfo.setPhase(BackupPhase.PREPARE_INCREMENTAL);
@@ -434,4 +452,63 @@ protected Path getBulkOutputDir() {
434452
path = new Path(path, backupId);
435453
return path;
436454
}
455+
456+
private Map<TableName, String> getFullBackupIds() throws IOException {
457+
// Ancestors are stored from newest to oldest, so we can iterate backwards
458+
// in order to populate our backupId map with the most recent full backup
459+
// for a given table
460+
List<BackupManifest.BackupImage> images = getAncestors(backupInfo);
461+
Map<TableName, String> results = new HashMap<>();
462+
for (int i = images.size() - 1; i >= 0; i--) {
463+
BackupManifest.BackupImage image = images.get(i);
464+
if (image.getType() != BackupType.FULL) {
465+
continue;
466+
}
467+
468+
for (TableName tn : image.getTableNames()) {
469+
results.put(tn, image.getBackupId());
470+
}
471+
}
472+
return results;
473+
}
474+
475+
private void verifyHtd(TableName tn, BackupInfo fullBackupInfo) throws IOException {
476+
try (Admin admin = conn.getAdmin()) {
477+
ColumnFamilyDescriptor[] currentCfs = admin.getDescriptor(tn).getColumnFamilies();
478+
String snapshotName = fullBackupInfo.getSnapshotName(tn);
479+
Path root = HBackupFileSystem.getTableBackupPath(tn,
480+
new Path(fullBackupInfo.getBackupRootDir()), fullBackupInfo.getBackupId());
481+
Path manifestDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, root);
482+
483+
FileSystem fs;
484+
try {
485+
fs = FileSystem.get(new URI(fullBackupInfo.getBackupRootDir()), conf);
486+
} catch (URISyntaxException e) {
487+
throw new IOException("Unable to get fs", e);
488+
}
489+
490+
SnapshotProtos.SnapshotDescription snapshotDescription =
491+
SnapshotDescriptionUtils.readSnapshotInfo(fs, manifestDir);
492+
SnapshotManifest manifest = SnapshotManifest.open(conf, fs, manifestDir, snapshotDescription);
493+
494+
ColumnFamilyDescriptor[] backupCfs = manifest.getTableDescriptor().getColumnFamilies();
495+
verifyCfs(tn, currentCfs, backupCfs);
496+
}
497+
}
498+
499+
private static void verifyCfs(TableName tn, ColumnFamilyDescriptor[] currentCfs,
500+
ColumnFamilyDescriptor[] backupCfs) throws IOException {
501+
if (currentCfs.length != backupCfs.length) {
502+
throw ColumnFamilyMismatchException.create(tn, currentCfs, backupCfs);
503+
}
504+
505+
for (int i = 0; i < backupCfs.length; i++) {
506+
String currentCf = currentCfs[i].getNameAsString();
507+
String backupCf = backupCfs[i].getNameAsString();
508+
509+
if (!currentCf.equals(backupCf)) {
510+
throw ColumnFamilyMismatchException.create(tn, currentCfs, backupCfs);
511+
}
512+
}
513+
}
437514
}

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

Lines changed: 22 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+
assertEquals(ColumnFamilyMismatchException.class, Throwables.getRootCause(ex).getClass());
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,12 @@ public void TestIncBackupRestore() throws Exception {
227234
admin.close();
228235
}
229236
}
237+
238+
private String takeFullBackup(List<TableName> tables, BackupAdminImpl backupAdmin)
239+
throws IOException {
240+
BackupRequest req = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
241+
String backupId = backupAdmin.backupTables(req);
242+
checkSucceeded(backupId);
243+
return backupId;
244+
}
230245
}

0 commit comments

Comments
 (0)