Elffuss BraidHub

kikocis/java-driver public

Prevent concurrent retries of the same query (JAVA-449).
Changeset8b1fec78dfd42b02ece790df7790c3304f2501fbe67b4527c90169985ba5e5db
Authorolim7t <omichallat+github@gmail.com> · 2014-09-17 11:26 -07:00
Committerolim7t <omichallat+github@gmail.com> · 2014-09-23 11:19 +02:00
Parents5ee7f07801b7

Browse the files at this changeset

2 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -5,6 +5,7 @@
55 ------
66
77 - [bug] Handle null pool in PooledConnection.release (JAVA-449)
8+- [bug] Prevent concurrent retries of the same query (JAVA-449)
89
910
1011 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +33 −18
@@ -24,8 +24,9 @@
2424 import java.util.Iterator;
2525 import java.util.List;
2626 import java.util.Map;
27-import java.util.concurrent.TimeUnit;
28-import java.util.concurrent.TimeoutException;
27+import java.util.concurrent.*;
28+import java.util.concurrent.locks.Lock;
29+import java.util.concurrent.locks.ReentrantLock;
2930
3031 import com.codahale.metrics.Timer;
3132 import org.slf4j.Logger;
@@ -71,6 +72,10 @@
7172 private final Timer.Context timerContext;
7273 private final long startTime;
7374
75+ // Tracks whether there is a retry already in progress
76+ private final Lock retryLock = new ReentrantLock();
77+ private Future<?> currentRetry; // guarded by retryLock
78+
7479 public RequestHandler(SessionManager manager, Callback callback, Statement statement) {
7580 this.manager = manager;
7681 this.callback = callback;
@@ -162,24 +167,34 @@
162167 }
163168
164169 private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) {
165- final Host h = current;
166- this.retryConsistencyLevel = newConsistencyLevel;
167-
168- // We should not retry on the current thread as this will be an IO thread.
169- manager.executor().execute(new Runnable() {
170- @Override
171- public void run() {
172- try {
173- if (retryCurrent) {
174- if (query(h))
175- return;
170+ try {
171+ // We lock to prevent two retries from launching concurrently
172+ retryLock.lock();
173+
174+ if (currentRetry != null && !currentRetry.isDone())
175+ return;
176+
177+ final Host h = current;
178+ this.retryConsistencyLevel = newConsistencyLevel;
179+
180+ // We should not retry on the current thread as this will be an IO thread.
181+ currentRetry = manager.executor().submit(new Runnable() {
182+ @Override
183+ public void run() {
184+ try {
185+ if (retryCurrent) {
186+ if (query(h))
187+ return;
188+ }
189+ sendRequest();
190+ } catch (Exception e) {
191+ setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e));
176192 }
177- sendRequest();
178- } catch (Exception e) {
179- setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e));
180193 }
181- }
182- });
194+ });
195+ } finally {
196+ retryLock.unlock();
197+ }
183198 }
184199
185200 public void cancel() {