HBASE-26015 Should implement getRegionServers(boolean) method in Asyn… (#3406)

Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
GeorryHuang 2021-06-27 21:58:18 +08:00 committed by Duo Zhang
parent 84ae6185b8
commit c7c5d56084
2 changed files with 51 additions and 0 deletions

View File

@ -17,6 +17,8 @@
*/
package org.apache.hadoop.hbase.client;
import static org.apache.hadoop.hbase.util.FutureUtils.addListener;
import com.google.protobuf.RpcChannel;
import java.util.Arrays;
import java.util.Collection;
@ -49,6 +51,8 @@ import org.apache.hadoop.hbase.security.access.Permission;
import org.apache.hadoop.hbase.security.access.UserPermission;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
/**
* The asynchronous administrative API for HBase.
* @since 2.0.0
@ -1058,6 +1062,32 @@ public interface AsyncAdmin {
.thenApply(ClusterMetrics::getServersName);
}
default CompletableFuture<Collection<ServerName>> getRegionServers(
boolean excludeDecommissionedRS) {
CompletableFuture<Collection<ServerName>> 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}
*/

View File

@ -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<ServerName> serverNames = new ArrayList<>(admin.getRegionServers(true).get());
assertEquals(2, serverNames.size());
List<ServerName> 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<RegionMetrics> regionLoadCluster,
Collection<RegionMetrics> regionLoads) {