Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import org.apache.hadoop.hbase.util.Pair;
import org.apache.yetus.audience.InterfaceAudience;

import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;

/**
* The administrative API for HBase. Obtain an instance from {@link Connection#getAdmin()} and
* call {@link #close()} when done.
Expand Down Expand Up @@ -1064,7 +1066,28 @@ default Collection<ServerName> getBackupMasters() throws IOException {
* @throws IOException if a remote or network exception occurs
*/
default Collection<ServerName> getRegionServers() throws IOException {
return getClusterMetrics(EnumSet.of(Option.SERVERS_NAME)).getServersName();
return getRegionServers(false);
}

/**
* Retrieve all current live region servers including decommissioned
* if excludeDecommissionedRS is false, else non-decommissioned ones only
*
* @param excludeDecommissionedRS should we exclude decommissioned RS nodes
* @return all current live region servers including/excluding decommissioned hosts
* @throws IOException if a remote or network exception occurs
*/
default Collection<ServerName> getRegionServers(boolean excludeDecommissionedRS)
throws IOException {
List<ServerName> allServers =
getClusterMetrics(EnumSet.of(Option.SERVERS_NAME)).getServersName();
if (!excludeDecommissionedRS) {
return allServers;
}
List<ServerName> decommissionedRegionServers = listDecommissionedRegionServers();
return allServers.stream()
.filter(s -> !decommissionedRegionServers.contains(s))
.collect(ImmutableList.toImmutableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -864,4 +865,44 @@ public void testSlowLogResponses() throws Exception {
Assert.assertEquals(onlineLogRecords.size(), 0);
}

@Test
public void testGetRegionServers() throws Exception {
// get all live server names
List<ServerName> serverNames = new ArrayList<>(ADMIN.getRegionServers(true));
Assert.assertEquals(3, serverNames.size());

List<ServerName> serversToDecom = new ArrayList<>();
ServerName serverToDecommission = serverNames.get(0);

serversToDecom.add(serverToDecommission);
ADMIN.decommissionRegionServers(serversToDecom, false);
waitForServerCommissioned(serverToDecommission, true);

Assert.assertEquals(2, ADMIN.getRegionServers(true).size());
Assert.assertEquals(3, ADMIN.getRegionServers(false).size());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Assert.assertEquals(3, ADMIN.getRegionServers(false).size()); for paranoid reviewers (cough cough me).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, definitely

ADMIN.recommissionRegionServer(serverToDecommission, Collections.emptyList());
waitForServerCommissioned(null, false);

Assert.assertEquals(3, ADMIN.getRegionServers(true).size());
Assert.assertEquals(3, ADMIN.getRegionServers(false).size());
}

private static void waitForServerCommissioned(ServerName excludeServer,
boolean anyServerDecommissioned) {
TEST_UTIL.waitFor(3000, () -> {
try {
List<ServerName> decomServers = TEST_UTIL.getAdmin().listDecommissionedRegionServers();
if (anyServerDecommissioned) {
return decomServers.size() == 1
&& decomServers.get(0).equals(excludeServer);
} else {
return decomServers.size() == 0;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

}