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 java.io.IOException;
021import java.util.HashMap;
022import java.util.Map;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
026import org.apache.hadoop.hbase.regionserver.compactions.ExponentialCompactionWindowFactory;
027import org.apache.hadoop.hbase.testclassification.RegionServerTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ RegionServerTests.class, SmallTests.class })
034public class TestDateTieredCompactionPolicyHeterogeneousStorage
035    extends AbstractTestDateTieredCompactionPolicy {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestDateTieredCompactionPolicyHeterogeneousStorage.class);
039  public static final String HOT_WINDOW_SP = "ALL_SSD";
040  public static final String WARM_WINDOW_SP = "ONE_SSD";
041  public static final String COLD_WINDOW_SP = "HOT";
042
043  @Override
044  protected void config() {
045    super.config();
046
047    // Set up policy
048    conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY,
049      "org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine");
050    conf.setLong(CompactionConfiguration.DATE_TIERED_MAX_AGE_MILLIS_KEY, 100);
051    conf.setLong(CompactionConfiguration.DATE_TIERED_INCOMING_WINDOW_MIN_KEY, 3);
052    conf.setLong(ExponentialCompactionWindowFactory.BASE_WINDOW_MILLIS_KEY, 6);
053    conf.setInt(ExponentialCompactionWindowFactory.WINDOWS_PER_TIER_KEY, 4);
054    conf.setBoolean(CompactionConfiguration.DATE_TIERED_SINGLE_OUTPUT_FOR_MINOR_COMPACTION_KEY,
055      false);
056
057    // Special settings for compaction policy per window
058    this.conf.setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY, 2);
059    this.conf.setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MAX_KEY, 12);
060    this.conf.setFloat(CompactionConfiguration.HBASE_HSTORE_COMPACTION_RATIO_KEY, 1.2F);
061
062    conf.setInt(HStore.BLOCKING_STOREFILES_KEY, 20);
063    conf.setLong(HConstants.MAJOR_COMPACTION_PERIOD, 5);
064
065    // Set Storage Policy for different type window
066    conf.setBoolean(CompactionConfiguration.DATE_TIERED_STORAGE_POLICY_ENABLE_KEY, true);
067    conf.setLong(CompactionConfiguration.DATE_TIERED_HOT_WINDOW_AGE_MILLIS_KEY, 6);
068    conf.set(CompactionConfiguration.DATE_TIERED_HOT_WINDOW_STORAGE_POLICY_KEY, HOT_WINDOW_SP);
069    conf.setLong(CompactionConfiguration.DATE_TIERED_WARM_WINDOW_AGE_MILLIS_KEY, 12);
070    conf.set(CompactionConfiguration.DATE_TIERED_WARM_WINDOW_STORAGE_POLICY_KEY, WARM_WINDOW_SP);
071    conf.set(CompactionConfiguration.DATE_TIERED_COLD_WINDOW_STORAGE_POLICY_KEY, COLD_WINDOW_SP);
072  }
073
074  /**
075   * Test for minor compaction of incoming window.
076   * Incoming window start ts >= now - hot age. So it is HOT window, will use HOT_WINDOW_SP.
077   * @throws IOException with error
078   */
079  @Test
080  public void testIncomingWindowHot() throws IOException {
081    long[] minTimestamps = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
082    long[] maxTimestamps = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
083    long[] sizes = new long[] { 30, 31, 32, 33, 34, 20, 21, 22, 23, 24, 25, 10, 11, 12, 13 };
084    Map<Long, String> expected = new HashMap<>();
085    // expected DateTieredCompactionRequest boundaries = { Long.MIN_VALUE, 12 }
086    // test whether DateTieredCompactionRequest boundariesPolicies matches expected
087    expected.put(12L, HOT_WINDOW_SP);
088    compactEqualsStoragePolicy(16, sfCreate(minTimestamps, maxTimestamps, sizes),
089      expected, false, true);
090  }
091
092  /**
093   * Test for not incoming window.
094   * now - hot age > window start >= now - warm age,
095   * so this window and is WARM window, will use WARM_WINDOW_SP
096   * @throws IOException with error
097   */
098  @Test
099  public void testNotIncomingWindowWarm() throws IOException {
100    long[] minTimestamps = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
101    long[] maxTimestamps = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
102    long[] sizes = new long[] { 30, 31, 32, 33, 34, 20, 21, 22, 23, 24, 25, 10, 11 };
103    Map<Long, String> expected = new HashMap<>();
104    // expected DateTieredCompactionRequest boundaries = { Long.MIN_VALUE, 6 }
105    expected.put(6L, WARM_WINDOW_SP);
106    compactEqualsStoragePolicy(16, sfCreate(minTimestamps, maxTimestamps, sizes),
107      expected, false, true);
108  }
109
110  /**
111   * Test for not incoming window.
112   * this window start ts >= ow - hot age,
113   * So this incoming window and is HOT window. Use HOT_WINDOW_SP
114   * @throws IOException with error
115   */
116  @Test
117  public void testNotIncomingWindowAndIsHot() throws IOException {
118    long[] minTimestamps = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
119    long[] maxTimestamps = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
120    long[] sizes = new long[] { 30, 31, 32, 33, 34, 20, 21, 22, 23, 24, 25, 10, 11 };
121    Map<Long, String> expected = new HashMap<>();
122    // expected DateTieredCompactionRequest boundaries = { Long.MIN_VALUE, 6 }
123    expected.put(6L, HOT_WINDOW_SP);
124    compactEqualsStoragePolicy(12, sfCreate(minTimestamps, maxTimestamps, sizes),
125      expected, false, true);
126  }
127
128  /**
129   * Test for not incoming window.
130   * COLD window start timestamp < now - warm age, so use COLD_WINDOW_SP
131   * @throws IOException with error
132   */
133  @Test
134  public void testColdWindow() throws IOException {
135    long[] minTimestamps = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
136    long[] maxTimestamps = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
137    long[] sizes = new long[] { 30, 31, 32, 33, 34, 20, 21, 22, 23, 24, 25, 10 };
138    Map<Long, String> expected = new HashMap<>();
139    // expected DateTieredCompactionRequest boundaries = { Long.MIN_VALUE, 6 }
140    expected.put(6L, COLD_WINDOW_SP);
141    compactEqualsStoragePolicy(22, sfCreate(minTimestamps, maxTimestamps, sizes),
142      expected, false, true);
143  }
144
145  /**
146   * Test for not incoming window. but not all hfiles will be selected to compact.
147   * Apply exploring logic on non-incoming window. More than one hfile left in this window.
148   * this means minor compact single out is true. boundaries only contains Long.MIN_VALUE
149   * @throws IOException with error
150   */
151  @Test
152  public void testRatioT0() throws IOException {
153    long[] minTimestamps = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
154    long[] maxTimestamps = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
155    long[] sizes = new long[] { 30, 31, 32, 33, 34, 20, 21, 22, 280, 23, 24, 1 };
156    Map<Long, String> expected = new HashMap<>();
157    // window start = 6, expected DateTieredCompactionRequest boundaries = { Long.MIN_VALUE }
158    expected.put(Long.MIN_VALUE, WARM_WINDOW_SP);
159    compactEqualsStoragePolicy(16, sfCreate(minTimestamps, maxTimestamps, sizes),
160      expected, false, true);
161  }
162
163  /**
164   * Test for Major compaction. It will compact all files and create multi output files
165   * with different window storage policy.
166   * @throws IOException with error
167   */
168  @Test
169  public void testMajorCompation() throws IOException {
170    long[] minTimestamps = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
171    long[] maxTimestamps = new long[] { 44, 60, 61, 96, 100, 104, 105, 106, 113, 145, 157 };
172    long[] sizes = new long[] { 0, 50, 51, 40, 41, 42, 33, 30, 31, 2, 1 };
173    Map<Long, String> expected = new HashMap<>();
174    expected.put(Long.MIN_VALUE, COLD_WINDOW_SP);
175    expected.put(24L, COLD_WINDOW_SP);
176    expected.put(48L, COLD_WINDOW_SP);
177    expected.put(72L, COLD_WINDOW_SP);
178    expected.put(96L, COLD_WINDOW_SP);
179    expected.put(120L, COLD_WINDOW_SP);
180    expected.put(144L, COLD_WINDOW_SP);
181    expected.put(150L, WARM_WINDOW_SP);
182    expected.put(156L, HOT_WINDOW_SP);
183    compactEquals(161, sfCreate(minTimestamps, maxTimestamps, sizes),
184      new long[] { 0, 50, 51, 40, 41, 42, 33, 30, 31, 2, 1 },
185      new long[] { Long.MIN_VALUE, 24, 48, 72, 96, 120, 144, 150, 156 }, true, true);
186    compactEqualsStoragePolicy(161, sfCreate(minTimestamps, maxTimestamps, sizes),
187      expected,true, true);
188  }
189}