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