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;
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.HBaseZKTestingUtil;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.Delete;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.ResultScanner;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.client.TableDescriptor;
037import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.master.cleaner.DirScanPool;
039import org.apache.hadoop.hbase.master.region.MasterRegion;
040import org.apache.hadoop.hbase.master.region.MasterRegionFactory;
041import org.apache.hadoop.hbase.master.region.MasterRegionParams;
042import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
043import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
044import org.apache.hadoop.hbase.util.CommonFSUtils;
045import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
046import org.junit.AfterClass;
047import org.junit.BeforeClass;
048
049public abstract class MasterStateStoreTestBase {
050
051  protected static HBaseZKTestingUtil UTIL = new HBaseZKTestingUtil();
052
053  protected static MasterRegion REGION;
054
055  protected static ChoreService CHORE_SERVICE;
056
057  protected static DirScanPool HFILE_CLEANER_POOL;
058
059  protected static DirScanPool LOG_CLEANER_POOL;
060
061  protected static TableDescriptor TD =
062    TableDescriptorBuilder.newBuilder(TableName.valueOf("test:local"))
063      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(MasterRegionFactory.STATE_FAMILY)).build();
064
065  @BeforeClass
066  public static void setUpBeforeClass() throws Exception {
067    Configuration conf = UTIL.getConfiguration();
068    conf.setBoolean(MemStoreLAB.USEMSLAB_KEY, false);
069    // Runs on local filesystem. Test does not need sync. Turn off checks.
070    conf.setBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE, false);
071    CHORE_SERVICE = new ChoreService("TestMasterStateStore");
072    HFILE_CLEANER_POOL = DirScanPool.getHFileCleanerScanPool(conf);
073    LOG_CLEANER_POOL = DirScanPool.getLogCleanerScanPool(conf);
074    MasterServices server = mock(MasterServices.class);
075    when(server.getConfiguration()).thenReturn(conf);
076    when(server.getServerName())
077      .thenReturn(ServerName.valueOf("localhost", 12345, EnvironmentEdgeManager.currentTime()));
078    when(server.getChoreService()).thenReturn(CHORE_SERVICE);
079    Path testDir = UTIL.getDataTestDir();
080    CommonFSUtils.setRootDir(conf, testDir);
081    MasterRegionParams params = new MasterRegionParams();
082    TableDescriptor td = TableDescriptorBuilder
083      .newBuilder(TD).setValue(StoreFileTrackerFactory.TRACKER_IMPL, conf
084        .get(StoreFileTrackerFactory.TRACKER_IMPL, StoreFileTrackerFactory.Trackers.DEFAULT.name()))
085      .build();
086    params.server(server).regionDirName("local").tableDescriptor(td)
087      .flushSize(TableDescriptorBuilder.DEFAULT_MEMSTORE_FLUSH_SIZE).flushPerChanges(1_000_000)
088      .flushIntervalMs(TimeUnit.MINUTES.toMillis(15)).compactMin(4).maxWals(32).useHsync(false)
089      .ringBufferSlotCount(16).rollPeriodMs(TimeUnit.MINUTES.toMillis(15))
090      .archivedWalSuffix(MasterRegionFactory.ARCHIVED_WAL_SUFFIX)
091      .archivedHFileSuffix(MasterRegionFactory.ARCHIVED_HFILE_SUFFIX);
092    REGION = MasterRegion.create(params);
093    UTIL.startMiniZKCluster();
094  }
095
096  @AfterClass
097  public static void tearDownAfterClass() throws IOException {
098    REGION.close(true);
099    HFILE_CLEANER_POOL.shutdownNow();
100    LOG_CLEANER_POOL.shutdownNow();
101    CHORE_SERVICE.shutdown();
102    UTIL.shutdownMiniZKCluster();
103    UTIL.cleanupTestDir();
104  }
105
106  protected static void cleanup() throws IOException {
107    try (ResultScanner scanner = REGION.getScanner(new Scan())) {
108      for (;;) {
109        Result result = scanner.next();
110        if (result == null) {
111          break;
112        }
113        REGION.update(r -> r.delete(new Delete(result.getRow())));
114      }
115    }
116  }
117}