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 org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.backup.impl.BackupManifest;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * View to an on-disk Backup Image FileSytem Provides the set of methods necessary to interact with
032 * the on-disk Backup Image data.
033 */
034@InterfaceAudience.Private
035public final class HBackupFileSystem {
036  public static final Logger LOG = LoggerFactory.getLogger(HBackupFileSystem.class);
037
038  /**
039   * This is utility class.
040   */
041  private HBackupFileSystem() {
042  }
043
044  /**
045   * Given the backup root dir, backup id and the table name, return the backup image location.
046   * Return value look like:
047   * "hdfs://backup.hbase.org:9000/user/biadmin/backup/backup_1396650096738/default/t1_dn/", where
048   * "hdfs://backup.hbase.org:9000/user/biadmin/backup" is a backup root directory
049   * @param backupRootDir backup root directory
050   * @param backupId      backup id
051   * @param tableName     table name
052   * @return backupPath String for the particular table
053   */
054  public static String getTableBackupDir(String backupRootDir, String backupId,
055    TableName tableName) {
056    return backupRootDir + Path.SEPARATOR + backupId + Path.SEPARATOR
057      + tableName.getNamespaceAsString() + Path.SEPARATOR + tableName.getQualifierAsString()
058      + Path.SEPARATOR;
059  }
060
061  /**
062   * Get backup temporary directory
063   * @param backupRootDir backup root
064   * @return backup tmp directory path
065   */
066  public static Path getBackupTmpDirPath(String backupRootDir) {
067    return new Path(backupRootDir, ".tmp");
068  }
069
070  /**
071   * Get backup tmp directory for backupId
072   * @param backupRoot backup root
073   * @param backupId   backup id
074   * @return backup tmp directory path
075   */
076  public static Path getBackupTmpDirPathForBackupId(String backupRoot, String backupId) {
077    return new Path(getBackupTmpDirPath(backupRoot), backupId);
078  }
079
080  public static Path getBackupPath(String backupRootDir, String backupId) {
081    return new Path(backupRootDir + Path.SEPARATOR + backupId);
082  }
083
084  /**
085   * Given the backup root dir, backup id and the table name, return the backup image location,
086   * which is also where the backup manifest file is. return value look like:
087   * "hdfs://backup.hbase.org:9000/user/biadmin/backup/backup_1396650096738/default/t1_dn/", where
088   * "hdfs://backup.hbase.org:9000/user/biadmin/backup" is a backup root directory
089   * @param backupRootPath backup root path
090   * @param tableName      table name
091   * @param backupId       backup Id
092   * @return backupPath for the particular table
093   */
094  public static Path getTableBackupPath(TableName tableName, Path backupRootPath, String backupId) {
095    return new Path(getTableBackupDir(backupRootPath.toString(), backupId, tableName));
096  }
097
098  private static Path getManifestPath(Configuration conf, Path backupRootPath, String backupId)
099    throws IOException {
100    FileSystem fs = backupRootPath.getFileSystem(conf);
101    Path manifestPath = new Path(getBackupPath(backupRootPath.toString(), backupId) + Path.SEPARATOR
102      + BackupManifest.MANIFEST_FILE_NAME);
103    if (!fs.exists(manifestPath)) {
104      String errorMsg = "Could not find backup manifest " + BackupManifest.MANIFEST_FILE_NAME
105        + " for " + backupId + ". File " + manifestPath + " does not exists. Did " + backupId
106        + " correspond to previously taken backup ?";
107      throw new IOException(errorMsg);
108    }
109    return manifestPath;
110  }
111
112  public static BackupManifest getManifest(Configuration conf, Path backupRootPath, String backupId)
113    throws IOException {
114    BackupManifest manifest =
115      new BackupManifest(conf, getManifestPath(conf, backupRootPath, backupId));
116    return manifest;
117  }
118}