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 java.util.concurrent.atomic.AtomicInteger; 027import org.apache.hadoop.fs.FSDataOutputStream; 028import org.apache.hadoop.fs.FileStatus; 029import org.apache.hadoop.fs.FileSystem; 030import org.apache.hadoop.fs.FilterFileSystem; 031import org.apache.hadoop.fs.Path; 032import org.apache.hadoop.hbase.HBaseTestingUtil; 033import org.apache.hadoop.hbase.HConstants; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 036import org.apache.hadoop.hbase.client.RegionInfo; 037import org.apache.hadoop.hbase.client.RegionInfoBuilder; 038import org.apache.hadoop.hbase.io.HFileLink; 039import org.apache.hadoop.hbase.io.Reference; 040import org.apache.hadoop.hbase.io.hfile.ReaderContext; 041import org.apache.hadoop.hbase.io.hfile.ReaderContext.ReaderType; 042import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerForTest; 043import org.apache.hadoop.hbase.testclassification.RegionServerTests; 044import org.apache.hadoop.hbase.testclassification.SmallTests; 045import org.junit.jupiter.api.Tag; 046import org.junit.jupiter.api.Test; 047 048/** 049 * Test HStoreFile 050 */ 051@Tag(RegionServerTests.TAG) 052@Tag(SmallTests.TAG) 053public class TestStoreFileInfo { 054 055 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 056 057 private static final String PLAIN_HFILE_NAME = "abc0123456789abcdef0123456789ab"; 058 059 private static FileSystem fileSystemCountingGetFileStatus(FileSystem base, 060 AtomicInteger statCalls) { 061 return new FilterFileSystem(base) { 062 @Override 063 public FileStatus getFileStatus(Path path) throws IOException { 064 statCalls.incrementAndGet(); 065 return super.getFileStatus(path); 066 } 067 }; 068 } 069 070 private static Path createPlainHFileOnTestFs(Path dir) throws IOException { 071 FileSystem fs = TEST_UTIL.getTestFileSystem(); 072 fs.mkdirs(dir); 073 Path file = new Path(dir, PLAIN_HFILE_NAME); 074 try (FSDataOutputStream out = fs.create(file)) { 075 out.write(new byte[128]); 076 } 077 return file; 078 } 079 080 private static StoreContext newStoreContextForTest(FileSystem fs, Path regionDir) 081 throws IOException { 082 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(TableName.valueOf("table1")).build(); 083 return StoreContext.getBuilder() 084 .withRegionFileSystem( 085 HRegionFileSystem.create(TEST_UTIL.getConfiguration(), fs, regionDir, regionInfo)) 086 .withColumnFamilyDescriptor( 087 ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes()).build()) 088 .build(); 089 } 090 091 /** 092 * Validate that we can handle valid tables with '.', '_', and '-' chars. 093 */ 094 @Test 095 public void testStoreFileNames() { 096 String[] legalHFileLink = { "MyTable_02=abc012-def345", "MyTable_02.300=abc012-def345", 097 "MyTable_02-400=abc012-def345", "MyTable_02-400.200=abc012-def345", 098 "MyTable_02=abc012-def345_SeqId_1_", "MyTable_02=abc012-def345_SeqId_20_" }; 099 for (String name : legalHFileLink) { 100 assertTrue(HFileLink.isHFileLink(name), "should be a valid link: " + name); 101 assertTrue(StoreFileInfo.validateStoreFileName(name), "should be a valid StoreFile" + name); 102 assertFalse(StoreFileInfo.isReference(name), "should not be a valid reference: " + name); 103 104 String refName = name + ".6789"; 105 assertTrue(StoreFileInfo.isReference(refName), 106 "should be a valid link reference: " + refName); 107 assertTrue(StoreFileInfo.validateStoreFileName(refName), 108 "should be a valid StoreFile" + refName); 109 } 110 111 String[] illegalHFileLink = { ".MyTable_02=abc012-def345", "-MyTable_02.300=abc012-def345", 112 "MyTable_02-400=abc0_12-def345", "MyTable_02-400.200=abc012-def345...." }; 113 for (String name : illegalHFileLink) { 114 assertFalse(HFileLink.isHFileLink(name), "should not be a valid link: " + name); 115 } 116 } 117 118 @Test 119 public void testEqualsWithLink() throws IOException { 120 Path origin = new Path("/origin"); 121 Path tmp = TEST_UTIL.getDataTestDir(); 122 Path mob = new Path("/mob"); 123 Path archive = new Path("/archive"); 124 HFileLink link1 = new HFileLink(new Path(origin, "f1"), new Path(tmp, "f1"), 125 new Path(mob, "f1"), new Path(archive, "f1")); 126 HFileLink link2 = new HFileLink(new Path(origin, "f1"), new Path(tmp, "f1"), 127 new Path(mob, "f1"), new Path(archive, "f1")); 128 129 StoreFileInfo info1 = new StoreFileInfo(TEST_UTIL.getConfiguration(), 130 TEST_UTIL.getTestFileSystem(), new Path(archive, "f1"), link1); 131 StoreFileInfo info2 = new StoreFileInfo(TEST_UTIL.getConfiguration(), 132 TEST_UTIL.getTestFileSystem(), new Path(archive, "f1"), link2); 133 134 assertEquals(info1, info2); 135 assertEquals(info1.hashCode(), info2.hashCode()); 136 } 137 138 /** 139 * Plain HFiles should not call {@code getFileStatus} again when opening a reader context if size 140 * and status were established at {@link StoreFileInfo} construction. 141 */ 142 @Test 143 public void testCreateReaderContextSkipsRestatForPlainHFile() throws IOException { 144 Path dir = TEST_UTIL.getDataTestDirOnTestFS("plain-hfile-no-restat"); 145 Path file = createPlainHFileOnTestFs(dir); 146 FileSystem baseFs = TEST_UTIL.getTestFileSystem(); 147 FileStatus listedStatus = baseFs.getFileStatus(file); 148 AtomicInteger statCalls = new AtomicInteger(); 149 FileSystem fs = fileSystemCountingGetFileStatus(baseFs, statCalls); 150 StoreFileInfo info = new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, 151 listedStatus.getModificationTime(), file, listedStatus.getLen(), null, null, false); 152 assertEquals(0, statCalls.get(), "construction should not stat when size is supplied"); 153 statCalls.set(0); 154 ReaderContext context = 155 info.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null); 156 assertEquals(0, statCalls.get(), 157 "createReaderContext should reuse cached status for plain HFiles"); 158 assertEquals(listedStatus.getLen(), context.getFileSize()); 159 context.getInputStreamWrapper().close(); 160 } 161 162 @Test 163 public void testGetReferencedFileStatusSkipsRestatForPlainHFile() throws IOException { 164 Path regionDir = TEST_UTIL.getDataTestDirOnTestFS("plain-hfile-referenced-status"); 165 Path file = createPlainHFileOnTestFs(regionDir); 166 FileSystem baseFs = TEST_UTIL.getTestFileSystem(); 167 FileStatus listedStatus = baseFs.getFileStatus(file); 168 AtomicInteger statCalls = new AtomicInteger(); 169 FileSystem fs = fileSystemCountingGetFileStatus(baseFs, statCalls); 170 StoreFileInfo info = 171 new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, listedStatus.getModificationTime(), file, 172 listedStatus.getLen(), null, null, false, listedStatus); 173 statCalls.set(0); 174 assertEquals(listedStatus.getLen(), info.getReferencedFileStatus(fs).getLen()); 175 assertEquals(0, statCalls.get(), 176 "getReferencedFileStatus should reuse cached status that does not need refresh"); 177 statCalls.set(0); 178 assertEquals(listedStatus.getLen(), info.getFileStatus().getLen()); 179 assertEquals(0, statCalls.get()); 180 } 181 182 @Test 183 public void testPlainHFileStatusNeedsRefreshForBlockDistribution() throws IOException { 184 Path dir = TEST_UTIL.getDataTestDirOnTestFS("plain-hfile-needs-refresh"); 185 Path file = createPlainHFileOnTestFs(dir); 186 FileSystem baseFs = TEST_UTIL.getTestFileSystem(); 187 FileStatus listedStatus = baseFs.getFileStatus(file); 188 AtomicInteger statCalls = new AtomicInteger(); 189 FileSystem fs = fileSystemCountingGetFileStatus(baseFs, statCalls); 190 StoreFileInfo info = new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, 191 listedStatus.getModificationTime(), file, listedStatus.getLen(), null, null, false); 192 statCalls.set(0); 193 info.getReferencedFileStatus(fs); 194 assertEquals(1, statCalls.get(), "inferred status must be refreshed for block locality"); 195 statCalls.set(0); 196 info.getReferencedFileStatus(fs); 197 assertEquals(0, statCalls.get(), "second getReferencedFileStatus should use refreshed status"); 198 statCalls.set(0); 199 info.computeHDFSBlocksDistribution(fs); 200 assertEquals(0, statCalls.get(), "block distribution should use refreshed status"); 201 statCalls.set(0); 202 ReaderContext context = 203 info.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null); 204 assertEquals(0, statCalls.get(), "reader open should still avoid restat"); 205 context.getInputStreamWrapper().close(); 206 } 207 208 @Test 209 public void testTrackerGetStoreFileInfoWithStatusSkipsRestatOnOpen() throws IOException { 210 Path regionDir = TEST_UTIL.getDataTestDirOnTestFS("tracker-with-status-no-restat"); 211 Path file = createPlainHFileOnTestFs(regionDir); 212 FileSystem baseFs = TEST_UTIL.getTestFileSystem(); 213 FileStatus listedStatus = baseFs.getFileStatus(file); 214 AtomicInteger statCalls = new AtomicInteger(); 215 FileSystem fs = fileSystemCountingGetFileStatus(baseFs, statCalls); 216 StoreFileTrackerForTest tracker = new StoreFileTrackerForTest(TEST_UTIL.getConfiguration(), 217 true, newStoreContextForTest(fs, regionDir)); 218 statCalls.set(0); 219 StoreFileInfo info = tracker.getStoreFileInfo(listedStatus, file, true); 220 assertEquals(0, statCalls.get(), "tracker should not stat when fileStatus is supplied"); 221 statCalls.set(0); 222 info.getReferencedFileStatus(fs); 223 ReaderContext context = 224 info.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null); 225 assertEquals(0, statCalls.get(), "open and referenced status should reuse listed fileStatus"); 226 assertEquals(listedStatus.getLen(), context.getFileSize()); 227 context.getInputStreamWrapper().close(); 228 } 229 230 @Test 231 public void testTrackerGetStoreFileInfoWithoutStatusSkipsRestatOnOpen() throws IOException { 232 Path regionDir = TEST_UTIL.getDataTestDirOnTestFS("tracker-no-status-no-restat"); 233 Path file = createPlainHFileOnTestFs(regionDir); 234 FileSystem baseFs = TEST_UTIL.getTestFileSystem(); 235 AtomicInteger statCalls = new AtomicInteger(); 236 FileSystem fs = fileSystemCountingGetFileStatus(baseFs, statCalls); 237 StoreFileTrackerForTest tracker = new StoreFileTrackerForTest(TEST_UTIL.getConfiguration(), 238 true, newStoreContextForTest(fs, regionDir)); 239 statCalls.set(0); 240 StoreFileInfo info = tracker.getStoreFileInfo(file, true); 241 assertEquals(1, statCalls.get(), "tracker should stat once when fileStatus is absent"); 242 statCalls.set(0); 243 ReaderContext context = 244 info.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null); 245 assertEquals(0, statCalls.get(), 246 "createReaderContext should not restat after tracker registration"); 247 assertEquals(128, context.getFileSize()); 248 context.getInputStreamWrapper().close(); 249 statCalls.set(0); 250 info.getReferencedFileStatus(fs); 251 assertEquals(0, statCalls.get(), 252 "getReferencedFileStatus should reuse status from tracker getFileStatus"); 253 statCalls.set(0); 254 info.getReferencedFileStatus(fs); 255 assertEquals(0, statCalls.get()); 256 } 257 258 @Test 259 public void testHFileLinkReferenceRetainsReferencePathInReaderContext() throws IOException { 260 Path dir = TEST_UTIL.getDataTestDirOnTestFS("hfile-link-reference-reader-path"); 261 Path linkedFile = createPlainHFileOnTestFs(new Path(dir, "linked")); 262 Path missing = new Path(dir, "missing"); 263 HFileLink link = new HFileLink(linkedFile, new Path(missing, "tmp"), new Path(missing, "mob"), 264 new Path(missing, "archive")); 265 Path referencePath = new Path(dir, "table=region-" + PLAIN_HFILE_NAME + ".abcd"); 266 Reference reference = Reference.createBottomReference(HConstants.EMPTY_START_ROW); 267 StoreFileInfo info = new StoreFileInfo(TEST_UTIL.getConfiguration(), 268 TEST_UTIL.getTestFileSystem(), 0, referencePath, 0, reference, link, true); 269 270 ReaderContext context = 271 info.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null); 272 assertEquals(referencePath, context.getFilePath(), 273 "HFileLink reference should retain its logical reference path"); 274 assertEquals(128, context.getFileSize()); 275 context.getInputStreamWrapper().close(); 276 } 277 278 @Test 279 public void testOpenErrorMessageReference() throws IOException { 280 // Test file link exception 281 // Try to open nonsense hfilelink. Make sure exception is from HFileLink. 282 Path p = new Path(TEST_UTIL.getDataTestDirOnTestFS(), "4567.abcd"); 283 FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration()); 284 fs.mkdirs(p.getParent()); 285 Reference r = Reference.createBottomReference(HConstants.EMPTY_START_ROW); 286 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(TableName.valueOf("table1")).build(); 287 StoreContext storeContext = StoreContext.getBuilder() 288 .withRegionFileSystem(HRegionFileSystem.create(TEST_UTIL.getConfiguration(), fs, 289 TEST_UTIL.getDataTestDirOnTestFS(), regionInfo)) 290 .withColumnFamilyDescriptor( 291 ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes()).build()) 292 .build(); 293 StoreFileTrackerForTest storeFileTrackerForTest = 294 new StoreFileTrackerForTest(TEST_UTIL.getConfiguration(), true, storeContext); 295 storeFileTrackerForTest.createReference(r, p); 296 StoreFileInfo sfi = storeFileTrackerForTest.getStoreFileInfo(p, true); 297 try { 298 ReaderContext context = 299 sfi.createReaderContext(false, 1000, ReaderType.PREAD, null, null, null); 300 sfi.createReader(context, null); 301 throw new IllegalStateException(); 302 } catch (FileNotFoundException fnfe) { 303 assertTrue(fnfe.getMessage().contains("->")); 304 } 305 } 306}