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.regionserver;
019
020import static org.junit.Assert.*;
021
022import java.io.FileNotFoundException;
023import java.io.IOException;
024import java.util.concurrent.atomic.AtomicInteger;
025
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.io.HFileLink;
032import org.apache.hadoop.hbase.io.Reference;
033import org.apache.hadoop.hbase.testclassification.RegionServerTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039/**
040 * Test HStoreFile
041 */
042@Category({RegionServerTests.class, SmallTests.class})
043public class TestStoreFileInfo {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestStoreFileInfo.class);
048
049  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050
051  /**
052   * Validate that we can handle valid tables with '.', '_', and '-' chars.
053   */
054  @Test
055  public void testStoreFileNames() {
056    String[] legalHFileLink = { "MyTable_02=abc012-def345", "MyTable_02.300=abc012-def345",
057      "MyTable_02-400=abc012-def345", "MyTable_02-400.200=abc012-def345",
058      "MyTable_02=abc012-def345_SeqId_1_", "MyTable_02=abc012-def345_SeqId_20_" };
059    for (String name: legalHFileLink) {
060      assertTrue("should be a valid link: " + name, HFileLink.isHFileLink(name));
061      assertTrue("should be a valid StoreFile" + name, StoreFileInfo.validateStoreFileName(name));
062      assertFalse("should not be a valid reference: " + name, StoreFileInfo.isReference(name));
063
064      String refName = name + ".6789";
065      assertTrue("should be a valid link reference: " + refName,
066          StoreFileInfo.isReference(refName));
067      assertTrue("should be a valid StoreFile" + refName,
068          StoreFileInfo.validateStoreFileName(refName));
069    }
070
071    String[] illegalHFileLink = { ".MyTable_02=abc012-def345", "-MyTable_02.300=abc012-def345",
072      "MyTable_02-400=abc0_12-def345", "MyTable_02-400.200=abc012-def345...." };
073    for (String name: illegalHFileLink) {
074      assertFalse("should not be a valid link: " + name, HFileLink.isHFileLink(name));
075    }
076  }
077
078  @Test
079  public void testEqualsWithLink() throws IOException {
080    Path origin = new Path("/origin");
081    Path tmp = TEST_UTIL.getDataTestDir();
082    Path mob = new Path("/mob");
083    Path archive = new Path("/archive");
084    HFileLink link1 = new HFileLink(new Path(origin, "f1"), new Path(tmp, "f1"),
085      new Path(mob, "f1"), new Path(archive, "f1"));
086    HFileLink link2 = new HFileLink(new Path(origin, "f1"), new Path(tmp, "f1"),
087      new Path(mob, "f1"), new Path(archive, "f1"));
088
089    StoreFileInfo info1 = new StoreFileInfo(TEST_UTIL.getConfiguration(),
090      TEST_UTIL.getTestFileSystem(), null, link1);
091    StoreFileInfo info2 = new StoreFileInfo(TEST_UTIL.getConfiguration(),
092      TEST_UTIL.getTestFileSystem(), null, link2);
093
094    assertEquals(info1, info2);
095    assertEquals(info1.hashCode(), info2.hashCode());
096  }
097
098  @Test
099  public void testOpenErrorMessageHFileLink() throws IOException, IllegalStateException {
100    // Test file link exception
101    // Try to open nonsense hfilelink. Make sure exception is from HFileLink.
102    Path p = new Path("/hbase/test/0123/cf/testtb=4567-abcd");
103    try (FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration())) {
104      StoreFileInfo sfi = new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, p);
105      try {
106        sfi.open(fs, null, false, 1000, true, new AtomicInteger(), false);
107        throw new IllegalStateException();
108      } catch (FileNotFoundException fnfe) {
109        assertTrue(fnfe.getMessage().contains(HFileLink.class.getSimpleName()));
110      }
111    }
112  }
113
114  @Test
115  public void testOpenErrorMessageReference() throws IOException {
116    // Test file link exception
117    // Try to open nonsense hfilelink. Make sure exception is from HFileLink.
118    Path p = new Path(TEST_UTIL.getDataTestDirOnTestFS(),"4567.abcd");
119    FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
120    fs.mkdirs(p.getParent());
121    Reference r = Reference.createBottomReference(HConstants.EMPTY_START_ROW);
122    r.write(fs, p);
123    StoreFileInfo sfi = new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, p);
124    try {
125      sfi.open(fs, null, false, 1000, true, new AtomicInteger(), false);
126      throw new IllegalStateException();
127    } catch (FileNotFoundException fnfe) {
128      assertTrue(fnfe.getMessage().contains("->"));
129    }
130  }
131}
132