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.HBaseCommonTestingUtil;
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.apache.hadoop.hbase.util.EnvironmentEdgeManager;
040import org.junit.After;
041import org.junit.Before;
042
043public class MasterRegionTestBase {
044
045  protected HBaseCommonTestingUtil htu;
046
047  protected MasterRegion region;
048
049  protected ChoreService choreService;
050
051  protected DirScanPool hfileCleanerPool;
052
053  protected DirScanPool logCleanerPool;
054
055  protected static byte[] CF1 = Bytes.toBytes("f1");
056
057  protected static byte[] CF2 = Bytes.toBytes("f2");
058
059  protected static byte[] QUALIFIER = Bytes.toBytes("q");
060
061  protected static String REGION_DIR_NAME = "local";
062
063  protected static TableDescriptor TD =
064    TableDescriptorBuilder.newBuilder(TableName.valueOf("test:local"))
065      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF1))
066      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF2)).build();
067
068  protected void configure(Configuration conf) throws IOException {
069  }
070
071  protected void configure(MasterRegionParams params) {
072  }
073
074  protected void postSetUp() throws IOException {
075  }
076
077  @Before
078  public void setUp() throws IOException {
079    htu = new HBaseCommonTestingUtil();
080    htu.getConfiguration().setBoolean(MemStoreLAB.USEMSLAB_KEY, false);
081    // Runs on local filesystem. Test does not need sync. Turn off checks.
082    htu.getConfiguration().setBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE, false);
083
084    createMasterRegion();
085  }
086
087  /**
088   * Creates a new MasterRegion using an existing {@code htu} on this class.
089   */
090  protected void createMasterRegion() throws IOException {
091    configure(htu.getConfiguration());
092    choreService = new ChoreService(getClass().getSimpleName());
093    hfileCleanerPool = DirScanPool.getHFileCleanerScanPool(htu.getConfiguration());
094    logCleanerPool = DirScanPool.getLogCleanerScanPool(htu.getConfiguration());
095    Server server = mock(Server.class);
096    when(server.getConfiguration()).thenReturn(htu.getConfiguration());
097    when(server.getServerName())
098      .thenReturn(ServerName.valueOf("localhost", 12345, EnvironmentEdgeManager.currentTime()));
099    when(server.getChoreService()).thenReturn(choreService);
100    Path testDir = htu.getDataTestDir();
101    CommonFSUtils.setRootDir(htu.getConfiguration(), testDir);
102    MasterRegionParams params = new MasterRegionParams();
103    params.server(server).regionDirName(REGION_DIR_NAME).tableDescriptor(TD)
104      .flushSize(TableDescriptorBuilder.DEFAULT_MEMSTORE_FLUSH_SIZE).flushPerChanges(1_000_000)
105      .flushIntervalMs(TimeUnit.MINUTES.toMillis(15)).compactMin(4).maxWals(32).useHsync(false)
106      .ringBufferSlotCount(16).rollPeriodMs(TimeUnit.MINUTES.toMillis(15))
107      .archivedWalSuffix(MasterRegionFactory.ARCHIVED_WAL_SUFFIX)
108      .archivedHFileSuffix(MasterRegionFactory.ARCHIVED_HFILE_SUFFIX);
109    configure(params);
110    region = MasterRegion.create(params);
111    postSetUp();
112  }
113
114  @After
115  public void tearDown() throws IOException {
116    region.close(true);
117    hfileCleanerPool.shutdownNow();
118    logCleanerPool.shutdownNow();
119    choreService.shutdown();
120    htu.cleanupTestDir();
121  }
122}