|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.regionserver; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertNull; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | +import static org.junit.Assert.fail; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 31 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 32 | +import org.apache.hadoop.hbase.StartTestingClusterOption; |
| 33 | +import org.apache.hadoop.hbase.TableName; |
| 34 | +import org.apache.hadoop.hbase.TableNameTestRule; |
| 35 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; |
| 36 | +import org.apache.hadoop.hbase.client.TableDescriptor; |
| 37 | +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; |
| 38 | +import org.apache.hadoop.hbase.executor.ExecutorType; |
| 39 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 40 | +import org.apache.hadoop.hbase.testclassification.RegionServerTests; |
| 41 | +import org.apache.hadoop.hbase.util.Bytes; |
| 42 | +import org.apache.hadoop.hbase.util.Pair; |
| 43 | +import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil; |
| 44 | +import org.junit.AfterClass; |
| 45 | +import org.junit.BeforeClass; |
| 46 | +import org.junit.ClassRule; |
| 47 | +import org.junit.Rule; |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.experimental.categories.Category; |
| 50 | + |
| 51 | +@Category({ RegionServerTests.class, MediumTests.class }) |
| 52 | +public class TestRegionReplicaWaitForPrimaryFlushConf { |
| 53 | + @ClassRule |
| 54 | + public static final HBaseClassTestRule CLASS_RULE = |
| 55 | + HBaseClassTestRule.forClass(TestRegionReplicaWaitForPrimaryFlushConf.class); |
| 56 | + |
| 57 | + private static final byte[] FAMILY = Bytes.toBytes("family_test"); |
| 58 | + |
| 59 | + private TableName tableName; |
| 60 | + |
| 61 | + @Rule |
| 62 | + public final TableNameTestRule name = new TableNameTestRule(); |
| 63 | + private static final HBaseTestingUtil HTU = new HBaseTestingUtil(); |
| 64 | + |
| 65 | + @BeforeClass |
| 66 | + public static void setUpBeforeClass() throws Exception { |
| 67 | + Configuration conf = HTU.getConfiguration(); |
| 68 | + conf.setBoolean(ServerRegionReplicaUtil.REGION_REPLICA_REPLICATION_CONF_KEY, true); |
| 69 | + conf.setBoolean(ServerRegionReplicaUtil.REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY, false); |
| 70 | + HTU.startMiniCluster(StartTestingClusterOption.builder().numRegionServers(2).build()); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + @AfterClass |
| 75 | + public static void tearDownAfterClass() throws Exception { |
| 76 | + HTU.shutdownMiniCluster(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This test is for HBASE-26811,when |
| 81 | + * {@link ServerRegionReplicaUtil#REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY} is false and set |
| 82 | + * {@link TableDescriptorBuilder#setRegionMemStoreReplication} to true explicitly,the secondary |
| 83 | + * replica would be disabled for read after open. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + public void testSecondaryReplicaReadEnabled() throws Exception { |
| 87 | + tableName = name.getTableName(); |
| 88 | + TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) |
| 89 | + .setRegionReplication(2).setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)) |
| 90 | + .setRegionMemStoreReplication(true).build(); |
| 91 | + HTU.getAdmin().createTable(tableDescriptor); |
| 92 | + |
| 93 | + final ArrayList<Pair<HRegion, HRegionServer>> regionAndRegionServers = |
| 94 | + new ArrayList<Pair<HRegion, HRegionServer>>(Arrays.asList(null, null)); |
| 95 | + |
| 96 | + for (int i = 0; i < 2; i++) { |
| 97 | + HRegionServer rs = HTU.getMiniHBaseCluster().getRegionServer(i); |
| 98 | + List<HRegion> onlineRegions = rs.getRegions(tableName); |
| 99 | + for (HRegion region : onlineRegions) { |
| 100 | + int replicaId = region.getRegionInfo().getReplicaId(); |
| 101 | + assertNull(regionAndRegionServers.get(replicaId)); |
| 102 | + regionAndRegionServers.set(replicaId, new Pair<HRegion, HRegionServer>(region, rs)); |
| 103 | + } |
| 104 | + } |
| 105 | + for (Pair<HRegion, HRegionServer> pair : regionAndRegionServers) { |
| 106 | + assertNotNull(pair); |
| 107 | + } |
| 108 | + |
| 109 | + HRegionServer secondaryRs = regionAndRegionServers.get(1).getSecond(); |
| 110 | + |
| 111 | + try { |
| 112 | + secondaryRs.getExecutorService() |
| 113 | + .getExecutorThreadPool(ExecutorType.RS_REGION_REPLICA_FLUSH_OPS); |
| 114 | + fail(); |
| 115 | + } catch (NullPointerException e) { |
| 116 | + assertTrue(e != null); |
| 117 | + } |
| 118 | + HRegion secondaryRegion = regionAndRegionServers.get(1).getFirst(); |
| 119 | + assertFalse( |
| 120 | + ServerRegionReplicaUtil.isRegionReplicaWaitForPrimaryFlushEnabled(secondaryRegion.conf)); |
| 121 | + assertTrue(secondaryRegion.isReadsEnabled()); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments