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