HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / regionserver / TestRegionReplicasAreDistributed.java
blobe98b78f1bb49f00632ecb7ed36fb3a1cb3690285
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.regionserver;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HBaseClassTestRule;
29 import org.apache.hadoop.hbase.HBaseTestingUtil;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.client.RegionInfo;
34 import org.apache.hadoop.hbase.client.Table;
35 import org.apache.hadoop.hbase.client.TableDescriptor;
36 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
37 import org.apache.hadoop.hbase.testclassification.MediumTests;
38 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.apache.hadoop.hbase.util.RegionSplitter;
41 import org.junit.AfterClass;
42 import org.junit.BeforeClass;
43 import org.junit.ClassRule;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 @Category({ RegionServerTests.class, MediumTests.class })
50 public class TestRegionReplicasAreDistributed {
52 @ClassRule
53 public static final HBaseClassTestRule CLASS_RULE =
54 HBaseClassTestRule.forClass(TestRegionReplicasAreDistributed.class);
56 private static final Logger LOG = LoggerFactory.getLogger(TestRegionReplicasAreDistributed.class);
58 private static final int NB_SERVERS = 3;
59 private static Table table;
61 private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
62 private static final byte[] f = HConstants.CATALOG_FAMILY;
63 Map<ServerName, Collection<RegionInfo>> serverVsOnlineRegions;
64 Map<ServerName, Collection<RegionInfo>> serverVsOnlineRegions2;
65 Map<ServerName, Collection<RegionInfo>> serverVsOnlineRegions3;
66 Map<ServerName, Collection<RegionInfo>> serverVsOnlineRegions4;
68 @BeforeClass
69 public static void before() throws Exception {
70 HTU.getConfiguration().setInt("hbase.master.wait.on.regionservers.mintostart", 3);
72 HTU.startMiniCluster(NB_SERVERS);
73 Thread.sleep(3000);
74 final TableName tableName =
75 TableName.valueOf(TestRegionReplicasAreDistributed.class.getSimpleName());
77 // Create table then get the single region for our new table.
78 createTableDirectlyFromHTD(tableName);
81 private static void createTableDirectlyFromHTD(final TableName tableName) throws IOException {
82 TableDescriptor htd =
83 TableDescriptorBuilder.newBuilder(tableName).setRegionReplication(3).build();
84 // create a table with 3 replication
86 table = HTU.createTable(htd, new byte[][] { f }, getSplits(20),
87 new Configuration(HTU.getConfiguration()));
90 private static byte[][] getSplits(int numRegions) {
91 RegionSplitter.UniformSplit split = new RegionSplitter.UniformSplit();
92 split.setFirstRow(Bytes.toBytes(0L));
93 split.setLastRow(Bytes.toBytes(Long.MAX_VALUE));
94 return split.split(numRegions);
97 @AfterClass
98 public static void afterClass() throws Exception {
99 HRegionServer.TEST_SKIP_REPORTING_TRANSITION = false;
100 table.close();
101 HTU.shutdownMiniCluster();
104 private HRegionServer getRS() {
105 return HTU.getMiniHBaseCluster().getRegionServer(0);
108 private HRegionServer getSecondaryRS() {
109 return HTU.getMiniHBaseCluster().getRegionServer(1);
112 private HRegionServer getTertiaryRS() {
113 return HTU.getMiniHBaseCluster().getRegionServer(2);
116 @Test
117 public void testRegionReplicasCreatedAreDistributed() throws Exception {
118 try {
119 checkAndAssertRegionDistribution(false);
120 // now diesbale and enable the table again. It should be truly distributed
121 HTU.getAdmin().disableTable(table.getName());
122 LOG.info("Disabled the table " + table.getName());
123 LOG.info("enabling the table " + table.getName());
124 HTU.getAdmin().enableTable(table.getName());
125 LOG.info("Enabled the table " + table.getName());
126 boolean res = checkAndAssertRegionDistribution(true);
127 assertTrue("Region retainment not done ", res);
128 } finally {
129 HTU.getAdmin().disableTable(table.getName());
130 HTU.getAdmin().deleteTable(table.getName());
134 private boolean checkAndAssertRegionDistribution(boolean checkfourth) throws Exception {
135 Collection<RegionInfo> onlineRegions =
136 new ArrayList<RegionInfo>(getRS().getOnlineRegionsLocalContext().size());
137 for (HRegion region : getRS().getOnlineRegionsLocalContext()) {
138 onlineRegions.add(region.getRegionInfo());
140 if (this.serverVsOnlineRegions == null) {
141 this.serverVsOnlineRegions = new HashMap<ServerName, Collection<RegionInfo>>();
142 this.serverVsOnlineRegions.put(getRS().getServerName(), onlineRegions);
143 } else {
144 Collection<RegionInfo> existingRegions =
145 new ArrayList<RegionInfo>(this.serverVsOnlineRegions.get(getRS().getServerName()));
146 LOG.info("Count is " + existingRegions.size() + " " + onlineRegions.size());
147 for (RegionInfo existingRegion : existingRegions) {
148 if (!onlineRegions.contains(existingRegion)) {
149 return false;
153 Collection<RegionInfo> onlineRegions2 =
154 new ArrayList<RegionInfo>(getSecondaryRS().getOnlineRegionsLocalContext().size());
155 for (HRegion region : getSecondaryRS().getOnlineRegionsLocalContext()) {
156 onlineRegions2.add(region.getRegionInfo());
158 if (this.serverVsOnlineRegions2 == null) {
159 this.serverVsOnlineRegions2 = new HashMap<ServerName, Collection<RegionInfo>>();
160 this.serverVsOnlineRegions2.put(getSecondaryRS().getServerName(), onlineRegions2);
161 } else {
162 Collection<RegionInfo> existingRegions = new ArrayList<RegionInfo>(
163 this.serverVsOnlineRegions2.get(getSecondaryRS().getServerName()));
164 LOG.info("Count is " + existingRegions.size() + " " + onlineRegions2.size());
165 for (RegionInfo existingRegion : existingRegions) {
166 if (!onlineRegions2.contains(existingRegion)) {
167 return false;
171 Collection<RegionInfo> onlineRegions3 =
172 new ArrayList<RegionInfo>(getTertiaryRS().getOnlineRegionsLocalContext().size());
173 for (HRegion region : getTertiaryRS().getOnlineRegionsLocalContext()) {
174 onlineRegions3.add(region.getRegionInfo());
176 if (this.serverVsOnlineRegions3 == null) {
177 this.serverVsOnlineRegions3 = new HashMap<ServerName, Collection<RegionInfo>>();
178 this.serverVsOnlineRegions3.put(getTertiaryRS().getServerName(), onlineRegions3);
179 } else {
180 Collection<RegionInfo> existingRegions = new ArrayList<RegionInfo>(
181 this.serverVsOnlineRegions3.get(getTertiaryRS().getServerName()));
182 LOG.info("Count is " + existingRegions.size() + " " + onlineRegions3.size());
183 for (RegionInfo existingRegion : existingRegions) {
184 if (!onlineRegions3.contains(existingRegion)) {
185 return false;
189 // META and namespace to be added
190 return true;