001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import java.io.IOException; 021 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.fs.Path; 024import org.apache.hadoop.hbase.HConstants; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.client.RegionInfo; 027import org.apache.hadoop.hbase.regionserver.HRegion; 028import org.apache.hadoop.hbase.regionserver.HStore; 029import org.apache.yetus.audience.InterfaceAudience; 030 031/** 032 * Helper class for all utilities related to archival/retrieval of HFiles 033 */ 034@InterfaceAudience.Private 035public final class HFileArchiveUtil { 036 private HFileArchiveUtil() { 037 // non-external instantiation - util class 038 } 039 040 /** 041 * Get the directory to archive a store directory 042 * @param conf {@link Configuration} to read for the archive directory name 043 * @param tableName table name under which the store currently lives 044 * @param regionName region encoded name under which the store currently lives 045 * @param familyName name of the family in the store 046 * @return {@link Path} to the directory to archive the given store or 047 * <tt>null</tt> if it should not be archived 048 */ 049 public static Path getStoreArchivePath(final Configuration conf, 050 final TableName tableName, 051 final String regionName, final String familyName) throws IOException { 052 Path tableArchiveDir = getTableArchivePath(conf, tableName); 053 return HStore.getStoreHomedir(tableArchiveDir, regionName, Bytes.toBytes(familyName)); 054 } 055 056 /** 057 * Get the directory to archive a store directory 058 * @param conf {@link Configuration} to read for the archive directory name. 059 * @param region parent region information under which the store currently lives 060 * @param tabledir directory for the table under which the store currently lives 061 * @param family name of the family in the store 062 * @return {@link Path} to the directory to archive the given store or <tt>null</tt> if it should 063 * not be archived 064 */ 065 public static Path getStoreArchivePath(Configuration conf, 066 RegionInfo region, 067 Path tabledir, 068 byte[] family) throws IOException { 069 return getStoreArchivePath(conf, region, family); 070 } 071 072 /** 073 * Gets the directory to archive a store directory. 074 * @param conf {@link Configuration} to read for the archive directory name. 075 * @param region parent region information under which the store currently lives 076 * @param family name of the family in the store 077 * @return {@link Path} to the directory to archive the given store or <tt>null</tt> if it should 078 * not be archived 079 */ 080 public static Path getStoreArchivePath(Configuration conf, 081 RegionInfo region, 082 byte[] family) throws IOException { 083 Path rootDir = FSUtils.getRootDir(conf); 084 Path tableArchiveDir = getTableArchivePath(rootDir, region.getTable()); 085 return HStore.getStoreHomedir(tableArchiveDir, region, family); 086 } 087 088 /** 089 * Get the archive directory for a given region under the specified table 090 * @param tableName the table name. Cannot be null. 091 * @param regiondir the path to the region directory. Cannot be null. 092 * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it 093 * should not be archived 094 */ 095 public static Path getRegionArchiveDir(Path rootDir, 096 TableName tableName, 097 Path regiondir) { 098 // get the archive directory for a table 099 Path archiveDir = getTableArchivePath(rootDir, tableName); 100 101 // then add on the region path under the archive 102 String encodedRegionName = regiondir.getName(); 103 return HRegion.getRegionDir(archiveDir, encodedRegionName); 104 } 105 106 /** 107 * Get the archive directory for a given region under the specified table 108 * @param rootDir {@link Path} to the root directory where hbase files are stored (for building 109 * the archive path) 110 * @param tableName name of the table to archive. Cannot be null. 111 * @return {@link Path} to the directory to archive the given region, or <tt>null</tt> if it 112 * should not be archived 113 */ 114 public static Path getRegionArchiveDir(Path rootDir, 115 TableName tableName, String encodedRegionName) { 116 // get the archive directory for a table 117 Path archiveDir = getTableArchivePath(rootDir, tableName); 118 return HRegion.getRegionDir(archiveDir, encodedRegionName); 119 } 120 121 /** 122 * Get the path to the table archive directory based on the configured archive directory. 123 * <p> 124 * Get the path to the table's archive directory. 125 * <p> 126 * Generally of the form: /hbase/.archive/[tablename] 127 * @param rootdir {@link Path} to the root directory where hbase files are stored (for building 128 * the archive path) 129 * @param tableName Name of the table to be archived. Cannot be null. 130 * @return {@link Path} to the archive directory for the table 131 */ 132 public static Path getTableArchivePath(final Path rootdir, final TableName tableName) { 133 return FSUtils.getTableDir(getArchivePath(rootdir), tableName); 134 } 135 136 /** 137 * Get the path to the table archive directory based on the configured archive directory. 138 * <p> 139 * Assumed that the table should already be archived. 140 * @param conf {@link Configuration} to read the archive directory property. Can be null 141 * @param tableName Name of the table to be archived. Cannot be null. 142 * @return {@link Path} to the archive directory for the table 143 */ 144 public static Path getTableArchivePath(final Configuration conf, 145 final TableName tableName) 146 throws IOException { 147 return FSUtils.getTableDir(getArchivePath(conf), tableName); 148 } 149 150 /** 151 * Get the full path to the archive directory on the configured 152 * {@link org.apache.hadoop.hbase.master.MasterFileSystem} 153 * @param conf to look for archive directory name and root directory. Cannot be null. Notes for 154 * testing: requires a FileSystem root directory to be specified. 155 * @return the full {@link Path} to the archive directory, as defined by the configuration 156 * @throws IOException if an unexpected error occurs 157 */ 158 public static Path getArchivePath(Configuration conf) throws IOException { 159 return getArchivePath(FSUtils.getRootDir(conf)); 160 } 161 162 /** 163 * Get the full path to the archive directory on the configured 164 * {@link org.apache.hadoop.hbase.master.MasterFileSystem} 165 * @param rootdir {@link Path} to the root directory where hbase files are stored (for building 166 * the archive path) 167 * @return the full {@link Path} to the archive directory, as defined by the configuration 168 */ 169 private static Path getArchivePath(final Path rootdir) { 170 return new Path(rootdir, HConstants.HFILE_ARCHIVE_DIRECTORY); 171 } 172 173 /* 174 * @return table name given archive file path 175 */ 176 public static TableName getTableName(Path archivePath) { 177 Path p = archivePath; 178 String tbl = null; 179 // namespace is the 4th parent of file 180 for (int i = 0; i < 5; i++) { 181 if (p == null) return null; 182 if (i == 3) tbl = p.getName(); 183 p = p.getParent(); 184 } 185 if (p == null) return null; 186 return TableName.valueOf(p.getName(), tbl); 187 } 188}