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