View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements. See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership. The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License. You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.backup;
21  
22  import java.io.IOException;
23  import java.util.HashMap;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.backup.impl.BackupManifest;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.classification.InterfaceStability;
35  
36  /**
37   * View to an on-disk Backup Image FileSytem
38   * Provides the set of methods necessary to interact with the on-disk Backup Image data.
39   */
40  @InterfaceAudience.Private
41  @InterfaceStability.Evolving
42  public class HBackupFileSystem {
43    public static final Log LOG = LogFactory.getLog(HBackupFileSystem.class);
44  
45    /**
46     * This is utility class.
47     */
48    private HBackupFileSystem() {
49    }
50  
51    /**
52     * Given the backup root dir, backup id and the table name, return the backup image location,
53     * which is also where the backup manifest file is. return value look like:
54     * "hdfs://backup.hbase.org:9000/user/biadmin/backup1/backup_1396650096738/default/t1_dn/"
55     * @param backupRootDir backup root directory
56     * @param backupId  backup id
57     * @param table table name
58     * @return backupPath String for the particular table
59     */
60    public static String getTableBackupDir(String backupRootDir, String backupId,
61        TableName tableName) {
62      return backupRootDir + Path.SEPARATOR+ backupId + Path.SEPARATOR + 
63          tableName.getNamespaceAsString() + Path.SEPARATOR
64          + tableName.getQualifierAsString() + Path.SEPARATOR ;
65    }
66  
67    /**
68     * Given the backup root dir, backup id and the table name, return the backup image location,
69     * which is also where the backup manifest file is. return value look like:
70     * "hdfs://backup.hbase.org:9000/user/biadmin/backup_1396650096738/backup1/default/t1_dn/"
71     * @param backupRootPath backup root path
72     * @param tableName table name
73     * @param backupId backup Id
74     * @return backupPath for the particular table
75     */
76    public static Path getTableBackupPath(TableName tableName, Path backupRootPath, String backupId) {
77      return new Path(getTableBackupDir(backupRootPath.toString(), backupId, tableName));
78    }
79  
80    /**
81     * Given the backup root dir and the backup id, return the log file location for an incremental
82     * backup.
83     * @param backupRootDir backup root directory
84     * @param backupId backup id
85     * @return logBackupDir: ".../user/biadmin/backup1/WALs/backup_1396650096738"
86     */
87    public static String getLogBackupDir(String backupRootDir, String backupId) {
88      return backupRootDir + Path.SEPARATOR + backupId+ Path.SEPARATOR
89          + HConstants.HREGION_LOGDIR_NAME;
90    }
91  
92    public static Path getLogBackupPath(String backupRootDir, String backupId) {
93      return new Path(getLogBackupDir(backupRootDir, backupId));
94    }
95  
96    private static Path getManifestPath(TableName tableName, Configuration conf,
97        Path backupRootPath, String backupId) throws IOException {
98      Path manifestPath = new Path(getTableBackupPath(tableName, backupRootPath, backupId),
99        BackupManifest.MANIFEST_FILE_NAME);
100     FileSystem fs = backupRootPath.getFileSystem(conf);
101     if (!fs.exists(manifestPath)) {
102       // check log dir for incremental backup case
103       manifestPath =
104           new Path(getLogBackupDir(backupRootPath.toString(), backupId) + Path.SEPARATOR
105             + BackupManifest.MANIFEST_FILE_NAME);
106       if (!fs.exists(manifestPath)) {
107         String errorMsg =
108             "Could not find backup manifest " + BackupManifest.MANIFEST_FILE_NAME + " for " +
109                 backupId + " in " + backupRootPath.toString() +
110                 ". Did " + backupId + " correspond to previously taken backup ?";
111         throw new IOException(errorMsg);
112       }
113     }
114     return manifestPath;
115   }
116 
117   public static BackupManifest getManifest(TableName tableName, Configuration conf,
118       Path backupRootPath, String backupId) throws IOException {
119     BackupManifest manifest = new BackupManifest(conf,
120       getManifestPath(tableName, conf, backupRootPath, backupId));
121     return manifest;
122   }
123 
124   /**
125    * Check whether the backup image path and there is manifest file in the path.
126    * @param backupManifestMap If all the manifests are found, then they are put into this map
127    * @param tableArray the tables involved
128    * @throws IOException exception
129    */
130   public static void checkImageManifestExist(HashMap<TableName, BackupManifest> backupManifestMap,
131       TableName[] tableArray, Configuration conf,
132       Path backupRootPath, String backupId) throws IOException {
133     for (TableName tableName : tableArray) {
134       BackupManifest manifest = getManifest(tableName, conf, backupRootPath, backupId);
135       backupManifestMap.put(tableName, manifest);
136     }
137   }
138 }