Skip to content

Commit 2144690

Browse files
committed
HBASE-23125 TestRSGroupsAdmin2 is flaky
1 parent 95c9911 commit 2144690

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin2.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,34 +618,36 @@ private Pair<ServerName, RegionStateNode> randomlySetOneRegionStateToSplitting(
618618
}
619619

620620
private Pair<ServerName, RegionStateNode> randomlySetRegionState(RSGroupInfo groupInfo,
621-
RegionState.State state, TableName... tableNames) throws IOException {
621+
RegionState.State state, TableName... tableNames) throws IOException {
622622
Preconditions.checkArgument(tableNames.length == 1 || tableNames.length == 2,
623-
"only support one or two tables");
624-
Map<ServerName, List<String>> assignMap = getTableServerRegionMap().get(tableNames[0]);
625-
if(tableNames.length == 2) {
626-
Map<ServerName, List<String>> assignMap2 = getTableServerRegionMap().get(tableNames[1]);
627-
assignMap2.forEach((k ,v) -> {
628-
if(!assignMap.containsKey(k)) {
623+
"only support one or two tables");
624+
Map<TableName, Map<ServerName, List<String>>> tableServerRegionMap = getTableServerRegionMap();
625+
Map<ServerName, List<String>> assignMap = tableServerRegionMap.get(tableNames[0]);
626+
if (tableNames.length == 2) {
627+
Map<ServerName, List<String>> assignMap2 = tableServerRegionMap.get(tableNames[1]);
628+
assignMap2.forEach((k, v) -> {
629+
if (!assignMap.containsKey(k)) {
629630
assignMap.remove(k);
630631
}
631632
});
632633
}
633634
String toCorrectRegionName = null;
634635
ServerName srcServer = null;
635636
for (ServerName server : assignMap.keySet()) {
636-
toCorrectRegionName = assignMap.get(server).size() >= 1 &&
637-
!groupInfo.containsServer(server.getAddress()) ? assignMap.get(server).get(0) : null;
637+
toCorrectRegionName =
638+
assignMap.get(server).size() >= 1 && !groupInfo.containsServer(server.getAddress())
639+
? assignMap.get(server).get(0)
640+
: null;
638641
if (toCorrectRegionName != null) {
639642
srcServer = server;
640643
break;
641644
}
642645
}
643646
assert srcServer != null;
644647
RegionInfo toCorrectRegionInfo = TEST_UTIL.getMiniHBaseCluster().getMaster()
645-
.getAssignmentManager().getRegionInfo(Bytes.toBytesBinary(toCorrectRegionName));
646-
RegionStateNode rsn =
647-
TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates()
648-
.getRegionStateNode(toCorrectRegionInfo);
648+
.getAssignmentManager().getRegionInfo(Bytes.toBytesBinary(toCorrectRegionName));
649+
RegionStateNode rsn = TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager()
650+
.getRegionStates().getRegionStateNode(toCorrectRegionInfo);
649651
rsn.setState(state);
650652
return new Pair<>(srcServer, rsn);
651653
}

hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsBase.java

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertTrue;
2121

2222
import java.io.IOException;
23+
import java.util.ArrayList;
2324
import java.util.EnumSet;
2425
import java.util.HashSet;
2526
import java.util.LinkedList;
@@ -37,8 +38,6 @@
3738
import org.apache.hadoop.hbase.HConstants;
3839
import org.apache.hadoop.hbase.MiniHBaseCluster;
3940
import org.apache.hadoop.hbase.NamespaceDescriptor;
40-
import org.apache.hadoop.hbase.RegionMetrics;
41-
import org.apache.hadoop.hbase.ServerMetrics;
4241
import org.apache.hadoop.hbase.ServerName;
4342
import org.apache.hadoop.hbase.TableName;
4443
import org.apache.hadoop.hbase.Waiter;
@@ -55,7 +54,6 @@
5554
import org.apache.hadoop.hbase.master.ServerManager;
5655
import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
5756
import org.apache.hadoop.hbase.net.Address;
58-
import org.apache.hadoop.hbase.util.Bytes;
5957
import org.junit.Rule;
6058
import org.junit.rules.TestName;
6159
import org.slf4j.Logger;
@@ -185,7 +183,7 @@ public boolean evaluate() throws Exception {
185183
});
186184
}
187185

188-
public RSGroupInfo addGroup(String groupName, int serverCount)
186+
protected RSGroupInfo addGroup(String groupName, int serverCount)
189187
throws IOException, InterruptedException {
190188
RSGroupInfo defaultInfo = rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP);
191189
rsGroupAdmin.addRSGroup(groupName);
@@ -201,29 +199,29 @@ public RSGroupInfo addGroup(String groupName, int serverCount)
201199
return result;
202200
}
203201

