|
22 | 22 |
|
23 | 23 | /** |
24 | 24 | * A health {@link Diagnostic} on the availability of nodes in the cluster. It reports, globally and |
25 | | - * for each datacenter, how many nodes were found, and how many were up and how many were down. |
| 25 | + * for each datacenter or rack, how many nodes were found, and how many were up and how many were |
| 26 | + * down. |
26 | 27 | */ |
27 | 28 | public interface TopologyDiagnostic extends Diagnostic { |
28 | 29 |
|
| 30 | + /** @return the total number of nodes in this group. */ |
| 31 | + int getTotal(); |
| 32 | + |
29 | 33 | /** |
30 | | - * Returns a {@link NodeGroupDiagnostic} for the entire cluster as a whole, regardless of |
31 | | - * datacenter boundaries. |
| 34 | + * Returns the number of nodes in this group whose {@linkplain |
| 35 | + * com.datastax.oss.driver.api.core.metadata.Node#getState() state} is {@link |
| 36 | + * com.datastax.oss.driver.api.core.metadata.NodeState#UP UP}. |
| 37 | + * |
| 38 | + * @return the number of nodes in this group that are known to be up. |
32 | 39 | */ |
33 | | - @NonNull |
34 | | - NodeGroupDiagnostic getGlobalDiagnostic(); |
| 40 | + int getUp(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the number of nodes in this group whose {@linkplain |
| 44 | + * com.datastax.oss.driver.api.core.metadata.Node#getState() state} is {@link |
| 45 | + * com.datastax.oss.driver.api.core.metadata.NodeState#DOWN DOWN} or {@link |
| 46 | + * com.datastax.oss.driver.api.core.metadata.NodeState#FORCED_DOWN FORCED_DOWN}. |
| 47 | + * |
| 48 | + * @return the number of nodes in this group that are known to be down. |
| 49 | + */ |
| 50 | + int getDown(); |
35 | 51 |
|
36 | 52 | /** |
37 | | - * Returns datacenter-specific {@link NodeGroupDiagnostic} instances, keyed by datacenter name. |
| 53 | + * Returns the number of nodes in this group whose {@linkplain |
| 54 | + * com.datastax.oss.driver.api.core.metadata.Node#getState() state} is {@link |
| 55 | + * com.datastax.oss.driver.api.core.metadata.NodeState#UNKNOWN UNKNOWN}. |
| 56 | + * |
| 57 | + * <p>Nodes may be in an unknown state if the driver hasn't connected to them at all, and it has |
| 58 | + * not received any Gossip event indicating their actual state. Most of such nodes should actually |
| 59 | + * be up. |
| 60 | + * |
| 61 | + * @return the number of nodes in this group that are in unknown state. |
38 | 62 | */ |
| 63 | + int getUnknown(); |
| 64 | + |
| 65 | + /** Returns a map of local {@link TopologyDiagnostic} instances, keyed by datacenter name. */ |
39 | 66 | @NonNull |
40 | | - Map<String, NodeGroupDiagnostic> getLocalDiagnostics(); |
| 67 | + Map<String, TopologyDiagnostic> getLocalDiagnostics(); |
41 | 68 |
|
42 | 69 | /** |
43 | | - * Returns the status of the cluster topology. By default this is the same as {@code |
44 | | - * getGlobalDiagnostic().getStatus()}. See {@link NodeGroupDiagnostic#getStatus()} for details |
45 | | - * about possible statuses and their meanings. |
| 70 | + * Returns the status of the cluster topology. The status will be {@link Status#AVAILABLE} if all |
| 71 | + * nodes are up; {@link Status#UNAVAILABLE} if all nodes are down, or if the group has no node at |
| 72 | + * all. In all other cases the status will be {@link Status#PARTIALLY_AVAILABLE}. |
46 | 73 | */ |
47 | 74 | @NonNull |
48 | 75 | @Override |
49 | 76 | default Status getStatus() { |
50 | | - return getGlobalDiagnostic().getStatus(); |
| 77 | + if (getTotal() == 0 || getDown() == getTotal()) { |
| 78 | + return Status.UNAVAILABLE; |
| 79 | + } |
| 80 | + if (getDown() == 0 && getUnknown() == 0) { |
| 81 | + return Status.AVAILABLE; |
| 82 | + } |
| 83 | + return Status.PARTIALLY_AVAILABLE; |
51 | 84 | } |
52 | 85 |
|
53 | 86 | @NonNull |
54 | 87 | @Override |
55 | 88 | default Map<String, Object> getDetails() { |
56 | | - return ImmutableMap.<String, Object>builder() |
57 | | - .put("status", getStatus()) |
58 | | - .putAll(getGlobalDiagnostic().getDetails()) |
59 | | - .putAll( |
60 | | - getLocalDiagnostics().entrySet().stream() |
61 | | - .collect( |
62 | | - ImmutableMap.toImmutableMap( |
63 | | - Entry::getKey, entry -> entry.getValue().getDetails()))) |
64 | | - .build(); |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * A health {@link Diagnostic} for a group of nodes, detailing how many nodes were found in total, |
69 | | - * how many were up, and how many were down. |
70 | | - * |
71 | | - * <p>The breadth of the report's scope depends on how it was created; it may refer to the entire |
72 | | - * cluster, or to just a datacenter. |
73 | | - */ |
74 | | - interface NodeGroupDiagnostic extends Diagnostic { |
75 | | - |
76 | | - /** @return the total number of nodes in this group. */ |
77 | | - int getTotal(); |
78 | | - |
79 | | - /** |
80 | | - * Returns the number of nodes in this group whose {@linkplain |
81 | | - * com.datastax.oss.driver.api.core.metadata.Node#getState() state} is {@link |
82 | | - * com.datastax.oss.driver.api.core.metadata.NodeState#UP UP}. |
83 | | - * |
84 | | - * @return the number of nodes in this group that are known to be up. |
85 | | - */ |
86 | | - int getUp(); |
87 | | - |
88 | | - /** |
89 | | - * Returns the number of nodes in this group whose {@linkplain |
90 | | - * com.datastax.oss.driver.api.core.metadata.Node#getState() state} is {@link |
91 | | - * com.datastax.oss.driver.api.core.metadata.NodeState#DOWN DOWN} or {@link |
92 | | - * com.datastax.oss.driver.api.core.metadata.NodeState#FORCED_DOWN FORCED_DOWN}. |
93 | | - * |
94 | | - * @return the number of nodes in this group that are known to be down. |
95 | | - */ |
96 | | - int getDown(); |
97 | | - |
98 | | - /** |
99 | | - * Returns the number of nodes in this group whose {@linkplain |
100 | | - * com.datastax.oss.driver.api.core.metadata.Node#getState() state} is {@link |
101 | | - * com.datastax.oss.driver.api.core.metadata.NodeState#UNKNOWN UNKNOWN}. |
102 | | - * |
103 | | - * <p>Nodes may be in an unknown state if the driver hasn't connected to them at all, and it has |
104 | | - * not received any Gossip event indicating their actual state. Most of such nodes should |
105 | | - * actually be up. |
106 | | - * |
107 | | - * @return the number of nodes in this group that are in unknown state. |
108 | | - */ |
109 | | - int getUnknown(); |
110 | | - |
111 | | - /** |
112 | | - * Returns the status of this node group. The status will be {@link Status#AVAILABLE} if all |
113 | | - * nodes are up; {@link Status#UNAVAILABLE} if all nodes are down, or if the group has no node |
114 | | - * at all. In all other cases the status will be {@link Status#PARTIALLY_AVAILABLE}. |
115 | | - */ |
116 | | - @NonNull |
117 | | - @Override |
118 | | - default Status getStatus() { |
119 | | - if (getTotal() == 0 || getDown() == getTotal()) { |
120 | | - return Status.UNAVAILABLE; |
121 | | - } |
122 | | - if (getDown() == 0 && getUnknown() == 0) { |
123 | | - return Status.AVAILABLE; |
124 | | - } |
125 | | - return Status.PARTIALLY_AVAILABLE; |
126 | | - } |
127 | | - |
128 | | - @NonNull |
129 | | - @Override |
130 | | - default Map<String, Object> getDetails() { |
131 | | - return ImmutableMap.of( |
132 | | - "total", getTotal(), "up", getUp(), "down", getDown(), "unknown", getUnknown()); |
| 89 | + ImmutableMap.Builder<String, Object> builder = |
| 90 | + ImmutableMap.<String, Object>builder() |
| 91 | + .put("status", getStatus()) |
| 92 | + .put("total", getTotal()) |
| 93 | + .put("up", getUp()) |
| 94 | + .put("down", getDown()) |
| 95 | + .put("unknown", getUnknown()); |
| 96 | + if (!getLocalDiagnostics().isEmpty()) { |
| 97 | + builder.putAll( |
| 98 | + getLocalDiagnostics().entrySet().stream() |
| 99 | + .collect( |
| 100 | + ImmutableMap.toImmutableMap( |
| 101 | + Entry::getKey, entry -> entry.getValue().getDetails()))); |
133 | 102 | } |
| 103 | + return builder.build(); |
134 | 104 | } |
135 | 105 | } |
0 commit comments