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;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026
027import org.apache.hadoop.hbase.regionserver.compactions.DateTieredCompactionPolicy;
028import org.apache.hadoop.hbase.regionserver.compactions.DateTieredCompactionRequest;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
031
032import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
033import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
034
035public class AbstractTestDateTieredCompactionPolicy extends TestCompactionPolicy {
036
037  protected ArrayList<HStoreFile> sfCreate(long[] minTimestamps, long[] maxTimestamps, long[] sizes)
038      throws IOException {
039    ManualEnvironmentEdge timeMachine = new ManualEnvironmentEdge();
040    EnvironmentEdgeManager.injectEdge(timeMachine);
041    // Has to be > 0 and < now.
042    timeMachine.setValue(1);
043    ArrayList<Long> ageInDisk = new ArrayList<>();
044    for (int i = 0; i < sizes.length; i++) {
045      ageInDisk.add(0L);
046    }
047
048    ArrayList<HStoreFile> ret = Lists.newArrayList();
049    for (int i = 0; i < sizes.length; i++) {
050      MockHStoreFile msf =
051          new MockHStoreFile(TEST_UTIL, TEST_FILE, sizes[i], ageInDisk.get(i), false, i);
052      msf.setTimeRangeTracker(TimeRangeTracker.create(TimeRangeTracker.Type.SYNC, minTimestamps[i], maxTimestamps[i]));
053      ret.add(msf);
054    }
055    return ret;
056  }
057
058  protected void compactEquals(long now, ArrayList<HStoreFile> candidates, long[] expectedFileSizes,
059      long[] expectedBoundaries, boolean isMajor, boolean toCompact) throws IOException {
060    ManualEnvironmentEdge timeMachine = new ManualEnvironmentEdge();
061    EnvironmentEdgeManager.injectEdge(timeMachine);
062    timeMachine.setValue(now);
063    DateTieredCompactionRequest request;
064    DateTieredCompactionPolicy policy =
065        (DateTieredCompactionPolicy) store.storeEngine.getCompactionPolicy();
066    if (isMajor) {
067      for (HStoreFile file : candidates) {
068        ((MockHStoreFile) file).setIsMajor(true);
069      }
070      assertEquals(toCompact, policy.shouldPerformMajorCompaction(candidates));
071      request = (DateTieredCompactionRequest) policy.selectMajorCompaction(candidates);
072    } else {
073      assertEquals(toCompact, policy.needsCompaction(candidates, ImmutableList.of()));
074      request =
075          (DateTieredCompactionRequest) policy.selectMinorCompaction(candidates, false, false);
076    }
077    List<HStoreFile> actual = Lists.newArrayList(request.getFiles());
078    assertEquals(Arrays.toString(expectedFileSizes), Arrays.toString(getSizes(actual)));
079    assertEquals(Arrays.toString(expectedBoundaries),
080      Arrays.toString(request.getBoundaries().toArray()));
081  }
082}