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.master.region; 019 020import static org.mockito.Mockito.mock; 021import static org.mockito.Mockito.when; 022 023import java.io.IOException; 024import java.util.concurrent.TimeUnit; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.ChoreService; 028import org.apache.hadoop.hbase.HBaseCommonTestingUtility; 029import org.apache.hadoop.hbase.Server; 030import org.apache.hadoop.hbase.ServerName; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 033import org.apache.hadoop.hbase.client.TableDescriptor; 034import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 035import org.apache.hadoop.hbase.master.cleaner.DirScanPool; 036import org.apache.hadoop.hbase.regionserver.MemStoreLAB; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.apache.hadoop.hbase.util.CommonFSUtils; 039import org.junit.After; 040import org.junit.Before; 041 042public class MasterRegionTestBase { 043 044 protected HBaseCommonTestingUtility htu; 045 046 protected MasterRegion region; 047 048 protected ChoreService choreService; 049 050 protected DirScanPool cleanerPool; 051 052 protected static byte[] CF1 = Bytes.toBytes("f1"); 053 054 protected static byte[] CF2 = Bytes.toBytes("f2"); 055 056 protected static byte[] QUALIFIER = Bytes.toBytes("q"); 057 058 protected static String REGION_DIR_NAME = "local"; 059 060 protected static TableDescriptor TD = 061 TableDescriptorBuilder.newBuilder(TableName.valueOf("test:local")) 062 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF1)) 063 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF2)).build(); 064 065 protected void configure(Configuration conf) throws IOException { 066 } 067 068 protected void configure(MasterRegionParams params) { 069 } 070 071 protected void postSetUp() throws IOException { 072 } 073 074 @Before 075 public void setUp() throws IOException { 076 htu = new HBaseCommonTestingUtility(); 077 htu.getConfiguration().setBoolean(MemStoreLAB.USEMSLAB_KEY, false); 078 // Runs on local filesystem. Test does not need sync. Turn off checks. 079 htu.getConfiguration().setBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE, false); 080 configure(htu.getConfiguration()); 081 choreService = new ChoreService(getClass().getSimpleName()); 082 cleanerPool = new DirScanPool(htu.getConfiguration()); 083 Server server = mock(Server.class); 084 when(server.getConfiguration()).thenReturn(htu.getConfiguration()); 085 when(server.getServerName()) 086 .thenReturn(ServerName.valueOf("localhost", 12345, System.currentTimeMillis())); 087 when(server.getChoreService()).thenReturn(choreService); 088 Path testDir = htu.getDataTestDir(); 089 CommonFSUtils.setRootDir(htu.getConfiguration(), testDir); 090 MasterRegionParams params = new MasterRegionParams(); 091 params.server(server).regionDirName(REGION_DIR_NAME).tableDescriptor(TD) 092 .flushSize(TableDescriptorBuilder.DEFAULT_MEMSTORE_FLUSH_SIZE).flushPerChanges(1_000_000) 093 .flushIntervalMs(TimeUnit.MINUTES.toMillis(15)).compactMin(4).maxWals(32).useHsync(false) 094 .ringBufferSlotCount(16).rollPeriodMs(TimeUnit.MINUTES.toMillis(15)) 095 .archivedWalSuffix(MasterRegionFactory.ARCHIVED_WAL_SUFFIX) 096 .archivedHFileSuffix(MasterRegionFactory.ARCHIVED_HFILE_SUFFIX); 097 configure(params); 098 region = MasterRegion.create(params); 099 postSetUp(); 100 } 101 102 @After 103 public void tearDown() throws IOException { 104 region.close(true); 105 cleanerPool.shutdownNow(); 106 choreService.shutdown(); 107 htu.cleanupTestDir(); 108 } 109}