Search in sources :

Example 1 with ThriftHostsExtractingVisitor

use of com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor in project atlasdb by palantir.

the class CassandraService method getAddressForHost.

@VisibleForTesting
InetSocketAddress getAddressForHost(String inputHost) throws UnknownHostException {
    if (config.addressTranslation().containsKey(inputHost)) {
        return config.addressTranslation().get(inputHost);
    }
    Map<String, String> hostnamesByIp = hostnameByIpSupplier.get();
    String host = hostnamesByIp.getOrDefault(inputHost, inputHost);
    InetAddress resolvedHost = InetAddress.getByName(host);
    Set<InetSocketAddress> allKnownHosts = Sets.union(currentPools.keySet(), config.servers().accept(new ThriftHostsExtractingVisitor()));
    for (InetSocketAddress address : allKnownHosts) {
        if (Objects.equals(address.getAddress(), resolvedHost)) {
            return address;
        }
    }
    Set<Integer> allKnownPorts = allKnownHosts.stream().map(InetSocketAddress::getPort).collect(Collectors.toSet());
    if (allKnownPorts.size() == 1) {
        // if everyone is on one port, try and use that
        return new InetSocketAddress(resolvedHost, Iterables.getOnlyElement(allKnownPorts));
    } else {
        throw new UnknownHostException("Couldn't find the provided host in server list or current servers");
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ThriftHostsExtractingVisitor(com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor) InetAddress(java.net.InetAddress) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ThriftHostsExtractingVisitor

use of com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor in project atlasdb by palantir.

the class CassandraClientPoolTest method setup.

@Before
public void setup() {
    config = mock(CassandraKeyValueServiceConfig.class);
    runtimeConfig = mock(CassandraKeyValueServiceRuntimeConfig.class);
    refreshableRuntimeConfig = Refreshable.only(runtimeConfig);
    when(config.poolRefreshIntervalSeconds()).thenReturn(POOL_REFRESH_INTERVAL_SECONDS);
    when(config.timeBetweenConnectionEvictionRunsSeconds()).thenReturn(TIME_BETWEEN_EVICTION_RUNS_SECONDS);
    when(runtimeConfig.unresponsiveHostBackoffTimeSeconds()).thenReturn(UNRESPONSIVE_HOST_BACKOFF_SECONDS);
    when(config.credentials()).thenReturn(mock(CassandraCredentialsConfig.class));
    when(config.getKeyspaceOrThrow()).thenReturn("ks");
    blacklist = new Blacklist(config, Refreshable.only(runtimeConfig.unresponsiveHostBackoffTimeSeconds()));
    doAnswer(invocation -> {
        Set<InetSocketAddress> inetSocketAddresses = runtimeConfig.servers().accept(new ThriftHostsExtractingVisitor());
        return inetSocketAddresses.stream().map(CassandraServer::of).collect(Collectors.toSet());
    }).when(cassandra).getCurrentServerListFromConfig();
    doAnswer(invocation -> poolServers.add(getInvocationAddress(invocation))).when(cassandra).addPool(any());
    doAnswer(invocation -> poolServers.add(getInvocationAddress(invocation))).when(cassandra).returnOrCreatePool(any(), any());
    doAnswer(invocation -> {
        poolServers.remove(getInvocationAddress(invocation));
        return mock(CassandraClientPoolingContainer.class);
    }).when(cassandra).removePool(any());
    doAnswer(invocation -> poolServers.stream().collect(Collectors.toMap(x -> x, x -> mock(CassandraClientPoolingContainer.class)))).when(cassandra).getPools();
    when(config.socketTimeoutMillis()).thenReturn(1);
}
Also used : CassandraKeyValueServiceRuntimeConfig(com.palantir.atlasdb.cassandra.CassandraKeyValueServiceRuntimeConfig) InetSocketAddress(java.net.InetSocketAddress) CassandraCredentialsConfig(com.palantir.atlasdb.cassandra.CassandraCredentialsConfig) ThriftHostsExtractingVisitor(com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor) CassandraKeyValueServiceConfig(com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfig) Before(org.junit.Before)

Example 3 with ThriftHostsExtractingVisitor

use of com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor in project atlasdb by palantir.

the class CassandraService method cacheInitialCassandraHosts.

public void cacheInitialCassandraHosts() {
    Set<InetSocketAddress> thriftSocket = config.servers().accept(new ThriftHostsExtractingVisitor());
    cassandraHosts = thriftSocket.stream().sorted(Comparator.comparing(InetSocketAddress::toString)).collect(Collectors.toList());
    cassandraHosts.forEach(this::addPool);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ThriftHostsExtractingVisitor(com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor)

Aggregations

ThriftHostsExtractingVisitor (com.palantir.atlasdb.cassandra.CassandraServersConfigs.ThriftHostsExtractingVisitor)3 InetSocketAddress (java.net.InetSocketAddress)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CassandraCredentialsConfig (com.palantir.atlasdb.cassandra.CassandraCredentialsConfig)1 CassandraKeyValueServiceConfig (com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfig)1 CassandraKeyValueServiceRuntimeConfig (com.palantir.atlasdb.cassandra.CassandraKeyValueServiceRuntimeConfig)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Before (org.junit.Before)1