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.assertNotNull; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022import static org.junit.jupiter.api.Assertions.fail; 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.UncheckedIOException; 027import java.lang.ref.SoftReference; 028import java.util.ArrayList; 029import java.util.Collections; 030import java.util.List; 031import java.util.concurrent.CompletableFuture; 032import org.apache.hadoop.fs.FSDataInputStream; 033import org.apache.hadoop.fs.FileSystem; 034import org.apache.hadoop.fs.FilterFileSystem; 035import org.apache.hadoop.fs.FutureDataInputStreamBuilder; 036import org.apache.hadoop.fs.Path; 037import org.apache.hadoop.fs.PositionedReadable; 038import org.apache.hadoop.hbase.HBaseTestingUtil; 039import org.apache.hadoop.hbase.HConstants; 040import org.apache.hadoop.hbase.KeyValue; 041import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 042import org.apache.hadoop.hbase.TableName; 043import org.apache.hadoop.hbase.client.Admin; 044import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 045import org.apache.hadoop.hbase.client.Table; 046import org.apache.hadoop.hbase.client.TableDescriptor; 047import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 048import org.apache.hadoop.hbase.fs.HFileSystem; 049import org.apache.hadoop.hbase.io.hfile.CacheConfig; 050import org.apache.hadoop.hbase.io.hfile.HFileContext; 051import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; 052import org.apache.hadoop.hbase.io.hfile.HFileScanner; 053import org.apache.hadoop.hbase.testclassification.LargeTests; 054import org.apache.hadoop.hbase.testclassification.RegionServerTests; 055import org.apache.hadoop.hbase.util.Bytes; 056import org.junit.jupiter.api.Assumptions; 057import org.junit.jupiter.api.BeforeEach; 058import org.junit.jupiter.api.Tag; 059import org.junit.jupiter.api.Test; 060import org.junit.jupiter.api.TestInfo; 061import org.slf4j.Logger; 062import org.slf4j.LoggerFactory; 063 064/** 065 * Test cases that ensure that file system level errors are bubbled up appropriately to clients, 066 * rather than swallowed. 067 */ 068@Tag(RegionServerTests.TAG) 069@Tag(LargeTests.TAG) 070public class TestFSErrorsExposed { 071 072 private static final Logger LOG = LoggerFactory.getLogger(TestFSErrorsExposed.class); 073 074 HBaseTestingUtil util = new HBaseTestingUtil(); 075 private String name; 076 077 @BeforeEach 078 public void setTestName(TestInfo testInfo) { 079 this.name = testInfo.getTestMethod().get().getName(); 080 } 081 082 /** 083 * Injects errors into the pread calls of an on-disk file, and makes sure those bubble up to the 084 * HFile scanner 085 */ 086 @Test 087 public void testHFileScannerThrowsErrors() throws IOException { 088 Path hfilePath = new Path( 089 new Path(util.getDataTestDir("internalScannerExposesErrors"), "regionname"), "familyname"); 090 HFileSystem hfs = (HFileSystem) util.getTestFileSystem(); 091 FaultyFileSystem faultyfs = new FaultyFileSystem(hfs.getBackingFs()); 092 FileSystem fs = new HFileSystem(faultyfs); 093 CacheConfig cacheConf = new CacheConfig(util.getConfiguration()); 094 HFileContext meta = new HFileContextBuilder().withBlockSize(2 * 1024).build(); 095 StoreFileWriter writer = new StoreFileWriter.Builder(util.getConfiguration(), cacheConf, hfs) 096 .withOutputDir(hfilePath).withFileContext(meta).build(); 097 TestHStoreFile.writeStoreFile(writer, Bytes.toBytes("cf"), Bytes.toBytes("qual")); 098 099 StoreFileInfo storeFileInfo = StoreFileInfo.createStoreFileInfoForHFile(util.getConfiguration(), 100 fs, writer.getPath(), true); 101 HStoreFile sf = new HStoreFile(storeFileInfo, BloomType.NONE, cacheConf); 102 sf.initReader(); 103 StoreFileReader reader = sf.getReader(); 104 try (HFileScanner scanner = reader.getScanner(false, true, false)) { 105 FaultyInputStream inStream = faultyfs.inStreams.get(0).get(); 106 assertNotNull(inStream); 107 108 scanner.seekTo(); 109 // Do at least one successful read 110 assertTrue(scanner.next()); 111 112 faultyfs.startFaults(); 113 114 try { 115 int scanned = 0; 116 while (scanner.next()) { 117 scanned++; 118 } 119 fail("Scanner didn't throw after faults injected"); 120 } catch (IOException ioe) { 121 LOG.info("Got expected exception", ioe); 122 assertTrue(ioe.getMessage().contains("Fault")); 123 } 124 } 125 reader.close(true); // end of test so evictOnClose 126 } 127 128 /** 129 * Injects errors into the pread calls of an on-disk file, and makes sure those bubble up to the 130 * StoreFileScanner 131 */ 132 @Test 133 public void testStoreFileScannerThrowsErrors() throws IOException { 134 Path hfilePath = new Path( 135 new Path(util.getDataTestDir("internalScannerExposesErrors"), "regionname"), "familyname"); 136 HFileSystem hfs = (HFileSystem) util.getTestFileSystem(); 137 FaultyFileSystem faultyfs = new FaultyFileSystem(hfs.getBackingFs()); 138 HFileSystem fs = new HFileSystem(faultyfs); 139 CacheConfig cacheConf = new CacheConfig(util.getConfiguration()); 140 HFileContext meta = new HFileContextBuilder().withBlockSize(2 * 1024).build(); 141 StoreFileWriter writer = new StoreFileWriter.Builder(util.getConfiguration(), cacheConf, hfs) 142 .withOutputDir(hfilePath).withFileContext(meta).build(); 143 TestHStoreFile.writeStoreFile(writer, Bytes.toBytes("cf"), Bytes.toBytes("qual")); 144 145 StoreFileInfo storeFileInfo = StoreFileInfo.createStoreFileInfoForHFile(util.getConfiguration(), 146 fs, writer.getPath(), true); 147 HStoreFile sf = new HStoreFile(storeFileInfo, BloomType.NONE, cacheConf); 148 149 List<StoreFileScanner> scanners = StoreFileScanner.getScannersForStoreFiles( 150 Collections.singletonList(sf), false, true, false, false, 151 // 0 is passed as readpoint because this test operates on HStoreFile directly 152 0); 153 try { 154 KeyValueScanner scanner = scanners.get(0); 155 156 FaultyInputStream inStream = faultyfs.inStreams.get(0).get(); 157 assertNotNull(inStream); 158 159 scanner.seek(KeyValue.LOWESTKEY); 160 // Do at least one successful read 161 assertNotNull(scanner.next()); 162 faultyfs.startFaults(); 163 164 try { 165 int scanned = 0; 166 while (scanner.next() != null) { 167 scanned++; 168 } 169 fail("Scanner didn't throw after faults injected"); 170 } catch (IOException ioe) { 171 LOG.info("Got expected exception", ioe); 172 assertTrue(ioe.getMessage().contains("Could not iterate")); 173 } 174 } finally { 175 for (StoreFileScanner scanner : scanners) { 176 scanner.close(); 177 } 178 } 179 } 180 181 /** 182 * Cluster test which starts a region server with a region, then removes the data from HDFS 183 * underneath it, and ensures that errors are bubbled to the client. 184 */ 185 @Test 186 public void testFullSystemBubblesFSErrors() throws Exception { 187 // We won't have an error if the datanode is not there if we use short circuit 188 // it's a known 'feature'. 189 Assumptions.assumeTrue(!util.isReadShortCircuitOn()); 190 191 try { 192 // Make it fail faster. 193 util.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 194 util.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 90000); 195 util.getConfiguration().setInt("hbase.lease.recovery.timeout", 10000); 196 util.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 1000); 197 util.startMiniCluster(1); 198 final TableName tableName = TableName.valueOf(name); 199 byte[] fam = Bytes.toBytes("fam"); 200 201 Admin admin = util.getAdmin(); 202 TableDescriptor tableDescriptor = 203 TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder 204 .newBuilder(fam).setMaxVersions(1).setBlockCacheEnabled(false).build()).build(); 205 admin.createTable(tableDescriptor); 206 207 // Make a new Configuration so it makes a new connection that has the 208 // above configuration on it; else we use the old one w/ 10 as default. 209 try (Table table = util.getConnection().getTable(tableName)) { 210 // Load some data 211 util.loadTable(table, fam, false); 212 util.flush(); 213 HBaseTestingUtil.countRows(table); 214 215 // Kill the DFS cluster 216 util.getDFSCluster().shutdownDataNodes(); 217 218 try { 219 HBaseTestingUtil.countRows(table); 220 fail("Did not fail to count after removing data"); 221 } catch (Exception e) { 222 LOG.info("Got expected error", e); 223 assertTrue(e.getMessage().contains("Could not seek")); 224 } 225 } 226 227 // Restart data nodes so that HBase can shut down cleanly. 228 util.getDFSCluster().restartDataNodes(); 229 230 } finally { 231 SingleProcessHBaseCluster cluster = util.getMiniHBaseCluster(); 232 if (cluster != null) cluster.killAll(); 233 util.shutdownMiniCluster(); 234 } 235 } 236 237 static class FaultyFileSystem extends FilterFileSystem { 238 List<SoftReference<FaultyInputStream>> inStreams = new ArrayList<>(); 239 240 public FaultyFileSystem(FileSystem testFileSystem) { 241 super(testFileSystem); 242 } 243 244 @Override 245 public FSDataInputStream open(Path p, int bufferSize) throws IOException { 246 return wrapOpenedStream(fs.open(p, bufferSize)); 247 } 248 249 /** 250 * Plain HFile reads may use {@link FileSystem#openFile(Path)} (e.g. via {@code withFileStatus}) 251 * instead of {@link #open(Path, int)}. Route those opens through the same faulty wrapper. 252 */ 253 @Override 254 public FutureDataInputStreamBuilder openFile(Path path) 255 throws IOException, UnsupportedOperationException { 256 FutureDataInputStreamBuilder delegate; 257 try { 258 delegate = fs.openFile(path); 259 } catch (UnsupportedOperationException e) { 260 delegate = null; 261 } 262 return new FaultyFutureDataInputStreamBuilder(path, delegate); 263 } 264 265 private FSDataInputStream wrapOpenedStream(FSDataInputStream orig) throws IOException { 266 FaultyInputStream faulty = new FaultyInputStream(orig); 267 inStreams.add(new SoftReference<>(faulty)); 268 return faulty; 269 } 270 271 private FSDataInputStream wrapOpenedStreamUnchecked(FSDataInputStream orig) { 272 try { 273 return wrapOpenedStream(orig); 274 } catch (IOException e) { 275 throw new UncheckedIOException(e); 276 } 277 } 278 279 private final class FaultyFutureDataInputStreamBuilder implements FutureDataInputStreamBuilder { 280 281 private final Path path; 282 private final FutureDataInputStreamBuilder delegate; 283 284 private FaultyFutureDataInputStreamBuilder(Path path, FutureDataInputStreamBuilder delegate) { 285 this.path = path; 286 this.delegate = delegate; 287 } 288 289 @Override 290 public CompletableFuture<FSDataInputStream> build() 291 throws IllegalArgumentException, UnsupportedOperationException, IOException { 292 if (delegate != null) { 293 return delegate.build().thenApply(FaultyFileSystem.this::wrapOpenedStreamUnchecked); 294 } 295 return CompletableFuture.completedFuture(open(path)); 296 } 297 298 @Override 299 public FutureDataInputStreamBuilder opt(String key, String value) { 300 if (delegate != null) { 301 delegate.opt(key, value); 302 } 303 return this; 304 } 305 306 @Override 307 public FutureDataInputStreamBuilder opt(String key, String... values) { 308 if (delegate != null) { 309 delegate.opt(key, values); 310 } 311 return this; 312 } 313 314 @Override 315 public FutureDataInputStreamBuilder must(String key, String value) { 316 if (delegate != null) { 317 delegate.must(key, value); 318 } 319 return this; 320 } 321 322 @Override 323 public FutureDataInputStreamBuilder must(String key, String... values) { 324 if (delegate != null) { 325 delegate.must(key, values); 326 } 327 return this; 328 } 329 } 330 331 /** 332 * Starts to simulate faults on all streams opened so far 333 */ 334 public void startFaults() { 335 for (SoftReference<FaultyInputStream> is : inStreams) { 336 is.get().startFaults(); 337 } 338 } 339 } 340 341 static class FaultyInputStream extends FSDataInputStream { 342 boolean faultsStarted = false; 343 344 public FaultyInputStream(InputStream in) throws IOException { 345 super(in); 346 } 347 348 public void startFaults() { 349 faultsStarted = true; 350 } 351 352 @Override 353 public int read(long position, byte[] buffer, int offset, int length) throws IOException { 354 injectFault(); 355 return ((PositionedReadable) in).read(position, buffer, offset, length); 356 } 357 358 private void injectFault() throws IOException { 359 if (faultsStarted) { 360 throw new IOException("Fault injected"); 361 } 362 } 363 } 364}