private void tryInitialize() { cassandra.cacheInitialCassandraHosts(); refreshPoolFuture = refreshDaemon.scheduleWithFixedDelay(() -> { try { refreshPool(); } catch (Throwable t) { log.warn("Failed to refresh Cassandra KVS pool." + " Extended periods of being unable to refresh will cause perf degradation.", t); } }, config.poolRefreshIntervalSeconds(), config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); // for testability, mock/spy are bad at mockability of things called in constructors if (startupChecks == StartupChecks.RUN) { runOneTimeStartupChecks(); } refreshPool(); // ensure we've initialized before returning metrics.registerAggregateMetrics(blacklist::size); }
@Before public void setup() { config = mock(CassandraKeyValueServiceConfig.class); when(config.poolRefreshIntervalSeconds()).thenReturn(POOL_REFRESH_INTERVAL_SECONDS); when(config.timeBetweenConnectionEvictionRunsSeconds()).thenReturn(TIME_BETWEEN_EVICTION_RUNS_SECONDS); when(config.unresponsiveHostBackoffTimeSeconds()).thenReturn(UNRESPONSIVE_HOST_BACKOFF_SECONDS); blacklist = new Blacklist(config); doAnswer(invocation -> poolServers.add(getInvocationAddress(invocation))).when(cassandra).addPool(any()); doAnswer(invocation -> poolServers.remove(getInvocationAddress(invocation))).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); }
} catch (Exception e) { log.info("Couldn't grab new token ranges for token aware cassandra mapping. We will retry in {} seconds.", SafeArg.of("poolRefreshIntervalSeconds", config.poolRefreshIntervalSeconds()), e);
@Test public void hostIsAutomaticallyRemovedOnRefresh() { when(config.servers()).thenReturn(ImmutableSet.of(HOST_1, HOST_2, HOST_3)); when(config.autoRefreshNodes()).thenReturn(true); setCassandraServersTo(HOST_1, HOST_2, HOST_3); createClientPool(); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2, HOST_3); setCassandraServersTo(HOST_1, HOST_2); deterministicExecutor.tick(config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); }
@Test public void hostsAreNotRemovedOrAddedWhenRefreshIsDisabled() { when(config.servers()).thenReturn(ImmutableSet.of(HOST_1, HOST_2)); when(config.autoRefreshNodes()).thenReturn(false); setCassandraServersTo(HOST_1); createClientPool(); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); setCassandraServersTo(HOST_1, HOST_2, HOST_3); deterministicExecutor.tick(config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); }
@Test public void hostIsAutomaticallyAddedOnRefresh() { when(config.servers()).thenReturn(ImmutableSet.of(HOST_1, HOST_2)); when(config.autoRefreshNodes()).thenReturn(true); setCassandraServersTo(HOST_1, HOST_2); createClientPool(); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); setCassandraServersTo(HOST_1, HOST_2, HOST_3); deterministicExecutor.tick(config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2, HOST_3); }
@Test public void hostsAreResetToConfigOnRefreshWhenRefreshIsDisabled() { when(config.servers()).thenReturn(ImmutableSet.of(HOST_1, HOST_2)); when(config.autoRefreshNodes()).thenReturn(false); setCassandraServersTo(HOST_1); createClientPool(); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); cassandra.addPool(HOST_3); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2, HOST_3); deterministicExecutor.tick(config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); setCassandraServersTo(HOST_2, HOST_3); cassandra.removePool(HOST_1); assertThat(poolServers).containsExactlyInAnyOrder(HOST_2); deterministicExecutor.tick(config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); assertThat(poolServers).containsExactlyInAnyOrder(HOST_1, HOST_2); }
private void tryInitialize() { cassandra.cacheInitialCassandraHosts(); refreshPoolFuture = refreshDaemon.scheduleWithFixedDelay(() -> { try { refreshPool(); } catch (Throwable t) { log.warn("Failed to refresh Cassandra KVS pool." + " Extended periods of being unable to refresh will cause perf degradation.", t); } }, config.poolRefreshIntervalSeconds(), config.poolRefreshIntervalSeconds(), TimeUnit.SECONDS); // for testability, mock/spy are bad at mockability of things called in constructors if (startupChecks == StartupChecks.RUN) { runOneTimeStartupChecks(); } refreshPool(); // ensure we've initialized before returning metrics.registerAggregateMetrics(blacklist::size); }
} catch (Exception e) { log.info("Couldn't grab new token ranges for token aware cassandra mapping. We will retry in {} seconds.", SafeArg.of("poolRefreshIntervalSeconds", config.poolRefreshIntervalSeconds()), e);