001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022
023import java.io.IOException;
024import java.nio.ByteBuffer;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028import java.util.UUID;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.LocatedFileStatus;
031import org.apache.hadoop.fs.Path;
032import org.apache.hadoop.fs.RemoteIterator;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
035import org.apache.hadoop.hbase.backup.impl.BackupManifest;
036import org.apache.hadoop.hbase.backup.impl.ColumnFamilyMismatchException;
037import org.apache.hadoop.hbase.tool.BulkLoadHFiles;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.CommonFSUtils;
040import org.apache.hadoop.hbase.util.HFileTestUtil;
041
042import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
043
044public class IncrementalBackupRestoreTestBase extends TestBackupBase {
045
046  private static final byte[] BULKLOAD_START_KEY = new byte[] { 0x00 };
047  private static final byte[] BULKLOAD_END_KEY = new byte[] { Byte.MAX_VALUE };
048
049  static {
050    provider = "multiwal";
051  }
052
053  protected void checkThrowsCFMismatch(IOException ex, List<TableName> tables) {
054    Throwable cause = Throwables.getRootCause(ex);
055    assertEquals(cause.getClass(), ColumnFamilyMismatchException.class);
056    ColumnFamilyMismatchException e = (ColumnFamilyMismatchException) cause;
057    assertEquals(tables, e.getMismatchedTables());
058  }
059
060  protected String takeFullBackup(List<TableName> tables, BackupAdminImpl backupAdmin)
061    throws IOException {
062    return takeFullBackup(tables, backupAdmin, false);
063  }
064
065  protected String takeFullBackup(List<TableName> tables, BackupAdminImpl backupAdmin,
066    boolean noChecksumVerify) throws IOException {
067    BackupRequest req =
068      createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR, noChecksumVerify);
069    String backupId = backupAdmin.backupTables(req);
070    checkSucceeded(backupId);
071    return backupId;
072  }
073
074  protected static Path doBulkload(TableName tn, String regionName, byte[]... fams)
075    throws IOException {
076    Path regionDir = createHFiles(tn, regionName, fams);
077    Map<BulkLoadHFiles.LoadQueueItem, ByteBuffer> results =
078      BulkLoadHFiles.create(conf1).bulkLoad(tn, regionDir);
079    assertFalse(results.isEmpty());
080    return regionDir;
081  }
082
083  private static Path createHFiles(TableName tn, String regionName, byte[]... fams)
084    throws IOException {
085    Path rootdir = CommonFSUtils.getRootDir(conf1);
086    Path regionDir = CommonFSUtils.getRegionDir(rootdir, tn, regionName);
087
088    FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
089    fs.mkdirs(rootdir);
090
091    for (byte[] fam : fams) {
092      Path famDir = new Path(regionDir, Bytes.toString(fam));
093      Path hFileDir = new Path(famDir, UUID.randomUUID().toString());
094      HFileTestUtil.createHFile(conf1, fs, hFileDir, fam, qualName, BULKLOAD_START_KEY,
095        BULKLOAD_END_KEY, 1000);
096    }
097
098    return regionDir;
099  }
100
101  /**
102   * Check that backup manifest can be produced for a different root. Users may want to move
103   * existing backups to a different location.
104   */
105  protected void validateRootPathCanBeOverridden(String originalPath, String backupId)
106    throws IOException {
107    String anotherRootDir = "/some/other/root/dir";
108    Path anotherPath = new Path(anotherRootDir, backupId);
109    BackupManifest.BackupImage differentLocationImage = BackupManifest.hydrateRootDir(
110      HBackupFileSystem.getManifest(conf1, new Path(originalPath), backupId).getBackupImage(),
111      anotherPath);
112    assertEquals(differentLocationImage.getRootDir(), anotherRootDir);
113    for (BackupManifest.BackupImage ancestor : differentLocationImage.getAncestors()) {
114      assertEquals(anotherRootDir, ancestor.getRootDir());
115    }
116  }
117
118  protected List<LocatedFileStatus> getBackupFiles() throws IOException {
119    FileSystem fs = TEST_UTIL.getTestFileSystem();
120    RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path(BACKUP_ROOT_DIR), true);
121    List<LocatedFileStatus> files = new ArrayList<>();
122
123    while (iter.hasNext()) {
124      files.add(iter.next());
125    }
126
127    return files;
128  }
129}