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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
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.HBaseTestingUtil;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.io.HFileLink;
035import org.apache.hadoop.hbase.io.Reference;
036import org.apache.hadoop.hbase.io.hfile.ReaderContext;
037import org.apache.hadoop.hbase.io.hfile.ReaderContext.ReaderType;
038import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerForTest;
039import org.apache.hadoop.hbase.testclassification.RegionServerTests;
040import org.apache.hadoop.hbase.testclassification.SmallTests;
041import org.junit.jupiter.api.Tag;
042import org.junit.jupiter.api.Test;
043
044/**
045 * Test HStoreFile
046 */
047@Tag(RegionServerTests.TAG)
048@Tag(SmallTests.TAG)
049public class TestStoreFileInfo {
050
051  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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(HFileLink.isHFileLink(name), "should be a valid link: " + name);
063      assertTrue(StoreFileInfo.validateStoreFileName(name), "should be a valid StoreFile" + name);
064      assertFalse(StoreFileInfo.isReference(name), "should not be a valid reference: " + name);
065
066      String refName = name + ".6789";
067      assertTrue(StoreFileInfo.isReference(refName),
068        "should be a valid link reference: " + refName);
069      assertTrue(StoreFileInfo.validateStoreFileName(refName),
070        "should be a valid StoreFile" + 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(HFileLink.isHFileLink(name), "should not be a valid link: " + 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 = new StoreFileInfo(TEST_UTIL.getConfiguration(),
092      TEST_UTIL.getTestFileSystem(), new Path(archive, "f1"), link1);
093    StoreFileInfo info2 = new StoreFileInfo(TEST_UTIL.getConfiguration(),
094      TEST_UTIL.getTestFileSystem(), new Path(archive, "f1"), link2);
095
096    assertEquals(info1, info2);
097    assertEquals(info1.hashCode(), info2.hashCode());
098  }
099
100  @Test
101  public void testOpenErrorMessageReference() throws IOException {
102    // Test file link exception
103    // Try to open nonsense hfilelink. Make sure exception is from HFileLink.
104    Path p = new Path(TEST_UTIL.getDataTestDirOnTestFS(), "4567.abcd");
105    FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
106    fs.mkdirs(p.getParent());
107    Reference r = Reference.createBottomReference(HConstants.EMPTY_START_ROW);
108    RegionInfo regionInfo = RegionInfoBuilder.newBuilder(TableName.valueOf("table1")).build();
109    StoreContext storeContext = StoreContext.getBuilder()
110      .withRegionFileSystem(HRegionFileSystem.create(TEST_UTIL.getConfiguration(), fs,
111        TEST_UTIL.getDataTestDirOnTestFS(), regionInfo))
112      .withColumnFamilyDescriptor(
113        ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes()).build())
114      .build();
115    StoreFileTrackerForTest storeFileTrackerForTest =
116      new StoreFileTrackerForTest(TEST_UTIL.getConfiguration(), true, storeContext);
117    storeFileTrackerForTest.createReference(r, p);
118    StoreFileInfo sfi = storeFileTrackerForTest.getStoreFileInfo(p, true);
119    try {
120      ReaderContext context =
121        sfi.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null);
122      sfi.createReader(context, null);
123      throw new IllegalStateException();
124    } catch (FileNotFoundException fnfe) {
125      assertTrue(fnfe.getMessage().contains("->"));
126    }
127  }
128}