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.wal;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.MediumTests;
025import org.apache.hadoop.hbase.testclassification.RegionServerTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029import org.junit.runner.RunWith;
030import org.junit.runners.Parameterized;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * Tests for TestBoundedRegionGroupingStrategy which use WALPerformanceEvaluation for WAL data
036 * creation. This class was created as part of refactoring for hbase-diagnostics module creation in
037 * HBASE-28432 to break cyclic dependency.
038 */
039@RunWith(Parameterized.class)
040@Category({ RegionServerTests.class, MediumTests.class })
041public class TestBoundedRegionGroupingStrategyUsingWPETool
042  extends TestBoundedRegionGroupingStrategy {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestBoundedRegionGroupingStrategyUsingWPETool.class);
047
048  private static final Logger LOG =
049    LoggerFactory.getLogger(TestBoundedRegionGroupingStrategyUsingWPETool.class);
050
051  /**
052   * Write to a log file with three concurrent threads and verifying all data is written.
053   */
054  @Test
055  public void testConcurrentWrites() throws Exception {
056    // Run the WPE tool with three threads writing 3000 edits each concurrently.
057    // When done, verify that all edits were written.
058    int errCode = WALPerformanceEvaluation.innerMain(new Configuration(CONF),
059      new String[] { "-threads", "3", "-verify", "-noclosefs", "-iterations", "3000" });
060    assertEquals(0, errCode);
061  }
062
063  /**
064   * Make sure we can successfully run with more regions then our bound.
065   */
066  @Test
067  public void testMoreRegionsThanBound() throws Exception {
068    final String parallelism =
069      Integer.toString(BoundedGroupingStrategy.DEFAULT_NUM_REGION_GROUPS * 2);
070    int errCode =
071      WALPerformanceEvaluation.innerMain(new Configuration(CONF), new String[] { "-threads",
072        parallelism, "-verify", "-noclosefs", "-iterations", "3000", "-regions", parallelism });
073    assertEquals(0, errCode);
074  }
075
076  @Test
077  public void testBoundsGreaterThanDefault() throws Exception {
078    final int temp = CONF.getInt(BoundedGroupingStrategy.NUM_REGION_GROUPS,
079      BoundedGroupingStrategy.DEFAULT_NUM_REGION_GROUPS);
080    try {
081      CONF.setInt(BoundedGroupingStrategy.NUM_REGION_GROUPS, temp * 4);
082      final String parallelism = Integer.toString(temp * 4);
083      int errCode =
084        WALPerformanceEvaluation.innerMain(new Configuration(CONF), new String[] { "-threads",
085          parallelism, "-verify", "-noclosefs", "-iterations", "3000", "-regions", parallelism });
086      assertEquals(0, errCode);
087    } finally {
088      CONF.setInt(BoundedGroupingStrategy.NUM_REGION_GROUPS, temp);
089    }
090  }
091
092  @Test
093  public void testMoreRegionsThanBoundWithBoundsGreaterThanDefault() throws Exception {
094    final int temp = CONF.getInt(BoundedGroupingStrategy.NUM_REGION_GROUPS,
095      BoundedGroupingStrategy.DEFAULT_NUM_REGION_GROUPS);
096    try {
097      CONF.setInt(BoundedGroupingStrategy.NUM_REGION_GROUPS, temp * 4);
098      final String parallelism = Integer.toString(temp * 4 * 2);
099      int errCode =
100        WALPerformanceEvaluation.innerMain(new Configuration(CONF), new String[] { "-threads",
101          parallelism, "-verify", "-noclosefs", "-iterations", "3000", "-regions", parallelism });
102      assertEquals(0, errCode);
103    } finally {
104      CONF.setInt(BoundedGroupingStrategy.NUM_REGION_GROUPS, temp);
105    }
106  }
107}