Browse files

Merge pull request #588 from dmdevito/fixingfirewallconnectioncut

provide a way to cut idle connection (to reopen it) before the firewall,...
  • Loading branch information...
2 parents 310c83d + 289aedb commit 0653fefa2c167a2d1be62d9c354e593afff868c0 @zznate zznate committed Feb 27, 2013
View
8 core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java
@@ -72,6 +72,14 @@ public HClient borrowClient() throws HectorException {
try {
if (cassandraClient != null) {
+ 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();
+ cassandraClient = null;
+ }
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(),
View
1 core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java
@@ -257,6 +257,7 @@ public void operateWithFailover(Operation<?> op) throws HectorException {
op.executeAndSetResult(c, pool.getCassandraHost());
success = true;
+ client.updateLastSuccessTime();
timer.stop(timerToken, op.stopWatchTagName, true);
break;
View
12 core/src/main/java/me/prettyprint/cassandra/connection/client/HClient.java
@@ -117,4 +117,16 @@
*/
void clearAuthentication();
+ /**
+ * Retrieves the time of the last success in milliseconds.
+ *
+ * @return -1 if no successful operation has already happened, or the time
+ * of the last success in milliseconds.
+ */
+ long getLastSuccessTime();
+
+ /**
+ * Update the time of the last success with the current time.
+ */
+ void updateLastSuccessTime();
}
View
18 core/src/main/java/me/prettyprint/cassandra/connection/client/HThriftClient.java
@@ -54,6 +54,8 @@
protected TTransport transport;
protected Cassandra.Client cassandraClient;
private TSSLTransportParameters params;
+
+ private volatile long lastSuccessTime;
private final Map<String, String> credentials = new HashMap<String, String>();
@@ -295,4 +297,20 @@ public void setAuthenticated(Map<String, String> credentials) {
public long getCreatedTime() {
return createdTime;
}
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long getLastSuccessTime() {
+ return lastSuccessTime;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void updateLastSuccessTime() {
+ lastSuccessTime = System.currentTimeMillis();
+ }
}
View
18 core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java
@@ -41,10 +41,15 @@
public static final boolean DEFAULT_LIFO = true;
/**
- * The default number of milliseconds we'll allow a connection to stay open. Default value
- * is negative which means indefinitely.
+ * The default number of milliseconds (since creation time) we'll allow a connection
+ * to stay open. Default value is negative which means indefinitely.
*/
public static final long DEFAULT_MAX_CONNECT_TIME = -1;
+ /**
+ * The default number of milliseconds (since last success) we'll allow a connection
+ * to stay open. Default value is negative which means indefinitely.
+ */
+ public static final long DEFAULT_MAX_LAST_SUCCESS_TIME = -1;
private final String host, ip, url;
private final int port;
@@ -60,6 +65,7 @@
private int maxFrameSize = DEFAULT_MAX_FRAME_SIZE;
private boolean useSocketKeepalive;
private long maxConnectTimeMillis = DEFAULT_MAX_CONNECT_TIME;
+ private long maxLastSuccessTimeMillis = DEFAULT_MAX_LAST_SUCCESS_TIME;
//TODO(ran): private FailoverPolicy failoverPolicy = DEFAULT_FAILOVER_POLICY;
public CassandraHost(String url) {
@@ -218,5 +224,13 @@ public void setMaxConnectTimeMillis(long maxConnectTimeMillis) {
this.maxConnectTimeMillis = maxConnectTimeMillis;
}
+ public long getMaxLastSuccessTimeMillis() {
+ return this.maxLastSuccessTimeMillis;
+ }
+
+ public void setMaxLastSuccessTimeMillis(long maxLastSuccessTimeMillis) {
+ this.maxLastSuccessTimeMillis = maxLastSuccessTimeMillis;
+ }
+
}
View
2 core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java
@@ -52,6 +52,7 @@
private HOpTimer opTimer = new NullOpTimer();
private Class<? extends HClientFactory> clientFactoryClass = HThriftClientFactoryImpl.class;
private long maxConnectTimeMillis = CassandraHost.DEFAULT_MAX_CONNECT_TIME;
+ private long maxLastSuccessTimeMillis = CassandraHost.DEFAULT_MAX_LAST_SUCCESS_TIME;
public CassandraHostConfigurator() {
this.hosts = null;
@@ -89,6 +90,7 @@ public void applyConfig(CassandraHost cassandraHost) {
cassandraHost.setMaxFrameSize(maxFrameSize);
cassandraHost.setUseSocketKeepalive(useSocketKeepalive);
cassandraHost.setMaxConnectTimeMillis(maxConnectTimeMillis);
+ cassandraHost.setMaxLastSuccessTimeMillis(maxLastSuccessTimeMillis);
// this is special as it can be passed in as a system property
if (cassandraThriftSocketTimeout > 0) {

0 comments on commit 0653fef

Please sign in to comment.