Skip to content

Commit 923195c

Browse files
Reiddddddchia7712
authored andcommitted
HBASE-15511 ClusterStatus should be able to return responses by scope
Signed-off-by: Chia-Ping Tsai <[email protected]>
1 parent 173dce7 commit 923195c

File tree

15 files changed

+710
-54
lines changed

15 files changed

+710
-54
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/ClusterStatus.java

Lines changed: 295 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@
4747
* <li>Regions in transition at master</li>
4848
* <li>The unique cluster ID</li>
4949
* </ul>
50+
* <tt>{@link Options}</tt> provides a way to filter out infos which unwanted.
51+
* The following codes will retrieve all the cluster information.
52+
* <pre>
53+
* {@code
54+
* // Original version still works
55+
* Admin admin = connection.getAdmin();
56+
* ClusterStatus status = admin.getClusterStatus();
57+
* // or below, a new version which has the same effects
58+
* ClusterStatus status = admin.getClusterStatus(Options.defaultOptions());
59+
* }
60+
* </pre>
61+
* If information about dead servers and master coprocessors are unwanted,
62+
* then codes in the following way:
63+
* <pre>
64+
* {@code
65+
* Admin admin = connection.getAdmin();
66+
* ClusterStatus status = admin.getClusterStatus(
67+
* Options.defaultOptions()
68+
* .excludeDeadServers()
69+
* .excludeMasterCoprocessors());
70+
* }
71+
* </pre>
5072
*/
5173
@InterfaceAudience.Public
5274
public class ClusterStatus extends VersionedWritable {
@@ -72,6 +94,12 @@ public class ClusterStatus extends VersionedWritable {
7294
private String[] masterCoprocessors;
7395
private Boolean balancerOn;
7496

97+
/**
98+
* Use {@link ClusterStatus.Builder} to construct a ClusterStatus instead.
99+
* @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
100+
* (<a href="https://issues.apache.org/jira/browse/HBASE-15511">HBASE-15511</a>).
101+
*/
102+
@Deprecated
75103
public ClusterStatus(final String hbaseVersion, final String clusterid,
76104
final Map<ServerName, ServerLoad> servers,
77105
final Collection<ServerName> deadServers,
@@ -80,8 +108,8 @@ public ClusterStatus(final String hbaseVersion, final String clusterid,
80108
final List<RegionState> rit,
81109
final String[] masterCoprocessors,
82110
final Boolean balancerOn) {
111+
// TODO: make this constructor private
83112
this.hbaseVersion = hbaseVersion;
84-
85113
this.liveServers = servers;
86114
this.deadServers = deadServers;
87115
this.master = master;
@@ -133,7 +161,8 @@ public int getDeadServersSize() {
133161
*/
134162
public double getAverageLoad() {
135163
int load = getRegionsCount();
136-
return (double)load / (double)getServersSize();
164+
int serverSize = getServersSize();
165+
return serverSize != 0 ? (double)load / (double)serverSize : 0.0;
137166
}
138167

139168
/**
@@ -333,4 +362,268 @@ public String toString() {
333362
}
334363
return sb.toString();
335364
}
365+
366+
public static Builder newBuilder() {
367+
return new Builder();
368+
}
369+
370+
/**
371+
* Builder for construct a ClusterStatus.
372+
*/
373+
public static class Builder {
374+
private String hbaseVersion = null;
375+
private Map<ServerName, ServerLoad> liveServers = null;
376+
private Collection<ServerName> deadServers = null;
377+
private ServerName master = null;
378+
private Collection<ServerName> backupMasters = null;
379+
private List<RegionState> intransition = null;
380+
private String clusterId = null;
381+
private String[] masterCoprocessors = null;
382+
private Boolean balancerOn = null;
383+
384+
private Builder() {}
385+
386+
public Builder setHBaseVersion(String hbaseVersion) {
387+
this.hbaseVersion = hbaseVersion;
388+
return this;
389+
}
390+
391+
public Builder setLiveServers(Map<ServerName, ServerLoad> liveServers) {
392+
this.liveServers = liveServers;
393+
return this;
394+
}
395+
396+
public Builder setDeadServers(Collection<ServerName> deadServers) {
397+
this.deadServers = deadServers;
398+
return this;
399+
}
400+
401+
public Builder setMaster(ServerName master) {
402+
this.master = master;
403+
return this;
404+
}
405+
406+
public Builder setBackupMasters(Collection<ServerName> backupMasters) {
407+
this.backupMasters = backupMasters;
408+
return this;
409+
}
410+
411+
public Builder setRegionState(List<RegionState> intransition) {
412+
this.intransition = intransition;
413+
return this;
414+
}
415+
416+
public Builder setClusterId(String clusterId) {
417+
this.clusterId = clusterId;
418+
return this;
419+
}
420+
421+
public Builder setMasterCoprocessors(String[] masterCoprocessors) {
422+
this.masterCoprocessors = masterCoprocessors;
423+
return this;
424+
}
425+
426+
public Builder setBalancerOn(Boolean balancerOn) {
427+
this.balancerOn = balancerOn;
428+
return this;
429+
}
430+
431+
public ClusterStatus build() {
432+
return new ClusterStatus(hbaseVersion, clusterId, liveServers,
433+
deadServers, master, backupMasters, intransition, masterCoprocessors,
434+
balancerOn);
435+
}
436+
}
437+
438+
/**
439+
* Options provides a way to filter out unwanted information.
440+
* For compatibility, default options includes all the information about a ClusterStatus.
441+
* To filter out unwanted information, use the specific excludeXXX() method.
442+
*/
443+
public static class Options {
444+
private boolean includeHBaseVersion = true;
445+
private boolean includeLiveServers = true;
446+
private boolean includeDeadServers = true;
447+
private boolean includeMaster = true;
448+
private boolean includeBackupMasters = true;
449+
private boolean includeRegionState = true;
450+
private boolean includeClusterId = true;
451+
private boolean includeMasterCoprocessors = true;
452+
private boolean includeBalancerOn = true;
453+
454+
private Options() {}
455+
456+
/**
457+
* Include all information about a ClusterStatus.
458+
*/
459+
public static Options getDefaultOptions() {
460+
return new Options();
461+
}
462+
463+
/**
464+
* Filter out hbase verision.
465+
*/
466+
public Options excludeHBaseVersion() {
467+
includeHBaseVersion = false;
468+
return this;
469+
}
470+
471+
/**
472+
* Filter out live servers.
473+
*/
474+
public Options excludeLiveServers() {
475+
includeLiveServers = false;
476+
return this;
477+
}
478+
479+
/**
480+
* Filter out dead servers info.
481+
*/
482+
public Options excludeDeadServers() {
483+
includeDeadServers = false;
484+
return this;
485+
}
486+
487+
/**
488+
* Filter out master info.
489+
*/
490+
public Options excludeMaster() {
491+
includeMaster = false;
492+
return this;
493+
}
494+
495+
/**
496+
* Filter out backup masters info.
497+
*/
498+
public Options excludeBackupMasters() {
499+
includeBackupMasters = false;
500+
return this;
501+
}
502+
503+
/**
504+
* Filter out region state.
505+
*/
506+
public Options excludeRegionState() {
507+
includeRegionState = false;
508+
return this;
509+
}
510+
511+
/**
512+
* Filter out cluster id.
513+
*/
514+
public Options excludeClusterId() {
515+
includeClusterId = false;
516+
return this;
517+
}
518+
519+
/**
520+
* Filter out master's coprocessors info.
521+
*/
522+
public Options excludeMasterCoprocessors() {
523+
includeMasterCoprocessors = false;
524+
return this;
525+
}
526+
527+
/**
528+
* Filter out balancer on info.
529+
*/
530+
public Options excludeBalancerOn() {
531+
includeBalancerOn = false;
532+
return this;
533+
}
534+
535+
/**
536+
* Include hbase version info.
537+
*/
538+
public boolean includeHBaseVersion() {
539+
return includeHBaseVersion;
540+
}
541+
542+
/**
543+
* Include live servers info.
544+
*/
545+
public boolean includeLiveServers() {
546+
return includeLiveServers;
547+
}
548+
549+
/**
550+
* Include dead servers info.
551+
*/
552+
public boolean includeDeadServers() {
553+
return includeDeadServers;
554+
}
555+
556+
/**
557+
* Include master info.
558+
*/
559+
public boolean includeMaster() {
560+
return includeMaster;
561+
}
562+
563+
/**
564+
* Include backup masters info.
565+
*/
566+
public boolean includeBackupMasters() {
567+
return includeBackupMasters;
568+
}
569+
570+
/**
571+
* Include region states info.
572+
*/
573+
public boolean includeRegionState() {
574+
return includeRegionState;
575+
}
576+
577+
/**
578+
* Include cluster id info.
579+
*/
580+
public boolean includeClusterId() {
581+
return includeClusterId;
582+
}
583+
584+
/**
585+
* Include master's coprocessors.
586+
*/
587+
public boolean includeMasterCoprocessors() {
588+
return includeMasterCoprocessors;
589+
}
590+
591+
/**
592+
* Include balancer on info.
593+
*/
594+
public boolean includeBalancerOn() {
595+
return includeBalancerOn;
596+
}
597+
598+
/**
599+
* For an options reusable convenience, reset options to default.
600+
*/
601+
public Options reset() {
602+
includeHBaseVersion = true;
603+
includeLiveServers = true;
604+
includeDeadServers = true;
605+
includeMaster = true;
606+
includeBackupMasters = true;
607+
includeRegionState = true;
608+
includeClusterId = true;
609+
includeMasterCoprocessors = true;
610+
includeBalancerOn = true;
611+
return this;
612+
}
613+
614+
@Override
615+
public String toString() {
616+
StringBuilder builder = new StringBuilder("ClusterStatus info: [");
617+
builder.append("include hbase version: " + includeHBaseVersion + ", ");
618+
builder.append("include cluster id: " + includeClusterId + ", ");
619+
builder.append("include master info: " + includeMaster + ", ");
620+
builder.append("include backup masters info: " + includeBackupMasters + ", ");
621+
builder.append("include live servers info: " + includeLiveServers + ", ");
622+
builder.append("include dead servers info: " + includeDeadServers + ", ");
623+
builder.append("include masters coprocessors: " + includeMasterCoprocessors + ", ");
624+
builder.append("include region state: " + includeRegionState + ", ");
625+
builder.append("include balancer on: " + includeBalancerOn + "]");
626+
return builder.toString();
627+
}
628+
}
336629
}

hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.hadoop.conf.Configuration;
3232
import org.apache.hadoop.hbase.Abortable;
3333
import org.apache.hadoop.hbase.ClusterStatus;
34+
import org.apache.hadoop.hbase.ClusterStatus.Options;
3435
import org.apache.hadoop.hbase.HColumnDescriptor;
3536
import org.apache.hadoop.hbase.HRegionInfo;
3637
import org.apache.hadoop.hbase.HTableDescriptor;
@@ -1304,6 +1305,13 @@ Future<Void> modifyTableAsync(TableDescriptor td)
13041305
*/
13051306
ClusterStatus getClusterStatus() throws IOException;
13061307

1308+
/**
1309+
* Get cluster status with options to filter out unwanted status.
1310+
* @return cluster status
1311+
* @throws IOException if a remote or network exception occurs
1312+
*/
1313+
ClusterStatus getClusterStatus(Options options) throws IOException;
1314+
13071315
/**
13081316
* Get {@link RegionLoad} of all regions hosted on a regionserver.
13091317
*

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.regex.Pattern;
2828

2929
import org.apache.hadoop.hbase.ClusterStatus;
30+
import org.apache.hadoop.hbase.ClusterStatus.Options;
3031
import org.apache.hadoop.hbase.HRegionInfo;
3132
import org.apache.hadoop.hbase.ProcedureInfo;
3233
import org.apache.hadoop.hbase.RegionLoad;
@@ -832,6 +833,11 @@ CompletableFuture<Boolean> isProcedureFinished(String signature, String instance
832833
*/
833834
CompletableFuture<ClusterStatus> getClusterStatus();
834835

836+
/**
837+
* @return cluster status wrapped by {@link CompletableFuture}
838+
*/
839+
CompletableFuture<ClusterStatus> getClusterStatus(Options options);
840+
835841
/**
836842
* @return current master server name wrapped by {@link CompletableFuture}
837843
*/

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.commons.logging.Log;
3737
import org.apache.commons.logging.LogFactory;
3838
import org.apache.hadoop.hbase.ClusterStatus;
39+
import org.apache.hadoop.hbase.ClusterStatus.Options;
3940
import org.apache.hadoop.hbase.HRegionInfo;
4041
import org.apache.hadoop.hbase.ProcedureInfo;
4142
import org.apache.hadoop.hbase.RegionLoad;
@@ -493,7 +494,12 @@ public CompletableFuture<Void> removeDrainFromRegionServers(List<ServerName> ser
493494

494495
@Override
495496
public CompletableFuture<ClusterStatus> getClusterStatus() {
496-
return wrap(rawAdmin.getClusterStatus());
497+
return getClusterStatus(Options.getDefaultOptions());
498+
}
499+
500+
@Override
501+
public CompletableFuture<ClusterStatus> getClusterStatus(Options options) {
502+
return wrap(rawAdmin.getClusterStatus(options));
497503
}
498504

499505
@Override

0 commit comments

Comments
 (0)