diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java index f7fddfc42f3..a5b1510ea2b 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java @@ -55,6 +55,7 @@ import org.apache.hadoop.hbase.security.access.UserPermission; import org.apache.hadoop.hbase.util.Pair; import org.apache.yetus.audience.InterfaceAudience; +import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList; import org.apache.hbase.thirdparty.com.google.protobuf.RpcChannel; /** @@ -1084,6 +1085,32 @@ public interface AsyncAdmin { .thenApply(ClusterMetrics::getServersName); } + default CompletableFuture> getRegionServers( + boolean excludeDecommissionedRS) { + CompletableFuture> future = new CompletableFuture<>(); + addListener( + getClusterMetrics(EnumSet.of(Option.SERVERS_NAME)).thenApply(ClusterMetrics::getServersName), + (allServers, err) -> { + if (err != null) { + future.completeExceptionally(err); + } else { + if (!excludeDecommissionedRS) { + future.complete(allServers); + } else { + addListener(listDecommissionedRegionServers(), (decomServers, decomErr) -> { + if (decomErr != null) { + future.completeExceptionally(decomErr); + } else { + future.complete(allServers.stream().filter(s -> !decomServers.contains(s)) + .collect(ImmutableList.toImmutableList())); + } + }); + } + } + }); + return future; + } + /** * @return a list of master coprocessors wrapped by {@link CompletableFuture} */ diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncClusterAdminApi.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncClusterAdminApi.java index 0116a6e4a36..808bc5d86d6 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncClusterAdminApi.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncClusterAdminApi.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.EnumSet; import java.util.List; import java.util.Map; @@ -262,6 +263,26 @@ public class TestAsyncClusterAdminApi extends TestAsyncAdminBase { } } + @Test + public void testGetRegionServers() throws Exception{ + List serverNames = new ArrayList<>(admin.getRegionServers(true).get()); + assertEquals(2, serverNames.size()); + + List serversToDecom = new ArrayList<>(); + ServerName serverToDecommission = serverNames.get(0); + + serversToDecom.add(serverToDecommission); + admin.decommissionRegionServers(serversToDecom, false).join(); + + assertEquals(1, admin.getRegionServers(true).get().size()); + assertEquals(2, admin.getRegionServers(false).get().size()); + + admin.recommissionRegionServer(serverToDecommission, Collections.emptyList()).join(); + + assertEquals(2, admin.getRegionServers(true).get().size()); + assertEquals(2, admin.getRegionServers(false).get().size()); + } + private void compareRegionLoads(Collection regionLoadCluster, Collection regionLoads) {