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 return getStoreArchivePath(conf, region, family); 68 } 69 70 /** 71 * Gets the directory to archive a store directory. 72 * @param conf {@link Configuration} to read for the archive directory name. 73 * @param region parent region information 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, 79 HRegionInfo region, 80 byte[] family) throws IOException { 81 Path rootDir = FSUtils.getRootDir(conf); 82 Path tableArchiveDir = getTableArchivePath(rootDir, region.getTable()); 83 return HStore.getStoreHomedir(tableArchiveDir, region, family); 84 } 85 86 /** 87 * Get the archive directory for a given region under the specified table 88 * @param tableName the table name. 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(Path rootDir, 94 TableName tableName, 95 Path regiondir) { 96 // get the archive directory for a table 97 Path archiveDir = getTableArchivePath(rootDir, tableName); 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 archive directory for a given region under the specified table 106 * @param rootDir {@link Path} to the root directory where hbase files are stored (for building 107 * the archive path) 108 * @param tableName name of the table to archive. 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 111 */ 112 public static Path getRegionArchiveDir(Path rootDir, 113 TableName tableName, String encodedRegionName) { 114 // get the archive directory for a table 115 Path archiveDir = getTableArchivePath(rootDir, tableName); 116 return HRegion.getRegionDir(archiveDir, encodedRegionName); 117 } 118 119 /** 120 * Get the path to the table archive directory based on the configured archive directory. 121 * <p> 122 * Get the path to the table's archive directory. 123 * <p> 124 * Generally of the form: /hbase/.archive/[tablename] 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 be archived. Cannot be null. 128 * @return {@link Path} to the archive directory for the table 129 */ 130 public static Path getTableArchivePath(final Path rootdir, final TableName tableName) { 131 return FSUtils.getTableDir(getArchivePath(rootdir), tableName); 132 } 133 134 /** 135 * Get the path to the table archive directory based on the configured archive directory. 136 * <p> 137 * Assumed that the table should already be archived. 138 * @param conf {@link Configuration} to read the archive directory property. Can be null 139 * @param tableName Name of the table to be archived. Cannot be null. 140 * @return {@link Path} to the archive directory for the table 141 */ 142 public static Path getTableArchivePath(final Configuration conf, 143 final TableName tableName) 144 throws IOException { 145 return FSUtils.getTableDir(getArchivePath(conf), tableName); 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 conf to look for archive directory name and root directory. Cannot be null. Notes for 152 * testing: requires a FileSystem root directory to be specified. 153 * @return the full {@link Path} to the archive directory, as defined by the configuration 154 * @throws IOException if an unexpected error occurs 155 */ 156 public static Path getArchivePath(Configuration conf) throws IOException { 157 return getArchivePath(FSUtils.getRootDir(conf)); 158 } 159 160 /** 161 * Get the full path to the archive directory on the configured 162 * {@link org.apache.hadoop.hbase.master.MasterFileSystem} 163 * @param rootdir {@link Path} to the root directory where hbase files are stored (for building 164 * the archive path) 165 * @return the full {@link Path} to the archive directory, as defined by the configuration 166 */ 167 private static Path getArchivePath(final Path rootdir) { 168 return new Path(rootdir, HConstants.HFILE_ARCHIVE_DIRECTORY); 169 } 170 171 /* 172 * @return table name given archive file path 173 */ 174 public static TableName getTableName(Path archivePath) { 175 Path p = archivePath; 176 String tbl = null; 177 // namespace is the 4th parent of file 178 for (int i = 0; i < 5; i++) { 179 if (p == null) return null; 180 if (i == 3) tbl = p.getName(); 181 p = p.getParent(); 182 } 183 if (p == null) return null; 184 return TableName.valueOf(p.getName(), tbl); 185 } 186 }