204-
public void removeGroup(String groupName) throws IOException {
202+
protected void removeGroup(String groupName) throws IOException {
205203
RSGroupInfo groupInfo = rsGroupAdmin.getRSGroupInfo(groupName);
206204
rsGroupAdmin.moveTables(groupInfo.getTables(), RSGroupInfo.DEFAULT_GROUP);
207205
rsGroupAdmin.moveServers(groupInfo.getServers(), RSGroupInfo.DEFAULT_GROUP);
208206
rsGroupAdmin.removeRSGroup(groupName);
209207
}
210208

211-
public void deleteTableIfNecessary() throws IOException {
209+
protected void deleteTableIfNecessary() throws IOException {
212210
for (TableDescriptor desc : TEST_UTIL.getAdmin()
213211
.listTableDescriptors(Pattern.compile(tablePrefix + ".*"))) {
214212
TEST_UTIL.deleteTable(desc.getTableName());
215213
}
216214
}
217215

218-
public void deleteNamespaceIfNecessary() throws IOException {
216+
protected void deleteNamespaceIfNecessary() throws IOException {
219217
for (NamespaceDescriptor desc : TEST_UTIL.getAdmin().listNamespaceDescriptors()) {
220218
if(desc.getName().startsWith(tablePrefix)) {
221219
admin.deleteNamespace(desc.getName());
222220
}
223221
}
224222
}
225223

226-
public void deleteGroups() throws IOException {
224+
protected void deleteGroups() throws IOException {
227225
RSGroupAdmin groupAdmin = new RSGroupAdminClient(TEST_UTIL.getConnection());
228226
for(RSGroupInfo group: groupAdmin.listRSGroups()) {
229227
if(!group.getName().equals(RSGroupInfo.DEFAULT_GROUP)) {
@@ -234,7 +232,7 @@ public void deleteGroups() throws IOException {
234232
}
235233
}
236234

237-
public Map<TableName, List<String>> getTableRegionMap() throws IOException {
235+
protected Map<TableName, List<String>> getTableRegionMap() throws IOException {
238236
Map<TableName, List<String>> map = Maps.newTreeMap();
239237
Map<TableName, Map<ServerName, List<String>>> tableServerRegionMap
240238
= getTableServerRegionMap();
@@ -249,35 +247,24 @@ public Map<TableName, List<String>> getTableRegionMap() throws IOException {
249247
return map;
250248
}
251249

252-
public Map<TableName, Map<ServerName, List<String>>> getTableServerRegionMap()
253-
throws IOException {
250+
protected Map<TableName, Map<ServerName, List<String>>> getTableServerRegionMap()
251+
throws IOException {
254252
Map<TableName, Map<ServerName, List<String>>> map = Maps.newTreeMap();
255-
ClusterMetrics status = TEST_UTIL.getHBaseClusterInterface().getClusterMetrics();
256-
for (Map.Entry<ServerName, ServerMetrics> entry : status.getLiveServerMetrics().entrySet()) {
257-
ServerName serverName = entry.getKey();
258-
for(RegionMetrics rl : entry.getValue().getRegionMetrics().values()) {
259-
TableName tableName = null;
260-
try {
261-
tableName = RegionInfo.getTable(rl.getRegionName());
262-
} catch (IllegalArgumentException e) {
263-
LOG.warn("Failed parse a table name from regionname=" +
264-
Bytes.toStringBinary(rl.getRegionName()));
265-
continue;
266-
}
267-
if(!map.containsKey(tableName)) {
268-
map.put(tableName, new TreeMap<>());
269-
}
270-
if(!map.get(tableName).containsKey(serverName)) {
271-
map.get(tableName).put(serverName, new LinkedList<>());
272-
}
273-
map.get(tableName).get(serverName).add(rl.getNameAsString());
253+
Admin admin = TEST_UTIL.getAdmin();
254+
ClusterMetrics metrics =
255+
admin.getClusterMetrics(EnumSet.of(ClusterMetrics.Option.SERVERS_NAME));
256+
for (ServerName serverName : metrics.getServersName()) {
257+
for (RegionInfo region : admin.getRegions(serverName)) {
258+
TableName tableName = region.getTable();
259+
map.computeIfAbsent(tableName, k -> new TreeMap<>())
260+
.computeIfAbsent(serverName, k -> new ArrayList<>()).add(region.getRegionNameAsString());
274261
}
275262
}
276263
return map;
277264
}
278265

279266
// return the real number of region servers, excluding the master embedded region server in 2.0+
280-
public int getNumServers() throws IOException {
267+
protected int getNumServers() throws IOException {
281268
ClusterMetrics status =
282269
admin.getClusterMetrics(EnumSet.of(Option.MASTER, Option.LIVE_SERVERS));
283270
ServerName masterName = status.getMasterName();
@@ -290,7 +277,7 @@ public int getNumServers() throws IOException {
290277
return count;
291278
}
292279

293-
public String getGroupName(String baseName) {
280+
protected String getGroupName(String baseName) {
294281
return groupPrefix + "_" + baseName + "_" + rand.nextInt(Integer.MAX_VALUE);
295282
}
296283

0 commit comments

Comments
 (0)