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 java.io.IOException;
021import java.util.HashMap;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.backup.impl.BackupManifest;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * View to an on-disk Backup Image FileSytem Provides the set of methods necessary to interact with
034 * the on-disk Backup Image data.
035 */
036@InterfaceAudience.Private
037public final class HBackupFileSystem {
038  public static final Logger LOG = LoggerFactory.getLogger(HBackupFileSystem.class);
039
040  /**
041   * This is utility class.
042   */
043  private HBackupFileSystem() {
044  }
045
046  /**
047   * Given the backup root dir, backup id and the table name, return the backup image location,
048   * which is also where the backup manifest file is. return value look like:
049   * "hdfs://backup.hbase.org:9000/user/biadmin/backup/backup_1396650096738/default/t1_dn/", where
050   * "hdfs://backup.hbase.org:9000/user/biadmin/backup" is a backup root directory
051   * @param backupRootDir backup root directory
052   * @param backupId      backup id
053   * @param tableName     table name
054   * @return backupPath String for the particular table
055   */
056  public static String getTableBackupDir(String backupRootDir, String backupId,
057    TableName tableName) {
058    return backupRootDir + Path.SEPARATOR + backupId + Path.SEPARATOR
059      + tableName.getNamespaceAsString() + Path.SEPARATOR + tableName.getQualifierAsString()
060      + Path.SEPARATOR;
061  }
062
063  /**
064   * Get backup temporary directory
065   * @param backupRootDir backup root
066   * @return backup tmp directory path
067   */
068  public static Path getBackupTmpDirPath(String backupRootDir) {
069    return new Path(backupRootDir, ".tmp");
070  }
071
072  /**
073   * Get backup tmp directory for backupId
074   * @param backupRoot backup root
075   * @param backupId   backup id
076   * @return backup tmp directory path
077   */
078  public static Path getBackupTmpDirPathForBackupId(String backupRoot, String backupId) {
079    return new Path(getBackupTmpDirPath(backupRoot), backupId);
080  }
081
082  public static String getTableBackupDataDir(String backupRootDir, String backupId,
083    TableName tableName) {
084    return getTableBackupDir(backupRootDir, backupId, tableName) + Path.SEPARATOR + "data";
085  }
086
087  public static Path getBackupPath(String backupRootDir, String backupId) {
088    return new Path(backupRootDir + Path.SEPARATOR + backupId);
089  }
090
091  /**
092   * Given the backup root dir, backup id and the table name, return the backup image location,
093   * which is also where the backup manifest file is. return value look like:
094   * "hdfs://backup.hbase.org:9000/user/biadmin/backup/backup_1396650096738/default/t1_dn/", where
095   * "hdfs://backup.hbase.org:9000/user/biadmin/backup" is a backup root directory
096   * @param backupRootPath backup root path
097   * @param tableName      table name
098   * @param backupId       backup Id
099   * @return backupPath for the particular table
100   */
101  public static Path getTableBackupPath(TableName tableName, Path backupRootPath, String backupId) {
102    return new Path(getTableBackupDir(backupRootPath.toString(), backupId, tableName));
103  }
104
105  /**
106   * Given the backup root dir and the backup id, return the log file location for an incremental
107   * backup.
108   * @param backupRootDir backup root directory
109   * @param backupId      backup id
110   * @return logBackupDir: ".../user/biadmin/backup/WALs/backup_1396650096738"
111   */
112  public static String getLogBackupDir(String backupRootDir, String backupId) {
113    return backupRootDir + Path.SEPARATOR + backupId + Path.SEPARATOR
114      + HConstants.HREGION_LOGDIR_NAME;
115  }
116
117  public static Path getLogBackupPath(String backupRootDir, String backupId) {
118    return new Path(getLogBackupDir(backupRootDir, backupId));
119  }
120
121  // TODO we do not keep WAL files anymore
122  // Move manifest file to other place
123  private static Path getManifestPath(Configuration conf, Path backupRootPath, String backupId)
124    throws IOException {
125    FileSystem fs = backupRootPath.getFileSystem(conf);
126    Path manifestPath = new Path(getBackupPath(backupRootPath.toString(), backupId) + Path.SEPARATOR
127      + BackupManifest.MANIFEST_FILE_NAME);
128    if (!fs.exists(manifestPath)) {
129      String errorMsg = "Could not find backup manifest " + BackupManifest.MANIFEST_FILE_NAME
130        + " for " + backupId + ". File " + manifestPath + " does not exists. Did " + backupId
131        + " correspond to previously taken backup ?";
132      throw new IOException(errorMsg);
133    }
134    return manifestPath;
135  }
136
137  public static BackupManifest getManifest(Configuration conf, Path backupRootPath, String backupId)
138    throws IOException {
139    BackupManifest manifest =
140      new BackupManifest(conf, getManifestPath(conf, backupRootPath, backupId));
141    return manifest;
142  }
143
144  /**
145   * Check whether the backup image path and there is manifest file in the path.
146   * @param backupManifestMap If all the manifests are found, then they are put into this map
147   * @param tableArray        the tables involved
148   * @throws IOException exception
149   */
150  public static void checkImageManifestExist(HashMap<TableName, BackupManifest> backupManifestMap,
151    TableName[] tableArray, Configuration conf, Path backupRootPath, String backupId)
152    throws IOException {
153    for (TableName tableName : tableArray) {
154      BackupManifest manifest = getManifest(conf, backupRootPath, backupId);
155      backupManifestMap.put(tableName, manifest);
156    }
157  }
158}