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