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