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