Java Code Examples for org.apache.cassandra.config.DatabaseDescriptor#getEndpointSnitch()

The following examples show how to use org.apache.cassandra.config.DatabaseDescriptor#getEndpointSnitch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InternalMetadataFactory.java    From cassandra-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<EndpointMetadata> endpointMetadata(final InetAddress endpoint) {
    final IEndpointSnitch endpointSnitch = DatabaseDescriptor.getEndpointSnitch();

    return Optional.of(new EndpointMetadata() {
        @Override
        public String dataCenter() {
            return endpointSnitch.getDatacenter(endpoint);
        }

        @Override
        public String rack() {
            return endpointSnitch.getRack(endpoint);
        }
    });
}
 
Example 2
Source File: TokenMetadata.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Stores current DC/rack assignment for ep
 */
protected void addEndpoint(InetAddress ep)
{
    IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
    String dc = snitch.getDatacenter(ep);
    String rack = snitch.getRack(ep);
    Pair<String, String> current = currentLocations.get(ep);
    if (current != null)
    {
        if (current.left.equals(dc) && current.right.equals(rack))
            return;
        dcRacks.get(current.left).remove(current.right, ep);
        dcEndpoints.remove(current.left, ep);
    }

    dcEndpoints.put(dc, ep);

    if (!dcRacks.containsKey(dc))
        dcRacks.put(dc, HashMultimap.<String, InetAddress>create());
    dcRacks.get(dc).put(rack, ep);

    currentLocations.put(ep, Pair.create(dc, rack));
}
 
Example 3
Source File: OutboundTcpConnectionPool.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static boolean isEncryptedChannel(InetAddress address)
{
    IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
    switch (DatabaseDescriptor.getServerEncryptionOptions().internode_encryption)
    {
        case none:
            return false; // if nothing needs to be encrypted then return immediately.
        case all:
            break;
        case dc:
            if (snitch.getDatacenter(address).equals(snitch.getDatacenter(FBUtilities.getBroadcastAddress())))
                return false;
            break;
        case rack:
            // for rack then check if the DC's are the same.
            if (snitch.getRack(address).equals(snitch.getRack(FBUtilities.getBroadcastAddress()))
                    && snitch.getDatacenter(address).equals(snitch.getDatacenter(FBUtilities.getBroadcastAddress())))
                return false;
            break;
    }
    return true;
}
 
Example 4
Source File: StorageService.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void gossipSnitchInfo()
{
    IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
    String dc = snitch.getDatacenter(FBUtilities.getBroadcastAddress());
    String rack = snitch.getRack(FBUtilities.getBroadcastAddress());
    Gossiper.instance.addLocalApplicationState(ApplicationState.DC, StorageService.instance.valueFactory.datacenter(dc));
    Gossiper.instance.addLocalApplicationState(ApplicationState.RACK, StorageService.instance.valueFactory.rack(rack));
}
 
Example 5
Source File: StorageService.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Finds living endpoints responsible for the given ranges
 *
 * @param keyspaceName the keyspace ranges belong to
 * @param ranges the ranges to find sources for
 * @return multimap of addresses to ranges the address is responsible for
 */
private Multimap<InetAddress, Range<Token>> getNewSourceRanges(String keyspaceName, Set<Range<Token>> ranges)
{
    InetAddress myAddress = FBUtilities.getBroadcastAddress();
    Multimap<Range<Token>, InetAddress> rangeAddresses = Keyspace.open(keyspaceName).getReplicationStrategy().getRangeAddresses(tokenMetadata.cloneOnlyTokenMap());
    Multimap<InetAddress, Range<Token>> sourceRanges = HashMultimap.create();
    IFailureDetector failureDetector = FailureDetector.instance;

    // find alive sources for our new ranges
    for (Range<Token> range : ranges)
    {
        Collection<InetAddress> possibleRanges = rangeAddresses.get(range);
        IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
        List<InetAddress> sources = snitch.getSortedListByProximity(myAddress, possibleRanges);

        assert (!sources.contains(myAddress));

        for (InetAddress source : sources)
        {
            if (failureDetector.isAlive(source))
            {
                sourceRanges.put(source, range);
                break;
            }
        }
    }
    return sourceRanges;
}
 
