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  
19  package org.apache.hadoop.hbase.io;
20  
21  import java.io.IOException;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.regionserver.HRegion;
35  import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
36  import org.apache.hadoop.hbase.util.FSUtils;
37  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
38  
39  /**
40   * HFileLink describes a link to an hfile.
41   *
42   * An hfile can be served from a region or from the hfile archive directory (/hbase/.archive)
43   * HFileLink allows to access the referenced hfile regardless of the location where it is.
44   *
45   * <p>Searches for hfiles in the following order and locations:
46   * <ul>
47   *  <li>/hbase/table/region/cf/hfile</li>
48   *  <li>/hbase/.archive/table/region/cf/hfile</li>
49   * </ul>
50   *
51   * The link checks first in the original path if it is not present
52   * it fallbacks to the archived path.
53   */
54  @InterfaceAudience.Private
55  public class HFileLink extends FileLink {
56    private static final Log LOG = LogFactory.getLog(HFileLink.class);
57  
58    /**
59     * A non-capture group, for HFileLink, so that this can be embedded.
60     * The HFileLink describe a link to an hfile in a different table/region
61     * and the name is in the form: table=region-hfile.
62     * <p>
63     * Table name is ([a-zA-Z_0-9][a-zA-Z_0-9.-]*), so '=' is an invalid character for the table name.
64     * Region name is ([a-f0-9]+), so '-' is an invalid character for the region name.
65     * HFile is ([0-9a-f]+(?:_SeqId_[0-9]+_)?) covering the plain hfiles (uuid)
66     * and the bulk loaded (_SeqId_[0-9]+_) hfiles.
67     */
68    public static final String LINK_NAME_REGEX =
69      String.format("%s=%s-%s", HTableDescriptor.VALID_USER_TABLE_REGEX,
70        HRegionInfo.ENCODED_REGION_NAME_REGEX, StoreFileInfo.HFILE_NAME_REGEX);
71  
72    /** Define the HFile Link name parser in the form of: table=region-hfile */
73    private static final Pattern LINK_NAME_PATTERN =
74      Pattern.compile(String.format("^(%s)=(%s)-(%s)$", HTableDescriptor.VALID_USER_TABLE_REGEX,
75        HRegionInfo.ENCODED_REGION_NAME_REGEX, StoreFileInfo.HFILE_NAME_REGEX));
76  
77    /**
78     * The pattern should be used for hfile and reference links
79     * that can be found in /hbase/table/region/family/
80     */
81    private static final Pattern REF_OR_HFILE_LINK_PATTERN =
82      Pattern.compile(String.format("^(%s)=(%s)-(.+)$", HTableDescriptor.VALID_USER_TABLE_REGEX,
83        HRegionInfo.ENCODED_REGION_NAME_REGEX));
84  
85    private final Path archivePath;
86    private final Path originPath;
87    private final Path tempPath;
88  
89    /**
90     * @param conf {@link Configuration} from which to extract specific archive locations
91     * @param path The path of the HFile Link.
92     * @throws IOException on unexpected error.
93     */
94    public HFileLink(Configuration conf, Path path) throws IOException {
95      this(FSUtils.getRootDir(conf), HFileArchiveUtil.getArchivePath(conf), path);
96    }
97  
98    /**
99     * @param rootDir Path to the root directory where hbase files are stored
100    * @param archiveDir Path to the hbase archive directory
101    * @param path The path of the HFile Link.
102    */
103   public HFileLink(final Path rootDir, final Path archiveDir, final Path path) {
104     Path hfilePath = getRelativeTablePath(path);
105     this.tempPath = new Path(new Path(rootDir, HConstants.HBASE_TEMP_DIRECTORY), hfilePath);
106     this.originPath = new Path(rootDir, hfilePath);
107     this.archivePath = new Path(archiveDir, hfilePath);
108     setLocations(originPath, tempPath, archivePath);
109   }
110 
111   /**
112    * @return the origin path of the hfile.
113    */
114   public Path getOriginPath() {
115     return this.originPath;
116   }
117 
118   /**
119    * @return the path of the archived hfile.
120    */
121   public Path getArchivePath() {
122     return this.archivePath;
123   }
124 
125   /**
126    * @param path Path to check.
127    * @return True if the path is a HFileLink.
128    */
129   public static boolean isHFileLink(final Path path) {
130     return isHFileLink(path.getName());
131   }
132 
133 
134   /**
135    * @param fileName File name to check.
136    * @return True if the path is a HFileLink.
137    */
138   public static boolean isHFileLink(String fileName) {
139     Matcher m = LINK_NAME_PATTERN.matcher(fileName);
140     if (!m.matches()) return false;
141 
142     return m.groupCount() > 2 && m.group(3) != null && m.group(2) != null && m.group(1) != null;
143   }
144 
145   /**
146    * Convert a HFileLink path to a table relative path.
147    * e.g. the link: /hbase/test/0123/cf/testtb=4567-abcd
148    *      becomes: /hbase/testtb/4567/cf/abcd
149    *
150    * @param path HFileLink path
151    * @return Relative table path
152    * @throws IOException on unexpected error.
153    */
154   private static Path getRelativeTablePath(final Path path) {
155     // table=region-hfile
156     Matcher m = REF_OR_HFILE_LINK_PATTERN.matcher(path.getName());
157     if (!m.matches()) {
158       throw new IllegalArgumentException(path.getName() + " is not a valid HFileLink name!");
159     }
160 
161     // Convert the HFileLink name into a real table/region/cf/hfile path.
162     String tableName = m.group(1);
163     String regionName = m.group(2);
164     String hfileName = m.group(3);
165     String familyName = path.getParent().getName();
166     return new Path(new Path(tableName, regionName), new Path(familyName, hfileName));
167   }
168 
169   /**
170    * Get the HFile name of the referenced link
171    *
172    * @param fileName HFileLink file name
173    * @return the name of the referenced HFile
174    */
175   public static String getReferencedHFileName(final String fileName) {
176     Matcher m = REF_OR_HFILE_LINK_PATTERN.matcher(fileName);
177     if (!m.matches()) {
178       throw new IllegalArgumentException(fileName + " is not a valid HFileLink name!");
179     }
180     return(m.group(3));
181   }
182 
183   /**
184    * Get the Region name of the referenced link
185    *
186    * @param fileName HFileLink file name
187    * @return the name of the referenced Region
188    */
189   public static String getReferencedRegionName(final String fileName) {
190     Matcher m = REF_OR_HFILE_LINK_PATTERN.matcher(fileName);
191     if (!m.matches()) {
192       throw new IllegalArgumentException(fileName + " is not a valid HFileLink name!");
193     }
194     return(m.group(2));
195   }
196 
197   /**
198    * Get the Table name of the referenced link
199    *
200    * @param fileName HFileLink file name
201    * @return the name of the referenced Table
202    */
203   public static String getReferencedTableName(final String fileName) {
204     Matcher m = REF_OR_HFILE_LINK_PATTERN.matcher(fileName);
205     if (!m.matches()) {
206       throw new IllegalArgumentException(fileName + " is not a valid HFileLink name!");
207     }
208     return(m.group(1));
209   }
210 
211   /**
212    * Create a new HFileLink name
213    *
214    * @param hfileRegionInfo - Linked HFile Region Info
215    * @param hfileName - Linked HFile name
216    * @return file name of the HFile Link
217    */
218   public static String createHFileLinkName(final HRegionInfo hfileRegionInfo,
219       final String hfileName) {
220     return createHFileLinkName(hfileRegionInfo.getTableNameAsString(),
221                       hfileRegionInfo.getEncodedName(), hfileName);
222   }
223 
224   /**
225    * Create a new HFileLink name
226    *
227    * @param tableName - Linked HFile table name
228    * @param regionName - Linked HFile region name
229    * @param hfileName - Linked HFile name
230    * @return file name of the HFile Link
231    */
232   public static String createHFileLinkName(final String tableName,
233       final String regionName, final String hfileName) {
234     return String.format("%s=%s-%s", tableName, regionName, hfileName);
235   }
236 
237   /**
238    * Create a new HFileLink
239    *
240    * <p>It also adds a back-reference to the hfile back-reference directory
241    * to simplify the reference-count and the cleaning process.
242    *
243    * @param conf {@link Configuration} to read for the archive directory name
244    * @param fs {@link FileSystem} on which to write the HFileLink
245    * @param dstFamilyPath - Destination path (table/region/cf/)
246    * @param hfileRegionInfo - Linked HFile Region Info
247    * @param hfileName - Linked HFile name
248    * @return true if the file is created, otherwise the file exists.
249    * @throws IOException on file or parent directory creation failure
250    */
251   public static boolean create(final Configuration conf, final FileSystem fs,
252       final Path dstFamilyPath, final HRegionInfo hfileRegionInfo,
253       final String hfileName) throws IOException {
254     String linkedTable = hfileRegionInfo.getTableNameAsString();
255     String linkedRegion = hfileRegionInfo.getEncodedName();
256     return create(conf, fs, dstFamilyPath, linkedTable, linkedRegion, hfileName);
257   }
258 
259   /**
260    * Create a new HFileLink
261    *
262    * <p>It also adds a back-reference to the hfile back-reference directory
263    * to simplify the reference-count and the cleaning process.
264    *
265    * @param conf {@link Configuration} to read for the archive directory name
266    * @param fs {@link FileSystem} on which to write the HFileLink
267    * @param dstFamilyPath - Destination path (table/region/cf/)
268    * @param linkedTable - Linked Table Name
269    * @param linkedRegion - Linked Region Name
270    * @param hfileName - Linked HFile name
271    * @return true if the file is created, otherwise the file exists.
272    * @throws IOException on file or parent directory creation failure
273    */
274   public static boolean create(final Configuration conf, final FileSystem fs,
275       final Path dstFamilyPath, final String linkedTable, final String linkedRegion,
276       final String hfileName) throws IOException {
277     String familyName = dstFamilyPath.getName();
278     String regionName = dstFamilyPath.getParent().getName();
279     String tableName = dstFamilyPath.getParent().getParent().getName();
280 
281     String name = createHFileLinkName(linkedTable, linkedRegion, hfileName);
282     String refName = createBackReferenceName(tableName, regionName);
283 
284     // Make sure the destination directory exists
285     fs.mkdirs(dstFamilyPath);
286 
287     // Make sure the FileLink reference directory exists
288     Path archiveStoreDir = HFileArchiveUtil.getStoreArchivePath(conf,
289           linkedTable, linkedRegion, familyName);
290     Path backRefssDir = getBackReferencesDir(archiveStoreDir, hfileName);
291     fs.mkdirs(backRefssDir);
292 
293     // Create the reference for the link
294     Path backRefPath = new Path(backRefssDir, refName);
295     fs.createNewFile(backRefPath);
296     try {
297       // Create the link
298       return fs.createNewFile(new Path(dstFamilyPath, name));
299     } catch (IOException e) {
300       LOG.error("couldn't create the link=" + name + " for " + dstFamilyPath, e);
301       // Revert the reference if the link creation failed
302       fs.delete(backRefPath, false);
303       throw e;
304     }
305   }
306 
307   /**
308    * Create a new HFileLink starting from a hfileLink name
309    *
310    * <p>It also adds a back-reference to the hfile back-reference directory
311    * to simplify the reference-count and the cleaning process.
312    *
313    * @param conf {@link Configuration} to read for the archive directory name
314    * @param fs {@link FileSystem} on which to write the HFileLink
315    * @param dstFamilyPath - Destination path (table/region/cf/)
316    * @param hfileLinkName - HFileLink name (it contains hfile-region-table)
317    * @return true if the file is created, otherwise the file exists.
318    * @throws IOException on file or parent directory creation failure
319    */
320   public static boolean createFromHFileLink(final Configuration conf, final FileSystem fs,
321       final Path dstFamilyPath, final String hfileLinkName) throws IOException {
322     Matcher m = LINK_NAME_PATTERN.matcher(hfileLinkName);
323     if (!m.matches()) {
324       throw new IllegalArgumentException(hfileLinkName + " is not a valid HFileLink name!");
325     }
326     return create(conf, fs, dstFamilyPath, m.group(1), m.group(2), m.group(3));
327   }
328 
329   /**
330    * Create the back reference name
331    */
332   private static String createBackReferenceName(final String tableName, final String regionName) {
333     return regionName + "." + tableName;
334   }
335 
336   /**
337    * Get the full path of the HFile referenced by the back reference
338    *
339    * @param rootDir root hbase directory
340    * @param linkRefPath Link Back Reference path
341    * @return full path of the referenced hfile
342    * @throws IOException on unexpected error.
343    */
344   public static Path getHFileFromBackReference(final Path rootDir, final Path linkRefPath) {
345     int separatorIndex = linkRefPath.getName().indexOf('.');
346     String linkRegionName = linkRefPath.getName().substring(0, separatorIndex);
347     String linkTableName = linkRefPath.getName().substring(separatorIndex + 1);
348     String hfileName = getBackReferenceFileName(linkRefPath.getParent());
349     Path familyPath = linkRefPath.getParent().getParent();
350     Path regionPath = familyPath.getParent();
351     Path tablePath = regionPath.getParent();
352 
353     String linkName = createHFileLinkName(tablePath.getName(), regionPath.getName(), hfileName);
354     Path linkTableDir = FSUtils.getTablePath(rootDir, linkTableName);
355     Path regionDir = HRegion.getRegionDir(linkTableDir, linkRegionName);
356     return new Path(new Path(regionDir, familyPath.getName()), linkName);
357   }
358 
359   /**
360    * Get the full path of the HFile referenced by the back reference
361    *
362    * @param conf {@link Configuration} to read for the archive directory name
363    * @param linkRefPath Link Back Reference path
364    * @return full path of the referenced hfile
365    * @throws IOException on unexpected error.
366    */
367   public static Path getHFileFromBackReference(final Configuration conf, final Path linkRefPath)
368       throws IOException {
369     return getHFileFromBackReference(FSUtils.getRootDir(conf), linkRefPath);
370   }
371 }