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