kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 07f7dc0d97aaf3bd21243325d4eabdacd0cea86bfb8cce5ea4582a6ba28caf01 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-20 15:37 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-20 15:37 +02:00 |
| Parents | a49b95c49f8d, 968015a12f3b |
15 files changed
modified changelog/README.md +1 −0
| @@ -38,6 +38,7 @@ | ||
| 38 | 38 | - [improvement] Debounce control connection queries (JAVA-657) |
| 39 | 39 | - [bug] LoadBalancingPolicy.distance() called before init() (JAVA-784) |
| 40 | 40 | - [new feature] Make driver-side metadata optional (JAVA-828) |
| 41 | +- [improvement] Allow hosts to remain partially up (JAVA-544) | |
| 41 | 42 | |
| 42 | 43 | Merged from 2.0.10_fixes branch: |
| 43 | 44 |
modified clirr-ignores.xml +17 −1
| @@ -6,4 +6,20 @@ | ||
| 6 | 6 | * check the reports in `<module>/target/site/clirr-report.html` |
| 7 | 7 | * add new differences if needed. Difference types are explained at http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html |
| 8 | 8 | --> |
| 9 | -<differences/> | |
| No newline at the end of the file | ||
| 9 | +<differences> | |
| 10 | + <difference> | |
| 11 | + <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 12 | + <className>com/datastax/driver/core/ConvictionPolicy$Factory</className> | |
| 13 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 14 | + </difference> | |
| 15 | + <difference> | |
| 16 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 17 | + <className>com/datastax/driver/core/ConvictionPolicy$Simple</className> | |
| 18 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 19 | + </difference> | |
| 20 | + <difference> | |
| 21 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 22 | + <className>com/datastax/driver/core/ConvictionPolicy$Simple$Factory</className> | |
| 23 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 24 | + </difference> | |
| 25 | +</differences> | |
| No newline at the end of the file |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +7 −4
| @@ -40,6 +40,7 @@ | ||
| 40 | 40 | |
| 41 | 41 | private static final Logger logger = LoggerFactory.getLogger(AbstractReconnectionHandler.class); |
| 42 | 42 | |
| 43 | + private final String name; | |
| 43 | 44 | private final ScheduledExecutorService executor; |
| 44 | 45 | private final ReconnectionPolicy.ReconnectionSchedule schedule; |
| 45 | 46 | /** |
| @@ -53,11 +54,12 @@ | ||
| 53 | 54 | |
| 54 | 55 | private final CountDownLatch ready = new CountDownLatch(1); |
| 55 | 56 | |
| 56 | - public AbstractReconnectionHandler(ScheduledExecutorService executor, ReconnectionPolicy.ReconnectionSchedule schedule, AtomicReference<ListenableFuture<?>> currentAttempt) { | |
| 57 | - this(executor, schedule, currentAttempt, -1); | |
| 57 | + public AbstractReconnectionHandler(String name, ScheduledExecutorService executor, ReconnectionPolicy.ReconnectionSchedule schedule, AtomicReference<ListenableFuture<?>> currentAttempt) { | |
| 58 | + this(name, executor, schedule, currentAttempt, -1); | |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | - public AbstractReconnectionHandler(ScheduledExecutorService executor, ReconnectionPolicy.ReconnectionSchedule schedule, AtomicReference<ListenableFuture<?>> currentAttempt, long initialDelayMs) { | |
| 61 | + public AbstractReconnectionHandler(String name, ScheduledExecutorService executor, ReconnectionPolicy.ReconnectionSchedule schedule, AtomicReference<ListenableFuture<?>> currentAttempt, long initialDelayMs) { | |
| 62 | + this.name = name; | |
| 61 | 63 | this.executor = executor; |
| 62 | 64 | this.schedule = schedule; |
| 63 | 65 | this.currentAttempt = currentAttempt; |
| @@ -92,7 +94,7 @@ | ||
| 92 | 94 | break; |
| 93 | 95 | } |
| 94 | 96 | if (currentAttempt.compareAndSet(previous, handlerFuture)) { |
| 95 | - logger.debug("Becoming the active handler"); | |
| 97 | + Host.statesLogger.debug("[{}] starting reconnection attempt", name); | |
| 96 | 98 | break; |
| 97 | 99 | } |
| 98 | 100 | } |
| @@ -176,6 +178,7 @@ | ||
| 176 | 178 | return; |
| 177 | 179 | } |
| 178 | 180 | |
| 181 | + Host.statesLogger.debug("[{}] next reconnection attempt in {} ms", name, nextDelay); | |
| 179 | 182 | handlerFuture.nextTry = executor.schedule(this, nextDelay, TimeUnit.MILLISECONDS); |
| 180 | 183 | } |
| 181 | 184 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +19 −17
| @@ -1213,7 +1213,7 @@ | ||
| 1213 | 1213 | Connection.Factory connectionFactory; |
| 1214 | 1214 | ControlConnection controlConnection; |
| 1215 | 1215 | |
| 1216 | - final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.Simple.Factory(); | |
| 1216 | + final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.DefaultConvictionPolicy.Factory(); | |
| 1217 | 1217 | |
| 1218 | 1218 | ScheduledThreadPoolExecutor reconnectionExecutor; |
| 1219 | 1219 | ScheduledThreadPoolExecutor scheduledTasksExecutor; |
| @@ -1555,8 +1555,6 @@ | ||
| 1555 | 1555 | |
| 1556 | 1556 | // Use triggerOnUp unless you're sure you want to run this on the current thread. |
| 1557 | 1557 | private void onUp(final Host host, Connection reusedConnection) throws InterruptedException, ExecutionException { |
| 1558 | - logger.debug("Host {} is UP", host); | |
| 1559 | - | |
| 1560 | 1558 | if (isClosed()) |
| 1561 | 1559 | return; |
| 1562 | 1560 | |
| @@ -1578,6 +1576,8 @@ | ||
| 1578 | 1576 | if (host.state == Host.State.UP) |
| 1579 | 1577 | return; |
| 1580 | 1578 | |
| 1579 | + Host.statesLogger.debug("[{}] marking host UP", host); | |
| 1580 | + | |
| 1581 | 1581 | // If there is a reconnection attempt scheduled for that node, cancel it |
| 1582 | 1582 | Future<?> scheduledAttempt = host.reconnectionAttempt.getAndSet(null); |
| 1583 | 1583 | if (scheduledAttempt != null) { |
| @@ -1650,7 +1650,7 @@ | ||
| 1650 | 1650 | } |
| 1651 | 1651 | |
| 1652 | 1652 | } finally { |
| 1653 | - if (reusedConnection != null && !reusedConnection.hasPool()) | |
| 1653 | + if (reusedConnection != null && !reusedConnection.hasOwner()) | |
| 1654 | 1654 | reusedConnection.closeAsync(); |
| 1655 | 1655 | } |
| 1656 | 1656 | } |
| @@ -1670,8 +1670,6 @@ | ||
| 1670 | 1670 | |
| 1671 | 1671 | // Use triggerOnDown unless you're sure you want to run this on the current thread. |
| 1672 | 1672 | private void onDown(final Host host, final boolean isHostAddition, boolean startReconnection) throws InterruptedException, ExecutionException { |
| 1673 | - logger.debug("Host {} is DOWN", host); | |
| 1674 | - | |
| 1675 | 1673 | if (isClosed()) |
| 1676 | 1674 | return; |
| 1677 | 1675 | |
| @@ -1692,6 +1690,8 @@ | ||
| 1692 | 1690 | return; |
| 1693 | 1691 | } |
| 1694 | 1692 | |
| 1693 | + Host.statesLogger.debug("[{}] marking host DOWN", host); | |
| 1694 | + | |
| 1695 | 1695 | // Remember if we care about this node at all. We must call this before |
| 1696 | 1696 | // we've signalled the load balancing policy, since most policy will always |
| 1697 | 1697 | // IGNORE down nodes anyway. |
| @@ -1717,7 +1717,6 @@ | ||
| 1717 | 1717 | if (distance == HostDistance.IGNORED || !startReconnection) |
| 1718 | 1718 | return; |
| 1719 | 1719 | |
| 1720 | - logger.debug("{} is down, scheduling connection retries", host); | |
| 1721 | 1720 | startPeriodicReconnectionAttempt(host, isHostAddition); |
| 1722 | 1721 | } finally { |
| 1723 | 1722 | host.notificationsLock.unlock(); |
| @@ -1725,7 +1724,7 @@ | ||
| 1725 | 1724 | } |
| 1726 | 1725 | |
| 1727 | 1726 | void startPeriodicReconnectionAttempt(final Host host, final boolean isHostAddition) { |
| 1728 | - new AbstractReconnectionHandler(reconnectionExecutor, reconnectionPolicy().newSchedule(), host.reconnectionAttempt) { | |
| 1727 | + new AbstractReconnectionHandler(host.toString(), reconnectionExecutor, reconnectionPolicy().newSchedule(), host.reconnectionAttempt) { | |
| 1729 | 1728 | |
| 1730 | 1729 | protected Connection tryReconnect() throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { |
| 1731 | 1730 | return connectionFactory.open(host); |
| @@ -1778,7 +1777,7 @@ | ||
| 1778 | 1777 | logger.debug("Scheduling one-time reconnection to {}", host); |
| 1779 | 1778 | |
| 1780 | 1779 | // Setting an initial delay of 0 to start immediately, and all the exception handlers return false to prevent further attempts |
| 1781 | - new AbstractReconnectionHandler(reconnectionExecutor, reconnectionPolicy().newSchedule(), host.reconnectionAttempt, 0) { | |
| 1780 | + new AbstractReconnectionHandler(host.toString(), reconnectionExecutor, reconnectionPolicy().newSchedule(), host.reconnectionAttempt, 0) { | |
| 1782 | 1781 | |
| 1783 | 1782 | protected Connection tryReconnect() throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { |
| 1784 | 1783 | return connectionFactory.open(host); |
| @@ -1834,8 +1833,6 @@ | ||
| 1834 | 1833 | if (isClosed()) |
| 1835 | 1834 | return; |
| 1836 | 1835 | |
| 1837 | - logger.info("New Cassandra host {} added", host); | |
| 1838 | - | |
| 1839 | 1836 | if (connectionFactory.protocolVersion == 2 && !supportsProtocolV2(host)) { |
| 1840 | 1837 | logUnsupportedVersionProtocol(host); |
| 1841 | 1838 | return; |
| @@ -1849,6 +1846,7 @@ | ||
| 1849 | 1846 | return; |
| 1850 | 1847 | } |
| 1851 | 1848 | try { |
| 1849 | + Host.statesLogger.debug("[{}] adding host", host); | |
| 1852 | 1850 | |
| 1853 | 1851 | // Adds to the load balancing first and foremost, as doing so might change the decision |
| 1854 | 1852 | // it will make for distance() on that node (not likely but we leave that possibility). |
| @@ -1921,7 +1919,7 @@ | ||
| 1921 | 1919 | } |
| 1922 | 1920 | |
| 1923 | 1921 | } finally { |
| 1924 | - if (reusedConnection != null && !reusedConnection.hasPool()) | |
| 1922 | + if (reusedConnection != null && !reusedConnection.hasOwner()) | |
| 1925 | 1923 | reusedConnection.closeAsync(); |
| 1926 | 1924 | } |
| 1927 | 1925 | } |
| @@ -1949,7 +1947,8 @@ | ||
| 1949 | 1947 | |
| 1950 | 1948 | host.setDown(); |
| 1951 | 1949 | |
| 1952 | - logger.debug("Removing host {}", host); | |
| 1950 | + Host.statesLogger.debug("[{}] removing host", host); | |
| 1951 | + | |
| 1953 | 1952 | loadBalancingPolicy().onRemove(host); |
| 1954 | 1953 | controlConnection.onRemove(host); |
| 1955 | 1954 | for (SessionManager s : sessions) |
| @@ -1962,13 +1961,14 @@ | ||
| 1962 | 1961 | } |
| 1963 | 1962 | } |
| 1964 | 1963 | |
| 1965 | - public boolean signalConnectionFailure(Host host, ConnectionException exception, boolean isHostAddition) { | |
| 1964 | + public boolean signalConnectionFailure(Host host, ConnectionException exception, | |
| 1965 | + boolean connectionInitialized, boolean isHostAddition) { | |
| 1966 | 1966 | // Don't signal failure until we've fully initialized the controlConnection as this might mess up with |
| 1967 | 1967 | // the protocol detection |
| 1968 | 1968 | if (!isFullyInit || isClosed()) |
| 1969 | 1969 | return true; |
| 1970 | 1970 | |
| 1971 | - boolean isDown = host.signalConnectionFailure(exception); | |
| 1971 | + boolean isDown = host.convictionPolicy.signalConnectionFailure(exception, connectionInitialized); | |
| 1972 | 1972 | if (isDown) |
| 1973 | 1973 | triggerOnDown(host, isHostAddition, true); |
| 1974 | 1974 | return isDown; |
| @@ -2175,6 +2175,7 @@ | ||
| 2175 | 2175 | case TOPOLOGY_CHANGE: |
| 2176 | 2176 | ProtocolEvent.TopologyChange tpc = (ProtocolEvent.TopologyChange)event; |
| 2177 | 2177 | InetSocketAddress tpAddr = translateAddress(tpc.node.getAddress()); |
| 2178 | + Host.statesLogger.debug("[{}] received event {}", tpAddr, tpc.change); | |
| 2178 | 2179 | switch (tpc.change) { |
| 2179 | 2180 | case NEW_NODE: |
| 2180 | 2181 | submitNodeRefresh(tpAddr, HostEvent.ADDED); |
| @@ -2190,6 +2191,7 @@ | ||
| 2190 | 2191 | case STATUS_CHANGE: |
| 2191 | 2192 | ProtocolEvent.StatusChange stc = (ProtocolEvent.StatusChange)event; |
| 2192 | 2193 | InetSocketAddress stAddr = translateAddress(stc.node.getAddress()); |
| 2194 | + Host.statesLogger.debug("[{}] received event {}", stAddr, stc.status); | |
| 2193 | 2195 | switch (stc.status) { |
| 2194 | 2196 | case UP: |
| 2195 | 2197 | submitNodeRefresh(stAddr, HostEvent.UP); |
| @@ -2256,7 +2258,7 @@ | ||
| 2256 | 2258 | // reconnecting (thus letting the loadBalancingPolicy pick a better node) |
| 2257 | 2259 | Host ccHost = controlConnection.connectedHost(); |
| 2258 | 2260 | if (ccHost == null || loadBalancingPolicy().distance(ccHost) != HostDistance.LOCAL) |
| 2259 | - controlConnection.reconnect(); | |
| 2261 | + controlConnection.triggerReconnect(); | |
| 2260 | 2262 | |
| 2261 | 2263 | for (SessionManager s : sessions) |
| 2262 | 2264 | s.updateCreatedPools(); |
| @@ -2266,7 +2268,7 @@ | ||
| 2266 | 2268 | // Deal with the control connection if it was using this host |
| 2267 | 2269 | Host ccHost = controlConnection.connectedHost(); |
| 2268 | 2270 | if (ccHost == null || ccHost.equals(host) && loadBalancingPolicy().distance(ccHost) != HostDistance.LOCAL) |
| 2269 | - controlConnection.reconnect(); | |
| 2271 | + controlConnection.triggerReconnect(); | |
| 2270 | 2272 | |
| 2271 | 2273 | for (SessionManager s : sessions) |
| 2272 | 2274 | s.updateCreatedPools(host); |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +48 −34
| @@ -85,11 +85,11 @@ | ||
| 85 | 85 | private volatile String keyspace; |
| 86 | 86 | |
| 87 | 87 | private volatile boolean isInitialized; |
| 88 | - private volatile boolean isDefunct; | |
| 88 | + private final AtomicBoolean isDefunct = new AtomicBoolean(); | |
| 89 | 89 | |
| 90 | 90 | private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>(); |
| 91 | 91 | |
| 92 | - private final AtomicReference<HostConnectionPool> poolRef = new AtomicReference<HostConnectionPool>(); | |
| 92 | + private final AtomicReference<Owner> ownerRef = new AtomicReference<Owner>(); | |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | /** |
| @@ -98,14 +98,14 @@ | ||
| 98 | 98 | * @param name the connection name |
| 99 | 99 | * @param address the remote address |
| 100 | 100 | * @param factory the connection factory to use |
| 101 | - * @param pool the pool this connection belongs to. May be null if this connection does not belong to a pool. | |
| 102 | - * Note that an existing connection can also be associated to a pool later with {@link #setPool(HostConnectionPool)}. | |
| 101 | + * @param owner the component owning this connection (may be null). | |
| 102 | + * Note that an existing connection can also be associated to an owner later with {@link #setOwner(Owner)}. | |
| 103 | 103 | */ |
| 104 | - protected Connection(String name, InetSocketAddress address, Factory factory, HostConnectionPool pool) { | |
| 104 | + protected Connection(String name, InetSocketAddress address, Factory factory, Owner owner) { | |
| 105 | 105 | this.address = address; |
| 106 | 106 | this.factory = factory; |
| 107 | 107 | this.name = name; |
| 108 | - this.poolRef.set(pool); | |
| 108 | + this.ownerRef.set(owner); | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
| @@ -196,6 +196,9 @@ | ||
| 196 | 196 | Futures.addCallback(initFuture, new FutureCallback<Void>() { |
| 197 | 197 | @Override |
| 198 | 198 | public void onSuccess(Void result) { |
| 199 | + Host host = factory.manager.metadata.getHost(address); | |
| 200 | + if(host != null) | |
| 201 | + host.convictionPolicy.signalConnectionCreated(); | |
| 199 | 202 | isInitialized = true; |
| 200 | 203 | } |
| 201 | 204 | |
| @@ -373,7 +376,7 @@ | ||
| 373 | 376 | } |
| 374 | 377 | |
| 375 | 378 | public boolean isDefunct() { |
| 376 | - return isDefunct; | |
| 379 | + return isDefunct.get(); | |
| 377 | 380 | } |
| 378 | 381 | |
| 379 | 382 | public int maxAvailableStreams() { |
| @@ -381,9 +384,13 @@ | ||
| 381 | 384 | } |
| 382 | 385 | |
| 383 | 386 | <E extends Exception> E defunct(E e) { |
| 384 | - if (logger.isDebugEnabled()) | |
| 385 | - logger.debug("Defuncting connection to " + address, e); | |
| 386 | - isDefunct = true; | |
| 387 | + if (!isDefunct.compareAndSet(false, true)) | |
| 388 | + return e; | |
| 389 | + | |
| 390 | + if (Host.statesLogger.isTraceEnabled()) | |
| 391 | + Host.statesLogger.trace("Defuncting " + this, e); | |
| 392 | + else if (Host.statesLogger.isDebugEnabled()) | |
| 393 | + Host.statesLogger.debug("Defuncting {} because: {}", this, e.getMessage()); | |
| 387 | 394 | |
| 388 | 395 | ConnectionException ce = e instanceof ConnectionException |
| 389 | 396 | ? (ConnectionException)e |
| @@ -391,10 +398,12 @@ | ||
| 391 | 398 | |
| 392 | 399 | Host host = factory.manager.metadata.getHost(address); |
| 393 | 400 | if (host != null) { |
| 394 | - // This will trigger onDown, including when the defunct Connection is part of a reconnection attempt, which is redundant. | |
| 395 | - // This is not too much of a problem since calling onDown on a node that is already down has no effect. | |
| 396 | - boolean isDown = factory.manager.signalConnectionFailure(host, ce, host.wasJustAdded()); | |
| 397 | - notifyOwnerWhenDefunct(isDown); | |
| 401 | + boolean isDown = factory.manager.signalConnectionFailure(host, ce, isInitialized, host.wasJustAdded()); | |
| 402 | + | |
| 403 | + if (!isDown) | |
| 404 | + notifyOwnerWhenDefunct(); | |
| 405 | + // else the driver will destroy all pools and notify the control connection as part of marking the host down, | |
| 406 | + // so no need to notify | |
| 398 | 407 | } |
| 399 | 408 | |
| 400 | 409 | // Force the connection to close to make sure the future completes. Otherwise force() might never get called and |
| @@ -405,20 +414,14 @@ | ||
| 405 | 414 | return e; |
| 406 | 415 | } |
| 407 | 416 | |
| 408 | - protected void notifyOwnerWhenDefunct(boolean hostIsDown) { | |
| 417 | + private void notifyOwnerWhenDefunct() { | |
| 409 | 418 | // If an error happens during initialization, the owner will detect it and take appropriate action |
| 410 | 419 | if (!isInitialized) |
| 411 | 420 | return; |
| 412 | 421 | |
| 413 | - HostConnectionPool pool = this.poolRef.get(); | |
| 414 | - if (pool == null) | |
| 415 | - return; | |
| 416 | - | |
| 417 | - if (hostIsDown) { | |
| 418 | - pool.closeAsync().force(); | |
| 419 | - } else { | |
| 420 | - pool.replaceDefunctConnection(this); | |
| 421 | - } | |
| 422 | + Owner owner = this.ownerRef.get(); | |
| 423 | + if (owner != null) | |
| 424 | + owner.onConnectionDefunct(this); | |
| 422 | 425 | } |
| 423 | 426 | |
| 424 | 427 | public String keyspace() { |
| @@ -505,7 +508,7 @@ | ||
| 505 | 508 | * having set the handler, we guarantee that even if we race with defunct/close, we may |
| 506 | 509 | * never leave a handler that won't get an answer or be errored out. |
| 507 | 510 | */ |
| 508 | - if (isDefunct) { | |
| 511 | + if (isDefunct.get()) { | |
| 509 | 512 | dispatcher.removeHandler(handler, true); |
| 510 | 513 | throw new ConnectionException(address, "Write attempt on defunct connection"); |
| 511 | 514 | } |
| @@ -567,13 +570,13 @@ | ||
| 567 | 570 | }; |
| 568 | 571 | } |
| 569 | 572 | |
| 570 | - boolean hasPool() { | |
| 571 | - return this.poolRef.get() != null; | |
| 573 | + boolean hasOwner() { | |
| 574 | + return this.ownerRef.get() != null; | |
| 572 | 575 | } |
| 573 | 576 | |
| 574 | - /** @return whether the connection was already associated with a pool */ | |
| 575 | - boolean setPool(HostConnectionPool pool) { | |
| 576 | - return poolRef.compareAndSet(null, pool); | |
| 577 | + /** @return whether the connection was already associated with an owner */ | |
| 578 | + boolean setOwner(Owner owner) { | |
| 579 | + return ownerRef.compareAndSet(null, owner); | |
| 577 | 580 | } |
| 578 | 581 | |
| 579 | 582 | /** |
| @@ -581,9 +584,9 @@ | ||
| 581 | 584 | * The connection should generally not be reused after that. |
| 582 | 585 | */ |
| 583 | 586 | void release() { |
| 584 | - HostConnectionPool pool = poolRef.get(); | |
| 585 | - if (pool != null) | |
| 586 | - pool.returnConnection(this); | |
| 587 | + Owner owner = ownerRef.get(); | |
| 588 | + if (owner instanceof HostConnectionPool) | |
| 589 | + ((HostConnectionPool)owner).returnConnection(this); | |
| 587 | 590 | } |
| 588 | 591 | |
| 589 | 592 | public boolean isClosed() { |
| @@ -611,6 +614,12 @@ | ||
| 611 | 614 | |
| 612 | 615 | logger.debug("{} closing connection", this); |
| 613 | 616 | |
| 617 | + if (isInitialized && !isDefunct.get()) { | |
| 618 | + Host host = factory.manager.metadata.getHost(address); | |
| 619 | + if(host != null) | |
| 620 | + host.convictionPolicy.signalConnectionClosed(); | |
| 621 | + } | |
| 622 | + | |
| 614 | 623 | boolean terminated = tryTerminate(false); |
| 615 | 624 | if (!terminated) { |
| 616 | 625 | // The time by which all pending requests should have normally completed (use twice the read timeout for a generous |
| @@ -981,7 +990,7 @@ | ||
| 981 | 990 | |
| 982 | 991 | @Override |
| 983 | 992 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| 984 | - if (evt instanceof IdleStateEvent && ((IdleStateEvent)evt).state() == ALL_IDLE) { | |
| 993 | + if (!isClosed() && evt instanceof IdleStateEvent && ((IdleStateEvent)evt).state() == ALL_IDLE) { | |
| 985 | 994 | logger.debug("{} was inactive for {} seconds, sending heartbeat", Connection.this, factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds()); |
| 986 | 995 | write(HEARTBEAT_CALLBACK); |
| 987 | 996 | } |
| @@ -1286,4 +1295,9 @@ | ||
| 1286 | 1295 | nettyOptions.afterChannelInitialized(channel); |
| 1287 | 1296 | } |
| 1288 | 1297 | } |
| 1298 | + | |
| 1299 | + /** A component that "owns" a connection, and should be notified when it dies. */ | |
| 1300 | + interface Owner { | |
| 1301 | + void onConnectionDefunct(Connection connection); | |
| 1302 | + } | |
| 1289 | 1303 | } |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +59 −63
| @@ -31,13 +31,13 @@ | ||
| 31 | 31 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 32 | 32 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 33 | 33 | |
| 34 | -class ControlConnection implements Host.StateListener { | |
| 34 | +class ControlConnection implements Host.StateListener, Connection.Owner { | |
| 35 | 35 | |
| 36 | 36 | private static final Logger logger = LoggerFactory.getLogger(ControlConnection.class); |
| 37 | 37 | |
| 38 | 38 | private static final InetAddress bindAllAddress; |
| 39 | - static | |
| 40 | - { | |
| 39 | + | |
| 40 | + static { | |
| 41 | 41 | try { |
| 42 | 42 | bindAllAddress = InetAddress.getByAddress(new byte[4]); |
| 43 | 43 | } catch (UnknownHostException e) { |
| @@ -68,7 +68,7 @@ | ||
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Only for the initial connection. Does not schedule retries if it fails |
| 71 | - public void connect() throws UnsupportedProtocolVersionException { | |
| 71 | + void connect() throws UnsupportedProtocolVersionException { | |
| 72 | 72 | if (isShutdown) |
| 73 | 73 | return; |
| 74 | 74 | |
| @@ -79,7 +79,7 @@ | ||
| 79 | 79 | setNewConnection(reconnectInternal(hosts.iterator(), true)); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - public CloseFuture closeAsync() { | |
| 82 | + CloseFuture closeAsync() { | |
| 83 | 83 | // We don't have to be fancy here. We just set a flag so that we stop trying to reconnect (and thus change the |
| 84 | 84 | // connection used) and shutdown the current one. |
| 85 | 85 | isShutdown = true; |
| @@ -95,26 +95,13 @@ | ||
| 95 | 95 | |
| 96 | 96 | Host connectedHost() { |
| 97 | 97 | Connection current = connectionRef.get(); |
| 98 | - return cluster.metadata.getHost(current.address); | |
| 98 | + return (current == null) | |
| 99 | + ? null | |
| 100 | + : cluster.metadata.getHost(current.address); | |
| 99 | 101 | } |
| 100 | 102 | |
| 101 | - void reconnect() { | |
| 102 | - if (isShutdown) | |
| 103 | - return; | |
| 104 | - | |
| 105 | - try { | |
| 106 | - setNewConnection(reconnectInternal(queryPlan(), false)); | |
| 107 | - } catch (NoHostAvailableException e) { | |
| 108 | - logger.error("[Control connection] Cannot connect to any host, scheduling retry"); | |
| 109 | - backgroundReconnect(-1); | |
| 110 | - } catch (UnsupportedProtocolVersionException e) { | |
| 111 | - // reconnectInternal only propagate those if we've not decided on the protocol version yet, | |
| 112 | - // which should only happen on the initial connection and thus in connect() but never here. | |
| 113 | - throw new AssertionError(); | |
| 114 | - } catch (Exception e) { | |
| 115 | - logger.error("[Control connection] Unknown error during reconnection, scheduling retry", e); | |
| 116 | - backgroundReconnect(-1); | |
| 117 | - } | |
| 103 | + void triggerReconnect() { | |
| 104 | + backgroundReconnect(0); | |
| 118 | 105 | } |
| 119 | 106 | |
| 120 | 107 | /** |
| @@ -124,7 +111,14 @@ | ||
| 124 | 111 | if (isShutdown) |
| 125 | 112 | return; |
| 126 | 113 | |
| 127 | - new AbstractReconnectionHandler(cluster.reconnectionExecutor, cluster.reconnectionPolicy().newSchedule(), reconnectionAttempt, initialDelayMs) { | |
| 114 | + // Abort if a reconnection is already in progress. This is not thread-safe: two threads might race through this and both | |
| 115 | + // schedule a reconnection; in that case AbstractReconnectionHandler knows how to deal with it correctly. | |
| 116 | + // But this cheap check can help us avoid creating the object unnecessarily. | |
| 117 | + ListenableFuture<?> reconnection = reconnectionAttempt.get(); | |
| 118 | + if (reconnection != null && !reconnection.isDone()) | |
| 119 | + return; | |
| 120 | + | |
| 121 | + new AbstractReconnectionHandler("Control connection", cluster.reconnectionExecutor, cluster.reconnectionPolicy().newSchedule(), reconnectionAttempt, initialDelayMs) { | |
| 128 | 122 | @Override |
| 129 | 123 | protected Connection tryReconnect() throws ConnectionException { |
| 130 | 124 | try { |
| @@ -163,17 +157,17 @@ | ||
| 163 | 157 | |
| 164 | 158 | private void signalError() { |
| 165 | 159 | Connection connection = connectionRef.get(); |
| 166 | - if (connection != null && connection.isDefunct() && cluster.metadata.getHost(connection.address) != null) { | |
| 167 | - // If the connection was marked as defunct and the host hadn't left, this already reported the | |
| 168 | - // host down, which will trigger a reconnect. | |
| 169 | - return; | |
| 170 | - } | |
| 171 | - // If the connection is not defunct, or the host has left, just reconnect manually | |
| 160 | + if (connection != null) | |
| 161 | + connection.closeAsync(); | |
| 162 | + | |
| 163 | + // If the error caused the host to go down, onDown might have already triggered a reconnect. | |
| 164 | + // But backgroundReconnect knows how to deal with that. | |
| 172 | 165 | backgroundReconnect(0); |
| 173 | 166 | } |
| 174 | 167 | |
| 175 | 168 | private void setNewConnection(Connection newConnection) { |
| 176 | - logger.debug("[Control connection] Successfully connected to {}", newConnection.address); | |
| 169 | + Host.statesLogger.debug("[Control connection] established to {}", newConnection.address); | |
| 170 | + newConnection.setOwner(this); | |
| 177 | 171 | Connection old = connectionRef.getAndSet(newConnection); |
| 178 | 172 | if (old != null && !old.isClosed()) |
| 179 | 173 | old.closeAsync(); |
| @@ -187,6 +181,8 @@ | ||
| 187 | 181 | try { |
| 188 | 182 | while (iter.hasNext()) { |
| 189 | 183 | host = iter.next(); |
| 184 | + if (!host.convictionPolicy.canReconnectNow()) | |
| 185 | + continue; | |
| 190 | 186 | try { |
| 191 | 187 | return tryConnect(host, isInitialConnection); |
| 192 | 188 | } catch (ConnectionException e) { |
| @@ -240,8 +236,7 @@ | ||
| 240 | 236 | return errors; |
| 241 | 237 | } |
| 242 | 238 | |
| 243 | - private Connection tryConnect(Host host, boolean isInitialConnection) throws ConnectionException, ExecutionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException | |
| 244 | - { | |
| 239 | + private Connection tryConnect(Host host, boolean isInitialConnection) throws ConnectionException, ExecutionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { | |
| 245 | 240 | Connection connection = cluster.connectionFactory.open(host); |
| 246 | 241 | |
| 247 | 242 | try { |
| @@ -284,7 +279,7 @@ | ||
| 284 | 279 | } |
| 285 | 280 | } |
| 286 | 281 | |
| 287 | - public void refreshSchema(String keyspace, String table) throws InterruptedException { | |
| 282 | + void refreshSchema(String keyspace, String table) throws InterruptedException { | |
| 288 | 283 | if(keyspace == null && table == null) { |
| 289 | 284 | logger.debug("[Control connection] Refreshing schema"); |
| 290 | 285 | } else { |
| @@ -293,7 +288,7 @@ | ||
| 293 | 288 | try { |
| 294 | 289 | Connection c = connectionRef.get(); |
| 295 | 290 | // At startup, when we add the initial nodes, this will be null, which is ok |
| 296 | - if (c == null) | |
| 291 | + if (c == null || c.isClosed()) | |
| 297 | 292 | return; |
| 298 | 293 | refreshSchema(c, keyspace, table, cluster); |
| 299 | 294 | // If the table is null, we either rebuild all from scratch or have an updated keyspace. In both cases, rebuild the token map |
| @@ -325,8 +320,8 @@ | ||
| 325 | 320 | } |
| 326 | 321 | |
| 327 | 322 | DefaultResultSetFuture ksFuture = table == null |
| 328 | - ? new DefaultResultSetFuture(null, new Requests.Query(SELECT_KEYSPACES + whereClause)) | |
| 329 | - : null; | |
| 323 | + ? new DefaultResultSetFuture(null, new Requests.Query(SELECT_KEYSPACES + whereClause)) | |
| 324 | + : null; | |
| 330 | 325 | DefaultResultSetFuture cfFuture = new DefaultResultSetFuture(null, new Requests.Query(SELECT_COLUMN_FAMILIES + whereClause)); |
| 331 | 326 | DefaultResultSetFuture colsFuture = new DefaultResultSetFuture(null, new Requests.Query(SELECT_COLUMNS + whereClause)); |
| 332 | 327 | |
| @@ -343,7 +338,7 @@ | ||
| 343 | 338 | if (host == null || host.getCassandraVersion() == null) { |
| 344 | 339 | cassandraVersion = cluster.protocolVersion() == 1 ? VersionNumber.parse("1.2.0") : VersionNumber.parse("2.0.0"); |
| 345 | 340 | logger.warn("Cannot find Cassandra version for host {} to parse the schema, using {} based on protocol version in use. " |
| 346 | - + "If parsing the schema fails, this could be the cause", connection.address, cassandraVersion); | |
| 341 | + + "If parsing the schema fails, this could be the cause", connection.address, cassandraVersion); | |
| 347 | 342 | } else { |
| 348 | 343 | cassandraVersion = host.getCassandraVersion(); |
| 349 | 344 | } |
| @@ -358,10 +353,10 @@ | ||
| 358 | 353 | } |
| 359 | 354 | } |
| 360 | 355 | |
| 361 | - public void refreshNodeListAndTokenMap() { | |
| 356 | + void refreshNodeListAndTokenMap() { | |
| 362 | 357 | Connection c = connectionRef.get(); |
| 363 | 358 | // At startup, when we add the initial nodes, this will be null, which is ok |
| 364 | - if (c == null) | |
| 359 | + if (c == null || c.isClosed()) | |
| 365 | 360 | return; |
| 366 | 361 | |
| 367 | 362 | try { |
| @@ -427,11 +422,11 @@ | ||
| 427 | 422 | /** |
| 428 | 423 | * @return whether we have enough information to bring the node back up |
| 429 | 424 | */ |
| 430 | - public boolean refreshNodeInfo(Host host) { | |
| 425 | + boolean refreshNodeInfo(Host host) { | |
| 431 | 426 | |
| 432 | 427 | Connection c = connectionRef.get(); |
| 433 | 428 | // At startup, when we add the initial nodes, this will be null, which is ok |
| 434 | - if (c == null) | |
| 429 | + if (c == null || c.isClosed()) | |
| 435 | 430 | return true; |
| 436 | 431 | |
| 437 | 432 | logger.debug("[Control connection] Refreshing node info on {}", host); |
| @@ -489,8 +484,8 @@ | ||
| 489 | 484 | String version = row.getString("release_version"); |
| 490 | 485 | // We don't know if it's a 'local' or a 'peers' row, and only 'peers' rows have the 'peer' field. |
| 491 | 486 | InetAddress listenAddress = row.getColumnDefinitions().contains("peer") |
| 492 | - ? row.getInet("peer") | |
| 493 | - : null; | |
| 487 | + ? row.getInet("peer") | |
| 488 | + : null; | |
| 494 | 489 | |
| 495 | 490 | host.setVersionAndListenAdress(version, listenAddress); |
| 496 | 491 | } |
| @@ -661,18 +656,33 @@ | ||
| 661 | 656 | |
| 662 | 657 | @Override |
| 663 | 658 | public void onDown(Host host) { |
| 664 | - // If that's the host we're connected to, and we haven't yet schedule a reconnection, preemptively start one | |
| 659 | + onHostGone(host); | |
| 660 | + } | |
| 661 | + | |
| 662 | + @Override | |
| 663 | + public void onRemove(Host host) { | |
| 664 | + onHostGone(host); | |
| 665 | + cluster.submitNodeListRefresh(); | |
| 666 | + } | |
| 667 | + | |
| 668 | + private void onHostGone(Host host) { | |
| 665 | 669 | Connection current = connectionRef.get(); |
| 666 | - if (logger.isDebugEnabled()) | |
| 667 | - logger.debug("[Control connection] {} is down, currently connected to {}", host, current == null ? "nobody" : current.address); | |
| 668 | 670 | |
| 669 | 671 | if (current != null && current.address.equals(host.getSocketAddress())) { |
| 670 | - // This starts an AbstractReconnectionHandler, which will take care of checking if another reconnection is | |
| 671 | - // already in progress | |
| 672 | + logger.debug("[Control connection] {} is down/removed and it was the control host, triggering reconnect", | |
| 673 | + current.address); | |
| 674 | + if (!current.isClosed()) | |
| 675 | + current.closeAsync(); | |
| 672 | 676 | backgroundReconnect(0); |
| 673 | 677 | } |
| 674 | 678 | } |
| 675 | 679 | |
| 680 | + @Override | |
| 681 | + public void onConnectionDefunct(Connection connection) { | |
| 682 | + if (connection == connectionRef.get()) | |
| 683 | + backgroundReconnect(0); | |
| 684 | + } | |
| 685 | + | |
| 676 | 686 | @Override |
| 677 | 687 | public void onSuspected(Host host) { |
| 678 | 688 | } |
| @@ -685,18 +695,4 @@ | ||
| 685 | 695 | if (host.getCassandraVersion() == null || tkmap == null || !tkmap.hosts.contains(host)) |
| 686 | 696 | cluster.submitNodeListRefresh(); |
| 687 | 697 | } |
| 688 | - | |
| 689 | - @Override | |
| 690 | - public void onRemove(Host host) { | |
| 691 | - Connection current = connectionRef.get(); | |
| 692 | - if (logger.isDebugEnabled()) | |
| 693 | - logger.debug("[Control connection] {} has been removed, currently connected to {}", host, current == null ? "nobody" : current.address); | |
| 694 | - | |
| 695 | - // Schedule a reconnection if that was our control host | |
| 696 | - if (current != null && current.address.equals(host.getSocketAddress())) { | |
| 697 | - backgroundReconnect(0); | |
| 698 | - } | |
| 699 | - | |
| 700 | - cluster.submitNodeListRefresh(); | |
| 701 | - } | |
| 702 | 698 | } |
modified driver-core/src/main/java/com/datastax/driver/core/ConvictionPolicy.java +95 −17
| @@ -15,6 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 19 | + | |
| 20 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; | |
| 21 | +import static java.util.concurrent.TimeUnit.NANOSECONDS; | |
| 22 | + | |
| 23 | +import com.datastax.driver.core.policies.ReconnectionPolicy; | |
| 24 | + | |
| 18 | 25 | /** |
| 19 | 26 | * The policy with which to decide whether a host should be considered down. |
| 20 | 27 | * |
| @@ -25,24 +32,36 @@ | ||
| 25 | 32 | */ |
| 26 | 33 | abstract class ConvictionPolicy { |
| 27 | 34 | |
| 35 | + /** | |
| 36 | + * Called when a new connection to the host has been successfully opened. | |
| 37 | + */ | |
| 38 | + abstract void signalConnectionCreated(); | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Called when a connection closed normally. | |
| 42 | + */ | |
| 43 | + abstract void signalConnectionClosed(); | |
| 44 | + | |
| 28 | 45 | /** |
| 29 | 46 | * Called when a connection error occurs on a connection to the host this policy applies to. |
| 30 | 47 | * |
| 31 | 48 | * @param exception the connection error that occurred. |
| 32 | - * | |
| 33 | - * @return {@code true} if the host should be considered down. | |
| 49 | + * @param connectionInitialized whether the connection was initialized. | |
| 50 | + * @return whether the host should be considered down. | |
| 34 | 51 | */ |
| 35 | - public abstract boolean addFailure(ConnectionException exception); | |
| 52 | + abstract boolean signalConnectionFailure(ConnectionException exception, boolean connectionInitialized); | |
| 53 | + | |
| 54 | + abstract boolean canReconnectNow(); | |
| 36 | 55 | |
| 37 | 56 | /** |
| 38 | - * Called when the host has been detected up. | |
| 57 | + * Called when the host goes down. | |
| 39 | 58 | */ |
| 40 | - public abstract void reset(); | |
| 59 | + abstract void reset(); | |
| 41 | 60 | |
| 42 | 61 | /** |
| 43 | 62 | * Simple factory interface to allow creating {@link ConvictionPolicy} instances. |
| 44 | 63 | */ |
| 45 | - public interface Factory { | |
| 64 | + interface Factory { | |
| 46 | 65 | |
| 47 | 66 | /** |
| 48 | 67 | * Creates a new ConvictionPolicy instance for {@code host}. |
| @@ -50,30 +69,89 @@ | ||
| 50 | 69 | * @param host the host this policy applies to |
| 51 | 70 | * @return the newly created {@link ConvictionPolicy} instance. |
| 52 | 71 | */ |
| 53 | - public ConvictionPolicy create(Host host); | |
| 72 | + ConvictionPolicy create(Host host, ReconnectionPolicy reconnectionPolicy); | |
| 54 | 73 | } |
| 55 | 74 | |
| 56 | - public static class Simple extends ConvictionPolicy { | |
| 57 | - private Simple(Host host) { | |
| 75 | + static class DefaultConvictionPolicy extends ConvictionPolicy { | |
| 76 | + private final Host host; | |
| 77 | + private final ReconnectionPolicy reconnectionPolicy; | |
| 78 | + private final AtomicInteger openConnections = new AtomicInteger(); | |
| 79 | + | |
| 80 | + private volatile long nextReconnectionTime = Long.MIN_VALUE; | |
| 81 | + private ReconnectionPolicy.ReconnectionSchedule reconnectionSchedule; | |
| 82 | + | |
| 83 | + private DefaultConvictionPolicy(Host host, ReconnectionPolicy reconnectionPolicy) { | |
| 84 | + this.host = host; | |
| 85 | + this.reconnectionPolicy = reconnectionPolicy; | |
| 86 | + } | |
| 87 | + | |
| 88 | + @Override | |
| 89 | + void signalConnectionCreated() { | |
| 90 | + int newCount = openConnections.incrementAndGet(); | |
| 91 | + Host.statesLogger.debug("[{}] new connection created, total = {}", host, newCount); | |
| 92 | + resetReconnectionTime(); | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Override | |
| 96 | + void signalConnectionClosed() { | |
| 97 | + if (host.state == Host.State.DOWN) | |
| 98 | + return; | |
| 99 | + int remaining = openConnections.decrementAndGet(); | |
| 100 | + assert remaining >= 0; | |
| 101 | + Host.statesLogger.debug("[{}] connection closed, remaining = {}", host, remaining); | |
| 58 | 102 | } |
| 59 | 103 | |
| 60 | 104 | @Override |
| 61 | - public boolean addFailure(ConnectionException exception) { | |
| 62 | - return true; | |
| 105 | + boolean signalConnectionFailure(ConnectionException exception, boolean wasFullyInitialized) { | |
| 106 | + if (host.state == Host.State.DOWN) | |
| 107 | + return false; | |
| 108 | + | |
| 109 | + updateReconnectionTime(); | |
| 110 | + int remaining = (wasFullyInitialized) | |
| 111 | + ? openConnections.decrementAndGet() | |
| 112 | + : openConnections.get(); | |
| 113 | + | |
| 114 | + assert remaining >= 0; | |
| 115 | + Host.statesLogger.debug("[{}] remaining connections = {}", host, remaining); | |
| 116 | + return remaining == 0; | |
| 63 | 117 | } |
| 64 | 118 | |
| 65 | - public boolean addFailureFromExternalDetector() { | |
| 66 | - return true; | |
| 119 | + private synchronized void updateReconnectionTime() { | |
| 120 | + long now = System.nanoTime(); | |
| 121 | + if (nextReconnectionTime > now) | |
| 122 | + // Someone else updated the time before us | |
| 123 | + return; | |
| 124 | + | |
| 125 | + if (reconnectionSchedule == null) | |
| 126 | + reconnectionSchedule = reconnectionPolicy.newSchedule(); | |
| 127 | + | |
| 128 | + long nextDelayMs = reconnectionSchedule.nextDelayMs(); | |
| 129 | + Host.statesLogger.debug("[{}] preventing new connections for the next {} ms", host, nextDelayMs); | |
| 130 | + nextReconnectionTime = now + NANOSECONDS.convert(nextDelayMs, MILLISECONDS); | |
| 131 | + } | |
| 132 | + | |
| 133 | + private synchronized void resetReconnectionTime() { | |
| 134 | + reconnectionSchedule = null; | |
| 135 | + nextReconnectionTime = Long.MIN_VALUE; | |
| 67 | 136 | } |
| 68 | 137 | |
| 69 | 138 | @Override |
| 70 | - public void reset() {} | |
| 139 | + boolean canReconnectNow() { | |
| 140 | + return nextReconnectionTime == Long.MIN_VALUE || | |
| 141 | + System.nanoTime() >= nextReconnectionTime; | |
| 142 | + } | |
| 143 | + | |
| 144 | + @Override | |
| 145 | + synchronized void reset() { | |
| 146 | + openConnections.set(0); | |
| 147 | + resetReconnectionTime(); | |
| 148 | + } | |
| 71 | 149 | |
| 72 | - public static class Factory implements ConvictionPolicy.Factory { | |
| 150 | + static class Factory implements ConvictionPolicy.Factory { | |
| 73 | 151 | |
| 74 | 152 | @Override |
| 75 | - public ConvictionPolicy create(Host host) { | |
| 76 | - return new Simple(host); | |
| 153 | + public ConvictionPolicy create(Host host, ReconnectionPolicy reconnectionPolicy) { | |
| 154 | + return new DefaultConvictionPolicy(host, reconnectionPolicy); | |
| 77 | 155 | } |
| 78 | 156 | } |
| 79 | 157 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +7 −31
| @@ -27,8 +27,6 @@ | ||
| 27 | 27 | import org.slf4j.Logger; |
| 28 | 28 | import org.slf4j.LoggerFactory; |
| 29 | 29 | |
| 30 | -import com.datastax.driver.core.utils.MoreFutures; | |
| 31 | - | |
| 32 | 30 | /** |
| 33 | 31 | * A Cassandra node. |
| 34 | 32 | * |
| @@ -38,6 +36,7 @@ | ||
| 38 | 36 | |
| 39 | 37 | private static final Logger logger = LoggerFactory.getLogger(Host.class); |
| 40 | 38 | |
| 39 | + static final Logger statesLogger = LoggerFactory.getLogger(Host.class.getName() + ".STATES"); | |
| 41 | 40 | |
| 42 | 41 | private final InetSocketAddress address; |
| 43 | 42 | |
| @@ -46,7 +45,7 @@ | ||
| 46 | 45 | /** Ensures state change notifications for that host are handled serially */ |
| 47 | 46 | final ReentrantLock notificationsLock = new ReentrantLock(true); |
| 48 | 47 | |
| 49 | - private final ConvictionPolicy policy; | |
| 48 | + final ConvictionPolicy convictionPolicy; | |
| 50 | 49 | private final Cluster.Manager manager; |
| 51 | 50 | |
| 52 | 51 | // Tracks later reconnection attempts to that host so we avoid adding multiple tasks. |
| @@ -69,12 +68,12 @@ | ||
| 69 | 68 | // ClusterMetadata keeps one Host object per inet address and we rely on this (more precisely, |
| 70 | 69 | // we rely on the fact that we can use Object equality as a valid equality), so don't use |
| 71 | 70 | // that constructor but ClusterMetadata.getHost instead. |
| 72 | - Host(InetSocketAddress address, ConvictionPolicy.Factory policy, Cluster.Manager manager) { | |
| 73 | - if (address == null || policy == null) | |
| 71 | + Host(InetSocketAddress address, ConvictionPolicy.Factory convictionPolicyFactory, Cluster.Manager manager) { | |
| 72 | + if (address == null || convictionPolicyFactory == null) | |
| 74 | 73 | throw new NullPointerException(); |
| 75 | 74 | |
| 76 | 75 | this.address = address; |
| 77 | - this.policy = policy.create(this); | |
| 76 | + this.convictionPolicy = convictionPolicyFactory.create(this, manager.reconnectionPolicy()); | |
| 78 | 77 | this.manager = manager; |
| 79 | 78 | this.defaultExecutionInfo = new ExecutionInfo(ImmutableList.of(this)); |
| 80 | 79 | this.state = State.ADDED; |
| @@ -276,17 +275,13 @@ | ||
| 276 | 275 | |
| 277 | 276 | void setDown() { |
| 278 | 277 | state = State.DOWN; |
| 278 | + convictionPolicy.reset(); | |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | void setUp() { |
| 282 | - policy.reset(); | |
| 283 | 282 | state = State.UP; |
| 284 | 283 | } |
| 285 | 284 | |
| 286 | - boolean signalConnectionFailure(ConnectionException exception) { | |
| 287 | - return policy.addFailure(exception); | |
| 288 | - } | |
| 289 | - | |
| 290 | 285 | /** |
| 291 | 286 | * Interface for listeners that are interested in hosts added, up, down and |
| 292 | 287 | * removed events. |
| @@ -314,26 +309,7 @@ | ||
| 314 | 309 | public void onUp(Host host); |
| 315 | 310 | |
| 316 | 311 | /** |
| 317 | - * Called when a node is suspected to be dead. | |
| 318 | - * <p> | |
| 319 | - * A node is suspected to be dead when an error occurs on one of it's | |
| 320 | - * opened connection. As soon as an host is suspected, a connection attempt | |
| 321 | - * to that host is immediately tried. If this succeed, then it means that | |
| 322 | - * the connection was disfunctional but that the node was not really down. | |
| 323 | - * If this fails however, this means the node is truly dead, onDown() is | |
| 324 | - * called and further reconnection attempts are scheduled according to the | |
| 325 | - * {@link ReconnectionPolicy} in place. | |
| 326 | - * <p> | |
| 327 | - * When this event is triggered, it is possible to call the host | |
| 328 | - * {@link getInitialReconnectionAttemptFuture} method to wait until the | |
| 329 | - * initial and immediate reconnection attempt succeed or fail. | |
| 330 | - * <p> | |
| 331 | - * Note that some StateListener may ignore that event. If a node that | |
| 332 | - * that is suspected down turns out to be truly down (that is, the driver | |
| 333 | - * cannot successfully connect to it right away), then {@link onDown} will | |
| 334 | - * be called. | |
| 335 | - * | |
| 336 | - * @deprecated the suspicion mechanism has been disabled. This will never | |
| 312 | + * @deprecated the "suspicion" mechanism has been deprecated. This will never | |
| 337 | 313 | * get called. |
| 338 | 314 | */ |
| 339 | 315 | @Deprecated |
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +64 −39
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.util.ArrayList; |
| 19 | 19 | import java.util.List; |
| 20 | +import java.util.ListIterator; | |
| 20 | 21 | import java.util.Set; |
| 21 | 22 | import java.util.concurrent.*; |
| 22 | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| @@ -26,7 +27,10 @@ | ||
| 26 | 27 | import java.util.concurrent.locks.ReentrantLock; |
| 27 | 28 | |
| 28 | 29 | import com.google.common.collect.Lists; |
| 29 | -import com.google.common.util.concurrent.*; | |
| 30 | +import com.google.common.util.concurrent.Futures; | |
| 31 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 32 | +import com.google.common.util.concurrent.MoreExecutors; | |
| 33 | +import com.google.common.util.concurrent.SettableFuture; | |
| 30 | 34 | import org.slf4j.Logger; |
| 31 | 35 | import org.slf4j.LoggerFactory; |
| 32 | 36 | |
| @@ -38,7 +42,7 @@ | ||
| 38 | 42 | import static com.datastax.driver.core.Connection.State.RESURRECTING; |
| 39 | 43 | import static com.datastax.driver.core.Connection.State.TRASHED; |
| 40 | 44 | |
| 41 | -class HostConnectionPool { | |
| 45 | +class HostConnectionPool implements Connection.Owner { | |
| 42 | 46 | |
| 43 | 47 | private static final Logger logger = LoggerFactory.getLogger(HostConnectionPool.class); |
| 44 | 48 | |
| @@ -99,14 +103,14 @@ | ||
| 99 | 103 | */ |
| 100 | 104 | public ListenableFuture<Void> initAsync(Connection reusedConnection) { |
| 101 | 105 | // Create initial core connections |
| 102 | - int capacity = options().getCoreConnectionsPerHost(hostDistance); | |
| 103 | - final List<Connection> connections = Lists.newArrayListWithCapacity(capacity); | |
| 104 | - final List<ListenableFuture<Void>> connectionFutures = Lists.newArrayListWithCapacity(capacity); | |
| 105 | - for (int i = 0; i < capacity; i++) { | |
| 106 | + final int coreSize = options().getCoreConnectionsPerHost(hostDistance); | |
| 107 | + final List<Connection> connections = Lists.newArrayListWithCapacity(coreSize); | |
| 108 | + final List<ListenableFuture<Void>> connectionFutures = Lists.newArrayListWithCapacity(coreSize); | |
| 109 | + for (int i = 0; i < coreSize; i++) { | |
| 106 | 110 | Connection connection; |
| 107 | 111 | ListenableFuture<Void> connectionFuture; |
| 108 | 112 | // reuse the existing connection only once |
| 109 | - if (reusedConnection != null && reusedConnection.setPool(this)) { | |
| 113 | + if (reusedConnection != null && reusedConnection.setOwner(this)) { | |
| 110 | 114 | connection = reusedConnection; |
| 111 | 115 | connectionFuture = MoreFutures.VOID_SUCCESS; |
| 112 | 116 | } else { |
| @@ -120,31 +124,33 @@ | ||
| 120 | 124 | |
| 121 | 125 | Executor initExecutor = manager.cluster.manager.configuration.getPoolingOptions().getInitializationExecutor(); |
| 122 | 126 | |
| 123 | - ListenableFuture<List<Void>> allConnectionsFuture = Futures.allAsList(connectionFutures); | |
| 127 | + ListenableFuture<List<Void>> allConnectionsFuture = Futures.successfulAsList(connectionFutures); | |
| 124 | 128 | |
| 125 | 129 | final SettableFuture<Void> initFuture = SettableFuture.create(); |
| 126 | - Futures.addCallback(allConnectionsFuture, new FutureCallback<List<Void>>() { | |
| 130 | + Futures.addCallback(allConnectionsFuture, new MoreFutures.SuccessCallback<List<Void>>() { | |
| 127 | 131 | @Override |
| 128 | 132 | public void onSuccess(List<Void> l) { |
| 133 | + // Some of the connections might have failed, keep only the successful ones | |
| 134 | + ListIterator<Connection> it = connections.listIterator(); | |
| 135 | + while (it.hasNext()) { | |
| 136 | + if (it.next().isClosed()) | |
| 137 | + it.remove(); | |
| 138 | + } | |
| 139 | + | |
| 129 | 140 | HostConnectionPool.this.connections.addAll(connections); |
| 130 | - open.set(l.size()); | |
| 141 | + open.set(connections.size()); | |
| 142 | + | |
| 131 | 143 | if (isClosed()) { |
| 132 | 144 | initFuture.setException(new ConnectionException(host.getSocketAddress(), "Pool was closed during initialization")); |
| 133 | 145 | // we're not sure if closeAsync() saw the connections, so ensure they get closed |
| 134 | 146 | forceClose(connections); |
| 135 | 147 | } else { |
| 136 | - logger.trace("Created connection pool to host {}", host); | |
| 148 | + logger.debug("Created connection pool to host {} ({} connections needed, {} successfully opened)", | |
| 149 | + host, coreSize, connections.size()); | |
| 137 | 150 | phase.compareAndSet(Phase.INITIALIZING, Phase.READY); |
| 138 | 151 | initFuture.set(null); |
| 139 | 152 | } |
| 140 | 153 | } |
| 141 | - | |
| 142 | - @Override | |
| 143 | - public void onFailure(Throwable t) { | |
| 144 | - phase.compareAndSet(Phase.INITIALIZING, Phase.INIT_FAILED); | |
| 145 | - forceClose(connections); | |
| 146 | - initFuture.setException(t); | |
| 147 | - } | |
| 148 | 154 | }, initExecutor); |
| 149 | 155 | return initFuture; |
| 150 | 156 | } |
| @@ -168,16 +174,25 @@ | ||
| 168 | 174 | throw new ConnectionException(host.getSocketAddress(), "Pool is " + phase); |
| 169 | 175 | |
| 170 | 176 | if (connections.isEmpty()) { |
| 171 | - for (int i = 0; i < options().getCoreConnectionsPerHost(hostDistance); i++) { | |
| 172 | - // We don't respect MAX_SIMULTANEOUS_CREATION here because it's only to | |
| 173 | - // protect against creating connection in excess of core too quickly | |
| 174 | - scheduledForCreation.incrementAndGet(); | |
| 175 | - manager.blockingExecutor().submit(newConnectionTask); | |
| 177 | + if (!host.convictionPolicy.canReconnectNow()) | |
| 178 | + throw new TimeoutException("Connection pool is empty, currently trying to reestablish connections"); | |
| 179 | + else { | |
| 180 | + int coreSize = options().getCoreConnectionsPerHost(hostDistance); | |
| 181 | + if (coreSize == 0) { | |
| 182 | + maybeSpawnNewConnection(); | |
| 183 | + } else { | |
| 184 | + for (int i = 0; i < coreSize; i++) { | |
| 185 | + // We don't respect MAX_SIMULTANEOUS_CREATION here because it's only to | |
| 186 | + // protect against creating connection in excess of core too quickly | |
| 187 | + scheduledForCreation.incrementAndGet(); | |
| 188 | + manager.blockingExecutor().submit(newConnectionTask); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + Connection c = waitForConnection(timeout, unit); | |
| 192 | + totalInFlight.incrementAndGet(); | |
| 193 | + c.setKeyspace(manager.poolsState.keyspace); | |
| 194 | + return c; | |
| 176 | 195 | } |
| 177 | - Connection c = waitForConnection(timeout, unit); | |
| 178 | - totalInFlight.incrementAndGet(); | |
| 179 | - c.setKeyspace(manager.poolsState.keyspace); | |
| 180 | - return c; | |
| 181 | 196 | } |
| 182 | 197 | |
| 183 | 198 | int minInFlight = Integer.MAX_VALUE; |
| @@ -221,7 +236,9 @@ | ||
| 221 | 236 | } |
| 222 | 237 | |
| 223 | 238 | int connectionCount = open.get() + scheduledForCreation.get(); |
| 224 | - if (connectionCount < options().getMaxConnectionsPerHost(hostDistance)) { | |
| 239 | + if (connectionCount < options().getCoreConnectionsPerHost(hostDistance)) { | |
| 240 | + maybeSpawnNewConnection(); | |
| 241 | + } else if (connectionCount < options().getMaxConnectionsPerHost(hostDistance)) { | |
| 225 | 242 | // Add a connection if we fill the first n-1 connections and almost fill the last one |
| 226 | 243 | int currentCapacity = (connectionCount - 1) * StreamIdGenerator.MAX_STREAM_PER_CONNECTION |
| 227 | 244 | + options().getMaxSimultaneousRequestsPerConnectionThreshold(hostDistance); |
| @@ -272,7 +289,7 @@ | ||
| 272 | 289 | |
| 273 | 290 | private Connection waitForConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { |
| 274 | 291 | if (timeout == 0) |
| 275 | - throw new TimeoutException(); | |
| 292 | + throw new TimeoutException("All connections are busy and pool timeout is 0"); | |
| 276 | 293 | |
| 277 | 294 | long start = System.nanoTime(); |
| 278 | 295 | long remaining = timeout; |
| @@ -315,7 +332,7 @@ | ||
| 315 | 332 | remaining = timeout - Cluster.timeSince(start, unit); |
| 316 | 333 | } while (remaining > 0); |
| 317 | 334 | |
| 318 | - throw new TimeoutException(); | |
| 335 | + throw new TimeoutException("All connections are busy"); | |
| 319 | 336 | } |
| 320 | 337 | |
| 321 | 338 | public void returnConnection(Connection connection) { |
| @@ -400,6 +417,10 @@ | ||
| 400 | 417 | try { |
| 401 | 418 | Connection newConnection = tryResurrectFromTrash(); |
| 402 | 419 | if (newConnection == null) { |
| 420 | + if (!host.convictionPolicy.canReconnectNow()) { | |
| 421 | + open.decrementAndGet(); | |
| 422 | + return false; | |
| 423 | + } | |
| 403 | 424 | logger.debug("Creating new connection on busy pool to {}", host); |
| 404 | 425 | newConnection = manager.connectionFactory().open(this); |
| 405 | 426 | } |
| @@ -464,6 +485,9 @@ | ||
| 464 | 485 | } |
| 465 | 486 | |
| 466 | 487 | private void maybeSpawnNewConnection() { |
| 488 | + if (!host.convictionPolicy.canReconnectNow()) | |
| 489 | + return; | |
| 490 | + | |
| 467 | 491 | while (true) { |
| 468 | 492 | int inCreation = scheduledForCreation.get(); |
| 469 | 493 | if (inCreation >= MAX_SIMULTANEOUS_CREATION) |
| @@ -475,17 +499,15 @@ | ||
| 475 | 499 | manager.blockingExecutor().submit(newConnectionTask); |
| 476 | 500 | } |
| 477 | 501 | |
| 478 | - void replaceDefunctConnection(final Connection connection) { | |
| 502 | + @Override | |
| 503 | + public void onConnectionDefunct(final Connection connection) { | |
| 479 | 504 | if (connection.state.compareAndSet(OPEN, GONE)) |
| 480 | 505 | open.decrementAndGet(); |
| 481 | - if (connections.remove(connection)) | |
| 482 | - manager.blockingExecutor().submit(new Runnable() { | |
| 483 | - @Override | |
| 484 | - public void run() { | |
| 485 | - addConnectionIfUnderMaximum(); | |
| 486 | - } | |
| 487 | - }); | |
| 488 | - connection.closeAsync(); | |
| 506 | + connections.remove(connection); | |
| 507 | + | |
| 508 | + // Don't try to replace the connection now. Connection.defunct already signaled the failure, | |
| 509 | + // and either the host will be marked DOWN (which destroys all pools), or we want to prevent | |
| 510 | + // new connections for some time | |
| 489 | 511 | } |
| 490 | 512 | |
| 491 | 513 | void cleanupIdleConnections(long now) { |
| @@ -603,6 +625,9 @@ | ||
| 603 | 625 | if (isClosed()) |
| 604 | 626 | return; |
| 605 | 627 | |
| 628 | + if (!host.convictionPolicy.canReconnectNow()) | |
| 629 | + return; | |
| 630 | + | |
| 606 | 631 | // Note: this process is a bit racy, but it doesn't matter since we're still guaranteed to not create |
| 607 | 632 | // more connection than maximum (and if we create more than core connection due to a race but this isn't |
| 608 | 633 | // justified by the load, the connection in excess will be quickly trashed anyway) |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −1
| @@ -313,7 +313,7 @@ | ||
| 313 | 313 | return false; |
| 314 | 314 | } catch (TimeoutException e) { |
| 315 | 315 | // We timeout, log it but move to the next node. |
| 316 | - logError(host.getSocketAddress(), new DriverException("Timeout while trying to acquire available connection (you may want to increase the driver number of per-host connections)")); | |
| 316 | + logError(host.getSocketAddress(), new DriverException("Timeout while trying to acquire available connection (you may want to increase the driver number of per-host connections)", e)); | |
| 317 | 317 | return false; |
| 318 | 318 | } catch (RuntimeException e) { |
| 319 | 319 | if (connection != null) |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +2 −2
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | schedule = new MockReconnectionSchedule(); |
| 53 | 53 | work = new MockReconnectionWork(); |
| 54 | 54 | future.set(null); |
| 55 | - handler = new AbstractReconnectionHandler(executor, schedule, future) { | |
| 55 | + handler = new AbstractReconnectionHandler("test", executor, schedule, future) { | |
| 56 | 56 | @Override |
| 57 | 57 | protected Connection tryReconnect() throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { |
| 58 | 58 | return work.tryReconnect(); |
| @@ -330,7 +330,7 @@ | ||
| 330 | 330 | private void waitForCompletion() { |
| 331 | 331 | executor.shutdown(); |
| 332 | 332 | try { |
| 333 | - boolean shutdown = executor.awaitTermination(10, TimeUnit.SECONDS); | |
| 333 | + boolean shutdown = executor.awaitTermination(30, TimeUnit.SECONDS); | |
| 334 | 334 | if (!shutdown) |
| 335 | 335 | fail("executor ran for longer than expected"); |
| 336 | 336 | } catch (InterruptedException e) { |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReplicationStrategyTest.java +11 −6
| @@ -25,8 +25,13 @@ | ||
| 25 | 25 | |
| 26 | 26 | import com.google.common.collect.ImmutableList; |
| 27 | 27 | import com.google.common.collect.ImmutableList.Builder; |
| 28 | +import org.mockito.Mockito; | |
| 29 | + | |
| 30 | +import static org.mockito.Mockito.mock; | |
| 28 | 31 | import static org.testng.Assert.*; |
| 29 | 32 | |
| 33 | +import com.datastax.driver.core.policies.ExponentialReconnectionPolicy; | |
| 34 | + | |
| 30 | 35 | /** |
| 31 | 36 | * Base class for replication strategy tests. Currently only supports testing |
| 32 | 37 | * using the default Murmur3Partitioner. |
| @@ -38,13 +43,13 @@ | ||
| 38 | 43 | protected static class HostMock extends Host { |
| 39 | 44 | private final String address; |
| 40 | 45 | |
| 41 | - private HostMock(String address) throws UnknownHostException { | |
| 42 | - super(new InetSocketAddress(InetAddress.getByName(address), 9042), new ConvictionPolicy.Simple.Factory(), null); | |
| 46 | + private HostMock(String address, Cluster.Manager manager) throws UnknownHostException { | |
| 47 | + super(new InetSocketAddress(InetAddress.getByName(address), 9042), new ConvictionPolicy.DefaultConvictionPolicy.Factory(), manager); | |
| 43 | 48 | this.address = address; |
| 44 | 49 | } |
| 45 | 50 | |
| 46 | - private HostMock(String address, String dc, String rack) throws UnknownHostException { | |
| 47 | - this(address); | |
| 51 | + private HostMock(String address, String dc, String rack, Cluster.Manager manager) throws UnknownHostException { | |
| 52 | + this(address, manager); | |
| 48 | 53 | this.setLocationInfo(dc, rack); |
| 49 | 54 | } |
| 50 | 55 | |
| @@ -81,7 +86,7 @@ | ||
| 81 | 86 | */ |
| 82 | 87 | protected static HostMock host(String address) { |
| 83 | 88 | try { |
| 84 | - return new HostMock(address); | |
| 89 | + return new HostMock(address, mock(Cluster.Manager.class)); | |
| 85 | 90 | } catch (UnknownHostException ex) { |
| 86 | 91 | throw new RuntimeException(ex); //wrap to avoid declarations |
| 87 | 92 | } |
| @@ -93,7 +98,7 @@ | ||
| 93 | 98 | */ |
| 94 | 99 | protected static HostMock host(String address, String dc, String rack) { |
| 95 | 100 | try { |
| 96 | - return new HostMock(address, dc, rack); | |
| 101 | + return new HostMock(address, dc, rack, mock(Cluster.Manager.class)); | |
| 97 | 102 | } catch (UnknownHostException ex) { |
| 98 | 103 | throw new RuntimeException(ex); //wrap to avoid declarations |
| 99 | 104 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +3 −2
| @@ -22,14 +22,14 @@ | ||
| 22 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 23 | 23 | |
| 24 | 24 | import com.google.common.base.Function; |
| 25 | -import com.google.common.collect.Collections2; | |
| 26 | 25 | import com.google.common.collect.HashMultiset; |
| 27 | 26 | import com.google.common.collect.Maps; |
| 28 | 27 | import org.slf4j.Logger; |
| 29 | 28 | import org.slf4j.LoggerFactory; |
| 30 | 29 | import org.testng.annotations.Test; |
| 31 | 30 | |
| 32 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 31 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 32 | +import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; | |
| 33 | 33 | |
| 34 | 34 | import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy; |
| 35 | 35 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| @@ -111,6 +111,7 @@ | ||
| 111 | 111 | |
| 112 | 112 | cluster = Cluster.builder() |
| 113 | 113 | .addContactPoint(CCMBridge.ipOfNode(1)) |
| 114 | + .withQueryOptions(nonDebouncingQueryOptions()) | |
| 114 | 115 | .build(); |
| 115 | 116 | cluster.init(); |
| 116 | 117 |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +0 −41
| @@ -299,47 +299,6 @@ | ||
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | - private void reprepareOnNewlyUpNodeTest(String ks, Session session) throws Exception { | |
| 303 | - | |
| 304 | - ks = ks == null ? "" : ks + '.'; | |
| 305 | - | |
| 306 | - session.execute("INSERT INTO " + ks + "test (k, i) VALUES ('123', 17)"); | |
| 307 | - session.execute("INSERT INTO " + ks + "test (k, i) VALUES ('124', 18)"); | |
| 308 | - | |
| 309 | - PreparedStatement ps = session.prepare("SELECT * FROM " + ks + "test WHERE k = ?"); | |
| 310 | - | |
| 311 | - assertEquals(session.execute(ps.bind("123")).one().getInt("i"), 17); | |
| 312 | - | |
| 313 | - ccmBridge.stop(); | |
| 314 | - waitForDown(CCMBridge.IP_PREFIX + '1', cluster); | |
| 315 | - | |
| 316 | - ccmBridge.start(); | |
| 317 | - waitFor(CCMBridge.IP_PREFIX + '1', cluster, 120); | |
| 318 | - | |
| 319 | - try | |
| 320 | - { | |
| 321 | - assertEquals(session.execute(ps.bind("124")).one().getInt("i"), 18); | |
| 322 | - } | |
| 323 | - catch (NoHostAvailableException e) | |
| 324 | - { | |
| 325 | - System.out.println(">> " + e.getErrors()); | |
| 326 | - throw e; | |
| 327 | - } | |
| 328 | - } | |
| 329 | - | |
| 330 | - @Test(groups = "long", priority = Integer.MAX_VALUE) | |
| 331 | - public void reprepareOnNewlyUpNodeTest() throws Exception { | |
| 332 | - reprepareOnNewlyUpNodeTest(null, session); | |
| 333 | - } | |
| 334 | - | |
| 335 | - @Test(groups = "long", priority = Integer.MAX_VALUE) | |
| 336 | - public void reprepareOnNewlyUpNodeNoKeyspaceTest() throws Exception { | |
| 337 | - | |
| 338 | - // This is the same test than reprepareOnNewlyUpNodeTest, except that the | |
| 339 | - // prepared statement is prepared while no current keyspace is used | |
| 340 | - reprepareOnNewlyUpNodeTest(keyspace, cluster.connect()); | |
| 341 | - } | |
| 342 | - | |
| 343 | 302 | @Test(groups = "short") |
| 344 | 303 | public void prepareWithNullValuesTest() throws Exception { |
| 345 | 304 |
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +11 −2
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import org.slf4j.Logger; |
| 21 | 21 | import org.slf4j.LoggerFactory; |
| 22 | 22 | import org.testng.annotations.AfterMethod; |
| 23 | +import org.testng.annotations.BeforeMethod; | |
| 23 | 24 | import org.testng.annotations.Test; |
| 24 | 25 | |
| 25 | 26 | |
| @@ -143,6 +144,14 @@ | ||
| 143 | 144 | .isNotReconnectingFromDown(); |
| 144 | 145 | } |
| 145 | 146 | |
| 147 | + @BeforeMethod(groups = "long") | |
| 148 | + public void clearFields() { | |
| 149 | + // Clear cluster and ccm instances between tests. | |
| 150 | + mainCluster = null; | |
| 151 | + mainCcm = null; | |
| 152 | + otherCcm = null; | |
| 153 | + } | |
| 154 | + | |
| 146 | 155 | @AfterMethod(groups = "long") |
| 147 | 156 | public void teardown() { |
| 148 | 157 | if (mainCluster != null) |
| @@ -164,8 +173,8 @@ | ||
| 164 | 173 | } |
| 165 | 174 | |
| 166 | 175 | private static void waitForCountUpHosts(Cluster cluster, int expectedCount) throws InterruptedException { |
| 167 | - int maxRetries = 30; | |
| 168 | - int interval = 10; | |
| 176 | + int maxRetries = 10; | |
| 177 | + int interval = 6; | |
| 169 | 178 | |
| 170 | 179 | for (int i = 0; i <= maxRetries; i++) { |
| 171 | 180 | int actualCount = countUpHosts(cluster); |