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; 026import java.util.Map; 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( 053 TimeRangeTracker.create(TimeRangeTracker.Type.SYNC, minTimestamps[i], maxTimestamps[i])); 054 ret.add(msf); 055 } 056 return ret; 057 } 058 059 protected void compactEquals(long now, ArrayList<HStoreFile> candidates, long[] expectedFileSizes, 060 long[] expectedBoundaries, boolean isMajor, boolean toCompact) throws IOException { 061 DateTieredCompactionRequest request = getRequest(now, candidates, isMajor, toCompact); 062 List<HStoreFile> actual = Lists.newArrayList(request.getFiles()); 063 assertEquals(Arrays.toString(expectedFileSizes), Arrays.toString(getSizes(actual))); 064 assertEquals(Arrays.toString(expectedBoundaries), 065 Arrays.toString(request.getBoundaries().toArray())); 066 } 067 068 private DateTieredCompactionRequest getRequest(long now, ArrayList<HStoreFile> candidates, 069 boolean isMajor, boolean toCompact) throws IOException { 070 ManualEnvironmentEdge timeMachine = new ManualEnvironmentEdge(); 071 EnvironmentEdgeManager.injectEdge(timeMachine); 072 timeMachine.setValue(now); 073 DateTieredCompactionRequest request; 074 DateTieredCompactionPolicy policy = 075 (DateTieredCompactionPolicy) store.storeEngine.getCompactionPolicy(); 076 if (isMajor) { 077 for (HStoreFile file : candidates) { 078 ((MockHStoreFile) file).setIsMajor(true); 079 } 080 assertEquals(toCompact, policy.shouldPerformMajorCompaction(candidates)); 081 request = (DateTieredCompactionRequest) policy.selectMajorCompaction(candidates); 082 } else { 083 assertEquals(toCompact, policy.needsCompaction(candidates, ImmutableList.of())); 084 request = 085 (DateTieredCompactionRequest) policy.selectMinorCompaction(candidates, false, false); 086 } 087 return request; 088 } 089 090 protected void compactEqualsStoragePolicy(long now, ArrayList<HStoreFile> candidates, 091 Map<Long, String> expectedBoundariesPolicies, boolean isMajor, boolean toCompact) 092 throws IOException { 093 DateTieredCompactionRequest request = getRequest(now, candidates, isMajor, toCompact); 094 Map<Long, String> boundariesPolicies = request.getBoundariesPolicies(); 095 for (Map.Entry<Long, String> entry : expectedBoundariesPolicies.entrySet()) { 096 assertEquals(entry.getValue(), boundariesPolicies.get(entry.getKey())); 097 } 098 } 099}