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.Path;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.regionserver.HStore;
28  
29  /**
30   * Helper class for all utilities related to archival/retrieval of HFiles
31   */
32  public class HFileArchiveUtil {
33    private HFileArchiveUtil() {
34      // non-external instantiation - util class
35    }
36  
37    /**
38     * Get the directory to archive a store directory
39     * @param conf {@link Configuration} to read for the archive directory name
40     * @param tableName table name under which the store currently lives
41     * @param regionName region encoded name under which the store currently lives
42     * @param familyName name of the family in the store
43     * @return {@link Path} to the directory to archive the given store or
44     *         <tt>null</tt> if it should not be archived
45     */
46    public static Path getStoreArchivePath(final Configuration conf,
47                                           final TableName tableName,
48        final String regionName, final String familyName) throws IOException {
49      Path tableArchiveDir = getTableArchivePath(conf, tableName);
50      return HStore.getStoreHomedir(tableArchiveDir, regionName, Bytes.toBytes(familyName));
51    }
52  
53    /**
54     * Get the directory to archive a store directory
55     * @param conf {@link Configuration} to read for the archive directory name.
56     * @param region parent region information under which the store currently lives
57     * @param tabledir directory for the table under which the store currently lives
58     * @param family name of the family in the store
59     * @return {@link Path} to the directory to archive the given store or <tt>null</tt> if it should
60     *         not be archived
61     */
62    public static Path getStoreArchivePath(Configuration conf,
63                                           HRegionInfo region,
64                                           Path tabledir,
65        byte[] family) throws IOException {
66      TableName tableName =
67          FSUtils.getTableName(tabledir);
68      Path rootDir = FSUtils.getRootDir(conf);
69      Path tableArchiveDir = getTableArchivePath(rootDir, tableName);
70      return HStore.getStoreHomedir(tableArchiveDir, region, family);
71    }
72  
73    /**
74     * Get the archive directory for a given region under the specified table
75     * @param tableName the table name. Cannot be null.
76     * @param regiondir the path to the region directory. Cannot be null.
77     * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it
78     *         should not be archived
79     */
80    public static Path getRegionArchiveDir(Path rootDir, TableName tableName, Path regiondir) {
81      // get the archive directory for a table
82      Path archiveDir = getTableArchivePath(rootDir, tableName);
83      // then add on the region path under the archive
84      String encodedRegionName = regiondir.getName();
85      return new Path(archiveDir, encodedRegionName);
86    }
87  
88    /**
89     * Get the archive directory for a given region under the specified table
90     * @param rootDir {@link Path} to the root directory where hbase files are stored (for building
91     *          the archive path)
92     * @param tableName name of the table to archive. Cannot be null.
93     * @param encodedRegionName encoded region name
94     * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it
95     *         should not be archived
96     */
97    public static Path getRegionArchiveDir(Path rootDir, TableName tableName,
98        String encodedRegionName) {
99      // get the archive directory for a table
100     Path archiveDir = getTableArchivePath(rootDir, tableName);
101     return new Path(archiveDir, encodedRegionName);
102   }
103 
104   /**
105    * Get the path to the table archive directory based on the configured archive directory.
106    * <p>
107    * Get the path to the table's archive directory.
108    * <p>
109    * Generally of the form: /hbase/.archive/[tablename]
110    * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
111    *          the archive path)
112    * @param tableName Name of the table to be archived. Cannot be null.
113    * @return {@link Path} to the archive directory for the table
114    */
115   public static Path getTableArchivePath(final Path rootdir, final TableName tableName) {
116     return FSUtils.getTableDir(getArchivePath(rootdir), tableName);
117   }
118 
119   /**
120    * Get the path to the table archive directory based on the configured archive directory.
121    * <p>
122    * Assumed that the table should already be archived.
123    * @param conf {@link Configuration} to read the archive directory property. Can be null
124    * @param tableName Name of the table to be archived. Cannot be null.
125    * @return {@link Path} to the archive directory for the table
126    */
127   public static Path getTableArchivePath(final Configuration conf,
128                                          final TableName tableName)
129       throws IOException {
130     return FSUtils.getTableDir(getArchivePath(conf), tableName);
131   }
132 
133   /**
134    * Get the full path to the archive directory on the configured 
135    * {@link org.apache.hadoop.hbase.master.MasterFileSystem}
136    * @param conf to look for archive directory name and root directory. Cannot be null. Notes for
137    *          testing: requires a FileSystem root directory to be specified.
138    * @return the full {@link Path} to the archive directory, as defined by the configuration
139    * @throws IOException if an unexpected error occurs
140    */
141   public static Path getArchivePath(Configuration conf) throws IOException {
142     return getArchivePath(FSUtils.getRootDir(conf));
143   }
144 
145   /**
146    * Get the full path to the archive directory on the configured 
147    * {@link org.apache.hadoop.hbase.master.MasterFileSystem}
148    * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
149    *          the archive path)
150    * @return the full {@link Path} to the archive directory, as defined by the configuration
151    */
152   private static Path getArchivePath(final Path rootdir) {
153     return new Path(rootdir, HConstants.HFILE_ARCHIVE_DIRECTORY);
154   }
155 }