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