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 */ 018 019package org.apache.hadoop.hbase.regionserver.compactions; 020 021import static org.mockito.Mockito.mock; 022import static org.mockito.Mockito.when; 023 024import java.util.LinkedList; 025import java.util.List; 026import java.util.Random; 027 028import org.apache.commons.lang3.RandomStringUtils; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.hbase.regionserver.HStoreFile; 031import org.apache.hadoop.hbase.regionserver.StoreFileReader; 032import org.apache.hadoop.util.StringUtils; 033 034import org.apache.hbase.thirdparty.com.google.common.base.MoreObjects; 035 036/** 037 * Base class of objects that can create mock store files with a given size. 038 */ 039class MockStoreFileGenerator { 040 /** How many chars long the store file name will be. */ 041 private static final int FILENAME_LENGTH = 10; 042 /** The random number generator. */ 043 protected Random random; 044 045 MockStoreFileGenerator(Class<?> klass) { 046 random = new Random(klass.getSimpleName().hashCode()); 047 } 048 049 protected List<HStoreFile> createStoreFileList(final int[] fs) { 050 List<HStoreFile> storeFiles = new LinkedList<>(); 051 for (int fileSize : fs) { 052 storeFiles.add(createMockStoreFile(fileSize)); 053 } 054 return storeFiles; 055 } 056 057 protected HStoreFile createMockStoreFile(final long size) { 058 return createMockStoreFile(size * 1024 * 1024, -1L); 059 } 060 061 protected HStoreFile createMockStoreFileBytes(final long size) { 062 return createMockStoreFile(size, -1L); 063 } 064 065 protected HStoreFile createMockStoreFile(final long sizeInBytes, final long seqId) { 066 HStoreFile mockSf = mock(HStoreFile.class); 067 StoreFileReader reader = mock(StoreFileReader.class); 068 String stringPath = "/hbase/testTable/regionA/" + 069 RandomStringUtils.random(FILENAME_LENGTH, 0, 0, true, true, null, random); 070 Path path = new Path(stringPath); 071 072 073 when(reader.getSequenceID()).thenReturn(seqId); 074 when(reader.getTotalUncompressedBytes()).thenReturn(sizeInBytes); 075 when(reader.length()).thenReturn(sizeInBytes); 076 077 when(mockSf.getPath()).thenReturn(path); 078 when(mockSf.excludeFromMinorCompaction()).thenReturn(false); 079 when(mockSf.isReference()).thenReturn(false); // TODO come back to 080 // this when selection takes this into account 081 when(mockSf.getReader()).thenReturn(reader); 082 String toString = MoreObjects.toStringHelper("MockStoreFile") 083 .add("isReference", false) 084 .add("fileSize", StringUtils.humanReadableInt(sizeInBytes)) 085 .add("seqId", seqId) 086 .add("path", stringPath).toString(); 087 when(mockSf.toString()).thenReturn(toString); 088 089 return mockSf; 090 } 091}