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.io;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.regex.Matcher;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.regionserver.HRegion;
028import org.apache.hadoop.hbase.testclassification.IOTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.FSUtils;
031import org.apache.hadoop.hbase.util.Pair;
032import org.junit.Assert;
033import org.junit.ClassRule;
034import org.junit.Rule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037import org.junit.rules.TestName;
038
039/**
040 * Test that FileLink switches between alternate locations
041 * when the current location moves or gets deleted.
042 */
043@Category({IOTests.class, SmallTests.class})
044public class TestHFileLink {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestHFileLink.class);
049
050  @Rule
051  public TestName name = new TestName();
052
053  @Test
054  public void testValidLinkNames() {
055    String validLinkNames[] = {"foo=fefefe-0123456", "ns=foo=abababa-fefefefe"};
056
057    for(String name : validLinkNames) {
058      Assert.assertTrue("Failed validating:" + name, name.matches(HFileLink.LINK_NAME_REGEX));
059    }
060
061    for(String name : validLinkNames) {
062      Assert.assertTrue("Failed validating:" + name, HFileLink.isHFileLink(name));
063    }
064
065    String testName = name.getMethodName() + "=fefefe-0123456";
066    Assert.assertEquals(TableName.valueOf(name.getMethodName()),
067        HFileLink.getReferencedTableName(testName));
068    Assert.assertEquals("fefefe", HFileLink.getReferencedRegionName(testName));
069    Assert.assertEquals("0123456", HFileLink.getReferencedHFileName(testName));
070    Assert.assertEquals(testName,
071        HFileLink.createHFileLinkName(TableName.valueOf(name.getMethodName()), "fefefe", "0123456"));
072
073    testName = "ns=" + name.getMethodName() + "=fefefe-0123456";
074    Assert.assertEquals(TableName.valueOf("ns", name.getMethodName()),
075        HFileLink.getReferencedTableName(testName));
076    Assert.assertEquals("fefefe", HFileLink.getReferencedRegionName(testName));
077    Assert.assertEquals("0123456", HFileLink.getReferencedHFileName(testName));
078    Assert.assertEquals(testName,
079        HFileLink.createHFileLinkName(TableName.valueOf("ns", name.getMethodName()), "fefefe", "0123456"));
080
081    for(String name : validLinkNames) {
082      Matcher m = HFileLink.LINK_NAME_PATTERN.matcher(name);
083      assertTrue(m.matches());
084      Assert.assertEquals(HFileLink.getReferencedTableName(name),
085          TableName.valueOf(m.group(1), m.group(2)));
086      Assert.assertEquals(HFileLink.getReferencedRegionName(name),
087          m.group(3));
088      Assert.assertEquals(HFileLink.getReferencedHFileName(name),
089          m.group(4));
090    }
091  }
092
093  @Test
094  public void testBackReference() {
095    Path rootDir = new Path("/root");
096    Path archiveDir = new Path(rootDir, ".archive");
097    String storeFileName = "121212";
098    String linkDir = FileLink.BACK_REFERENCES_DIRECTORY_PREFIX + storeFileName;
099    String encodedRegion = "FEFE";
100    String cf = "cf1";
101
102    TableName refTables[] = {TableName.valueOf(name.getMethodName()),
103        TableName.valueOf("ns", name.getMethodName())};
104
105    for(TableName refTable : refTables) {
106      Path refTableDir = FSUtils.getTableDir(archiveDir, refTable);
107      Path refRegionDir = HRegion.getRegionDir(refTableDir, encodedRegion);
108      Path refDir = new Path(refRegionDir, cf);
109      Path refLinkDir = new Path(refDir, linkDir);
110      String refStoreFileName = refTable.getNameAsString().replace(
111          TableName.NAMESPACE_DELIM, '=') + "=" + encodedRegion + "-" + storeFileName;
112
113      TableName tableNames[] = {TableName.valueOf(name.getMethodName() + "1"),
114              TableName.valueOf("ns", name.getMethodName() + "2"),
115              TableName.valueOf(name.getMethodName()+ ":" +name.getMethodName())};
116
117      for( TableName tableName : tableNames) {
118        Path tableDir = FSUtils.getTableDir(rootDir, tableName);
119        Path regionDir = HRegion.getRegionDir(tableDir, encodedRegion);
120        Path cfDir = new Path(regionDir, cf);
121
122        //Verify back reference creation
123        assertEquals(encodedRegion+"."+
124            tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='),
125            HFileLink.createBackReferenceName(FSUtils.getTableName(tableDir).getNameAsString(),
126                encodedRegion));
127
128        //verify parsing back reference
129        Pair<TableName, String> parsedRef =
130            HFileLink.parseBackReferenceName(encodedRegion+"."+
131                tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='));
132        assertEquals(parsedRef.getFirst(), tableName);
133        assertEquals(encodedRegion, parsedRef.getSecond());
134
135        //verify resolving back reference
136        Path storeFileDir =  new Path(refLinkDir, encodedRegion+"."+
137            tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='));
138        Path linkPath = new Path(cfDir, refStoreFileName);
139        assertEquals(linkPath, HFileLink.getHFileFromBackReference(rootDir, storeFileDir));
140      }
141    }
142  }
143
144
145}