kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 48eee9fbeb84b3e575381a919d05a5b8134ca9fadd5d142dd2b596aef90d1a32 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-07-10 17:15 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-07-17 13:31 +02:00 |
| Parents | 2cf768d3be54 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -11,6 +11,8 @@ | ||
| 11 | 11 | - [bug] Avoid classloader leak in Tomcat (JAVA-343) |
| 12 | 12 | - [bug] Avoid deadlock in onAdd/onUp (JAVA-387) |
| 13 | 13 | - [bug] Make metadata parsing more lenient (JAVA-377, JAVA-391) |
| 14 | +- [bug] Ensure defunct connections are completely closed (JAVA-394) | |
| 15 | +- [bug] Fix memory and resource leak on closed Sessions (JAVA-342, JAVA-390) | |
| 14 | 16 | |
| 15 | 17 | |
| 16 | 18 | 2.0.3: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +4 −0
| @@ -1197,6 +1197,10 @@ | ||
| 1197 | 1197 | return session; |
| 1198 | 1198 | } |
| 1199 | 1199 | |
| 1200 | + boolean removeSession(Session session) { | |
| 1201 | + return sessions.remove(session); | |
| 1202 | + } | |
| 1203 | + | |
| 1200 | 1204 | void reportLatency(Host host, long latencyNanos) { |
| 1201 | 1205 | for (LatencyTracker tracker : trackers) { |
| 1202 | 1206 | tracker.update(host, latencyNanos); |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +24 −2
| @@ -51,6 +51,7 @@ | ||
| 51 | 51 | private final Striped<Lock> poolCreationLocks = Striped.lazyWeakLock(5); |
| 52 | 52 | |
| 53 | 53 | private volatile boolean isInit; |
| 54 | + private volatile boolean isClosing; | |
| 54 | 55 | |
| 55 | 56 | // Package protected, only Cluster should construct that. |
| 56 | 57 | SessionManager(Cluster cluster) { |
| @@ -100,6 +101,9 @@ | ||
| 100 | 101 | if (future != null) |
| 101 | 102 | return future; |
| 102 | 103 | |
| 104 | + isClosing = true; | |
| 105 | + cluster.manager.removeSession(this); | |
| 106 | + | |
| 103 | 107 | List<CloseFuture> futures = new ArrayList<CloseFuture>(pools.size()); |
| 104 | 108 | for (HostConnectionPool pool : pools.values()) |
| 105 | 109 | futures.add(pool.closeAsync()); |
| @@ -192,13 +196,22 @@ | ||
| 192 | 196 | public Boolean call() { |
| 193 | 197 | while (true) { |
| 194 | 198 | try { |
| 195 | - HostConnectionPool previous = pools.put(host, new HostConnectionPool(host, distance, SessionManager.this)); | |
| 199 | + if (isClosing) | |
| 200 | + return true; | |
| 201 | + | |
| 202 | + HostConnectionPool newPool = new HostConnectionPool(host, distance, SessionManager.this); | |
| 203 | + HostConnectionPool previous = pools.put(host, newPool); | |
| 196 | 204 | if (previous == null) { |
| 197 | 205 | logger.debug("Added connection pool for {}", host); |
| 198 | 206 | } else { |
| 199 | 207 | logger.debug("Renewed connection pool for {}", host); |
| 200 | 208 | previous.closeAsync(); |
| 201 | 209 | } |
| 210 | + | |
| 211 | + // If we raced with a session shutdown, ensure that the pool will be closed. | |
| 212 | + if (isClosing) | |
| 213 | + newPool.closeAsync(); | |
| 214 | + | |
| 202 | 215 | return true; |
| 203 | 216 | } catch (Exception e) { |
| 204 | 217 | logger.error("Error creating pool to " + host, e); |
| @@ -215,6 +228,9 @@ | ||
| 215 | 228 | // ConcurrentMap only for that since it's the duplicate HostConnectionPool creation we |
| 216 | 229 | // want to avoid |
| 217 | 230 | private boolean replacePool(Host host, HostDistance distance, HostConnectionPool condition) throws ConnectionException, UnsupportedProtocolVersionException { |
| 231 | + if (isClosing) | |
| 232 | + return false; | |
| 233 | + | |
| 218 | 234 | Lock l = poolCreationLocks.get(host); |
| 219 | 235 | l.lock(); |
| 220 | 236 | try { |
| @@ -222,7 +238,13 @@ | ||
| 222 | 238 | if (previous != condition) |
| 223 | 239 | return false; |
| 224 | 240 | |
| 225 | - pools.put(host, new HostConnectionPool(host, distance, this)); | |
| 241 | + HostConnectionPool newPool = new HostConnectionPool(host, distance, this); | |
| 242 | + pools.put(host, newPool); | |
| 243 | + | |
| 244 | + // If we raced with a session shutdown, ensure that the pool will be closed. | |
| 245 | + if (isClosing) | |
| 246 | + newPool.closeAsync(); | |
| 247 | + | |
| 226 | 248 | return true; |
| 227 | 249 | } finally { |
| 228 | 250 | l.unlock(); |