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.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertTrue; 025 026import java.io.IOException; 027import java.net.URI; 028import java.util.Collection; 029import java.util.List; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.fs.FSDataInputStream; 032import org.apache.hadoop.fs.FSDataOutputStream; 033import org.apache.hadoop.fs.FileStatus; 034import org.apache.hadoop.fs.FileSystem; 035import org.apache.hadoop.fs.Path; 036import org.apache.hadoop.fs.permission.FsPermission; 037import org.apache.hadoop.hbase.HBaseClassTestRule; 038import org.apache.hadoop.hbase.HBaseTestingUtility; 039import org.apache.hadoop.hbase.HColumnDescriptor; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.client.Admin; 042import org.apache.hadoop.hbase.client.HTable; 043import org.apache.hadoop.hbase.client.Put; 044import org.apache.hadoop.hbase.client.RegionInfo; 045import org.apache.hadoop.hbase.client.RegionInfoBuilder; 046import org.apache.hadoop.hbase.fs.HFileSystem; 047import org.apache.hadoop.hbase.testclassification.LargeTests; 048import org.apache.hadoop.hbase.testclassification.RegionServerTests; 049import org.apache.hadoop.hbase.util.Bytes; 050import org.apache.hadoop.hbase.util.CommonFSUtils; 051import org.apache.hadoop.hbase.util.FSUtils; 052import org.apache.hadoop.util.Progressable; 053import org.junit.ClassRule; 054import org.junit.Rule; 055import org.junit.Test; 056import org.junit.experimental.categories.Category; 057import org.junit.rules.TestName; 058import org.slf4j.Logger; 059import org.slf4j.LoggerFactory; 060 061@Category({RegionServerTests.class, LargeTests.class}) 062public class TestHRegionFileSystem { 063 064 @ClassRule 065 public static final HBaseClassTestRule CLASS_RULE = 066 HBaseClassTestRule.forClass(TestHRegionFileSystem.class); 067 068 private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 069 private static final Logger LOG = LoggerFactory.getLogger(TestHRegionFileSystem.class); 070 071 public static final byte[] FAMILY_NAME = Bytes.toBytes("info"); 072 private static final byte[][] FAMILIES = { 073 Bytes.add(FAMILY_NAME, Bytes.toBytes("-A")), 074 Bytes.add(FAMILY_NAME, Bytes.toBytes("-B")) }; 075 private static final TableName TABLE_NAME = TableName.valueOf("TestTable"); 076 077 @Rule 078 public TestName name = new TestName(); 079 080 @Test 081 public void testBlockStoragePolicy() throws Exception { 082 TEST_UTIL = new HBaseTestingUtility(); 083 Configuration conf = TEST_UTIL.getConfiguration(); 084 TEST_UTIL.startMiniCluster(); 085 HTable table = (HTable) TEST_UTIL.createTable(TABLE_NAME, FAMILIES); 086 assertEquals("Should start with empty table", 0, TEST_UTIL.countRows(table)); 087 HRegionFileSystem regionFs = getHRegionFS(table, conf); 088 // the original block storage policy would be HOT 089 String spA = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[0])); 090 String spB = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[1])); 091 LOG.debug("Storage policy of cf 0: [" + spA + "]."); 092 LOG.debug("Storage policy of cf 1: [" + spB + "]."); 093 assertEquals("HOT", spA); 094 assertEquals("HOT", spB); 095 096 // Recreate table and make sure storage policy could be set through configuration 097 TEST_UTIL.shutdownMiniCluster(); 098 TEST_UTIL.getConfiguration().set(HStore.BLOCK_STORAGE_POLICY_KEY, "WARM"); 099 TEST_UTIL.startMiniCluster(); 100 table = (HTable) TEST_UTIL.createTable(TABLE_NAME, FAMILIES); 101 regionFs = getHRegionFS(table, conf); 102 103 try (Admin admin = TEST_UTIL.getConnection().getAdmin()) { 104 spA = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[0])); 105 spB = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[1])); 106 LOG.debug("Storage policy of cf 0: [" + spA + "]."); 107 LOG.debug("Storage policy of cf 1: [" + spB + "]."); 108 assertEquals("WARM", spA); 109 assertEquals("WARM", spB); 110 111 // alter table cf schema to change storage policies 112 // and make sure it could override settings in conf 113 HColumnDescriptor hcdA = new HColumnDescriptor(Bytes.toString(FAMILIES[0])); 114 // alter through setting HStore#BLOCK_STORAGE_POLICY_KEY in HColumnDescriptor 115 hcdA.setValue(HStore.BLOCK_STORAGE_POLICY_KEY, "ONE_SSD"); 116 admin.modifyColumnFamily(TABLE_NAME, hcdA); 117 while (TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager(). 118 getRegionStates().hasRegionsInTransition()) { 119 Thread.sleep(200); 120 LOG.debug("Waiting on table to finish schema altering"); 121 } 122 // alter through HColumnDescriptor#setStoragePolicy 123 HColumnDescriptor hcdB = new HColumnDescriptor(Bytes.toString(FAMILIES[1])); 124 hcdB.setStoragePolicy("ALL_SSD"); 125 admin.modifyColumnFamily(TABLE_NAME, hcdB); 126 while (TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates() 127 .hasRegionsInTransition()) { 128 Thread.sleep(200); 129 LOG.debug("Waiting on table to finish schema altering"); 130 } 131 spA = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[0])); 132 spB = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[1])); 133 LOG.debug("Storage policy of cf 0: [" + spA + "]."); 134 LOG.debug("Storage policy of cf 1: [" + spB + "]."); 135 assertNotNull(spA); 136 assertEquals("ONE_SSD", spA); 137 assertNotNull(spB); 138 assertEquals("ALL_SSD", spB); 139 140 // flush memstore snapshot into 3 files 141 for (long i = 0; i < 3; i++) { 142 Put put = new Put(Bytes.toBytes(i)); 143 put.addColumn(FAMILIES[0], Bytes.toBytes(i), Bytes.toBytes(i)); 144 table.put(put); 145 admin.flush(TABLE_NAME); 146 } 147 // there should be 3 files in store dir 148 FileSystem fs = TEST_UTIL.getDFSCluster().getFileSystem(); 149 Path storePath = regionFs.getStoreDir(Bytes.toString(FAMILIES[0])); 150 FileStatus[] storeFiles = CommonFSUtils.listStatus(fs, storePath); 151 assertNotNull(storeFiles); 152 assertEquals(3, storeFiles.length); 153 // store temp dir still exists but empty 154 Path storeTempDir = new Path(regionFs.getTempDir(), Bytes.toString(FAMILIES[0])); 155 assertTrue(fs.exists(storeTempDir)); 156 FileStatus[] tempFiles = CommonFSUtils.listStatus(fs, storeTempDir); 157 assertNull(tempFiles); 158 // storage policy of cf temp dir and 3 store files should be ONE_SSD 159 assertEquals("ONE_SSD", 160 ((HFileSystem) regionFs.getFileSystem()).getStoragePolicyName(storeTempDir)); 161 for (FileStatus status : storeFiles) { 162 assertEquals("ONE_SSD", 163 ((HFileSystem) regionFs.getFileSystem()).getStoragePolicyName(status.getPath())); 164 } 165 166 // change storage policies by calling raw api directly 167 regionFs.setStoragePolicy(Bytes.toString(FAMILIES[0]), "ALL_SSD"); 168 regionFs.setStoragePolicy(Bytes.toString(FAMILIES[1]), "ONE_SSD"); 169 spA = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[0])); 170 spB = regionFs.getStoragePolicyName(Bytes.toString(FAMILIES[1])); 171 LOG.debug("Storage policy of cf 0: [" + spA + "]."); 172 LOG.debug("Storage policy of cf 1: [" + spB + "]."); 173 assertNotNull(spA); 174 assertEquals("ALL_SSD", spA); 175 assertNotNull(spB); 176 assertEquals("ONE_SSD", spB); 177 } finally { 178 table.close(); 179 TEST_UTIL.deleteTable(TABLE_NAME); 180 TEST_UTIL.shutdownMiniCluster(); 181 } 182 } 183 184 private HRegionFileSystem getHRegionFS(HTable table, Configuration conf) throws IOException { 185 FileSystem fs = TEST_UTIL.getDFSCluster().getFileSystem(); 186 Path tableDir = CommonFSUtils.getTableDir(TEST_UTIL.getDefaultRootDirPath(), table.getName()); 187 List<Path> regionDirs = FSUtils.getRegionDirs(fs, tableDir); 188 assertEquals(1, regionDirs.size()); 189 List<Path> familyDirs = FSUtils.getFamilyDirs(fs, regionDirs.get(0)); 190 assertEquals(2, familyDirs.size()); 191 RegionInfo hri = table.getRegionLocator().getAllRegionLocations().get(0).getRegionInfo(); 192 HRegionFileSystem regionFs = new HRegionFileSystem(conf, new HFileSystem(fs), tableDir, hri); 193 return regionFs; 194 } 195 196 @Test 197 public void testOnDiskRegionCreation() throws IOException { 198 Path rootDir = TEST_UTIL.getDataTestDirOnTestFS(name.getMethodName()); 199 FileSystem fs = TEST_UTIL.getTestFileSystem(); 200 Configuration conf = TEST_UTIL.getConfiguration(); 201 202 // Create a Region 203 RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())).build(); 204 HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs, 205 CommonFSUtils.getTableDir(rootDir, hri.getTable()), hri); 206 207 // Verify if the region is on disk 208 Path regionDir = regionFs.getRegionDir(); 209 assertTrue("The region folder should be created", fs.exists(regionDir)); 210 211 // Verify the .regioninfo 212 RegionInfo hriVerify = HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir); 213 assertEquals(hri, hriVerify); 214 215 // Open the region 216 regionFs = HRegionFileSystem.openRegionFromFileSystem(conf, fs, 217 CommonFSUtils.getTableDir(rootDir, hri.getTable()), hri, false); 218 assertEquals(regionDir, regionFs.getRegionDir()); 219 220 // Delete the region 221 HRegionFileSystem.deleteRegionFromFileSystem(conf, fs, 222 CommonFSUtils.getTableDir(rootDir, hri.getTable()), hri); 223 assertFalse("The region folder should be removed", fs.exists(regionDir)); 224 225 fs.delete(rootDir, true); 226 } 227 228 @Test 229 public void testNonIdempotentOpsWithRetries() throws IOException { 230 Path rootDir = TEST_UTIL.getDataTestDirOnTestFS(name.getMethodName()); 231 FileSystem fs = TEST_UTIL.getTestFileSystem(); 232 Configuration conf = TEST_UTIL.getConfiguration(); 233 234 // Create a Region 235 RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())).build(); 236 HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs, rootDir, hri); 237 assertTrue(fs.exists(regionFs.getRegionDir())); 238 239 regionFs = new HRegionFileSystem(conf, new MockFileSystemForCreate(), rootDir, hri); 240 boolean result = regionFs.createDir(new Path("/foo/bar")); 241 assertTrue("Couldn't create the directory", result); 242 243 regionFs = new HRegionFileSystem(conf, new MockFileSystem(), rootDir, hri); 244 result = regionFs.rename(new Path("/foo/bar"), new Path("/foo/bar2")); 245 assertTrue("Couldn't rename the directory", result); 246 247 regionFs = new HRegionFileSystem(conf, new MockFileSystem(), rootDir, hri); 248 result = regionFs.deleteDir(new Path("/foo/bar")); 249 assertTrue("Couldn't delete the directory", result); 250 fs.delete(rootDir, true); 251 } 252 253 static class MockFileSystemForCreate extends MockFileSystem { 254 @Override 255 public boolean exists(Path path) { 256 return false; 257 } 258 } 259 260 /** 261 * a mock fs which throws exception for first 3 times, and then process the call (returns the 262 * excepted result). 263 */ 264 static class MockFileSystem extends FileSystem { 265 int retryCount; 266 final static int successRetryCount = 3; 267 268 public MockFileSystem() { 269 retryCount = 0; 270 } 271 272 @Override 273 public FSDataOutputStream append(Path arg0, int arg1, Progressable arg2) throws IOException { 274 throw new IOException(""); 275 } 276 277 @Override 278 public FSDataOutputStream create(Path arg0, FsPermission arg1, boolean arg2, int arg3, 279 short arg4, long arg5, Progressable arg6) throws IOException { 280 LOG.debug("Create, " + retryCount); 281 if (retryCount++ < successRetryCount) throw new IOException("Something bad happen"); 282 return null; 283 } 284 285 @Override 286 public boolean delete(Path arg0) throws IOException { 287 if (retryCount++ < successRetryCount) throw new IOException("Something bad happen"); 288 return true; 289 } 290 291 @Override 292 public boolean delete(Path arg0, boolean arg1) throws IOException { 293 if (retryCount++ < successRetryCount) throw new IOException("Something bad happen"); 294 return true; 295 } 296 297 @Override 298 public FileStatus getFileStatus(Path arg0) throws IOException { 299 FileStatus fs = new FileStatus(); 300 return fs; 301 } 302 303 @Override 304 public boolean exists(Path path) { 305 return true; 306 } 307 308 @Override 309 public URI getUri() { 310 throw new RuntimeException("Something bad happen"); 311 } 312 313 @Override 314 public Path getWorkingDirectory() { 315 throw new RuntimeException("Something bad happen"); 316 } 317 318 @Override 319 public FileStatus[] listStatus(Path arg0) throws IOException { 320 throw new IOException("Something bad happen"); 321 } 322 323 @Override 324 public boolean mkdirs(Path arg0, FsPermission arg1) throws IOException { 325 LOG.debug("mkdirs, " + retryCount); 326 if (retryCount++ < successRetryCount) throw new IOException("Something bad happen"); 327 return true; 328 } 329 330 @Override 331 public FSDataInputStream open(Path arg0, int arg1) throws IOException { 332 throw new IOException("Something bad happen"); 333 } 334 335 @Override 336 public boolean rename(Path arg0, Path arg1) throws IOException { 337 LOG.debug("rename, " + retryCount); 338 if (retryCount++ < successRetryCount) throw new IOException("Something bad happen"); 339 return true; 340 } 341 342 @Override 343 public void setWorkingDirectory(Path arg0) { 344 throw new RuntimeException("Something bad happen"); 345 } 346 } 347 348 @Test 349 public void testTempAndCommit() throws IOException { 350 Path rootDir = TEST_UTIL.getDataTestDirOnTestFS("testTempAndCommit"); 351 FileSystem fs = TEST_UTIL.getTestFileSystem(); 352 Configuration conf = TEST_UTIL.getConfiguration(); 353 354 // Create a Region 355 String familyName = "cf"; 356 ; 357 RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())).build(); 358 HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs, rootDir, hri); 359 360 // New region, no store files 361 Collection<StoreFileInfo> storeFiles = regionFs.getStoreFiles(familyName); 362 assertEquals(0, storeFiles != null ? storeFiles.size() : 0); 363 364 // Create a new file in temp (no files in the family) 365 Path buildPath = regionFs.createTempName(); 366 fs.createNewFile(buildPath); 367 storeFiles = regionFs.getStoreFiles(familyName); 368 assertEquals(0, storeFiles != null ? storeFiles.size() : 0); 369 370 // commit the file 371 Path dstPath = regionFs.commitStoreFile(familyName, buildPath); 372 storeFiles = regionFs.getStoreFiles(familyName); 373 assertEquals(0, storeFiles != null ? storeFiles.size() : 0); 374 assertFalse(fs.exists(buildPath)); 375 376 fs.delete(rootDir, true); 377 } 378}