HBASE-23197 'IllegalArgumentException: Wrong FS' on edits replay when… (#740)
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / util / HFileArchiveUtil.java
blob76d46f64470d9a9154a9a8424ca208797087a2c3
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.util;
20 import java.io.IOException;
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.fs.Path;
24 import org.apache.hadoop.hbase.HConstants;
25 import org.apache.hadoop.hbase.TableName;
26 import org.apache.hadoop.hbase.client.RegionInfo;
27 import org.apache.hadoop.hbase.regionserver.HRegion;
28 import org.apache.hadoop.hbase.regionserver.HStore;
29 import org.apache.yetus.audience.InterfaceAudience;
31 /**
32 * Helper class for all utilities related to archival/retrieval of HFiles
34 @InterfaceAudience.Private
35 public final class HFileArchiveUtil {
36 private HFileArchiveUtil() {
37 // non-external instantiation - util class
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
49 public static Path getStoreArchivePath(final Configuration conf,
50 final TableName tableName,
51 final String regionName, final String familyName) throws IOException {
52 Path tableArchiveDir = getTableArchivePath(conf, tableName);
53 return HStore.getStoreHomedir(tableArchiveDir, regionName, Bytes.toBytes(familyName));
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 lives
60 * @param tabledir directory for the table under which the store currently lives
61 * @param family name of the family in the store
62 * @return {@link Path} to the directory to archive the given store or <tt>null</tt> if it should
63 * not be archived
65 public static Path getStoreArchivePath(Configuration conf,
66 RegionInfo region,
67 Path tabledir,
68 byte[] family) throws IOException {
69 return getStoreArchivePath(conf, region, family);
72 /**
73 * Gets the directory to archive a store directory.
74 * @param conf {@link Configuration} to read for the archive directory name.
75 * @param region parent region information under which the store currently lives
76 * @param family name of the family in the store
77 * @return {@link Path} to the directory to archive the given store or <tt>null</tt> if it should
78 * not be archived
80 public static Path getStoreArchivePath(Configuration conf,
81 RegionInfo region,
82 byte[] family) throws IOException {
83 Path rootDir = FSUtils.getRootDir(conf);
84 Path tableArchiveDir = getTableArchivePath(rootDir, region.getTable());
85 return HStore.getStoreHomedir(tableArchiveDir, region, family);
88 /**
89 * Gets the archive directory under specified root dir. One scenario where this is useful is
90 * when WAL and root dir are configured under different file systems,
91 * i.e. root dir on S3 and WALs on HDFS.
92 * This is mostly useful for archiving recovered edits, when
93 * <b>hbase.region.archive.recovered.edits</b> is enabled.
94 * @param rootDir {@link Path} the root dir under which archive path should be created.
95 * @param region parent region information under which the store currently lives
96 * @param family name of the family in the store
97 * @return {@link Path} to the WAL FS directory to archive the given store
98 * or <tt>null</tt> if it should not be archived
100 public static Path getStoreArchivePathForRootDir(Path rootDir, RegionInfo region, byte[] family) {
101 Path tableArchiveDir = getTableArchivePath(rootDir, region.getTable());
102 return HStore.getStoreHomedir(tableArchiveDir, region, family);
106 * Get the archive directory for a given region under the specified table
107 * @param tableName the table name. Cannot be null.
108 * @param regiondir the path to the region directory. Cannot be null.
109 * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it
110 * should not be archived
112 public static Path getRegionArchiveDir(Path rootDir,
113 TableName tableName,
114 Path regiondir) {
115 // get the archive directory for a table
116 Path archiveDir = getTableArchivePath(rootDir, tableName);
118 // then add on the region path under the archive
119 String encodedRegionName = regiondir.getName();
120 return HRegion.getRegionDir(archiveDir, encodedRegionName);
124 * Get the archive directory for a given region under the specified table
125 * @param rootDir {@link Path} to the root directory where hbase files are stored (for building
126 * the archive path)
127 * @param tableName name of the table to archive. Cannot be null.
128 * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it
129 * should not be archived
131 public static Path getRegionArchiveDir(Path rootDir,
132 TableName tableName, String encodedRegionName) {
133 // get the archive directory for a table
134 Path archiveDir = getTableArchivePath(rootDir, tableName);
135 return HRegion.getRegionDir(archiveDir, encodedRegionName);
139 * Get the path to the table archive directory based on the configured archive directory.
140 * <p>
141 * Get the path to the table's archive directory.
142 * <p>
143 * Generally of the form: /hbase/.archive/[tablename]
144 * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
145 * the archive path)
146 * @param tableName Name of the table to be archived. Cannot be null.
147 * @return {@link Path} to the archive directory for the table
149 public static Path getTableArchivePath(final Path rootdir, final TableName tableName) {
150 return FSUtils.getTableDir(getArchivePath(rootdir), tableName);
154 * Get the path to the table archive directory based on the configured archive directory.
155 * <p>
156 * Assumed that the table should already be archived.
157 * @param conf {@link Configuration} to read the archive directory property. Can be null
158 * @param tableName Name of the table to be archived. Cannot be null.
159 * @return {@link Path} to the archive directory for the table
161 public static Path getTableArchivePath(final Configuration conf,
162 final TableName tableName)
163 throws IOException {
164 return FSUtils.getTableDir(getArchivePath(conf), tableName);
168 * Get the full path to the archive directory on the configured
169 * {@link org.apache.hadoop.hbase.master.MasterFileSystem}
170 * @param conf to look for archive directory name and root directory. Cannot be null. Notes for
171 * testing: requires a FileSystem root directory to be specified.
172 * @return the full {@link Path} to the archive directory, as defined by the configuration
173 * @throws IOException if an unexpected error occurs
175 public static Path getArchivePath(Configuration conf) throws IOException {
176 return getArchivePath(FSUtils.getRootDir(conf));
180 * Get the full path to the archive directory on the configured
181 * {@link org.apache.hadoop.hbase.master.MasterFileSystem}
182 * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
183 * the archive path)
184 * @return the full {@link Path} to the archive directory, as defined by the configuration
186 private static Path getArchivePath(final Path rootdir) {
187 return new Path(rootdir, HConstants.HFILE_ARCHIVE_DIRECTORY);
191 * @return table name given archive file path
193 public static TableName getTableName(Path archivePath) {
194 Path p = archivePath;
195 String tbl = null;
196 // namespace is the 4th parent of file
197 for (int i = 0; i < 5; i++) {
198 if (p == null) return null;
199 if (i == 3) tbl = p.getName();
200 p = p.getParent();
202 if (p == null) return null;
203 return TableName.valueOf(p.getName(), tbl);