View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.fs.FileSystem;
24  import org.apache.hadoop.fs.Path;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.regionserver.HRegion;
29  import org.apache.hadoop.hbase.regionserver.HStore;
30  
31  /**
32   * Helper class for all utilities related to archival/retrieval of HFiles
33   */
34  public class HFileArchiveUtil {
35    private HFileArchiveUtil() {
36      // non-external instantiation - util class
37    }
38  
39    /**
40     * Get the directory to archive a store directory
41     * @param conf {@link Configuration} to read for the archive directory name
42     * @param tableName table name under which the store currently lives
43     * @param regionName region encoded name under which the store currently lives
44     * @param familyName name of the family in the store
45     * @return {@link Path} to the directory to archive the given store or
46     *         <tt>null</tt> if it should not be archived
47     */
48    public static Path getStoreArchivePath(final Configuration conf,
49                                           final TableName tableName,
50        final String regionName, final String familyName) throws IOException {
51      Path tableArchiveDir = getTableArchivePath(conf, tableName);
52      return HStore.getStoreHomedir(tableArchiveDir, regionName, Bytes.toBytes(familyName));
53    }
54  
55    /**
56     * Get the directory to archive a store directory
57     * @param conf {@link Configuration} to read for the archive directory name.
58     * @param region parent region information under which the store currently lives
59     * @param tabledir directory for the table under which the store currently lives
60     * @param family name of the family in the store
61     * @return {@link Path} to the directory to archive the given store or <tt>null</tt> if it should
62     *         not be archived
63     */
64    public static Path getStoreArchivePath(Configuration conf,
65                                           HRegionInfo region,
66                                           Path tabledir,
67        byte[] family) throws IOException {
68      TableName tableName =
69          FSUtils.getTableName(tabledir);
70      Path rootDir = FSUtils.getRootDir(conf);
71      Path tableArchiveDir = getTableArchivePath(rootDir, tableName);
72      return HStore.getStoreHomedir(tableArchiveDir, region, family);
73    }
74  
75    /**
76     * Get the archive directory for a given region under the specified table
77     * @param tableName the table name. Cannot be null.
78     * @param regiondir the path to the region directory. Cannot be null.
79     * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it
80     *         should not be archived
81     */
82    public static Path getRegionArchiveDir(Path rootDir,
83                                           TableName tableName,
84                                           Path regiondir) {
85      // get the archive directory for a table
86      Path archiveDir = getTableArchivePath(rootDir, tableName);
87  
88      // then add on the region path under the archive
89      String encodedRegionName = regiondir.getName();
90      return HRegion.getRegionDir(archiveDir, encodedRegionName);
91    }
92  
93    /**
94     * Get the archive directory for a given region under the specified table
95     * @param rootDir {@link Path} to the root directory where hbase files are stored (for building
96     *          the archive path)
97     * @param tableName name of the table to archive. Cannot be null.
98     * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it
99     *         should not be archived
100    */
101   public static Path getRegionArchiveDir(Path rootDir,
102                                          TableName tableName, String encodedRegionName) {
103     // get the archive directory for a table
104     Path archiveDir = getTableArchivePath(rootDir, tableName);
105     return HRegion.getRegionDir(archiveDir, encodedRegionName);
106   }
107 
108   /**
109    * Get the path to the table archive directory based on the configured archive directory.
110    * <p>
111    * Get the path to the table's archive directory.
112    * <p>
113    * Generally of the form: /hbase/.archive/[tablename]
114    * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
115    *          the archive path)
116    * @param tableName Name of the table to be archived. Cannot be null.
117    * @return {@link Path} to the archive directory for the table
118    */
119   public static Path getTableArchivePath(final Path rootdir, final TableName tableName) {
120     return FSUtils.getTableDir(getArchivePath(rootdir), tableName);
121   }
122 
123   /**
124    * Get the path to the table archive directory based on the configured archive directory.
125    * <p>
126    * Assumed that the table should already be archived.
127    * @param conf {@link Configuration} to read the archive directory property. Can be null
128    * @param tableName Name of the table to be archived. Cannot be null.
129    * @return {@link Path} to the archive directory for the table
130    */
131   public static Path getTableArchivePath(final Configuration conf,
132                                          final TableName tableName)
133       throws IOException {
134     return FSUtils.getTableDir(getArchivePath(conf), tableName);
135   }
136 
137   /**
138    * Get the full path to the archive directory on the configured {@link FileSystem}
139    * @param conf to look for archive directory name and root directory. Cannot be null. Notes for
140    *          testing: requires a FileSystem root directory to be specified.
141    * @return the full {@link Path} to the archive directory, as defined by the configuration
142    * @throws IOException if an unexpected error occurs
143    */
144   public static Path getArchivePath(Configuration conf) throws IOException {
145     return getArchivePath(FSUtils.getRootDir(conf));
146   }
147 
148   /**
149    * Get the full path to the archive directory on the configured {@link FileSystem}
150    * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
151    *          the archive path)
152    * @return the full {@link Path} to the archive directory, as defined by the configuration
153    */
154   private static Path getArchivePath(final Path rootdir) {
155     return new Path(rootdir, HConstants.HFILE_ARCHIVE_DIRECTORY);
156   }
157 }