-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
DescribeCluster.java
148 lines (130 loc) · 5.98 KB
/
DescribeCluster.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.tools.nodetool;
import java.io.PrintStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import com.google.common.collect.ArrayListMultimap;
import io.airlift.airline.Command;
import org.apache.cassandra.locator.DynamicEndpointSnitch;
import org.apache.cassandra.locator.InetAddressAndPort;
import org.apache.cassandra.tools.NodeProbe;
import org.apache.cassandra.tools.NodeTool;
import org.apache.cassandra.tools.NodeTool.NodeToolCmd;
import static java.lang.String.format;
@Command(name = "describecluster", description = "Print the name, snitch, partitioner and schema version of a cluster")
public class DescribeCluster extends NodeToolCmd
{
private boolean resolveIp = false;
private String keyspace = null;
private Collection<String> joiningNodes, leavingNodes, movingNodes, liveNodes, unreachableNodes;
@Override
public void execute(NodeProbe probe)
{
PrintStream out = probe.output().out;
// display cluster name, snitch and partitioner
out.println("Cluster Information:");
out.println("\tName: " + probe.getClusterName());
String snitch = probe.getEndpointSnitchInfoProxy().getSnitchName();
boolean dynamicSnitchEnabled = false;
if (snitch.equals(DynamicEndpointSnitch.class.getName()))
{
snitch = probe.getDynamicEndpointSnitchInfoProxy().getSubsnitchClassName();
dynamicSnitchEnabled = true;
}
out.println("\tSnitch: " + snitch);
out.println("\tDynamicEndPointSnitch: " + (dynamicSnitchEnabled ? "enabled" : "disabled"));
out.println("\tPartitioner: " + probe.getPartitioner());
// display schema version for each node
out.println("\tSchema versions:");
Map<String, List<String>> schemaVersions = printPort ? probe.getSpProxy().getSchemaVersionsWithPort() : probe.getSpProxy().getSchemaVersions();
for (String version : schemaVersions.keySet())
{
out.println(format("\t\t%s: %s%n", version, schemaVersions.get(version)));
}
// Collect status information of all nodes
boolean withPort = true;
joiningNodes = probe.getJoiningNodes(withPort);
leavingNodes = probe.getLeavingNodes(withPort);
movingNodes = probe.getMovingNodes(withPort);
liveNodes = probe.getLiveNodes(withPort);
unreachableNodes = probe.getUnreachableNodes(withPort);
// Get the list of all keyspaces
List<String> keyspaces = probe.getKeyspaces();
out.println("Stats for all nodes:");
out.println("\tLive: " + liveNodes.size());
out.println("\tJoining: " + joiningNodes.size());
out.println("\tMoving: " + movingNodes.size());
out.println("\tLeaving: " + leavingNodes.size());
out.println("\tUnreachable: " + unreachableNodes.size());
Map<String, String> tokensToEndpoints = probe.getTokenToEndpointMap(withPort);
Map<String, Float> ownerships = null;
try
{
ownerships = probe.effectiveOwnershipWithPort(keyspace);
}
catch (IllegalStateException ex)
{
ownerships = probe.getOwnershipWithPort();
out.println("Error: " + ex.getMessage());
}
catch (IllegalArgumentException ex)
{
out.println("%nError: " + ex.getMessage());
System.exit(1);
}
SortedMap<String, SetHostStatWithPort> dcs = NodeTool.getOwnershipByDcWithPort(probe, resolveIp, tokensToEndpoints, ownerships);
out.println("\nData Centers: ");
for (Map.Entry<String, SetHostStatWithPort> dc : dcs.entrySet())
{
out.print("\t" + dc.getKey());
ArrayListMultimap<InetAddressAndPort, HostStatWithPort> hostToTokens = ArrayListMultimap.create();
for (HostStatWithPort stat : dc.getValue())
hostToTokens.put(stat.endpointWithPort, stat);
int totalNodes = 0; // total number of nodes in a datacenter
int downNodes = 0; // number of down nodes in a datacenter
for (InetAddressAndPort endpoint : hostToTokens.keySet())
{
totalNodes++;
if (unreachableNodes.contains(endpoint.getHostAddressAndPort()))
downNodes++;
}
out.print(" #Nodes: " + totalNodes);
out.println(" #Down: " + downNodes);
}
// display database version for each node
out.println("\nDatabase versions:");
Map<String, List<String>> databaseVersions = probe.getGossProxy().getReleaseVersionsWithPort();
for (String version : databaseVersions.keySet())
{
out.println(format("\t%s: %s%n", version, databaseVersions.get(version)));
}
out.println("Keyspaces:");
for (String keyspaceName : keyspaces)
{
String replicationInfo = probe.getKeyspaceReplicationInfo(keyspaceName);
if (replicationInfo == null)
{
out.println("something went wrong for keyspace: " + keyspaceName);
}
out.println("\t" + keyspaceName + " -> Replication class: " + replicationInfo);
}
}
}