org.apache.cassandra.tools.nodetool.Status Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-all Show documentation
Show all versions of cassandra-all Show documentation
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
/*
* 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 io.airlift.command.Arguments;
import io.airlift.command.Command;
import io.airlift.command.Option;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import org.apache.cassandra.locator.EndpointSnitchInfoMBean;
import org.apache.cassandra.tools.NodeProbe;
import org.apache.cassandra.tools.NodeTool;
import org.apache.cassandra.tools.NodeTool.NodeToolCmd;
import com.google.common.collect.ArrayListMultimap;
@Command(name = "status", description = "Print cluster information (state, load, IDs, ...)")
public class Status extends NodeToolCmd
{
@Arguments(usage = "[]", description = "The keyspace name")
private String keyspace = null;
@Option(title = "resolve_ip", name = {"-r", "--resolve-ip"}, description = "Show node domain names instead of IPs")
private boolean resolveIp = false;
private boolean isTokenPerNode = true;
private String format = null;
private Collection joiningNodes, leavingNodes, movingNodes, liveNodes, unreachableNodes;
private Map loadMap, hostIDMap;
private EndpointSnitchInfoMBean epSnitchInfo;
@Override
public void execute(NodeProbe probe)
{
PrintStream out = probe.output().out;
joiningNodes = probe.getJoiningNodes();
leavingNodes = probe.getLeavingNodes();
movingNodes = probe.getMovingNodes();
loadMap = probe.getLoadMap();
Map tokensToEndpoints = probe.getTokenToEndpointMap();
liveNodes = probe.getLiveNodes();
unreachableNodes = probe.getUnreachableNodes();
hostIDMap = probe.getHostIdMap();
epSnitchInfo = probe.getEndpointSnitchInfoProxy();
StringBuffer errors = new StringBuffer();
Map ownerships = null;
boolean hasEffectiveOwns = false;
try
{
ownerships = probe.effectiveOwnership(keyspace);
hasEffectiveOwns = true;
}
catch (IllegalStateException e)
{
ownerships = probe.getOwnership();
errors.append("Note: " + e.getMessage() + "%n");
}
catch (IllegalArgumentException ex)
{
out.printf("%nError: " + ex.getMessage() + "%n");
System.exit(1);
}
SortedMap dcs = NodeTool.getOwnershipByDc(probe, resolveIp, tokensToEndpoints, ownerships);
// More tokens than nodes (aka vnodes)?
if (dcs.values().size() < tokensToEndpoints.keySet().size())
isTokenPerNode = false;
int maxAddressLength = computeMaxAddressLength(dcs);
// Datacenters
for (Map.Entry dc : dcs.entrySet())
{
String dcHeader = String.format("Datacenter: %s%n", dc.getKey());
out.print(dcHeader);
for (int i = 0; i < (dcHeader.length() - 1); i++) out.print('=');
out.println();
// Legend
out.println("Status=Up/Down");
out.println("|/ State=Normal/Leaving/Joining/Moving");
printNodesHeader(hasEffectiveOwns, isTokenPerNode, maxAddressLength, out);
ArrayListMultimap hostToTokens = ArrayListMultimap.create();
for (HostStat stat : dc.getValue())
hostToTokens.put(stat.endpoint, stat);
for (InetAddress endpoint : hostToTokens.keySet())
{
Float owns = ownerships.get(endpoint);
List tokens = hostToTokens.get(endpoint);
printNode(endpoint.getHostAddress(), owns, tokens, hasEffectiveOwns, isTokenPerNode, maxAddressLength, out);
}
}
out.printf("%n" + errors);
}
private int computeMaxAddressLength(Map dcs)
{
int maxAddressLength = 0;
Set seenHosts = new HashSet<>();
for (SetHostStat stats : dcs.values())
for (HostStat stat : stats)
if (seenHosts.add(stat.endpoint))
maxAddressLength = Math.max(maxAddressLength, stat.ipOrDns().length());
return maxAddressLength;
}
private void printNodesHeader(boolean hasEffectiveOwns, boolean isTokenPerNode, int maxAddressLength, PrintStream out)
{
String fmt = getFormat(hasEffectiveOwns, isTokenPerNode, maxAddressLength);
String owns = hasEffectiveOwns ? "Owns (effective)" : "Owns";
if (isTokenPerNode)
out.printf(fmt, "-", "-", "Address", "Load", owns, "Host ID", "Token", "Rack");
else
out.printf(fmt, "-", "-", "Address", "Load", "Tokens", owns, "Host ID", "Rack");
}
private void printNode(String endpoint, Float owns, List tokens, boolean hasEffectiveOwns,
boolean isTokenPerNode, int maxAddressLength, PrintStream out)
{
String status, state, load, strOwns, hostID, rack, fmt;
fmt = getFormat(hasEffectiveOwns, isTokenPerNode, maxAddressLength);
if (liveNodes.contains(endpoint)) status = "U";
else if (unreachableNodes.contains(endpoint)) status = "D";
else status = "?";
if (joiningNodes.contains(endpoint)) state = "J";
else if (leavingNodes.contains(endpoint)) state = "L";
else if (movingNodes.contains(endpoint)) state = "M";
else state = "N";
load = loadMap.containsKey(endpoint) ? loadMap.get(endpoint) : "?";
strOwns = owns != null && hasEffectiveOwns ? new DecimalFormat("##0.0%").format(owns) : "?";
hostID = hostIDMap.get(endpoint);
try
{
rack = epSnitchInfo.getRack(endpoint);
} catch (UnknownHostException e)
{
throw new RuntimeException(e);
}
String endpointDns = tokens.get(0).ipOrDns();
if (isTokenPerNode)
out.printf(fmt, status, state, endpointDns, load, strOwns, hostID, tokens.get(0).token, rack);
else
out.printf(fmt, status, state, endpointDns, load, tokens.size(), strOwns, hostID, rack);
}
private String getFormat(boolean hasEffectiveOwns, boolean isTokenPerNode, int maxAddressLength)
{
if (format == null)
{
StringBuilder buf = new StringBuilder();
String addressPlaceholder = String.format("%%-%ds ", maxAddressLength);
buf.append("%s%s "); // status
buf.append(addressPlaceholder); // address
buf.append("%-9s "); // load
if (!isTokenPerNode)
buf.append("%-11s "); // "Tokens"
if (hasEffectiveOwns)
buf.append("%-16s "); // "Owns (effective)"
else
buf.append("%-6s "); // "Owns
buf.append("%-36s "); // Host ID
if (isTokenPerNode)
buf.append("%-39s "); // token
buf.append("%s%n"); // "Rack"
format = buf.toString();
}
return format;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy