@Test
  public void testAddingCompactionResults() throws Exception {
    StripeStoreFileManager manager = createManager();
    // First, add some L0 files and "compact" one with new stripe creation.
    HStoreFile sf_L0_0a = createFile(), sf_L0_0b = createFile();
    manager.insertNewFiles(al(sf_L0_0a, sf_L0_0b));

    // Try compacting with invalid new branches (gaps, overlaps) - no effect.
    verifyInvalidCompactionScenario(manager, al(sf_L0_0a), al(createFile(OPEN_KEY, KEY_B)));
    verifyInvalidCompactionScenario(manager, al(sf_L0_0a),
      al(createFile(OPEN_KEY, KEY_B), createFile(KEY_C, OPEN_KEY)));
    verifyInvalidCompactionScenario(manager, al(sf_L0_0a),
      al(createFile(OPEN_KEY, KEY_B), createFile(KEY_B, OPEN_KEY), createFile(KEY_A, KEY_D)));
    verifyInvalidCompactionScenario(manager, al(sf_L0_0a),
      al(createFile(OPEN_KEY, KEY_B), createFile(KEY_A, KEY_B), createFile(KEY_B, OPEN_KEY)));

    HStoreFile sf_i2B_0 = createFile(OPEN_KEY, KEY_B);
    HStoreFile sf_B2C_0 = createFile(KEY_B, KEY_C);
    HStoreFile sf_C2i_0 = createFile(KEY_C, OPEN_KEY);
    manager.addCompactionResults(al(sf_L0_0a), al(sf_i2B_0, sf_B2C_0, sf_C2i_0));
    manager.removeCompactedFiles(al(sf_L0_0a));
    verifyAllFiles(manager, al(sf_L0_0b, sf_i2B_0, sf_B2C_0, sf_C2i_0));

    // Add another l0 file, "compact" both L0 into two stripes
    HStoreFile sf_L0_1 = createFile();
    HStoreFile sf_i2B_1 = createFile(OPEN_KEY, KEY_B);
    HStoreFile sf_B2C_1 = createFile(KEY_B, KEY_C);
    manager.insertNewFiles(al(sf_L0_1));
    manager.addCompactionResults(al(sf_L0_0b, sf_L0_1), al(sf_i2B_1, sf_B2C_1));
    manager.removeCompactedFiles(al(sf_L0_0b, sf_L0_1));
    verifyAllFiles(manager, al(sf_i2B_0, sf_B2C_0, sf_C2i_0, sf_i2B_1, sf_B2C_1));

    // Try compacting with invalid file (no metadata) - should add files to L0.
    HStoreFile sf_L0_2 = createFile(null, null);
    manager.addCompactionResults(al(), al(sf_L0_2));
    manager.removeCompactedFiles(al());
    verifyAllFiles(manager, al(sf_i2B_0, sf_B2C_0, sf_C2i_0, sf_i2B_1, sf_B2C_1, sf_L0_2));
    // Remove it...
    manager.addCompactionResults(al(sf_L0_2), al());
    manager.removeCompactedFiles(al(sf_L0_2));

    // Do regular compaction in the first stripe.
    HStoreFile sf_i2B_3 = createFile(OPEN_KEY, KEY_B);
    manager.addCompactionResults(al(sf_i2B_0, sf_i2B_1), al(sf_i2B_3));
    manager.removeCompactedFiles(al(sf_i2B_0, sf_i2B_1));
    verifyAllFiles(manager, al(sf_B2C_0, sf_C2i_0, sf_B2C_1, sf_i2B_3));

    // Rebalance two stripes.
    HStoreFile sf_B2D_4 = createFile(KEY_B, KEY_D);
    HStoreFile sf_D2i_4 = createFile(KEY_D, OPEN_KEY);
    manager.addCompactionResults(al(sf_B2C_0, sf_C2i_0, sf_B2C_1), al(sf_B2D_4, sf_D2i_4));
    manager.removeCompactedFiles(al(sf_B2C_0, sf_C2i_0, sf_B2C_1));
    verifyAllFiles(manager, al(sf_i2B_3, sf_B2D_4, sf_D2i_4));

    // Split the first stripe.
    HStoreFile sf_i2A_5 = createFile(OPEN_KEY, KEY_A);
    HStoreFile sf_A2B_5 = createFile(KEY_A, KEY_B);
    manager.addCompactionResults(al(sf_i2B_3), al(sf_i2A_5, sf_A2B_5));
    manager.removeCompactedFiles(al(sf_i2B_3));
    verifyAllFiles(manager, al(sf_B2D_4, sf_D2i_4, sf_i2A_5, sf_A2B_5));

    // Split the middle stripe.
    HStoreFile sf_B2C_6 = createFile(KEY_B, KEY_C);
    HStoreFile sf_C2D_6 = createFile(KEY_C, KEY_D);
    manager.addCompactionResults(al(sf_B2D_4), al(sf_B2C_6, sf_C2D_6));
    manager.removeCompactedFiles(al(sf_B2D_4));
    verifyAllFiles(manager, al(sf_D2i_4, sf_i2A_5, sf_A2B_5, sf_B2C_6, sf_C2D_6));

    // Merge two different middle stripes.
    HStoreFile sf_A2C_7 = createFile(KEY_A, KEY_C);
    manager.addCompactionResults(al(sf_A2B_5, sf_B2C_6), al(sf_A2C_7));
    manager.removeCompactedFiles(al(sf_A2B_5, sf_B2C_6));
    verifyAllFiles(manager, al(sf_D2i_4, sf_i2A_5, sf_C2D_6, sf_A2C_7));

    // Merge lower half.
    HStoreFile sf_i2C_8 = createFile(OPEN_KEY, KEY_C);
    manager.addCompactionResults(al(sf_i2A_5, sf_A2C_7), al(sf_i2C_8));
    manager.removeCompactedFiles(al(sf_i2A_5, sf_A2C_7));
    verifyAllFiles(manager, al(sf_D2i_4, sf_C2D_6, sf_i2C_8));

    // Merge all.
    HStoreFile sf_i2i_9 = createFile(OPEN_KEY, OPEN_KEY);
    manager.addCompactionResults(al(sf_D2i_4, sf_C2D_6, sf_i2C_8), al(sf_i2i_9));
    manager.removeCompactedFiles(al(sf_D2i_4, sf_C2D_6, sf_i2C_8));
    verifyAllFiles(manager, al(sf_i2i_9));
  }
Verifies scenario for finding a split point.
@param splitPointAfter    Stripe to expect the split point at/after.
@param shouldSplitStripe  If true, the split point is expected in the middle of the above
                          stripe; if false, should be at the end.
@param splitRatioToVerify Maximum split imbalance ratio.
@param sizes              Stripe sizes.
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestStripeStoreFileManager.java