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.master; 019 020import static org.apache.hadoop.hbase.util.HFileArchiveTestingUtil.assertArchiveEqualToOriginal; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertFalse; 023import static org.junit.Assert.assertTrue; 024import static org.mockito.Mockito.doReturn; 025import static org.mockito.Mockito.spy; 026 027import java.io.IOException; 028import java.util.Map; 029import java.util.NavigableMap; 030import java.util.Objects; 031import java.util.SortedMap; 032import java.util.SortedSet; 033import java.util.TreeMap; 034import java.util.concurrent.ConcurrentSkipListMap; 035import org.apache.hadoop.fs.FSDataOutputStream; 036import org.apache.hadoop.fs.FileStatus; 037import org.apache.hadoop.fs.FileSystem; 038import org.apache.hadoop.fs.Path; 039import org.apache.hadoop.hbase.HBaseClassTestRule; 040import org.apache.hadoop.hbase.HBaseTestingUtility; 041import org.apache.hadoop.hbase.HColumnDescriptor; 042import org.apache.hadoop.hbase.HConstants; 043import org.apache.hadoop.hbase.HRegionInfo; 044import org.apache.hadoop.hbase.MetaMockingUtil; 045import org.apache.hadoop.hbase.ServerName; 046import org.apache.hadoop.hbase.TableName; 047import org.apache.hadoop.hbase.client.Result; 048import org.apache.hadoop.hbase.client.TableDescriptor; 049import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 050import org.apache.hadoop.hbase.io.Reference; 051import org.apache.hadoop.hbase.master.CatalogJanitor.SplitParentFirstComparator; 052import org.apache.hadoop.hbase.master.assignment.MockMasterServices; 053import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 054import org.apache.hadoop.hbase.regionserver.ChunkCreator; 055import org.apache.hadoop.hbase.regionserver.HStore; 056import org.apache.hadoop.hbase.regionserver.MemStoreLABImpl; 057import org.apache.hadoop.hbase.testclassification.MasterTests; 058import org.apache.hadoop.hbase.testclassification.SmallTests; 059import org.apache.hadoop.hbase.util.Bytes; 060import org.apache.hadoop.hbase.util.FSUtils; 061import org.apache.hadoop.hbase.util.HFileArchiveUtil; 062import org.apache.hadoop.hbase.util.Triple; 063import org.junit.After; 064import org.junit.Before; 065import org.junit.BeforeClass; 066import org.junit.ClassRule; 067import org.junit.Rule; 068import org.junit.Test; 069import org.junit.experimental.categories.Category; 070import org.junit.rules.TestName; 071import org.slf4j.Logger; 072import org.slf4j.LoggerFactory; 073 074@Category({MasterTests.class, SmallTests.class}) 075public class TestCatalogJanitor { 076 077 @ClassRule 078 public static final HBaseClassTestRule CLASS_RULE = 079 HBaseClassTestRule.forClass(TestCatalogJanitor.class); 080 081 private static final Logger LOG = LoggerFactory.getLogger(TestCatalogJanitor.class); 082 @Rule public final TestName name = new TestName(); 083 private static final HBaseTestingUtility HTU = new HBaseTestingUtility(); 084 private MockMasterServices masterServices; 085 private CatalogJanitor janitor; 086 087 @BeforeClass 088 public static void beforeClass() throws Exception { 089 ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); 090 } 091 092 @Before 093 public void setup() throws IOException { 094 setRootDirAndCleanIt(HTU, this.name.getMethodName()); 095 NavigableMap<ServerName, SortedSet<byte []>> regionsToRegionServers = 096 new ConcurrentSkipListMap<ServerName, SortedSet<byte []>>(); 097 this.masterServices = 098 new MockMasterServices(HTU.getConfiguration(), regionsToRegionServers); 099 this.masterServices.start(10, null); 100 this.janitor = new CatalogJanitor(masterServices); 101 } 102 103 @After 104 public void teardown() { 105 this.janitor.cancel(true); 106 this.masterServices.stop("DONE"); 107 } 108 109 /** 110 * Test clearing a split parent. 111 */ 112 @Test 113 public void testCleanParent() throws IOException, InterruptedException { 114 TableDescriptor td = createTableDescriptorForCurrentMethod(); 115 // Create regions. 116 HRegionInfo parent = 117 new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("eee")); 118 HRegionInfo splita = 119 new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("ccc")); 120 HRegionInfo splitb = 121 new HRegionInfo(td.getTableName(), Bytes.toBytes("ccc"), Bytes.toBytes("eee")); 122 // Test that when both daughter regions are in place, that we do not remove the parent. 123 Result r = createResult(parent, splita, splitb); 124 // Add a reference under splitA directory so we don't clear out the parent. 125 Path rootdir = this.masterServices.getMasterFileSystem().getRootDir(); 126 Path tabledir = FSUtils.getTableDir(rootdir, td.getTableName()); 127 Path parentdir = new Path(tabledir, parent.getEncodedName()); 128 Path storedir = HStore.getStoreHomedir(tabledir, splita, td.getColumnFamilies()[0].getName()); 129 Reference ref = Reference.createTopReference(Bytes.toBytes("ccc")); 130 long now = System.currentTimeMillis(); 131 // Reference name has this format: StoreFile#REF_NAME_PARSER 132 Path p = new Path(storedir, Long.toString(now) + "." + parent.getEncodedName()); 133 FileSystem fs = this.masterServices.getMasterFileSystem().getFileSystem(); 134 Path path = ref.write(fs, p); 135 assertTrue(fs.exists(path)); 136 LOG.info("Created reference " + path); 137 // Add a parentdir for kicks so can check it gets removed by the catalogjanitor. 138 fs.mkdirs(parentdir); 139 assertFalse(this.janitor.cleanParent(parent, r)); 140 ProcedureTestingUtility.waitAllProcedures(masterServices.getMasterProcedureExecutor()); 141 assertTrue(fs.exists(parentdir)); 142 // Remove the reference file and try again. 143 assertTrue(fs.delete(p, true)); 144 assertTrue(this.janitor.cleanParent(parent, r)); 145 // Parent cleanup is run async as a procedure. Make sure parentdir is removed. 146 ProcedureTestingUtility.waitAllProcedures(masterServices.getMasterProcedureExecutor()); 147 assertTrue(!fs.exists(parentdir)); 148 } 149 150 /** 151 * Make sure parent gets cleaned up even if daughter is cleaned up before it. 152 */ 153 @Test 154 public void testParentCleanedEvenIfDaughterGoneFirst() 155 throws IOException, InterruptedException { 156 parentWithSpecifiedEndKeyCleanedEvenIfDaughterGoneFirst(this.name.getMethodName(), 157 Bytes.toBytes("eee")); 158 } 159 160 /** 161 * Make sure last parent with empty end key gets cleaned up even if daughter is cleaned up before it. 162 */ 163 @Test 164 public void testLastParentCleanedEvenIfDaughterGoneFirst() 165 throws IOException, InterruptedException { 166 parentWithSpecifiedEndKeyCleanedEvenIfDaughterGoneFirst(this.name.getMethodName(), 167 new byte[0]); 168 } 169 170 /** 171 * @return A TableDescriptor with a tableName of current method name and a column 172 * family that is MockMasterServices.DEFAULT_COLUMN_FAMILY_NAME) 173 */ 174 private TableDescriptor createTableDescriptorForCurrentMethod() { 175 return TableDescriptorBuilder.newBuilder(TableName.valueOf(this.name.getMethodName())). 176 setColumnFamily(new HColumnDescriptor(MockMasterServices.DEFAULT_COLUMN_FAMILY_NAME)). 177 build(); 178 } 179 180 /** 181 * Make sure parent with specified end key gets cleaned up even if daughter is cleaned up before it. 182 * 183 * @param rootDir the test case name, used as the HBase testing utility root 184 * @param lastEndKey the end key of the split parent 185 */ 186 private void parentWithSpecifiedEndKeyCleanedEvenIfDaughterGoneFirst( 187 final String rootDir, final byte[] lastEndKey) 188 throws IOException, InterruptedException { 189 TableDescriptor td = createTableDescriptorForCurrentMethod(); 190 // Create regions: aaa->{lastEndKey}, aaa->ccc, aaa->bbb, bbb->ccc, etc. 191 HRegionInfo parent = new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), lastEndKey); 192 // Sleep a second else the encoded name on these regions comes out 193 // same for all with same start key and made in same second. 194 Thread.sleep(1001); 195 196 // Daughter a 197 HRegionInfo splita = 198 new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("ccc")); 199 Thread.sleep(1001); 200 // Make daughters of daughter a; splitaa and splitab. 201 HRegionInfo splitaa = 202 new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("bbb")); 203 HRegionInfo splitab = 204 new HRegionInfo(td.getTableName(), Bytes.toBytes("bbb"), Bytes.toBytes("ccc")); 205 206 // Daughter b 207 HRegionInfo splitb = 208 new HRegionInfo(td.getTableName(), Bytes.toBytes("ccc"), lastEndKey); 209 Thread.sleep(1001); 210 // Make Daughters of daughterb; splitba and splitbb. 211 HRegionInfo splitba = 212 new HRegionInfo(td.getTableName(), Bytes.toBytes("ccc"), Bytes.toBytes("ddd")); 213 HRegionInfo splitbb = 214 new HRegionInfo(td.getTableName(), Bytes.toBytes("ddd"), lastEndKey); 215 216 // First test that our Comparator works right up in CatalogJanitor. 217 SortedMap<HRegionInfo, Result> regions = 218 new TreeMap<>(new CatalogJanitor.SplitParentFirstComparator()); 219 // Now make sure that this regions map sorts as we expect it to. 220 regions.put(parent, createResult(parent, splita, splitb)); 221 regions.put(splitb, createResult(splitb, splitba, splitbb)); 222 regions.put(splita, createResult(splita, splitaa, splitab)); 223 // Assert its properly sorted. 224 int index = 0; 225 for (Map.Entry<HRegionInfo, Result> e: regions.entrySet()) { 226 if (index == 0) { 227 assertTrue(e.getKey().getEncodedName().equals(parent.getEncodedName())); 228 } else if (index == 1) { 229 assertTrue(e.getKey().getEncodedName().equals(splita.getEncodedName())); 230 } else if (index == 2) { 231 assertTrue(e.getKey().getEncodedName().equals(splitb.getEncodedName())); 232 } 233 index++; 234 } 235 236 // Now play around with the cleanParent function. Create a ref from splita up to the parent. 237 Path splitaRef = 238 createReferences(this.masterServices, td, parent, splita, Bytes.toBytes("ccc"), false); 239 // Make sure actual super parent sticks around because splita has a ref. 240 assertFalse(janitor.cleanParent(parent, regions.get(parent))); 241 242 //splitba, and split bb, do not have dirs in fs. That means that if 243 // we test splitb, it should get cleaned up. 244 assertTrue(janitor.cleanParent(splitb, regions.get(splitb))); 245 246 // Now remove ref from splita to parent... so parent can be let go and so 247 // the daughter splita can be split (can't split if still references). 248 // BUT make the timing such that the daughter gets cleaned up before we 249 // can get a chance to let go of the parent. 250 FileSystem fs = FileSystem.get(HTU.getConfiguration()); 251 assertTrue(fs.delete(splitaRef, true)); 252 // Create the refs from daughters of splita. 253 Path splitaaRef = 254 createReferences(this.masterServices, td, splita, splitaa, Bytes.toBytes("bbb"), false); 255 Path splitabRef = 256 createReferences(this.masterServices, td, splita, splitab, Bytes.toBytes("bbb"), true); 257 258 // Test splita. It should stick around because references from splitab, etc. 259 assertFalse(janitor.cleanParent(splita, regions.get(splita))); 260 261 // Now clean up parent daughter first. Remove references from its daughters. 262 assertTrue(fs.delete(splitaaRef, true)); 263 assertTrue(fs.delete(splitabRef, true)); 264 assertTrue(janitor.cleanParent(splita, regions.get(splita))); 265 266 // Super parent should get cleaned up now both splita and splitb are gone. 267 assertTrue(janitor.cleanParent(parent, regions.get(parent))); 268 } 269 270 /** 271 * CatalogJanitor.scan() should not clean parent regions if their own 272 * parents are still referencing them. This ensures that grandparent regions 273 * do not point to deleted parent regions. 274 */ 275 @Test 276 public void testScanDoesNotCleanRegionsWithExistingParents() throws Exception { 277 TableDescriptor td = createTableDescriptorForCurrentMethod(); 278 // Create regions: aaa->{lastEndKey}, aaa->ccc, aaa->bbb, bbb->ccc, etc. 279 280 // Parent 281 HRegionInfo parent = new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), 282 HConstants.EMPTY_BYTE_ARRAY, true); 283 // Sleep a second else the encoded name on these regions comes out 284 // same for all with same start key and made in same second. 285 Thread.sleep(1001); 286 287 // Daughter a 288 HRegionInfo splita = 289 new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("ccc"), true); 290 Thread.sleep(1001); 291 292 // Make daughters of daughter a; splitaa and splitab. 293 HRegionInfo splitaa = 294 new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("bbb"), false); 295 HRegionInfo splitab = 296 new HRegionInfo(td.getTableName(), Bytes.toBytes("bbb"), Bytes.toBytes("ccc"), false); 297 298 // Daughter b 299 HRegionInfo splitb = 300 new HRegionInfo(td.getTableName(), Bytes.toBytes("ccc"), HConstants.EMPTY_BYTE_ARRAY); 301 Thread.sleep(1001); 302 303 // Parent has daughters splita and splitb. Splita has daughters splitaa and splitab. 304 final Map<HRegionInfo, Result> splitParents = new TreeMap<>(new SplitParentFirstComparator()); 305 splitParents.put(parent, createResult(parent, splita, splitb)); 306 splita.setOffline(true); //simulate that splita goes offline when it is split 307 splitParents.put(splita, createResult(splita, splitaa, splitab)); 308 309 final Map<HRegionInfo, Result> mergedRegions = new TreeMap<>(); 310 CatalogJanitor spy = spy(this.janitor); 311 doReturn(new Triple<>(10, mergedRegions, splitParents)).when(spy). 312 getMergedRegionsAndSplitParents(); 313 314 // Create ref from splita to parent 315 LOG.info("parent=" + parent.getShortNameToLog() + ", splita=" + splita.getShortNameToLog()); 316 Path splitaRef = 317 createReferences(this.masterServices, td, parent, splita, Bytes.toBytes("ccc"), false); 318 LOG.info("Created reference " + splitaRef); 319 320 // Parent and splita should not be removed because a reference from splita to parent. 321 assertEquals(0, spy.scan()); 322 323 // Now delete the ref 324 FileSystem fs = FileSystem.get(HTU.getConfiguration()); 325 assertTrue(fs.delete(splitaRef, true)); 326 327 //now, both parent, and splita can be deleted 328 assertEquals(2, spy.scan()); 329 } 330 331 /** 332 * Test that we correctly archive all the storefiles when a region is deleted 333 * @throws Exception 334 */ 335 @Test 336 public void testSplitParentFirstComparator() { 337 SplitParentFirstComparator comp = new SplitParentFirstComparator(); 338 TableDescriptor td = createTableDescriptorForCurrentMethod(); 339 340 /* Region splits: 341 * 342 * rootRegion --- firstRegion --- firstRegiona 343 * | |- firstRegionb 344 * | 345 * |- lastRegion --- lastRegiona --- lastRegionaa 346 * | |- lastRegionab 347 * |- lastRegionb 348 * 349 * rootRegion : [] - [] 350 * firstRegion : [] - bbb 351 * lastRegion : bbb - [] 352 * firstRegiona : [] - aaa 353 * firstRegionb : aaa - bbb 354 * lastRegiona : bbb - ddd 355 * lastRegionb : ddd - [] 356 */ 357 358 // root region 359 HRegionInfo rootRegion = new HRegionInfo(td.getTableName(), 360 HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, true); 361 HRegionInfo firstRegion = new HRegionInfo(td.getTableName(), 362 HConstants.EMPTY_START_ROW, Bytes.toBytes("bbb"), true); 363 HRegionInfo lastRegion = new HRegionInfo(td.getTableName(), 364 Bytes.toBytes("bbb"), HConstants.EMPTY_END_ROW, true); 365 366 assertTrue(comp.compare(rootRegion, rootRegion) == 0); 367 assertTrue(comp.compare(firstRegion, firstRegion) == 0); 368 assertTrue(comp.compare(lastRegion, lastRegion) == 0); 369 assertTrue(comp.compare(rootRegion, firstRegion) < 0); 370 assertTrue(comp.compare(rootRegion, lastRegion) < 0); 371 assertTrue(comp.compare(firstRegion, lastRegion) < 0); 372 373 //first region split into a, b 374 HRegionInfo firstRegiona = new HRegionInfo(td.getTableName(), 375 HConstants.EMPTY_START_ROW, Bytes.toBytes("aaa"), true); 376 HRegionInfo firstRegionb = new HRegionInfo(td.getTableName(), 377 Bytes.toBytes("aaa"), Bytes.toBytes("bbb"), true); 378 //last region split into a, b 379 HRegionInfo lastRegiona = new HRegionInfo(td.getTableName(), 380 Bytes.toBytes("bbb"), Bytes.toBytes("ddd"), true); 381 HRegionInfo lastRegionb = new HRegionInfo(td.getTableName(), 382 Bytes.toBytes("ddd"), HConstants.EMPTY_END_ROW, true); 383 384 assertTrue(comp.compare(firstRegiona, firstRegiona) == 0); 385 assertTrue(comp.compare(firstRegionb, firstRegionb) == 0); 386 assertTrue(comp.compare(rootRegion, firstRegiona) < 0); 387 assertTrue(comp.compare(rootRegion, firstRegionb) < 0); 388 assertTrue(comp.compare(firstRegion, firstRegiona) < 0); 389 assertTrue(comp.compare(firstRegion, firstRegionb) < 0); 390 assertTrue(comp.compare(firstRegiona, firstRegionb) < 0); 391 392 assertTrue(comp.compare(lastRegiona, lastRegiona) == 0); 393 assertTrue(comp.compare(lastRegionb, lastRegionb) == 0); 394 assertTrue(comp.compare(rootRegion, lastRegiona) < 0); 395 assertTrue(comp.compare(rootRegion, lastRegionb) < 0); 396 assertTrue(comp.compare(lastRegion, lastRegiona) < 0); 397 assertTrue(comp.compare(lastRegion, lastRegionb) < 0); 398 assertTrue(comp.compare(lastRegiona, lastRegionb) < 0); 399 400 assertTrue(comp.compare(firstRegiona, lastRegiona) < 0); 401 assertTrue(comp.compare(firstRegiona, lastRegionb) < 0); 402 assertTrue(comp.compare(firstRegionb, lastRegiona) < 0); 403 assertTrue(comp.compare(firstRegionb, lastRegionb) < 0); 404 405 HRegionInfo lastRegionaa = new HRegionInfo(td.getTableName(), 406 Bytes.toBytes("bbb"), Bytes.toBytes("ccc"), false); 407 HRegionInfo lastRegionab = new HRegionInfo(td.getTableName(), 408 Bytes.toBytes("ccc"), Bytes.toBytes("ddd"), false); 409 410 assertTrue(comp.compare(lastRegiona, lastRegionaa) < 0); 411 assertTrue(comp.compare(lastRegiona, lastRegionab) < 0); 412 assertTrue(comp.compare(lastRegionaa, lastRegionab) < 0); 413 } 414 415 @Test 416 public void testArchiveOldRegion() throws Exception { 417 // Create regions. 418 TableDescriptor td = createTableDescriptorForCurrentMethod(); 419 HRegionInfo parent = new HRegionInfo(td.getTableName(), 420 Bytes.toBytes("aaa"), Bytes.toBytes("eee")); 421 HRegionInfo splita = new HRegionInfo(td.getTableName(), 422 Bytes.toBytes("aaa"), Bytes.toBytes("ccc")); 423 HRegionInfo splitb = new HRegionInfo(td.getTableName(), 424 Bytes.toBytes("ccc"), Bytes.toBytes("eee")); 425 426 // Test that when both daughter regions are in place, that we do not 427 // remove the parent. 428 Result parentMetaRow = createResult(parent, splita, splitb); 429 FileSystem fs = FileSystem.get(HTU.getConfiguration()); 430 Path rootdir = this.masterServices.getMasterFileSystem().getRootDir(); 431 // have to set the root directory since we use it in HFileDisposer to figure out to get to the 432 // archive directory. Otherwise, it just seems to pick the first root directory it can find (so 433 // the single test passes, but when the full suite is run, things get borked). 434 FSUtils.setRootDir(fs.getConf(), rootdir); 435 Path tabledir = FSUtils.getTableDir(rootdir, td.getTableName()); 436 Path storedir = HStore.getStoreHomedir(tabledir, parent, td.getColumnFamilies()[0].getName()); 437 Path storeArchive = 438 HFileArchiveUtil.getStoreArchivePath(this.masterServices.getConfiguration(), parent, 439 tabledir, td.getColumnFamilies()[0].getName()); 440 LOG.debug("Table dir:" + tabledir); 441 LOG.debug("Store dir:" + storedir); 442 LOG.debug("Store archive dir:" + storeArchive); 443 444 // add a couple of store files that we can check for 445 FileStatus[] mockFiles = addMockStoreFiles(2, this.masterServices, storedir); 446 // get the current store files for comparison 447 FileStatus[] storeFiles = fs.listStatus(storedir); 448 int index = 0; 449 for (FileStatus file : storeFiles) { 450 LOG.debug("Have store file:" + file.getPath()); 451 assertEquals("Got unexpected store file", mockFiles[index].getPath(), 452 storeFiles[index].getPath()); 453 index++; 454 } 455 456 // do the cleaning of the parent 457 assertTrue(janitor.cleanParent(parent, parentMetaRow)); 458 Path parentDir = new Path(tabledir, parent.getEncodedName()); 459 // Cleanup procedure runs async. Wait till it done. 460 ProcedureTestingUtility.waitAllProcedures(masterServices.getMasterProcedureExecutor()); 461 assertTrue(!fs.exists(parentDir)); 462 LOG.debug("Finished cleanup of parent region"); 463 464 // and now check to make sure that the files have actually been archived 465 FileStatus[] archivedStoreFiles = fs.listStatus(storeArchive); 466 logFiles("archived files", storeFiles); 467 logFiles("archived files", archivedStoreFiles); 468 469 assertArchiveEqualToOriginal(storeFiles, archivedStoreFiles, fs); 470 471 // cleanup 472 FSUtils.delete(fs, rootdir, true); 473 } 474 475 /** 476 * @param description description of the files for logging 477 * @param storeFiles the status of the files to log 478 */ 479 private void logFiles(String description, FileStatus[] storeFiles) { 480 LOG.debug("Current " + description + ": "); 481 for (FileStatus file : storeFiles) { 482 LOG.debug(Objects.toString(file.getPath())); 483 } 484 } 485 486 /** 487 * Test that if a store file with the same name is present as those already backed up cause the 488 * already archived files to be timestamped backup 489 */ 490 @Test 491 public void testDuplicateHFileResolution() throws Exception { 492 TableDescriptor td = createTableDescriptorForCurrentMethod(); 493 494 // Create regions. 495 HRegionInfo parent = new HRegionInfo(td.getTableName(), 496 Bytes.toBytes("aaa"), Bytes.toBytes("eee")); 497 HRegionInfo splita = new HRegionInfo(td.getTableName(), 498 Bytes.toBytes("aaa"), Bytes.toBytes("ccc")); 499 HRegionInfo splitb = new HRegionInfo(td.getTableName(), 500 Bytes.toBytes("ccc"), Bytes.toBytes("eee")); 501 // Test that when both daughter regions are in place, that we do not 502 // remove the parent. 503 Result r = createResult(parent, splita, splitb); 504 FileSystem fs = FileSystem.get(HTU.getConfiguration()); 505 Path rootdir = this.masterServices.getMasterFileSystem().getRootDir(); 506 // Have to set the root directory since we use it in HFileDisposer to figure out to get to the 507 // archive directory. Otherwise, it just seems to pick the first root directory it can find (so 508 // the single test passes, but when the full suite is run, things get borked). 509 FSUtils.setRootDir(fs.getConf(), rootdir); 510 Path tabledir = FSUtils.getTableDir(rootdir, parent.getTable()); 511 Path storedir = HStore.getStoreHomedir(tabledir, parent, td.getColumnFamilies()[0].getName()); 512 System.out.println("Old root:" + rootdir); 513 System.out.println("Old table:" + tabledir); 514 System.out.println("Old store:" + storedir); 515 516 Path storeArchive = 517 HFileArchiveUtil.getStoreArchivePath(this.masterServices.getConfiguration(), parent, 518 tabledir, td.getColumnFamilies()[0].getName()); 519 System.out.println("Old archive:" + storeArchive); 520 521 // enable archiving, make sure that files get archived 522 addMockStoreFiles(2, this.masterServices, storedir); 523 // get the current store files for comparison 524 FileStatus[] storeFiles = fs.listStatus(storedir); 525 // Do the cleaning of the parent 526 assertTrue(janitor.cleanParent(parent, r)); 527 Path parentDir = new Path(tabledir, parent.getEncodedName()); 528 ProcedureTestingUtility.waitAllProcedures(masterServices.getMasterProcedureExecutor()); 529 assertTrue(!fs.exists(parentDir)); 530 531 // And now check to make sure that the files have actually been archived 532 FileStatus[] archivedStoreFiles = fs.listStatus(storeArchive); 533 assertArchiveEqualToOriginal(storeFiles, archivedStoreFiles, fs); 534 535 // now add store files with the same names as before to check backup 536 // enable archiving, make sure that files get archived 537 addMockStoreFiles(2, this.masterServices, storedir); 538 539 // Do the cleaning of the parent 540 assertTrue(janitor.cleanParent(parent, r)); 541 // Cleanup procedure runs async. Wait till it done. 542 ProcedureTestingUtility.waitAllProcedures(masterServices.getMasterProcedureExecutor()); 543 assertTrue(!fs.exists(parentDir)); 544 545 // and now check to make sure that the files have actually been archived 546 archivedStoreFiles = fs.listStatus(storeArchive); 547 assertArchiveEqualToOriginal(storeFiles, archivedStoreFiles, fs, true); 548 } 549 550 private FileStatus[] addMockStoreFiles(int count, MasterServices services, Path storedir) 551 throws IOException { 552 // get the existing store files 553 FileSystem fs = services.getMasterFileSystem().getFileSystem(); 554 fs.mkdirs(storedir); 555 // create the store files in the parent 556 for (int i = 0; i < count; i++) { 557 Path storeFile = new Path(storedir, "_store" + i); 558 FSDataOutputStream dos = fs.create(storeFile, true); 559 dos.writeBytes("Some data: " + i); 560 dos.close(); 561 } 562 LOG.debug("Adding " + count + " store files to the storedir:" + storedir); 563 // make sure the mock store files are there 564 FileStatus[] storeFiles = fs.listStatus(storedir); 565 assertEquals("Didn't have expected store files", count, storeFiles.length); 566 return storeFiles; 567 } 568 569 private String setRootDirAndCleanIt(final HBaseTestingUtility htu, final String subdir) 570 throws IOException { 571 Path testdir = htu.getDataTestDir(subdir); 572 FileSystem fs = FileSystem.get(htu.getConfiguration()); 573 if (fs.exists(testdir)) assertTrue(fs.delete(testdir, true)); 574 FSUtils.setRootDir(htu.getConfiguration(), testdir); 575 return FSUtils.getRootDir(htu.getConfiguration()).toString(); 576 } 577 578 private Path createReferences(final MasterServices services, 579 final TableDescriptor td, final HRegionInfo parent, 580 final HRegionInfo daughter, final byte [] midkey, final boolean top) 581 throws IOException { 582 Path rootdir = services.getMasterFileSystem().getRootDir(); 583 Path tabledir = FSUtils.getTableDir(rootdir, parent.getTable()); 584 Path storedir = HStore.getStoreHomedir(tabledir, daughter, 585 td.getColumnFamilies()[0].getName()); 586 Reference ref = 587 top? Reference.createTopReference(midkey): Reference.createBottomReference(midkey); 588 long now = System.currentTimeMillis(); 589 // Reference name has this format: StoreFile#REF_NAME_PARSER 590 Path p = new Path(storedir, Long.toString(now) + "." + parent.getEncodedName()); 591 FileSystem fs = services.getMasterFileSystem().getFileSystem(); 592 ref.write(fs, p); 593 return p; 594 } 595 596 private Result createResult(final HRegionInfo parent, final HRegionInfo a, 597 final HRegionInfo b) 598 throws IOException { 599 return MetaMockingUtil.getMetaTableRowResult(parent, null, a, b); 600 } 601}