Example 6
Source File: StorageService.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void updateSnitch(String epSnitchClassName, Boolean dynamic, Integer dynamicUpdateInterval, Integer dynamicResetInterval, Double dynamicBadnessThreshold) throws ClassNotFoundException
{
    IEndpointSnitch oldSnitch = DatabaseDescriptor.getEndpointSnitch();

    // new snitch registers mbean during construction
    IEndpointSnitch newSnitch;
    try
    {
        newSnitch = FBUtilities.construct(epSnitchClassName, "snitch");
    }
    catch (ConfigurationException e)
    {
        throw new ClassNotFoundException(e.getMessage());
    }
    if (dynamic)
    {
        DatabaseDescriptor.setDynamicUpdateInterval(dynamicUpdateInterval);
        DatabaseDescriptor.setDynamicResetInterval(dynamicResetInterval);
        DatabaseDescriptor.setDynamicBadnessThreshold(dynamicBadnessThreshold);
        newSnitch = new DynamicEndpointSnitch(newSnitch);
    }

    // point snitch references to the new instance
    DatabaseDescriptor.setEndpointSnitch(newSnitch);
    for (String ks : Schema.instance.getKeyspaces())
    {
        Keyspace.open(ks).getReplicationStrategy().snitch = newSnitch;
    }

    if (oldSnitch instanceof DynamicEndpointSnitch)
        ((DynamicEndpointSnitch)oldSnitch).unregisterMBean();
}
 
Example 7
Source File: StorageProxy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Predicate<InetAddress> sameDCPredicateFor(final String dc)
{
    final IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
    return new Predicate<InetAddress>()
    {
        public boolean apply(InetAddress host)
        {
            return dc.equals(snitch.getDatacenter(host));
        }
    };
}
 
Example 8
Source File: StorageProxy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Find a suitable replica as leader for counter update.
 * For now, we pick a random replica in the local DC (or ask the snitch if
 * there is no replica alive in the local DC).
 * TODO: if we track the latency of the counter writes (which makes sense
 * contrarily to standard writes since there is a read involved), we could
 * trust the dynamic snitch entirely, which may be a better solution. It
 * is unclear we want to mix those latencies with read latencies, so this
 * may be a bit involved.
 */
private static InetAddress findSuitableEndpoint(String keyspaceName, ByteBuffer key, String localDataCenter, ConsistencyLevel cl) throws UnavailableException
{
    Keyspace keyspace = Keyspace.open(keyspaceName);
    IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
    List<InetAddress> endpoints = StorageService.instance.getLiveNaturalEndpoints(keyspace, key);
    if (endpoints.isEmpty())
        // TODO have a way to compute the consistency level
        throw new UnavailableException(cl, cl.blockFor(keyspace), 0);

    List<InetAddress> localEndpoints = new ArrayList<InetAddress>();
    for (InetAddress endpoint : endpoints)
    {
        if (snitch.getDatacenter(endpoint).equals(localDataCenter))
            localEndpoints.add(endpoint);
    }
    if (localEndpoints.isEmpty())
    {
        // No endpoint in local DC, pick the closest endpoint according to the snitch
        snitch.sortByProximity(FBUtilities.getBroadcastAddress(), endpoints);
        return endpoints.get(0);
    }
    else
    {
        return localEndpoints.get(ThreadLocalRandom.current().nextInt(localEndpoints.size()));
    }
}
 
Example 9
Source File: SystemKeyspace.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static void setupVersion()
{
    String req = "INSERT INTO system.%s (key, release_version, cql_version, thrift_version, native_protocol_version, data_center, rack, partitioner) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    IEndpointSnitch snitch = DatabaseDescriptor.getEndpointSnitch();
    executeOnceInternal(String.format(req, LOCAL_CF),
                        LOCAL_KEY,
                        FBUtilities.getReleaseVersionString(),
                        QueryProcessor.CQL_VERSION.toString(),
                        cassandraConstants.VERSION,
                        String.valueOf(Server.CURRENT_VERSION),
                        snitch.getDatacenter(FBUtilities.getBroadcastAddress()),
                        snitch.getRack(FBUtilities.getBroadcastAddress()),
                        DatabaseDescriptor.getPartitioner().getClass().getName());
}
 
Example 10
Source File: StreamManager.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public StreamRateLimiter(InetAddress peer)
{
    double throughput = ((double) DatabaseDescriptor.getStreamThroughputOutboundMegabitsPerSec()) * BYTES_PER_MEGABIT;
    mayUpdateThroughput(throughput, limiter);

    double interDCThroughput = ((double) DatabaseDescriptor.getInterDCStreamThroughputOutboundMegabitsPerSec()) * BYTES_PER_MEGABIT;
    mayUpdateThroughput(interDCThroughput, interDCLimiter);

    if (DatabaseDescriptor.getLocalDataCenter() != null && DatabaseDescriptor.getEndpointSnitch() != null)
        isLocalDC = DatabaseDescriptor.getLocalDataCenter().equals(
                    DatabaseDescriptor.getEndpointSnitch().getDatacenter(peer));
    else
        isLocalDC = true;
}
 
Example 11
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public String describe_snitch() throws TException
{
    if (DatabaseDescriptor.getEndpointSnitch() instanceof DynamicEndpointSnitch)
        return ((DynamicEndpointSnitch)DatabaseDescriptor.getEndpointSnitch()).subsnitch.getClass().getName();
    return DatabaseDescriptor.getEndpointSnitch().getClass().getName();
}