-
Notifications
You must be signed in to change notification settings - Fork 3.4k
HBASE-28897: Force CF compatibility during incremental backup #6340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...ckup/src/main/java/org/apache/hadoop/hbase/backup/impl/ColumnFamilyMismatchException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hbase.backup.impl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.hadoop.hbase.TableName; | ||
| import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; | ||
| import org.apache.yetus.audience.InterfaceAudience; | ||
|
|
||
| @InterfaceAudience.Public | ||
| public final class ColumnFamilyMismatchException extends BackupException { | ||
| private final List<TableName> mismatchedTables; | ||
|
|
||
| private ColumnFamilyMismatchException(String msg, List<TableName> mismatchedTables) { | ||
| super(msg); | ||
| this.mismatchedTables = mismatchedTables; | ||
| } | ||
|
|
||
| public static final class ColumnFamilyMismatchExceptionBuilder { | ||
| private final List<TableName> mismatchedTables = new ArrayList<>(); | ||
| private final StringBuilder msg = new StringBuilder(); | ||
|
|
||
| public ColumnFamilyMismatchExceptionBuilder addMismatchedTable(TableName tableName, | ||
| ColumnFamilyDescriptor[] currentCfs, ColumnFamilyDescriptor[] backupCfs) { | ||
| this.mismatchedTables.add(tableName); | ||
|
|
||
| String currentCfsParsed = StringUtils.join(currentCfs, ','); | ||
| String backupCfsParsed = StringUtils.join(backupCfs, ','); | ||
| msg.append("\nMismatch in column family descriptor for table: ").append(tableName) | ||
| .append("\n"); | ||
| msg.append("Current families: ").append(currentCfsParsed).append("\n"); | ||
| msg.append("Backup families: ").append(backupCfsParsed); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public ColumnFamilyMismatchException build() { | ||
| return new ColumnFamilyMismatchException(msg.toString(), mismatchedTables); | ||
| } | ||
| } | ||
|
|
||
| public List<TableName> getMismatchedTables() { | ||
| return mismatchedTables; | ||
| } | ||
|
|
||
| public static ColumnFamilyMismatchExceptionBuilder newBuilder() { | ||
| return new ColumnFamilyMismatchExceptionBuilder(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
@@ -32,15 +33,20 @@ | |
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hbase.TableName; | ||
| import org.apache.hadoop.hbase.backup.BackupCopyJob; | ||
| import org.apache.hadoop.hbase.backup.BackupInfo; | ||
| import org.apache.hadoop.hbase.backup.BackupInfo.BackupPhase; | ||
| import org.apache.hadoop.hbase.backup.BackupRequest; | ||
| import org.apache.hadoop.hbase.backup.BackupRestoreFactory; | ||
| import org.apache.hadoop.hbase.backup.BackupType; | ||
| import org.apache.hadoop.hbase.backup.HBackupFileSystem; | ||
| import org.apache.hadoop.hbase.backup.mapreduce.MapReduceBackupCopyJob; | ||
| import org.apache.hadoop.hbase.backup.util.BackupUtils; | ||
| import org.apache.hadoop.hbase.client.Admin; | ||
| import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; | ||
| import org.apache.hadoop.hbase.client.Connection; | ||
| import org.apache.hadoop.hbase.mapreduce.WALPlayer; | ||
| import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils; | ||
| import org.apache.hadoop.hbase.snapshot.SnapshotManifest; | ||
| import org.apache.hadoop.hbase.util.Bytes; | ||
| import org.apache.hadoop.hbase.util.CommonFSUtils; | ||
| import org.apache.hadoop.hbase.util.HFileArchiveUtil; | ||
|
|
@@ -51,6 +57,8 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos; | ||
|
|
||
| /** | ||
| * Incremental backup implementation. See the {@link #execute() execute} method. | ||
| */ | ||
|
|
@@ -260,8 +268,11 @@ private void updateFileLists(List<String> activeFiles, List<String> archiveFiles | |
| } | ||
|
|
||
| @Override | ||
| public void execute() throws IOException { | ||
| public void execute() throws IOException, ColumnFamilyMismatchException { | ||
| try { | ||
| Map<TableName, String> tablesToFullBackupIds = getFullBackupIds(); | ||
| verifyCfCompatibility(backupInfo.getTables(), tablesToFullBackupIds); | ||
|
|
||
| // case PREPARE_INCREMENTAL: | ||
| beginBackup(backupManager, backupInfo); | ||
| backupInfo.setPhase(BackupPhase.PREPARE_INCREMENTAL); | ||
|
|
@@ -434,4 +445,86 @@ protected Path getBulkOutputDir() { | |
| path = new Path(path, backupId); | ||
| return path; | ||
| } | ||
|
|
||
| private Map<TableName, String> getFullBackupIds() throws IOException { | ||
| // Ancestors are stored from newest to oldest, so we can iterate backwards | ||
| // in order to populate our backupId map with the most recent full backup | ||
| // for a given table | ||
| List<BackupManifest.BackupImage> images = getAncestors(backupInfo); | ||
| Map<TableName, String> results = new HashMap<>(); | ||
| for (int i = images.size() - 1; i >= 0; i--) { | ||
| BackupManifest.BackupImage image = images.get(i); | ||
| if (image.getType() != BackupType.FULL) { | ||
| continue; | ||
| } | ||
|
|
||
| for (TableName tn : image.getTableNames()) { | ||
| results.put(tn, image.getBackupId()); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that the current table descriptor CFs matches the descriptor CFs of the last full | ||
| * backup for the tables. This ensures CF compatibility across incremental backups. If a mismatch | ||
| * is detected, a full table backup should be taken, rather than an incremental one | ||
| */ | ||
| private void verifyCfCompatibility(Set<TableName> tables, | ||
| Map<TableName, String> tablesToFullBackupId) throws IOException, ColumnFamilyMismatchException { | ||
| ColumnFamilyMismatchException.ColumnFamilyMismatchExceptionBuilder exBuilder = | ||
| ColumnFamilyMismatchException.newBuilder(); | ||
| try (Admin admin = conn.getAdmin(); BackupAdminImpl backupAdmin = new BackupAdminImpl(conn)) { | ||
| for (TableName tn : tables) { | ||
| String backupId = tablesToFullBackupId.get(tn); | ||
| BackupInfo fullBackupInfo = backupAdmin.getBackupInfo(backupId); | ||
|
|
||
| ColumnFamilyDescriptor[] currentCfs = admin.getDescriptor(tn).getColumnFamilies(); | ||
| String snapshotName = fullBackupInfo.getSnapshotName(tn); | ||
| Path root = HBackupFileSystem.getTableBackupPath(tn, | ||
| new Path(fullBackupInfo.getBackupRootDir()), fullBackupInfo.getBackupId()); | ||
| Path manifestDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, root); | ||
|
|
||
| FileSystem fs; | ||
| try { | ||
| fs = FileSystem.get(new URI(fullBackupInfo.getBackupRootDir()), conf); | ||
| } catch (URISyntaxException e) { | ||
| throw new IOException("Unable to get fs", e); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "unable to get fs for backup foo." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will address this |
||
| } | ||
|
|
||
| SnapshotProtos.SnapshotDescription snapshotDescription = | ||
| SnapshotDescriptionUtils.readSnapshotInfo(fs, manifestDir); | ||
| SnapshotManifest manifest = | ||
| SnapshotManifest.open(conf, fs, manifestDir, snapshotDescription); | ||
|
|
||
| ColumnFamilyDescriptor[] backupCfs = manifest.getTableDescriptor().getColumnFamilies(); | ||
| if (!areCfsCompatible(currentCfs, backupCfs)) { | ||
| exBuilder.addMismatchedTable(tn, currentCfs, backupCfs); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ColumnFamilyMismatchException ex = exBuilder.build(); | ||
| if (!ex.getMismatchedTables().isEmpty()) { | ||
| throw ex; | ||
| } | ||
| } | ||
|
|
||
| private static boolean areCfsCompatible(ColumnFamilyDescriptor[] currentCfs, | ||
| ColumnFamilyDescriptor[] backupCfs) { | ||
| if (currentCfs.length != backupCfs.length) { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < backupCfs.length; i++) { | ||
| String currentCf = currentCfs[i].getNameAsString(); | ||
| String backupCf = backupCfs[i].getNameAsString(); | ||
|
|
||
| if (!currentCf.equals(backupCf)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heya folks. This class hierarchy mixes IA public and private -- no can do. We can't just make its parent class, BackupException, public, because it exposes a non-public member.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, I'm more than happy to make this extends
Exceptioninstead. I'm not necessarily sure that this is anIOException, per-se