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.regionserver.storefiletracker.StoreFileTrackerForTest; 030import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 031import org.apache.hadoop.hbase.util.ManualEnvironmentEdge; 032 033import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList; 034import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 035 036public class AbstractTestDateTieredCompactionPolicy extends TestCompactionPolicy { 037 038 protected ArrayList<HStoreFile> sfCreate(long[] minTimestamps, long[] maxTimestamps, long[] sizes) 039 throws IOException { 040 ManualEnvironmentEdge timeMachine = new ManualEnvironmentEdge(); 041 EnvironmentEdgeManager.injectEdge(timeMachine); 042 // Has to be > 0 and < now. 043 timeMachine.setValue(1); 044 ArrayList<Long> ageInDisk = new ArrayList<>(); 045 for (int i = 0; i < sizes.length; i++) { 046 ageInDisk.add(0L); 047 } 048 049 ArrayList<HStoreFile> ret = Lists.newArrayList(); 050 StoreFileTrackerForTest storeFileTrackerForTest = 051 new StoreFileTrackerForTest(conf, true, store.getStoreContext()); 052 for (int i = 0; i < sizes.length; i++) { 053 MockHStoreFile msf = new MockHStoreFile(TEST_UTIL, TEST_FILE, sizes[i], ageInDisk.get(i), 054 false, i, storeFileTrackerForTest); 055 msf.setTimeRangeTracker( 056 TimeRangeTracker.create(TimeRangeTracker.Type.SYNC, minTimestamps[i], maxTimestamps[i])); 057 ret.add(msf); 058 } 059 return ret; 060 } 061 062 protected void compactEquals(long now, ArrayList<HStoreFile> candidates, long[] expectedFileSizes, 063 long[] expectedBoundaries, boolean isMajor, boolean toCompact) throws IOException { 064 DateTieredCompactionRequest request = getRequest(now, candidates, isMajor, toCompact); 065 List<HStoreFile> actual = Lists.newArrayList(request.getFiles()); 066 assertEquals(Arrays.toString(expectedFileSizes), Arrays.toString(getSizes(actual))); 067 assertEquals(Arrays.toString(expectedBoundaries), 068 Arrays.toString(request.getBoundaries().toArray())); 069 } 070 071 private DateTieredCompactionRequest getRequest(long now, ArrayList<HStoreFile> candidates, 072 boolean isMajor, boolean toCompact) throws IOException { 073 ManualEnvironmentEdge timeMachine = new ManualEnvironmentEdge(); 074 EnvironmentEdgeManager.injectEdge(timeMachine); 075 timeMachine.setValue(now); 076 DateTieredCompactionRequest request; 077 DateTieredCompactionPolicy policy = 078 (DateTieredCompactionPolicy) store.storeEngine.getCompactionPolicy(); 079 if (isMajor) { 080 for (HStoreFile file : candidates) { 081 ((MockHStoreFile) file).setIsMajor(true); 082 } 083 assertEquals(toCompact, policy.shouldPerformMajorCompaction(candidates)); 084 request = (DateTieredCompactionRequest) policy.selectMajorCompaction(candidates); 085 } else { 086 assertEquals(toCompact, policy.needsCompaction(candidates, ImmutableList.of())); 087 request = 088 (DateTieredCompactionRequest) policy.selectMinorCompaction(candidates, false, false); 089 } 090 return request; 091 } 092 093 protected void compactEqualsStoragePolicy(long now, ArrayList<HStoreFile> candidates, 094 Map<Long, String> expectedBoundariesPolicies, boolean isMajor, boolean toCompact) 095 throws IOException { 096 DateTieredCompactionRequest request = getRequest(now, candidates, isMajor, toCompact); 097 Map<Long, String> boundariesPolicies = request.getBoundariesPolicies(); 098 for (Map.Entry<Long, String> entry : expectedBoundariesPolicies.entrySet()) { 099 assertEquals(entry.getValue(), boundariesPolicies.get(entry.getKey())); 100 } 101 } 102}