try { socket = params == null ? new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout) : TSSLTransportFactory.getClientSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout, params); } catch (TTransportException e) { throw new HectorTransportException("Could not get client socket: ", e); if ( cassandraHost.getUseSocketKeepalive() ) { try { socket.getSocket().setKeepAlive(true); log.debug("Unable to open transport to " + cassandraHost.getName()); throw new HectorTransportException("Unable to open transport to " + cassandraHost.getName() +" , " + e.getLocalizedMessage(), e);
public CassandraHost[] buildCassandraHosts() { if (this.hosts == null) { throw new IllegalArgumentException("Need to define at least one host in order to apply configuration."); } String[] hostVals = hosts.split(","); CassandraHost[] cassandraHosts = new CassandraHost[hostVals.length]; for (int x=0; x<hostVals.length; x++) { CassandraHost cassandraHost = this.port == CassandraHost.DEFAULT_PORT ? new CassandraHost(hostVals[x].trim()) : new CassandraHost(hostVals[x], this.port); applyConfig(cassandraHost); cassandraHosts[x] = cassandraHost; } return cassandraHosts; }
/** * If CassandraHost was not null we use {@link CassandraHost#getCassandraThriftSocketTimeout()} * if it was greater than zero. Otherwise look for an environment * variable name CASSANDRA_THRIFT_SOCKET_TIMEOUT value. * If doesn't exist, returns 0. * @param cassandraHost */ private int getTimeout(CassandraHost cassandraHost) { int timeoutVar = 0; if ( cassandraHost != null && cassandraHost.getCassandraThriftSocketTimeout() > 0 ) { timeoutVar = cassandraHost.getCassandraThriftSocketTimeout(); } else { String timeoutStr = System.getProperty( SystemProperties.CASSANDRA_THRIFT_SOCKET_TIMEOUT.toString()); if (timeoutStr != null && timeoutStr.length() > 0) { try { timeoutVar = Integer.parseInt(timeoutStr); } catch (NumberFormatException e) { log.error("Invalid value for CASSANDRA_THRIFT_SOCKET_TIMEOUT", e); } } } return timeoutVar; }
public ConcurrentHClientPool(HClientFactory clientFactory, CassandraHost host, CassandraClientMonitor monitor) { this.clientFactory = clientFactory; this.cassandraHost = host; this.monitor = monitor; availableClientQueue = new ArrayBlockingQueue<HClient>(cassandraHost.getMaxActive(), true); // This counter can be offset by as much as the number of threads. activeClientsCount = new AtomicInteger(0); realActiveClientsCount = new AtomicInteger(0); exhaustedStartTime = new AtomicLong(-1); numBlocked = new AtomicInteger(); active = new AtomicBoolean(true); maxWaitTimeWhenExhausted = cassandraHost.getMaxWaitTimeWhenExhausted() < 0 ? 0 : cassandraHost.getMaxWaitTimeWhenExhausted(); for (int i = 0; i < cassandraHost.getMaxActive() / 3; i++) { availableClientQueue.add(createClient()); } if ( log.isDebugEnabled() ) { log.debug("Concurrent Host pool started with {} active clients; max: {} exhausted wait: {}", new Object[]{getNumIdle(), cassandraHost.getMaxActive(), maxWaitTimeWhenExhausted}); } }
@Override public String getName() { return String.format("<ConcurrentCassandraClientPoolByHost>:{%s}", cassandraHost.getName()); }
try { if (params == null) socket = new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout); else socket = TSSLTransportFactory.getClientSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout, params); } catch (TTransportException e) { throw new HectorTransportException("Could not get client socket: ", e); if ( cassandraHost.getUseSocketKeepalive() ) { try { socket.getSocket().setKeepAlive(true);
if (cassandraClient.getCassandraHost().getMaxLastSuccessTimeMillis() > 0 && cassandraClient.getLastSuccessTime() > 0 && System.currentTimeMillis() - cassandraClient.getLastSuccessTime() > cassandraClient.getCassandraHost().getMaxLastSuccessTimeMillis()) { log.info("Closing connection to {} due to too long idle time of {} ms", cassandraClient.getCassandraHost().getHost(), System.currentTimeMillis() - cassandraClient.getLastSuccessTime()); cassandraClient.close(); if (cassandraClient.getCassandraHost().getMaxConnectTimeMillis() > 0 && System.currentTimeMillis() - cassandraClient.getCreatedTime() > cassandraClient.getCassandraHost().getMaxConnectTimeMillis()) { log.info("Closing connection to {} due to too long existence time of {} ms", cassandraClient.getCassandraHost().getHost(), System.currentTimeMillis() - cassandraClient.getCreatedTime()); cassandraClient.close(); if (currentActiveClients <= cassandraHost.getMaxActive()) { cassandraClient = createClient(); } else {
@Test public void testConfigValuesPropogated() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170,localhost:9171,localhost:9172"); cassandraHostConfigurator.setMaxActive(20); cassandraHostConfigurator.setCassandraThriftSocketTimeout(3000); cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(4000); cassandraHostConfigurator.setMaxExhaustedTimeBeforeMarkingAsDown(5000); CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts(); // no need to test all, just a smattering assertEquals(20, cassandraHosts[1].getMaxActive()); assertEquals(20, cassandraHosts[0].getMaxActive()); assertEquals(4000, cassandraHosts[1].getMaxWaitTimeWhenExhausted()); assertEquals(5000, cassandraHosts[0].getMaxExhaustedTimeBeforeMarkingAsDown()); assertEquals(3000, cassandraHosts[2].getCassandraThriftSocketTimeout()); assertEquals(3000, cassandraHosts[0].getCassandraThriftSocketTimeout()); }
@Test public void testApplyConfig() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170"); cassandraHostConfigurator.setMaxActive(15); cassandraHostConfigurator.setMaxConnectTimeMillis(30000); cassandraHostConfigurator.setMaxLastSuccessTimeMillis(40000); CassandraHost extraHost = new CassandraHost("localhost:9171"); cassandraHostConfigurator.applyConfig(extraHost); assertEquals(15, extraHost.getMaxActive()); assertEquals(30000, extraHost.getMaxConnectTimeMillis()); assertEquals(40000, extraHost.getMaxLastSuccessTimeMillis()); }
@Test public void testHostnameOnlyDefaultPortMultipleHosts() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("h1,h2,h3:1111"); CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts(); assertEquals(CassandraHost.DEFAULT_PORT, cassandraHosts[0].getPort()); assertEquals(CassandraHost.DEFAULT_PORT, cassandraHosts[1].getPort()); assertEquals(1111, cassandraHosts[2].getPort()); }
@Override public int getMaxActive() { return cassandraHost.getMaxActive(); }
/** * {@inheritDoc} */ public HClient createClient(CassandraHost ch) { params = SSLHelper.getTSSLTransportParameters(); if ( params != null ) { log.info("SSL enabled for client<->server communications."); if ( log.isDebugEnabled() ) { log.debug("Properties:"); log.debug(" ssl.truststore = {}", System.getProperty("ssl.truststore")); log.debug(" ssl.protocol = {}", System.getProperty("ssl.protocol")); log.debug(" ssl.store.type = {}", System.getProperty("ssl.store.type")); log.debug(" ssl.cipher.suites = {}", System.getProperty("ssl.cipher.suites")); log.debug("Creation of new client for host: " + ch.getIp()); } } return params == null ? new HThriftClient(ch) : new HThriftClient(ch, params); }
@Override public boolean equals(Object obj) { return ((LatencyAwareHClientPool) obj).getCassandraHost().equals(getCassandraHost()); } }
@Override public Set<String> getSuspendedCassandraHosts() { Set<CassandraHost> hosts = connectionManager.getSuspendedCassandraHosts(); Set<String> hostsStr = new HashSet<String>(); for (CassandraHost host : hosts) { hostsStr.add(host.getName()); } return hostsStr; }
try { if (params == null) socket = new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout); else socket = TSSLTransportFactory.getClientSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout, params); } catch (TTransportException e) { throw new HectorTransportException("Could not get client socket: ", e); if ( cassandraHost.getUseSocketKeepalive() ) { try { socket.getSocket().setKeepAlive(true);
if (cassandraClient.getCassandraHost().getMaxLastSuccessTimeMillis() > 0 && cassandraClient.getLastSuccessTime() > 0 && System.currentTimeMillis() - cassandraClient.getLastSuccessTime() > cassandraClient.getCassandraHost().getMaxLastSuccessTimeMillis()) { log.info("Closing connection to {} due to too long idle time of {} ms", cassandraClient.getCassandraHost().getHost(), System.currentTimeMillis() - cassandraClient.getLastSuccessTime()); cassandraClient.close(); if (cassandraClient.getCassandraHost().getMaxConnectTimeMillis() > 0 && System.currentTimeMillis() - cassandraClient.getCreatedTime() > cassandraClient.getCassandraHost().getMaxConnectTimeMillis()) { log.info("Closing connection to {} due to too long existence time of {} ms", cassandraClient.getCassandraHost().getHost(), System.currentTimeMillis() - cassandraClient.getCreatedTime()); cassandraClient.close(); if (currentActiveClients <= cassandraHost.getMaxActive()) { cassandraClient = createClient(); } else {
public ConcurrentHClientPool(HClientFactory clientFactory, CassandraHost host) { this.clientFactory = clientFactory; this.cassandraHost = host; availableClientQueue = new ArrayBlockingQueue<HClient>(cassandraHost.getMaxActive(), true); // This counter can be offset by as much as the number of threads. activeClientsCount = new AtomicInteger(0); realActiveClientsCount = new AtomicInteger(0); numBlocked = new AtomicInteger(); active = new AtomicBoolean(true); maxWaitTimeWhenExhausted = cassandraHost.getMaxWaitTimeWhenExhausted() < 0 ? 0 : cassandraHost.getMaxWaitTimeWhenExhausted(); for (int i = 0; i < cassandraHost.getMaxActive() / 3; i++) { availableClientQueue.add(createClient()); } if ( log.isDebugEnabled() ) { log.debug("Concurrent Host pool started with {} active clients; max: {} exhausted wait: {}", new Object[]{getNumIdle(), cassandraHost.getMaxActive(), maxWaitTimeWhenExhausted}); } }
@Test public void testHostnameOnlyDefaultPort() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost"); CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts(); assertEquals(CassandraHost.DEFAULT_PORT, cassandraHosts[0].getPort()); }