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