Elffuss BraidHub

kikocis/java-driver public

Don't let (unexpected) exceptions slip by on timeouts

JAVA-268 #fixes
Changeset270420cb6eb6748acaa8022a5cace3dd090187dca980ea87323f2fef1ea7d590
AuthorSylvain Lebresne <sylvain@datastax.com> · 2014-03-20 11:22 +01:00
CommitterSylvain Lebresne <sylvain@datastax.com> · 2014-03-20 11:22 +01:00
Parents0f1528ccd351

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +0 −1
@@ -221,4 +221,3 @@
221221 }
222222 }
223223 }
224-
modified driver-core/src/main/java/com/datastax/driver/core/PooledConnection.java +1 −2
@@ -33,8 +33,7 @@
3333 * Return the pooled connection to it's pool.
3434 * The connection should generally not be reuse after that.
3535 */
36- public void release()
37- {
36+ public void release() {
3837 pool.returnConnection(this);
3938 }
4039 }
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +51 −31
@@ -160,11 +160,15 @@
160160 manager.executor().execute(new Runnable() {
161161 @Override
162162 public void run() {
163- if (retryCurrent) {
164- if (query(h))
165- return;
163+ try {
164+ if (retryCurrent) {
165+ if (query(h))
166+ return;
167+ }
168+ sendRequest();
169+ } catch (Exception e) {
170+ setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e));
166171 }
167- sendRequest();
168172 }
169173 });
170174 }
@@ -196,33 +200,40 @@
196200 }
197201
198202 private void setFinalResult(Connection connection, Message.Response response) {
199- if (timerContext != null)
200- timerContext.stop();
201-
202- ExecutionInfo info = current.defaultExecutionInfo;
203- if (triedHosts != null)
204- {
205- triedHosts.add(current);
206- info = new ExecutionInfo(triedHosts);
203+ try {
204+ if (timerContext != null)
205+ timerContext.stop();
206+
207+ ExecutionInfo info = current.defaultExecutionInfo;
208+ if (triedHosts != null)
209+ {
210+ triedHosts.add(current);
211+ info = new ExecutionInfo(triedHosts);
212+ }
213+ if (retryConsistencyLevel != null)
214+ info = info.withAchievedConsistency(retryConsistencyLevel);
215+ callback.onSet(connection, response, info, System.nanoTime() - startTime);
216+ } catch (Exception e) {
217+ callback.onException(connection, new DriverInternalError("Unexpected exception while setting final result from " + response, e), System.nanoTime() - startTime);
207218 }
208- if (retryConsistencyLevel != null)
209- info = info.withAchievedConsistency(retryConsistencyLevel);
210- callback.onSet(connection, response, info, System.nanoTime() - startTime);
211219 }
212220
213221 private void setFinalException(Connection connection, Exception exception) {
214- if (timerContext != null)
215- timerContext.stop();
216- callback.onException(connection, exception, System.nanoTime() - startTime);
222+ try {
223+ if (timerContext != null)
224+ timerContext.stop();
225+ } finally {
226+ callback.onException(connection, exception, System.nanoTime() - startTime);
227+ }
217228 }
218229
219230 @Override
220231 public void onSet(Connection connection, Message.Response response, long latency) {
221- if (connection instanceof PooledConnection)
222- ((PooledConnection)connection).release();
223-
224232 Host queriedHost = current;
225233 try {
234+ if (connection instanceof PooledConnection)
235+ ((PooledConnection)connection).release();
236+
226237 switch (response.type) {
227238 case RESULT:
228239 setFinalResult(connection, response);
@@ -405,11 +416,12 @@
405416
406417 @Override
407418 public void onException(Connection connection, Exception exception, long latency) {
408- if (connection instanceof PooledConnection)
409- ((PooledConnection)connection).release();
410419
411420 Host queriedHost = current;
412421 try {
422+ if (connection instanceof PooledConnection)
423+ ((PooledConnection)connection).release();
424+
413425 if (exception instanceof ConnectionException) {
414426 if (metricsEnabled())
415427 metrics().getErrorMetrics().getConnectionErrors().inc();
@@ -420,6 +432,9 @@
420432 }
421433
422434 setFinalException(connection, exception);
435+ } catch (Exception e) {
436+ // This shouldn't happen, but if it does, we want to signal the callback, not let him hang indefinitively
437+ setFinalException(null, new DriverInternalError("An unexpected error happened while handling exception " + exception, e));
423438 } finally {
424439 if (queriedHost != null)
425440 manager.cluster.manager.reportLatency(queriedHost, latency);
@@ -428,15 +443,20 @@
428443
429444 @Override
430445 public void onTimeout(Connection connection, long latency) {
431- if (connection instanceof PooledConnection)
432- ((PooledConnection)connection).release();
433-
434446 Host queriedHost = current;
435- logError(connection.address, "Timeout during read");
436- retry(false, null);
437-
438- if (queriedHost != null)
439- manager.cluster.manager.reportLatency(queriedHost, latency);
447+ try {
448+ if (connection instanceof PooledConnection)
449+ ((PooledConnection)connection).release();
450+
451+ logError(connection.address, "Timeout during read");
452+ retry(false, null);
453+ } catch (Exception e) {
454+ // This shouldn't happen, but if it does, we want to signal the callback, not let him hang indefinitively
455+ setFinalException(null, new DriverInternalError("An unexpected error happened while handling timeout", e));
456+ } finally {
457+ if (queriedHost != null)
458+ manager.cluster.manager.reportLatency(queriedHost, latency);
459+ }
440460 }
441461
442462 interface Callback extends Connection.ResponseCallback {