kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8b1fec78dfd42b02ece790df7790c3304f2501fbe67b4527c90169985ba5e5db |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-09-17 11:26 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-09-23 11:19 +02:00 |
| Parents | 5ee7f07801b7 |
2 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | ------ |
| 6 | 6 | |
| 7 | 7 | - [bug] Handle null pool in PooledConnection.release (JAVA-449) |
| 8 | +- [bug] Prevent concurrent retries of the same query (JAVA-449) | |
| 8 | 9 | |
| 9 | 10 | |
| 10 | 11 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +33 −18
| @@ -24,8 +24,9 @@ | ||
| 24 | 24 | import java.util.Iterator; |
| 25 | 25 | import java.util.List; |
| 26 | 26 | 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; | |
| 29 | 30 | |
| 30 | 31 | import com.codahale.metrics.Timer; |
| 31 | 32 | import org.slf4j.Logger; |
| @@ -71,6 +72,10 @@ | ||
| 71 | 72 | private final Timer.Context timerContext; |
| 72 | 73 | private final long startTime; |
| 73 | 74 | |
| 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 | + | |
| 74 | 79 | public RequestHandler(SessionManager manager, Callback callback, Statement statement) { |
| 75 | 80 | this.manager = manager; |
| 76 | 81 | this.callback = callback; |
| @@ -162,24 +167,34 @@ | ||
| 162 | 167 | } |
| 163 | 168 | |
| 164 | 169 | 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)); | |
| 176 | 192 | } |
| 177 | - sendRequest(); | |
| 178 | - } catch (Exception e) { | |
| 179 | - setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e)); | |
| 180 | 193 | } |
| 181 | - } | |
| 182 | - }); | |
| 194 | + }); | |
| 195 | + } finally { | |
| 196 | + retryLock.unlock(); | |
| 197 | + } | |
| 183 | 198 | } |
| 184 | 199 | |
| 185 | 200 | public void cancel() { |