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