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.CommonFSUtils;
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 when the current location moves or gets
041 * 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, HFileLink
079      .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), m.group(3));
087      Assert.assertEquals(HFileLink.getReferencedHFileName(name), m.group(4));
088    }
089  }
090
091  @Test
092  public void testBackReference() {
093    Path rootDir = new Path("/root");
094    Path archiveDir = new Path(rootDir, ".archive");
095    String storeFileName = "121212";
096    String linkDir = FileLink.BACK_REFERENCES_DIRECTORY_PREFIX + storeFileName;
097    String encodedRegion = "FEFE";
098    String cf = "cf1";
099
100    TableName refTables[] =
101      { TableName.valueOf(name.getMethodName()), TableName.valueOf("ns", name.getMethodName()) };
102
103    for (TableName refTable : refTables) {
104      Path refTableDir = CommonFSUtils.getTableDir(archiveDir, refTable);
105      Path refRegionDir = HRegion.getRegionDir(refTableDir, encodedRegion);
106      Path refDir = new Path(refRegionDir, cf);
107      Path refLinkDir = new Path(refDir, linkDir);
108      String refStoreFileName = refTable.getNameAsString().replace(TableName.NAMESPACE_DELIM, '=')
109        + "=" + encodedRegion + "-" + storeFileName;
110
111      TableName tableNames[] = { TableName.valueOf(name.getMethodName() + "1"),
112        TableName.valueOf("ns", name.getMethodName() + "2"),
113        TableName.valueOf(name.getMethodName() + ":" + name.getMethodName()) };
114
115      for (TableName tableName : tableNames) {
116        Path tableDir = CommonFSUtils.getTableDir(rootDir, tableName);
117        Path regionDir = HRegion.getRegionDir(tableDir, encodedRegion);
118        Path cfDir = new Path(regionDir, cf);
119
120        // Verify back reference creation
121        assertEquals(
122          encodedRegion + "." + tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='),
123          HFileLink.createBackReferenceName(CommonFSUtils.getTableName(tableDir).getNameAsString(),
124            encodedRegion));
125
126        // verify parsing back reference
127        Pair<TableName, String> parsedRef = HFileLink.parseBackReferenceName(encodedRegion + "."
128          + tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='));
129        assertEquals(parsedRef.getFirst(), tableName);
130        assertEquals(encodedRegion, parsedRef.getSecond());
131
132        // verify resolving back reference
133        Path storeFileDir = new Path(refLinkDir, encodedRegion + "."
134          + tableName.getNameAsString().replace(TableName.NAMESPACE_DELIM, '='));
135        Path linkPath = new Path(cfDir, refStoreFileName);
136        assertEquals(linkPath, HFileLink.getHFileFromBackReference(rootDir, storeFileDir));
137      }
138    }
139  }
140
141}