/** * Gets the topology for a Cassandra keyspace as a Multimap, where the keys identify a rack (or availability zone * in Amazon) and the values are the token ranges for each host in that rack. For example, for a well distributed * ring of 12 hosts and a replication factor of 3 this method would return a Multimap with 3 keys and each key would * contain 4 token ranges. */ private Multimap<String, TokenRange> describeCassandraTopology(final Keyspace keyspace) { try { @SuppressWarnings ("unchecked") ConnectionPool<Cassandra.Client> connectionPool = (ConnectionPool<Cassandra.Client>) keyspace.getConnectionPool(); return connectionPool.executeWithFailover( new AbstractKeyspaceOperationImpl<Multimap<String, TokenRange>>(EmptyKeyspaceTracerFactory.getInstance().newTracer(CassandraOperationType.DESCRIBE_RING), keyspace.getKeyspaceName()) { @Override protected Multimap<String, TokenRange> internalExecute(Cassandra.Client client, ConnectionContext state) throws Exception { Multimap<String, TokenRange> racks = ArrayListMultimap.create(); for (org.apache.cassandra.thrift.TokenRange tokenRange : client.describe_local_ring(getKeyspace())) { // The final local endpoint "owns" the token range, the rest are for replication EndpointDetails endpointDetails = Iterables.getLast(tokenRange.getEndpoint_details()); racks.put(endpointDetails.getRack(), new TokenRangeImpl(tokenRange.getStart_token(), tokenRange.getEnd_token(), tokenRange.getEndpoints())); } return Multimaps.unmodifiableMultimap(racks); } }, keyspace.getConfig().getRetryPolicy().duplicate()).getResult(); } catch (ConnectionException e) { throw Throwables.propagate(e); } }
/** * The TokenRange for a given keyspace. * * @param keyspace The keyspace to fetch information about * * @return a List of TokenRange(s) for the given keyspace * * @throws InvalidRequestException if there is no ring information available about keyspace */ public List<TokenRange> describeRing(String keyspace) throws InvalidRequestException { if (keyspace == null || Keyspace.open(keyspace).getReplicationStrategy() instanceof LocalStrategy) throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace); List<TokenRange> ranges = new ArrayList<TokenRange>(); Token.TokenFactory tf = getPartitioner().getTokenFactory(); for (Map.Entry<Range<Token>, List<InetAddress>> entry : getRangeToAddressMap(keyspace).entrySet()) { Range range = entry.getKey(); List<InetAddress> addresses = entry.getValue(); List<String> endpoints = new ArrayList<String>(addresses.size()); List<String> rpc_endpoints = new ArrayList<String>(addresses.size()); List<EndpointDetails> epDetails = new ArrayList<EndpointDetails>(addresses.size()); for (InetAddress endpoint : addresses) { EndpointDetails details = new EndpointDetails(); details.host = endpoint.getHostAddress(); details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint); details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint); endpoints.add(details.host); rpc_endpoints.add(getRpcaddress(endpoint)); epDetails.add(details); } TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints) .setEndpoint_details(epDetails) .setRpc_endpoints(rpc_endpoints); ranges.add(tr); } return ranges; }
/** * The TokenRange for a given keyspace. * * @param keyspace The keyspace to fetch information about * * @return a List of TokenRange(s) for the given keyspace * * @throws InvalidRequestException if there is no ring information available about keyspace */ public List<TokenRange> describeRing(String keyspace) throws InvalidRequestException { if (keyspace == null || Table.open(keyspace).getReplicationStrategy() instanceof LocalStrategy) throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace); List<TokenRange> ranges = new ArrayList<TokenRange>(); Token.TokenFactory tf = getPartitioner().getTokenFactory(); for (Map.Entry<Range<Token>, List<InetAddress>> entry : getRangeToAddressMap(keyspace).entrySet()) { Range range = entry.getKey(); List<InetAddress> addresses = entry.getValue(); List<String> endpoints = new ArrayList<String>(addresses.size()); List<String> rpc_endpoints = new ArrayList<String>(addresses.size()); List<EndpointDetails> epDetails = new ArrayList<EndpointDetails>(addresses.size()); for (InetAddress endpoint : addresses) { EndpointDetails details = new EndpointDetails(); details.host = endpoint.getHostAddress(); details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint); details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint); endpoints.add(details.host); rpc_endpoints.add(getRpcaddress(endpoint)); epDetails.add(details); } TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints) .setEndpoint_details(epDetails) .setRpc_endpoints(rpc_endpoints); ranges.add(tr); } return ranges; }
public void testDescribeRing () throws Exception { TSocket sock = new TSocket("caleb-htpc", 9160, 5000); TTransport tr = new TFramedTransport(sock); tr.open(); Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(tr)); for ( TokenRange range : client.describe_ring("Outerspacecat") ) { for ( EndpointDetails endpoint : range.getEndpoint_details() ) { System.out.println(endpoint.getHost()); } } }
private List<TokenRange> describeRing(String keyspace, boolean includeOnlyLocalDC) throws InvalidRequestException { if (!Schema.instance.getKeyspaces().contains(keyspace)) throw new InvalidRequestException("No such keyspace: " + keyspace); if (keyspace == null || Keyspace.open(keyspace).getReplicationStrategy() instanceof LocalStrategy) throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace); List<TokenRange> ranges = new ArrayList<>(); Token.TokenFactory tf = getPartitioner().getTokenFactory(); Map<Range<Token>, List<InetAddress>> rangeToAddressMap = includeOnlyLocalDC ? getRangeToAddressMapInLocalDC(keyspace) : getRangeToAddressMap(keyspace); for (Map.Entry<Range<Token>, List<InetAddress>> entry : rangeToAddressMap.entrySet()) { Range range = entry.getKey(); List<InetAddress> addresses = entry.getValue(); List<String> endpoints = new ArrayList<>(addresses.size()); List<String> rpc_endpoints = new ArrayList<>(addresses.size()); List<EndpointDetails> epDetails = new ArrayList<>(addresses.size()); for (InetAddress endpoint : addresses) { EndpointDetails details = new EndpointDetails(); details.host = endpoint.getHostAddress(); details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint); details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint); endpoints.add(details.host); rpc_endpoints.add(getRpcaddress(endpoint)); epDetails.add(details); } TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints) .setEndpoint_details(epDetails) .setRpc_endpoints(rpc_endpoints); ranges.add(tr); } return ranges; }
private List<TokenRange> describeRing(String keyspace, boolean includeOnlyLocalDC) throws InvalidRequestException { if (!Schema.instance.getKeyspaces().contains(keyspace)) throw new InvalidRequestException("No such keyspace: " + keyspace); if (keyspace == null || Keyspace.open(keyspace).getReplicationStrategy() instanceof LocalStrategy) throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace); List<TokenRange> ranges = new ArrayList<>(); Token.TokenFactory tf = getTokenFactory(); Map<Range<Token>, List<InetAddress>> rangeToAddressMap = includeOnlyLocalDC ? getRangeToAddressMapInLocalDC(keyspace) : getRangeToAddressMap(keyspace); for (Map.Entry<Range<Token>, List<InetAddress>> entry : rangeToAddressMap.entrySet()) { Range<Token> range = entry.getKey(); List<InetAddress> addresses = entry.getValue(); List<String> endpoints = new ArrayList<>(addresses.size()); List<String> rpc_endpoints = new ArrayList<>(addresses.size()); List<EndpointDetails> epDetails = new ArrayList<>(addresses.size()); for (InetAddress endpoint : addresses) { EndpointDetails details = new EndpointDetails(); details.host = endpoint.getHostAddress(); details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint); details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint); endpoints.add(details.host); rpc_endpoints.add(getRpcaddress(endpoint)); epDetails.add(details); } TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints) .setEndpoint_details(epDetails) .setRpc_endpoints(rpc_endpoints); ranges.add(tr); } return ranges; }
private List<TokenRange> describeRing(String keyspace, boolean includeOnlyLocalDC) throws InvalidRequestException { if (!Schema.instance.getKeyspaces().contains(keyspace)) throw new InvalidRequestException("No such keyspace: " + keyspace); if (keyspace == null || Keyspace.open(keyspace).getReplicationStrategy() instanceof LocalStrategy) throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace); List<TokenRange> ranges = new ArrayList<TokenRange>(); Token.TokenFactory tf = getPartitioner().getTokenFactory(); Map<Range<Token>, List<InetAddress>> rangeToAddressMap = includeOnlyLocalDC ? getRangeToAddressMapInLocalDC(keyspace) : getRangeToAddressMap(keyspace); for (Map.Entry<Range<Token>, List<InetAddress>> entry : rangeToAddressMap.entrySet()) { Range range = entry.getKey(); List<InetAddress> addresses = entry.getValue(); List<String> endpoints = new ArrayList<String>(addresses.size()); List<String> rpc_endpoints = new ArrayList<String>(addresses.size()); List<EndpointDetails> epDetails = new ArrayList<EndpointDetails>(addresses.size()); for (InetAddress endpoint : addresses) { EndpointDetails details = new EndpointDetails(); details.host = endpoint.getHostAddress(); details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint); details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint); endpoints.add(details.host); rpc_endpoints.add(getRpcaddress(endpoint)); epDetails.add(details); } TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints) .setEndpoint_details(epDetails) .setRpc_endpoints(rpc_endpoints); ranges.add(tr); } return ranges; }
private List<TokenRange> describeRing(String keyspace, boolean includeOnlyLocalDC) throws InvalidRequestException { if (keyspace == null || Table.open(keyspace).getReplicationStrategy() instanceof LocalStrategy) throw new InvalidRequestException("There is no ring for the keyspace: " + keyspace); List<TokenRange> ranges = new ArrayList<TokenRange>(); Token.TokenFactory tf = getPartitioner().getTokenFactory(); Map<Range<Token>, List<InetAddress>> rangeToAddressMap = includeOnlyLocalDC ? getRangeToAddressMapInLocalDC(keyspace) : getRangeToAddressMap(keyspace); for (Map.Entry<Range<Token>, List<InetAddress>> entry : rangeToAddressMap.entrySet()) { Range range = entry.getKey(); List<InetAddress> addresses = entry.getValue(); List<String> endpoints = new ArrayList<String>(addresses.size()); List<String> rpc_endpoints = new ArrayList<String>(addresses.size()); List<EndpointDetails> epDetails = new ArrayList<EndpointDetails>(addresses.size()); for (InetAddress endpoint : addresses) { EndpointDetails details = new EndpointDetails(); details.host = endpoint.getHostAddress(); details.datacenter = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint); details.rack = DatabaseDescriptor.getEndpointSnitch().getRack(endpoint); endpoints.add(details.host); rpc_endpoints.add(getRpcaddress(endpoint)); epDetails.add(details); } TokenRange tr = new TokenRange(tf.toString(range.left.getToken()), tf.toString(range.right.getToken()), endpoints) .setEndpoint_details(epDetails) .setRpc_endpoints(rpc_endpoints); ranges.add(tr); } return ranges; }