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 org.apache.hadoop.fs.Path;
22  import org.apache.hadoop.hbase.testclassification.SmallTests;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.regionserver.HRegion;
25  import org.apache.hadoop.hbase.util.FSUtils;
26  import org.apache.hadoop.hbase.util.Pair;
27  import org.junit.Assert;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import java.util.regex.Matcher;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertFalse;
35  import static org.junit.Assert.assertTrue;
36  
37  /**
38   * Test that FileLink switches between alternate locations
39   * when the current location moves or gets deleted.
40   */
41  @Category(SmallTests.class)
42  public class TestHFileLink {
43  
44    @Test
45    public void testValidLinkNames() {
46      String validLinkNames[] = {"foo=fefefe-0123456", "ns=foo=abababa-fefefefe"};
47  
48      for(String name : validLinkNames) {
49        Assert.assertTrue("Failed validating:" + name, name.matches(HFileLink.LINK_NAME_REGEX));
50      }
51  
52      for(String name : validLinkNames) {
53        Assert.assertTrue("Failed validating:" + name, HFileLink.isHFileLink(name));
54      }
55  
56      String testName = "foo=fefefe-0123456";
57      Assert.assertEquals(TableName.valueOf("foo"),
58          HFileLink.getReferencedTableName(testName));
59      Assert.assertEquals("fefefe", HFileLink.getReferencedRegionName(testName));
60      Assert.assertEquals("0123456", HFileLink.getReferencedHFileName(testName));
61      Assert.assertEquals(testName,
62          HFileLink.createHFileLinkName(TableName.valueOf("foo"), "fefefe", "0123456"));
63  
64      testName = "ns=foo=fefefe-0123456";
65      Assert.assertEquals(TableName.valueOf("ns", "foo"),
66          HFileLink.getReferencedTableName(testName));
67      Assert.assertEquals("fefefe", HFileLink.getReferencedRegionName(testName));
68      Assert.assertEquals("0123456", HFileLink.getReferencedHFileName(testName));
69      Assert.assertEquals(testName,
70          HFileLink.createHFileLinkName(TableName.valueOf("ns", "foo"), "fefefe", "0123456"));
71  
72      for(String name : validLinkNames) {
73        Matcher m = HFileLink.LINK_NAME_PATTERN.matcher(name);
74        assertTrue(m.matches());
75        Assert.assertEquals(HFileLink.getReferencedTableName(name),
76            TableName.valueOf(m.group(1), m.group(2)));
77        Assert.assertEquals(HFileLink.getReferencedRegionName(name),
78            m.group(3));
79        Assert.assertEquals(HFileLink.getReferencedHFileName(name),
80            m.group(4));
81      }
82    }
83  
84    @Test
85    public void testBackReference() {
86      Path rootDir = new Path("/root");
87      Path archiveDir = new Path(rootDir, ".archive");
88      String storeFileName = "121212";
89      String linkDir = FileLink.BACK_REFERENCES_DIRECTORY_PREFIX + storeFileName;
90      String encodedRegion = "FEFE";
91      String cf = "cf1";
92  
93      TableName refTables[] = {TableName.valueOf("refTable"),
94          TableName.valueOf("ns", "refTable")};
95  
96      for(TableName refTable : refTables) {
97        Path refTableDir = FSUtils.getTableDir(archiveDir, refTable);
98        Path refRegionDir = HRegion.getRegionDir(refTableDir, encodedRegion);
99        Path refDir = new Path(refRegionDir, cf);
100       Path refLinkDir = new Path(refDir, linkDir);
101       String refStoreFileName = refTable.getNameAsString().replace(
102           TableName.NAMESPACE_DELIM, '=') + "=" + encodedRegion + "-" + storeFileName;
103 
104       TableName tableNames[] = {TableName.valueOf("tableName1"),
105           TableName.valueOf("ns", "tableName2")};
106 
107       for( TableName tableName : tableNames) {
108         Path tableDir = FSUtils.getTableDir(rootDir, tableName);
109         Path regionDir = HRegion.getRegionDir(tableDir, encodedRegion);
110         Path cfDir = new Path(regionDir, cf);
111 
112         //Verify back reference creation
113         assertEquals(encodedRegion+"."+
114             tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='),
115             HFileLink.createBackReferenceName(tableName.getNameAsString(),
116                 encodedRegion));
117 
118         //verify parsing back reference
119         Pair<TableName, String> parsedRef =
120             HFileLink.parseBackReferenceName(encodedRegion+"."+
121                 tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='));
122         assertEquals(parsedRef.getFirst(), tableName);
123         assertEquals(parsedRef.getSecond(), encodedRegion);
124 
125         //verify resolving back reference
126         Path storeFileDir =  new Path(refLinkDir, encodedRegion+"."+
127             tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='));
128         Path linkPath = new Path(cfDir, refStoreFileName);
129         assertEquals(linkPath, HFileLink.getHFileFromBackReference(rootDir, storeFileDir));
130       }
131     }
132   }
133 
134 
135 }