private List<TokenRange> getTokenRanges() throws Exception { return getRandomGoodHost().runWithPooledResource(CassandraUtils.getDescribeRing(config)); }
private void setFailureModeForHost(CassandraClientPoolingContainer poolingContainer, Exception failureMode) { try { when(poolingContainer.runWithPooledResource( Mockito.<FunctionCheckedException<CassandraClient, Object, Exception>>any())) .thenThrow(failureMode); } catch (Exception e) { throw Throwables.propagate(e); } }
private boolean isHostHealthy(CassandraClientPoolingContainer container) { try { container.runWithPooledResource(CassandraUtils.getDescribeRing(config)); container.runWithPooledResource(CassandraUtils.getValidatePartitioner(config)); return true; } catch (Exception e) { log.info("We tried to add blacklisted host '{}' back into the pool, but got an exception" + " that caused us to distrust this host further. Exception message was: {} : {}", SafeArg.of("host", CassandraLogHelper.host(container.getHost())), SafeArg.of("exceptionClass", e.getClass().getCanonicalName()), UnsafeArg.of("exceptionMessage", e.getMessage())); return false; } }
@SuppressWarnings("unchecked") // We know the types are correct within this test. private void setConditionalTimeoutFailureForHost(CassandraClientPoolingContainer container, Function<CassandraClientPoolingContainer, Boolean> condition) { try { when(container.runWithPooledResource(any(FunctionCheckedException.class))) .then(invocation -> { if (condition.apply(container)) { throw new SocketTimeoutException(); } return 42; }); } catch (Exception e) { throw Throwables.propagate(e); } }
private void verifyNumberOfAttemptsOnHost(InetSocketAddress host, CassandraClientPool cassandraClientPool, int numAttempts) { Mockito.verify(cassandraClientPool.getCurrentPools().get(host), Mockito.times(numAttempts)) .runWithPooledResource( Mockito.<FunctionCheckedException<CassandraClient, Object, RuntimeException>>any()); }
void inPool(CassandraClientPool cassandraClientPool) { CassandraClientPoolingContainer container = mock(CassandraClientPoolingContainer.class); when(container.getHost()).thenReturn(address); try { OngoingStubbing<Object> stubbing = when(container.runWithPooledResource( Mockito.<FunctionCheckedException<CassandraClient, Object, Exception>>any())); for (Exception ex : exceptions) { stubbing = stubbing.thenThrow(ex); } if (returnsValue) { stubbing.thenReturn("Response"); } } catch (Exception ex) { throw new RuntimeException(ex); } cassandraClientPool.getCurrentPools().put(address, container); } }
private <V, K extends Exception> V runWithPooledResourceRecordingMetrics( CassandraClientPoolingContainer hostPool, FunctionCheckedException<CassandraClient, V, K> fn) throws K { metrics.recordRequestOnHost(hostPool); try { return hostPool.runWithPooledResource(fn); } catch (Exception e) { metrics.recordExceptionOnHost(hostPool); if (CassandraRequestExceptionHandler.isConnectionException(e)) { metrics.recordConnectionExceptionOnHost(hostPool); } throw e; } }
private List<TokenRange> getTokenRanges() throws Exception { return getRandomGoodHost().runWithPooledResource(CassandraUtils.getDescribeRing(config)); }
private boolean isHostHealthy(CassandraClientPoolingContainer container) { try { container.runWithPooledResource(CassandraUtils.getDescribeRing(config)); container.runWithPooledResource(CassandraUtils.getValidatePartitioner(config)); return true; } catch (Exception e) { log.info("We tried to add blacklisted host '{}' back into the pool, but got an exception" + " that caused us to distrust this host further. Exception message was: {} : {}", SafeArg.of("host", CassandraLogHelper.host(container.getHost())), SafeArg.of("exceptionClass", e.getClass().getCanonicalName()), UnsafeArg.of("exceptionMessage", e.getMessage())); return false; } }
private <V, K extends Exception> V runWithPooledResourceRecordingMetrics( CassandraClientPoolingContainer hostPool, FunctionCheckedException<CassandraClient, V, K> fn) throws K { metrics.recordRequestOnHost(hostPool); try { return hostPool.runWithPooledResource(fn); } catch (Exception e) { metrics.recordExceptionOnHost(hostPool); if (CassandraRequestExceptionHandler.isConnectionException(e)) { metrics.recordConnectionExceptionOnHost(hostPool); } throw e; } }