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;
019
020import static org.apache.hadoop.hbase.AcidGuaranteesTestTool.FAMILIES;
021import static org.apache.hadoop.hbase.AcidGuaranteesTestTool.TABLE_NAME;
022
023import java.util.List;
024import java.util.stream.Stream;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
027import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
028import org.apache.hadoop.hbase.regionserver.CompactingMemStore;
029import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
030import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
031import org.junit.After;
032import org.junit.AfterClass;
033import org.junit.Before;
034import org.junit.BeforeClass;
035import org.junit.Test;
036
037import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
038
039/**
040 * Test case that uses multiple threads to read and write multifamily rows into a table, verifying
041 * that reads never see partially-complete writes. This can run as a junit test, or with a main()
042 * function which runs against a real cluster (eg for testing with failures, region movement, etc)
043 */
044public abstract class AcidGuaranteesTestBase {
045
046  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
047
048  private AcidGuaranteesTestTool tool = new AcidGuaranteesTestTool();
049
050  protected abstract MemoryCompactionPolicy getMemoryCompactionPolicy();
051
052  @BeforeClass
053  public static void setUpBeforeClass() throws Exception {
054    // Set small flush size for minicluster so we exercise reseeking scanners
055    Configuration conf = UTIL.getConfiguration();
056    conf.set(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, String.valueOf(128 * 1024));
057    // prevent aggressive region split
058    conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
059      ConstantSizeRegionSplitPolicy.class.getName());
060    conf.setInt("hfile.format.version", 3); // for mob tests
061    UTIL.startMiniCluster(1);
062  }
063
064  @AfterClass
065  public static void tearDownAfterClass() throws Exception {
066    UTIL.shutdownMiniCluster();
067  }
068
069  @Before
070  public void setUp() throws Exception {
071    MemoryCompactionPolicy policy = getMemoryCompactionPolicy();
072    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLE_NAME)
073        .setValue(CompactingMemStore.COMPACTING_MEMSTORE_TYPE_KEY, policy.name());
074    if (policy == MemoryCompactionPolicy.EAGER) {
075      builder.setValue(MemStoreLAB.USEMSLAB_KEY, "false");
076      builder.setValue(CompactingMemStore.IN_MEMORY_FLUSH_THRESHOLD_FACTOR_KEY, "0.9");
077    }
078    Stream.of(FAMILIES).map(ColumnFamilyDescriptorBuilder::of)
079        .forEachOrdered(builder::setColumnFamily);
080    UTIL.getAdmin().createTable(builder.build());
081    tool.setConf(UTIL.getConfiguration());
082  }
083
084  @After
085  public void tearDown() throws Exception {
086    UTIL.deleteTable(TABLE_NAME);
087  }
088
089  private void runTestAtomicity(long millisToRun, int numWriters, int numGetters, int numScanners,
090      int numUniqueRows) throws Exception {
091    runTestAtomicity(millisToRun, numWriters, numGetters, numScanners, numUniqueRows, false);
092  }
093
094  private void runTestAtomicity(long millisToRun, int numWriters, int numGetters, int numScanners,
095      int numUniqueRows, boolean useMob) throws Exception {
096    List<String> args = Lists.newArrayList("-millis", String.valueOf(millisToRun), "-numWriters",
097      String.valueOf(numWriters), "-numGetters", String.valueOf(numGetters), "-numScanners",
098      String.valueOf(numScanners), "-numUniqueRows", String.valueOf(numUniqueRows), "-crazyFlush");
099    if (useMob) {
100      args.add("-useMob");
101    }
102    tool.run(args.toArray(new String[0]));
103  }
104
105  @Test
106  public void testGetAtomicity() throws Exception {
107    runTestAtomicity(20000, 5, 5, 0, 3);
108  }
109
110  @Test
111  public void testScanAtomicity() throws Exception {
112    runTestAtomicity(20000, 5, 0, 5, 3);
113  }
114
115  @Test
116  public void testMixedAtomicity() throws Exception {
117    runTestAtomicity(20000, 5, 2, 2, 3);
118  }
119
120  @Test
121  public void testMobGetAtomicity() throws Exception {
122    runTestAtomicity(20000, 5, 5, 0, 3, true);
123  }
124
125  @Test
126  public void testMobScanAtomicity() throws Exception {
127    runTestAtomicity(20000, 5, 0, 5, 3, true);
128  }
129
130  @Test
131  public void testMobMixedAtomicity() throws Exception {
132    runTestAtomicity(20000, 5, 2, 2, 3, true);
133  }
134}