HBASE-21938 Add a new ClusterMetrics.Option SERVERS_NAME to only return the live region servers's name without metrics
Signed-off-by: Guanghao Zhang <zghao@apache.org>
This commit is contained in:
parent
02690b429f
commit
dfb95cfd83
|
@ -139,6 +139,8 @@ public interface ClusterMetrics {
|
|||
|
||||
int getMasterInfoPort();
|
||||
|
||||
List<ServerName> getServersName();
|
||||
|
||||
/**
|
||||
* @return the average cluster load
|
||||
*/
|
||||
|
@ -193,6 +195,10 @@ public interface ClusterMetrics {
|
|||
/**
|
||||
* metrics info port
|
||||
*/
|
||||
MASTER_INFO_PORT
|
||||
MASTER_INFO_PORT,
|
||||
/**
|
||||
* metrics about live region servers name
|
||||
*/
|
||||
SERVERS_NAME
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
|
|||
import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
|
||||
import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
|
||||
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
|
||||
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos.Option;
|
||||
import org.apache.hadoop.hbase.shaded.protobuf.generated.FSProtos;
|
||||
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
|
||||
|
||||
|
@ -67,7 +68,9 @@ public final class ClusterMetricsBuilder {
|
|||
.setRegionState(r.convert())
|
||||
.build())
|
||||
.collect(Collectors.toList()))
|
||||
.setMasterInfoPort(metrics.getMasterInfoPort());
|
||||
.setMasterInfoPort(metrics.getMasterInfoPort())
|
||||
.addAllServersName(metrics.getServersName().stream().map(ProtobufUtil::toServerName)
|
||||
.collect(Collectors.toList()));
|
||||
if (metrics.getMasterName() != null) {
|
||||
builder.setMaster(ProtobufUtil.toServerName((metrics.getMasterName())));
|
||||
}
|
||||
|
@ -103,6 +106,8 @@ public final class ClusterMetricsBuilder {
|
|||
.collect(Collectors.toList()))
|
||||
.setMasterCoprocessorNames(proto.getMasterCoprocessorsList().stream()
|
||||
.map(HBaseProtos.Coprocessor::getName)
|
||||
.collect(Collectors.toList()))
|
||||
.setServerNames(proto.getServersNameList().stream().map(ProtobufUtil::toServerName)
|
||||
.collect(Collectors.toList()));
|
||||
if (proto.hasClusterId()) {
|
||||
builder.setClusterId(ClusterId.convert(proto.getClusterId()).toString());
|
||||
|
@ -142,6 +147,7 @@ public final class ClusterMetricsBuilder {
|
|||
case MASTER: return ClusterMetrics.Option.MASTER;
|
||||
case BACKUP_MASTERS: return ClusterMetrics.Option.BACKUP_MASTERS;
|
||||
case BALANCER_ON: return ClusterMetrics.Option.BALANCER_ON;
|
||||
case SERVERS_NAME: return ClusterMetrics.Option.SERVERS_NAME;
|
||||
case MASTER_INFO_PORT: return ClusterMetrics.Option.MASTER_INFO_PORT;
|
||||
// should not reach here
|
||||
default: throw new IllegalArgumentException("Invalid option: " + option);
|
||||
|
@ -164,6 +170,7 @@ public final class ClusterMetricsBuilder {
|
|||
case MASTER: return ClusterStatusProtos.Option.MASTER;
|
||||
case BACKUP_MASTERS: return ClusterStatusProtos.Option.BACKUP_MASTERS;
|
||||
case BALANCER_ON: return ClusterStatusProtos.Option.BALANCER_ON;
|
||||
case SERVERS_NAME: return Option.SERVERS_NAME;
|
||||
case MASTER_INFO_PORT: return ClusterStatusProtos.Option.MASTER_INFO_PORT;
|
||||
// should not reach here
|
||||
default: throw new IllegalArgumentException("Invalid option: " + option);
|
||||
|
@ -206,6 +213,7 @@ public final class ClusterMetricsBuilder {
|
|||
@Nullable
|
||||
private Boolean balancerOn;
|
||||
private int masterInfoPort;
|
||||
private List<ServerName> serversName = Collections.emptyList();
|
||||
|
||||
private ClusterMetricsBuilder() {
|
||||
}
|
||||
|
@ -251,6 +259,10 @@ public final class ClusterMetricsBuilder {
|
|||
this.masterInfoPort = value;
|
||||
return this;
|
||||
}
|
||||
public ClusterMetricsBuilder setServerNames(List<ServerName> serversName) {
|
||||
this.serversName = serversName;
|
||||
return this;
|
||||
}
|
||||
public ClusterMetrics build() {
|
||||
return new ClusterMetricsImpl(
|
||||
hbaseVersion,
|
||||
|
@ -262,7 +274,8 @@ public final class ClusterMetricsBuilder {
|
|||
clusterId,
|
||||
masterCoprocessorNames,
|
||||
balancerOn,
|
||||
masterInfoPort);
|
||||
masterInfoPort,
|
||||
serversName);
|
||||
}
|
||||
private static class ClusterMetricsImpl implements ClusterMetrics {
|
||||
@Nullable
|
||||
|
@ -279,6 +292,7 @@ public final class ClusterMetricsBuilder {
|
|||
@Nullable
|
||||
private final Boolean balancerOn;
|
||||
private final int masterInfoPort;
|
||||
private final List<ServerName> serversName;
|
||||
|
||||
ClusterMetricsImpl(String hbaseVersion, List<ServerName> deadServerNames,
|
||||
Map<ServerName, ServerMetrics> liveServerMetrics,
|
||||
|
@ -288,7 +302,8 @@ public final class ClusterMetricsBuilder {
|
|||
String clusterId,
|
||||
List<String> masterCoprocessorNames,
|
||||
Boolean balancerOn,
|
||||
int masterInfoPort) {
|
||||
int masterInfoPort,
|
||||
List<ServerName> serversName) {
|
||||
this.hbaseVersion = hbaseVersion;
|
||||
this.deadServerNames = Preconditions.checkNotNull(deadServerNames);
|
||||
this.liveServerMetrics = Preconditions.checkNotNull(liveServerMetrics);
|
||||
|
@ -299,6 +314,7 @@ public final class ClusterMetricsBuilder {
|
|||
this.masterCoprocessorNames = Preconditions.checkNotNull(masterCoprocessorNames);
|
||||
this.balancerOn = balancerOn;
|
||||
this.masterInfoPort = masterInfoPort;
|
||||
this.serversName = serversName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -351,6 +367,11 @@ public final class ClusterMetricsBuilder {
|
|||
return masterInfoPort;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServerName> getServersName() {
|
||||
return Collections.unmodifiableList(serversName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(1024);
|
||||
|
@ -365,11 +386,17 @@ public final class ClusterMetricsBuilder {
|
|||
}
|
||||
|
||||
int serversSize = getLiveServerMetrics().size();
|
||||
sb.append("\nNumber of live region servers: " + serversSize);
|
||||
int serversNameSize = getServersName().size();
|
||||
sb.append("\nNumber of live region servers: "
|
||||
+ (serversSize > 0 ? serversSize : serversNameSize));
|
||||
if (serversSize > 0) {
|
||||
for (ServerName serverName : getLiveServerMetrics().keySet()) {
|
||||
sb.append("\n " + serverName.getServerName());
|
||||
}
|
||||
} else if (serversNameSize > 0) {
|
||||
for (ServerName serverName : getServersName()) {
|
||||
sb.append("\n " + serverName.getServerName());
|
||||
}
|
||||
}
|
||||
|
||||
int deadServerSize = getDeadServerNames().size();
|
||||
|
|
|
@ -344,6 +344,11 @@ public class ClusterStatus implements ClusterMetrics {
|
|||
return metrics.getMasterInfoPort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServerName> getServersName() {
|
||||
return metrics.getServersName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(1024);
|
||||
|
@ -358,11 +363,17 @@ public class ClusterStatus implements ClusterMetrics {
|
|||
}
|
||||
|
||||
int serversSize = getServersSize();
|
||||
sb.append("\nNumber of live region servers: " + serversSize);
|
||||
int serversNameSize = getServersName().size();
|
||||
sb.append("\nNumber of live region servers: "
|
||||
+ (serversSize > 0 ? serversSize : serversNameSize));
|
||||
if (serversSize > 0) {
|
||||
for (ServerName serverName : metrics.getLiveServerMetrics().keySet()) {
|
||||
sb.append("\n " + serverName.getServerName());
|
||||
}
|
||||
} else if (serversNameSize > 0) {
|
||||
for (ServerName serverName : getServersName()) {
|
||||
sb.append("\n " + serverName.getServerName());
|
||||
}
|
||||
}
|
||||
|
||||
int deadServerSize = metrics.getDeadServerNames().size();
|
||||
|
|
|
@ -232,6 +232,7 @@ message ClusterStatus {
|
|||
repeated ServerName backup_masters = 8;
|
||||
optional bool balancer_on = 9;
|
||||
optional int32 master_info_port = 10 [default = -1];
|
||||
repeated ServerName servers_name = 11;
|
||||
}
|
||||
|
||||
enum Option {
|
||||
|
@ -245,4 +246,5 @@ enum Option {
|
|||
REGIONS_IN_TRANSITION = 7;
|
||||
BALANCER_ON = 8;
|
||||
MASTER_INFO_PORT = 9;
|
||||
SERVERS_NAME = 10;
|
||||
}
|
||||
|
|
|
@ -2801,6 +2801,12 @@ public class HMaster extends HRegionServer implements MasterServices {
|
|||
}
|
||||
break;
|
||||
}
|
||||
case SERVERS_NAME: {
|
||||
if (serverManager != null) {
|
||||
builder.setServerNames(serverManager.getOnlineServersList());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
|
|
|
@ -96,6 +96,7 @@ public class TestClientClusterMetrics {
|
|||
Assert.assertEquals(origin.getLiveServerMetrics().size(),
|
||||
defaults.getLiveServerMetrics().size());
|
||||
Assert.assertEquals(origin.getMasterInfoPort(), defaults.getMasterInfoPort());
|
||||
Assert.assertEquals(origin.getServersName().size(), defaults.getServersName().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -121,6 +122,7 @@ public class TestClientClusterMetrics {
|
|||
Assert.assertEquals(origin.getLiveServerMetrics().size(),
|
||||
defaults.getLiveServerMetrics().size());
|
||||
Assert.assertEquals(origin.getMasterInfoPort(), defaults.getMasterInfoPort());
|
||||
Assert.assertEquals(origin.getServersName().size(), defaults.getServersName().size());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,7 +148,8 @@ public class TestClientClusterMetrics {
|
|||
}
|
||||
});
|
||||
// Retrieve live servers and dead servers info.
|
||||
EnumSet<Option> options = EnumSet.of(Option.LIVE_SERVERS, Option.DEAD_SERVERS);
|
||||
EnumSet<Option> options =
|
||||
EnumSet.of(Option.LIVE_SERVERS, Option.DEAD_SERVERS, Option.SERVERS_NAME);
|
||||
ClusterMetrics metrics = ADMIN.getClusterMetrics(options);
|
||||
Assert.assertNotNull(metrics);
|
||||
// exclude a dead region server
|
||||
|
@ -160,6 +163,8 @@ public class TestClientClusterMetrics {
|
|||
Assert.assertEquals(1, metrics.getDeadServerNames().size());
|
||||
ServerName deadServerName = metrics.getDeadServerNames().iterator().next();
|
||||
Assert.assertEquals(DEAD.getServerName(), deadServerName);
|
||||
Assert.assertNotNull(metrics.getServersName());
|
||||
Assert.assertEquals(numRs, metrics.getServersName().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -37,7 +37,6 @@ import org.apache.hadoop.hbase.regionserver.HRegionServer;
|
|||
import org.apache.hadoop.hbase.testclassification.MediumTests;
|
||||
import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
|
||||
import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
|
||||
import org.apache.hadoop.hbase.util.Threads;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -99,6 +98,7 @@ public class TestClientClusterStatus {
|
|||
Assert.assertTrue(origin.getServersSize() == defaults.getServersSize());
|
||||
Assert.assertTrue(origin.getMasterInfoPort() == defaults.getMasterInfoPort());
|
||||
Assert.assertTrue(origin.equals(defaults));
|
||||
Assert.assertTrue(origin.getServersName().size() == defaults.getServersName().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -136,7 +136,8 @@ public class TestClientClusterStatus {
|
|||
}
|
||||
});
|
||||
// Retrieve live servers and dead servers info.
|
||||
EnumSet<Option> options = EnumSet.of(Option.LIVE_SERVERS, Option.DEAD_SERVERS);
|
||||
EnumSet<Option> options =
|
||||
EnumSet.of(Option.LIVE_SERVERS, Option.DEAD_SERVERS, Option.SERVERS_NAME);
|
||||
ClusterStatus status = new ClusterStatus(ADMIN.getClusterMetrics(options));
|
||||
checkPbObjectNotNull(status);
|
||||
Assert.assertNotNull(status);
|
||||
|
@ -152,6 +153,8 @@ public class TestClientClusterStatus {
|
|||
Assert.assertEquals(1, status.getDeadServersSize());
|
||||
ServerName deadServerName = status.getDeadServerNames().iterator().next();
|
||||
Assert.assertEquals(DEAD.getServerName(), deadServerName);
|
||||
Assert.assertNotNull(status.getServersName());
|
||||
Assert.assertEquals(numRs, status.getServersName().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue