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.regionserver;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertNull;
023import static org.junit.Assert.assertTrue;
024import static org.junit.Assert.fail;
025
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.List;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtil;
032import org.apache.hadoop.hbase.StartTestingClusterOption;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.TableNameTestRule;
035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
036import org.apache.hadoop.hbase.client.TableDescriptor;
037import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.executor.ExecutorType;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.testclassification.RegionServerTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.hadoop.hbase.util.Pair;
043import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
044import org.junit.AfterClass;
045import org.junit.BeforeClass;
046import org.junit.ClassRule;
047import org.junit.Rule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050
051@Category({ RegionServerTests.class, MediumTests.class })
052public class TestRegionReplicaWaitForPrimaryFlushConf {
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestRegionReplicaWaitForPrimaryFlushConf.class);
056
057  private static final byte[] FAMILY = Bytes.toBytes("family_test");
058
059  private TableName tableName;
060
061  @Rule
062  public final TableNameTestRule name = new TableNameTestRule();
063  private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
064
065  @BeforeClass
066  public static void setUpBeforeClass() throws Exception {
067    Configuration conf = HTU.getConfiguration();
068    conf.setBoolean(ServerRegionReplicaUtil.REGION_REPLICA_REPLICATION_CONF_KEY, true);
069    conf.setBoolean(ServerRegionReplicaUtil.REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY, false);
070    HTU.startMiniCluster(StartTestingClusterOption.builder().numRegionServers(2).build());
071
072  }
073
074  @AfterClass
075  public static void tearDownAfterClass() throws Exception {
076    HTU.shutdownMiniCluster();
077  }
078
079  /**
080   * This test is for HBASE-26811,before HBASE-26811,when
081   * {@link ServerRegionReplicaUtil#REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY} is false and set
082   * {@link TableDescriptorBuilder#setRegionMemStoreReplication} to true explicitly,the secondary
083   * replica would be disabled for read after open,after HBASE-26811,the secondary replica would be
084   * enabled for read after open.
085   */
086  @Test
087  public void testSecondaryReplicaReadEnabled() throws Exception {
088    tableName = name.getTableName();
089    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
090      .setRegionReplication(2).setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
091      .setRegionMemStoreReplication(true).build();
092    HTU.getAdmin().createTable(tableDescriptor);
093
094    final ArrayList<Pair<HRegion, HRegionServer>> regionAndRegionServers =
095      new ArrayList<Pair<HRegion, HRegionServer>>(Arrays.asList(null, null));
096
097    for (int i = 0; i < 2; i++) {
098      HRegionServer rs = HTU.getMiniHBaseCluster().getRegionServer(i);
099      List<HRegion> onlineRegions = rs.getRegions(tableName);
100      for (HRegion region : onlineRegions) {
101        int replicaId = region.getRegionInfo().getReplicaId();
102        assertNull(regionAndRegionServers.get(replicaId));
103        regionAndRegionServers.set(replicaId, new Pair<HRegion, HRegionServer>(region, rs));
104      }
105    }
106    for (Pair<HRegion, HRegionServer> pair : regionAndRegionServers) {
107      assertNotNull(pair);
108    }
109
110    HRegionServer secondaryRs = regionAndRegionServers.get(1).getSecond();
111
112    try {
113      secondaryRs.getExecutorService()
114        .getExecutorThreadPool(ExecutorType.RS_REGION_REPLICA_FLUSH_OPS);
115      fail();
116    } catch (NullPointerException e) {
117      assertTrue(e != null);
118    }
119    HRegion secondaryRegion = regionAndRegionServers.get(1).getFirst();
120    assertFalse(
121      ServerRegionReplicaUtil.isRegionReplicaWaitForPrimaryFlushEnabled(secondaryRegion.conf));
122    assertTrue(secondaryRegion.isReadsEnabled());
123  }
124
125}