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.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import java.util.HashSet;
024import java.util.Set;
025import java.util.concurrent.ThreadLocalRandom;
026import java.util.stream.Stream;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.FileStatus;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
031import org.apache.hadoop.hbase.HBaseTestingUtil;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.testclassification.RegionServerTests;
036import org.apache.hadoop.hbase.util.CommonFSUtils;
037import org.apache.hadoop.hdfs.DistributedFileSystem;
038import org.junit.jupiter.api.AfterAll;
039import org.junit.jupiter.api.AfterEach;
040import org.junit.jupiter.api.BeforeAll;
041import org.junit.jupiter.api.BeforeEach;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.TestTemplate;
044import org.junit.jupiter.params.provider.Arguments;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048@Tag(RegionServerTests.TAG)
049@Tag(MediumTests.TAG)
050@HBaseParameterizedTestTemplate
051public class TestBoundedRegionGroupingStrategy {
052
053  private static final Logger LOG =
054    LoggerFactory.getLogger(TestBoundedRegionGroupingStrategy.class);
055
056  protected static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
057
058  protected static Configuration CONF;
059  protected static DistributedFileSystem FS;
060
061  public String walProvider;
062
063  public TestBoundedRegionGroupingStrategy(String walProvider) {
064    this.walProvider = walProvider;
065  }
066
067  public static Stream<Arguments> parameters() {
068    return Stream.of(Arguments.of("defaultProvider"), Arguments.of("asyncfs"));
069  }
070
071  @BeforeEach
072  public void setUp() throws Exception {
073    CONF.set(RegionGroupingProvider.DELEGATE_PROVIDER, walProvider);
074  }
075
076  @AfterEach
077  public void tearDown() throws Exception {
078    FileStatus[] entries = FS.listStatus(new Path("/"));
079    for (FileStatus dir : entries) {
080      FS.delete(dir.getPath(), true);
081    }
082  }
083
084  @BeforeAll
085  public static void setUpBeforeClass() throws Exception {
086    CONF = TEST_UTIL.getConfiguration();
087    // Make block sizes small.
088    CONF.setInt("dfs.blocksize", 1024 * 1024);
089    // quicker heartbeat interval for faster DN death notification
090    CONF.setInt("dfs.namenode.heartbeat.recheck-interval", 5000);
091    CONF.setInt("dfs.heartbeat.interval", 1);
092    CONF.setInt("dfs.client.socket-timeout", 5000);
093
094    // faster failover with cluster.shutdown();fs.close() idiom
095    CONF.setInt("hbase.ipc.client.connect.max.retries", 1);
096    CONF.setInt("dfs.client.block.recovery.retries", 1);
097    CONF.setInt("hbase.ipc.client.connection.maxidletime", 500);
098
099    CONF.setClass(WALFactory.WAL_PROVIDER, RegionGroupingProvider.class, WALProvider.class);
100    CONF.set(RegionGroupingProvider.REGION_GROUPING_STRATEGY,
101      RegionGroupingProvider.Strategies.bounded.name());
102
103    TEST_UTIL.startMiniDFSCluster(3);
104
105    FS = TEST_UTIL.getDFSCluster().getFileSystem();
106  }
107
108  @AfterAll
109  public static void tearDownAfterClass() throws Exception {
110    TEST_UTIL.shutdownMiniCluster();
111  }
112
113  /**
114   * Ensure that we can use Set.add to deduplicate WALs
115   */
116  @TestTemplate
117  public void setMembershipDedups() throws IOException {
118    final int temp = CONF.getInt(BoundedGroupingStrategy.NUM_REGION_GROUPS,
119      BoundedGroupingStrategy.DEFAULT_NUM_REGION_GROUPS);
120    WALFactory wals = null;
121    try {
122      CONF.setInt(BoundedGroupingStrategy.NUM_REGION_GROUPS, temp * 4);
123      // Set HDFS root directory for storing WAL
124      CommonFSUtils.setRootDir(CONF, TEST_UTIL.getDataTestDirOnTestFS());
125
126      wals = new WALFactory(CONF, "setMembershipDedups");
127      Set<WAL> seen = new HashSet<>(temp * 4);
128      int count = 0;
129      // we know that this should see one of the wals more than once
130      for (int i = 0; i < temp * 8; i++) {
131        WAL maybeNewWAL = wals.getWAL(RegionInfoBuilder
132          .newBuilder(TableName.valueOf("Table-" + ThreadLocalRandom.current().nextInt())).build());
133        LOG.info("Iteration " + i + ", checking wal " + maybeNewWAL);
134        if (seen.add(maybeNewWAL)) {
135          count++;
136        }
137      }
138      assertEquals(temp * 4, count,
139        "received back a different number of WALs that are not equal() to each other "
140          + "than the bound we placed.");
141    } finally {
142      if (wals != null) {
143        wals.close();
144      }
145      CONF.setInt(BoundedGroupingStrategy.NUM_REGION_GROUPS, temp);
146    }
147  }
148}