kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f69df2b52f63f2b4dcbcb4b3039f6f929f8f8cd855dc2536c8606c8f13d101ee |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-12-19 16:15 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-12-19 16:51 +01:00 |
| Parents | 3cb9a3ede246 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -19,6 +19,8 @@ | ||
| 19 | 19 | - temporarily revert "Add idle timeout to the connection pool" (JAVA-419) |
| 20 | 20 | - [bug] Ensure updateCreatedPools does not add pools for suspected hosts |
| 21 | 21 | (JAVA-593) |
| 22 | +- [bug] Ensure state change notifications for a given host are handled serially | |
| 23 | + (JAVA-594) | |
| 22 | 24 | |
| 23 | 25 | |
| 24 | 26 | 2.0.8: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +233 −181
| @@ -26,7 +26,6 @@ | ||
| 26 | 26 | import java.util.concurrent.atomic.AtomicReference; |
| 27 | 27 | |
| 28 | 28 | import com.google.common.annotations.VisibleForTesting; |
| 29 | -import com.google.common.base.Function; | |
| 30 | 29 | import com.google.common.base.Predicates; |
| 31 | 30 | import com.google.common.collect.*; |
| 32 | 31 | import com.google.common.util.concurrent.*; |
| @@ -1374,83 +1373,94 @@ | ||
| 1374 | 1373 | if (isClosed()) |
| 1375 | 1374 | return; |
| 1376 | 1375 | |
| 1377 | - // We don't want to use the public Host.isUp() as this would make us skip the rest for suspected hosts | |
| 1378 | - if (host.state == Host.State.UP) | |
| 1379 | - return; | |
| 1380 | - | |
| 1381 | 1376 | if (connectionFactory.protocolVersion == 2 && !supportsProtocolV2(host)) { |
| 1382 | 1377 | logUnsupportedVersionProtocol(host); |
| 1383 | 1378 | return; |
| 1384 | 1379 | } |
| 1385 | 1380 | |
| 1386 | - // If there is a reconnection attempt scheduled for that node, cancel it | |
| 1387 | - Future<?> scheduledAttempt = host.reconnectionAttempt.getAndSet(null); | |
| 1388 | - if (scheduledAttempt != null) { | |
| 1389 | - logger.debug("Cancelling reconnection attempt since node is UP"); | |
| 1390 | - scheduledAttempt.cancel(false); | |
| 1391 | - } | |
| 1392 | - | |
| 1393 | 1381 | try { |
| 1394 | - prepareAllQueries(host); | |
| 1395 | - } catch (InterruptedException e) { | |
| 1396 | - Thread.currentThread().interrupt(); | |
| 1397 | - // Don't propagate because we don't want to prevent other listener to run | |
| 1398 | - } catch (UnsupportedProtocolVersionException e) { | |
| 1399 | - logUnsupportedVersionProtocol(host); | |
| 1400 | - return; | |
| 1401 | - } catch (ClusterNameMismatchException e) { | |
| 1402 | - logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName); | |
| 1403 | - return; | |
| 1404 | - } | |
| 1405 | - | |
| 1406 | - // Session#onUp() expects the load balancing policy to have been updated first, so that | |
| 1407 | - // Host distances are up to date. This mean the policy could return the node before the | |
| 1408 | - // new pool have been created. This is harmless if there is no prior pool since RequestHandler | |
| 1409 | - // will ignore the node, but we do want to make sure there is no prior pool so we don't | |
| 1410 | - // query from a pool we will shutdown right away. | |
| 1411 | - for (SessionManager s : sessions) | |
| 1412 | - s.removePool(host); | |
| 1413 | - loadBalancingPolicy().onUp(host); | |
| 1414 | - controlConnection.onUp(host); | |
| 1415 | - | |
| 1416 | - logger.trace("Adding/renewing host pools for newly UP host {}", host); | |
| 1417 | - | |
| 1418 | - List<ListenableFuture<Boolean>> futures = new ArrayList<ListenableFuture<Boolean>>(sessions.size()); | |
| 1419 | - for (SessionManager s : sessions) | |
| 1420 | - futures.add(s.forceRenewPool(host, poolCreationExecutor)); | |
| 1421 | - | |
| 1422 | - // Only mark the node up once all session have re-added their pool (if the load-balancing | |
| 1423 | - // policy says it should), so that Host.isUp() don't return true before we're reconnected | |
| 1424 | - // to the node. | |
| 1425 | - ListenableFuture<List<Boolean>> f = Futures.allAsList(futures); | |
| 1426 | - Futures.addCallback(f, new FutureCallback<List<Boolean>>() { | |
| 1427 | - public void onSuccess(List<Boolean> poolCreationResults) { | |
| 1428 | - // If any of the creation failed, they will have signaled a connection failure | |
| 1429 | - // which will trigger a reconnection to the node. So don't bother marking UP. | |
| 1430 | - if (Iterables.any(poolCreationResults, Predicates.equalTo(false))) { | |
| 1431 | - logger.debug("Connection pool cannot be created, not marking {} UP", host); | |
| 1432 | - return; | |
| 1433 | - } | |
| 1382 | + boolean locked = host.notificationsLock.tryLock(10, TimeUnit.SECONDS); | |
| 1383 | + if (!locked) { | |
| 1384 | + logger.warn("Could not acquire notifications lock within 10 seconds, ignoring UP notification for {}", host); | |
| 1385 | + return; | |
| 1386 | + } | |
| 1434 | 1387 | |
| 1435 | - host.setUp(); | |
| 1388 | + // We don't want to use the public Host.isUp() as this would make us skip the rest for suspected hosts | |
| 1389 | + if (host.state == Host.State.UP) | |
| 1390 | + return; | |
| 1436 | 1391 | |
| 1437 | - for (Host.StateListener listener : listeners) | |
| 1438 | - listener.onUp(host); | |
| 1392 | + // If there is a reconnection attempt scheduled for that node, cancel it | |
| 1393 | + Future<?> scheduledAttempt = host.reconnectionAttempt.getAndSet(null); | |
| 1394 | + if (scheduledAttempt != null) { | |
| 1395 | + logger.debug("Cancelling reconnection attempt since node is UP"); | |
| 1396 | + scheduledAttempt.cancel(false); | |
| 1439 | 1397 | } |
| 1440 | 1398 | |
| 1441 | - public void onFailure(Throwable t) { | |
| 1442 | - // That future is not really supposed to throw unexpected exceptions | |
| 1443 | - if (!(t instanceof InterruptedException)) | |
| 1444 | - logger.error("Unexpected error while marking node UP: while this shouldn't happen, this shouldn't be critical", t); | |
| 1399 | + try { | |
| 1400 | + prepareAllQueries(host); | |
| 1401 | + } catch (InterruptedException e) { | |
| 1402 | + Thread.currentThread().interrupt(); | |
| 1403 | + // Don't propagate because we don't want to prevent other listener to run | |
| 1404 | + } catch (UnsupportedProtocolVersionException e) { | |
| 1405 | + logUnsupportedVersionProtocol(host); | |
| 1406 | + return; | |
| 1407 | + } catch (ClusterNameMismatchException e) { | |
| 1408 | + logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName); | |
| 1409 | + return; | |
| 1445 | 1410 | } |
| 1446 | - }); | |
| 1447 | 1411 | |
| 1448 | - f.get(); | |
| 1412 | + // Session#onUp() expects the load balancing policy to have been updated first, so that | |
| 1413 | + // Host distances are up to date. This mean the policy could return the node before the | |
| 1414 | + // new pool have been created. This is harmless if there is no prior pool since RequestHandler | |
| 1415 | + // will ignore the node, but we do want to make sure there is no prior pool so we don't | |
| 1416 | + // query from a pool we will shutdown right away. | |
| 1417 | + for (SessionManager s : sessions) | |
| 1418 | + s.removePool(host); | |
| 1419 | + loadBalancingPolicy().onUp(host); | |
| 1420 | + controlConnection.onUp(host); | |
| 1421 | + | |
| 1422 | + logger.trace("Adding/renewing host pools for newly UP host {}", host); | |
| 1423 | + | |
| 1424 | + List<ListenableFuture<Boolean>> futures = new ArrayList<ListenableFuture<Boolean>>(sessions.size()); | |
| 1425 | + for (SessionManager s : sessions) | |
| 1426 | + futures.add(s.forceRenewPool(host, poolCreationExecutor)); | |
| 1427 | + | |
| 1428 | + // Only mark the node up once all session have re-added their pool (if the load-balancing | |
| 1429 | + // policy says it should), so that Host.isUp() don't return true before we're reconnected | |
| 1430 | + // to the node. | |
| 1431 | + ListenableFuture<List<Boolean>> f = Futures.allAsList(futures); | |
| 1432 | + Futures.addCallback(f, new FutureCallback<List<Boolean>>() { | |
| 1433 | + public void onSuccess(List<Boolean> poolCreationResults) { | |
| 1434 | + // If any of the creation failed, they will have signaled a connection failure | |
| 1435 | + // which will trigger a reconnection to the node. So don't bother marking UP. | |
| 1436 | + if (Iterables.any(poolCreationResults, Predicates.equalTo(false))) { | |
| 1437 | + logger.debug("Connection pool cannot be created, not marking {} UP", host); | |
| 1438 | + return; | |
| 1439 | + } | |
| 1440 | + | |
| 1441 | + host.setUp(); | |
| 1442 | + | |
| 1443 | + for (Host.StateListener listener : listeners) | |
| 1444 | + listener.onUp(host); | |
| 1445 | + } | |
| 1446 | + | |
| 1447 | + public void onFailure(Throwable t) { | |
| 1448 | + // That future is not really supposed to throw unexpected exceptions | |
| 1449 | + if (!(t instanceof InterruptedException)) | |
| 1450 | + logger.error("Unexpected error while marking node UP: while this shouldn't happen, this shouldn't be critical", t); | |
| 1451 | + } | |
| 1452 | + }); | |
| 1449 | 1453 | |
| 1450 | - // Now, check if there isn't pools to create/remove following the addition. | |
| 1451 | - // We do that now only so that it's not called before we've set the node up. | |
| 1452 | - for (SessionManager s : sessions) | |
| 1453 | - s.updateCreatedPools(blockingExecutor); | |
| 1454 | + f.get(); | |
| 1455 | + | |
| 1456 | + // Now, check if there isn't pools to create/remove following the addition. | |
| 1457 | + // We do that now only so that it's not called before we've set the node up. | |
| 1458 | + for (SessionManager s : sessions) | |
| 1459 | + s.updateCreatedPools(blockingExecutor); | |
| 1460 | + | |
| 1461 | + } finally { | |
| 1462 | + host.notificationsLock.unlock(); | |
| 1463 | + } | |
| 1454 | 1464 | } |
| 1455 | 1465 | |
| 1456 | 1466 | public ListenableFuture<?> triggerOnDown(final Host host) { |
| @@ -1490,8 +1500,14 @@ | ||
| 1490 | 1500 | // If multiple threads get there, we want to start reconnection attempts only |
| 1491 | 1501 | // once, but we also don't want said threads to return from this method before |
| 1492 | 1502 | // the loadbalancing policy has been informed (otherwise those threads won't |
| 1493 | - // consider the host suspect but simply ignore it). So we synchronize. | |
| 1494 | - synchronized (host) { | |
| 1503 | + // consider the host suspect but simply ignore it). So we lock. | |
| 1504 | + try { | |
| 1505 | + boolean locked = host.notificationsLock.tryLock(10, TimeUnit.SECONDS); | |
| 1506 | + if (!locked) { | |
| 1507 | + logger.warn("Could not acquire notifications lock within 10 seconds, ignoring SUSPECTED notification for {}", host); | |
| 1508 | + return; | |
| 1509 | + } | |
| 1510 | + | |
| 1495 | 1511 | // If we've already mark the node down/suspected, ignore this |
| 1496 | 1512 | if (!host.setSuspected() || host.reconnectionAttempt.get() != null) |
| 1497 | 1513 | return; |
| @@ -1521,14 +1537,19 @@ | ||
| 1521 | 1537 | })); |
| 1522 | 1538 | |
| 1523 | 1539 | loadBalancingPolicy().onSuspected(host); |
| 1524 | - } | |
| 1525 | 1540 | |
| 1526 | - controlConnection.onSuspected(host); | |
| 1527 | - for (SessionManager s : sessions) | |
| 1528 | - s.onSuspected(host); | |
| 1541 | + controlConnection.onSuspected(host); | |
| 1542 | + for (SessionManager s : sessions) | |
| 1543 | + s.onSuspected(host); | |
| 1544 | + | |
| 1545 | + for (Host.StateListener listener : listeners) | |
| 1546 | + listener.onSuspected(host); | |
| 1529 | 1547 | |
| 1530 | - for (Host.StateListener listener : listeners) | |
| 1531 | - listener.onSuspected(host); | |
| 1548 | + } catch (InterruptedException e) { | |
| 1549 | + Thread.currentThread().interrupt(); | |
| 1550 | + } finally { | |
| 1551 | + host.notificationsLock.unlock(); | |
| 1552 | + } | |
| 1532 | 1553 | } |
| 1533 | 1554 | |
| 1534 | 1555 | // Use triggerOnDown unless you're sure you want to run this on the current thread. |
| @@ -1538,52 +1559,62 @@ | ||
| 1538 | 1559 | if (isClosed()) |
| 1539 | 1560 | return; |
| 1540 | 1561 | |
| 1541 | - // If we're SUSPECT and not the task validating the suspicion, then some other task is | |
| 1542 | - // already checking to verify if the node is really down (or if it's simply that the | |
| 1543 | - // connections where broken). So just skip this in that case. | |
| 1544 | - if (!isSuspectedVerification && host.state == Host.State.SUSPECT) { | |
| 1545 | - logger.debug("Aborting onDown because a reconnection is running on SUSPECT host {}", host); | |
| 1546 | - return; | |
| 1547 | - } | |
| 1562 | + try { | |
| 1563 | + boolean locked = host.notificationsLock.tryLock(10, TimeUnit.SECONDS); | |
| 1564 | + if (!locked) { | |
| 1565 | + logger.warn("Could not acquire notifications lock within 10 seconds, ignoring DOWN notification for {}", host); | |
| 1566 | + return; | |
| 1567 | + } | |
| 1548 | 1568 | |
| 1549 | - // Note: we don't want to skip that method if !host.isUp() because we set isUp | |
| 1550 | - // late in onUp, and so we can rely on isUp if there is an error during onUp. | |
| 1551 | - // But if there is a reconnection attempt in progress already, then we know | |
| 1552 | - // we've already gone through that method since the last successful onUp(), so | |
| 1553 | - // we're good skipping it. | |
| 1554 | - if (host.reconnectionAttempt.get() != null) { | |
| 1555 | - logger.debug("Aborting onDown because a reconnection is running on DOWN host {}", host); | |
| 1556 | - return; | |
| 1557 | - } | |
| 1569 | + // If we're SUSPECT and not the task validating the suspicion, then some other task is | |
| 1570 | + // already checking to verify if the node is really down (or if it's simply that the | |
| 1571 | + // connections where broken). So just skip this in that case. | |
| 1572 | + if (!isSuspectedVerification && host.state == Host.State.SUSPECT) { | |
| 1573 | + logger.debug("Aborting onDown because a reconnection is running on SUSPECT host {}", host); | |
| 1574 | + return; | |
| 1575 | + } | |
| 1558 | 1576 | |
| 1559 | - // Remember if we care about this node at all. We must call this before | |
| 1560 | - // we've signalled the load balancing policy, since most policy will always | |
| 1561 | - // IGNORE down nodes anyway. | |
| 1562 | - HostDistance distance = loadBalancingPolicy().distance(host); | |
| 1563 | - | |
| 1564 | - boolean wasUp = host.isUp(); | |
| 1565 | - host.setDown(); | |
| 1566 | - | |
| 1567 | - loadBalancingPolicy().onDown(host); | |
| 1568 | - controlConnection.onDown(host); | |
| 1569 | - for (SessionManager s : sessions) | |
| 1570 | - s.onDown(host); | |
| 1571 | - | |
| 1572 | - // Contrarily to other actions of that method, there is no reason to notify listeners | |
| 1573 | - // unless the host was UP at the beginning of this function since even if a onUp fail | |
| 1574 | - // mid-method, listeners won't have been notified of the UP. | |
| 1575 | - if (wasUp) { | |
| 1576 | - for (Host.StateListener listener : listeners) | |
| 1577 | - listener.onDown(host); | |
| 1578 | - } | |
| 1577 | + // Note: we don't want to skip that method if !host.isUp() because we set isUp | |
| 1578 | + // late in onUp, and so we can rely on isUp if there is an error during onUp. | |
| 1579 | + // But if there is a reconnection attempt in progress already, then we know | |
| 1580 | + // we've already gone through that method since the last successful onUp(), so | |
| 1581 | + // we're good skipping it. | |
| 1582 | + if (host.reconnectionAttempt.get() != null) { | |
| 1583 | + logger.debug("Aborting onDown because a reconnection is running on DOWN host {}", host); | |
| 1584 | + return; | |
| 1585 | + } | |
| 1579 | 1586 | |
| 1580 | - // Don't start a reconnection if we ignore the node anyway (JAVA-314) | |
| 1581 | - if (distance == HostDistance.IGNORED) | |
| 1582 | - return; | |
| 1587 | + // Remember if we care about this node at all. We must call this before | |
| 1588 | + // we've signalled the load balancing policy, since most policy will always | |
| 1589 | + // IGNORE down nodes anyway. | |
| 1590 | + HostDistance distance = loadBalancingPolicy().distance(host); | |
| 1591 | + | |
| 1592 | + boolean wasUp = host.isUp(); | |
| 1593 | + host.setDown(); | |
| 1594 | + | |
| 1595 | + loadBalancingPolicy().onDown(host); | |
| 1596 | + controlConnection.onDown(host); | |
| 1597 | + for (SessionManager s : sessions) | |
| 1598 | + s.onDown(host); | |
| 1599 | + | |
| 1600 | + // Contrarily to other actions of that method, there is no reason to notify listeners | |
| 1601 | + // unless the host was UP at the beginning of this function since even if a onUp fail | |
| 1602 | + // mid-method, listeners won't have been notified of the UP. | |
| 1603 | + if (wasUp) { | |
| 1604 | + for (Host.StateListener listener : listeners) | |
| 1605 | + listener.onDown(host); | |
| 1606 | + } | |
| 1607 | + | |
| 1608 | + // Don't start a reconnection if we ignore the node anyway (JAVA-314) | |
| 1609 | + if (distance == HostDistance.IGNORED) | |
| 1610 | + return; | |
| 1583 | 1611 | |
| 1584 | - // Note: we basically waste the first successful reconnection, but it's probably not a big deal | |
| 1585 | - logger.debug("{} is down, scheduling connection retries", host); | |
| 1586 | - startPeriodicReconnectionAttempt(host, isHostAddition); | |
| 1612 | + // Note: we basically waste the first successful reconnection, but it's probably not a big deal | |
| 1613 | + logger.debug("{} is down, scheduling connection retries", host); | |
| 1614 | + startPeriodicReconnectionAttempt(host, isHostAddition); | |
| 1615 | + } finally { | |
| 1616 | + host.notificationsLock.unlock(); | |
| 1617 | + } | |
| 1587 | 1618 | } |
| 1588 | 1619 | |
| 1589 | 1620 | void startPeriodicReconnectionAttempt(final Host host, final boolean isHostAddition) { |
| @@ -1709,74 +1740,85 @@ | ||
| 1709 | 1740 | return; |
| 1710 | 1741 | } |
| 1711 | 1742 | |
| 1712 | - // Adds to the load balancing first and foremost, as doing so might change the decision | |
| 1713 | - // it will make for distance() on that node (not likely but we leave that possibility). | |
| 1714 | - // This does mean the policy may start returning that node for query plan, but as long | |
| 1715 | - // as no pools have been created (below) this will be ignored by RequestHandler so it's fine. | |
| 1716 | - loadBalancingPolicy().onAdd(host); | |
| 1717 | - | |
| 1718 | - // Next, if the host should be ignored, well, ignore it. | |
| 1719 | - if (loadBalancingPolicy().distance(host) == HostDistance.IGNORED) { | |
| 1720 | - // We still mark the node UP though as it should be (and notifiy the listeners). | |
| 1721 | - // We'll mark it down if we have a notification anyway and we've documented that especially | |
| 1722 | - // for IGNORED hosts, the isUp() method was a best effort guess | |
| 1723 | - host.setUp(); | |
| 1724 | - for (Host.StateListener listener : listeners) | |
| 1725 | - listener.onAdd(host); | |
| 1726 | - return; | |
| 1727 | - } | |
| 1728 | - | |
| 1729 | 1743 | try { |
| 1730 | - prepareAllQueries(host); | |
| 1731 | - } catch (InterruptedException e) { | |
| 1732 | - Thread.currentThread().interrupt(); | |
| 1733 | - // Don't propagate because we don't want to prevent other listener to run | |
| 1734 | - } catch (UnsupportedProtocolVersionException e) { | |
| 1735 | - logUnsupportedVersionProtocol(host); | |
| 1736 | - return; | |
| 1737 | - } catch (ClusterNameMismatchException e) { | |
| 1738 | - logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName); | |
| 1739 | - return; | |
| 1740 | - } | |
| 1741 | - | |
| 1742 | - controlConnection.onAdd(host); | |
| 1743 | - | |
| 1744 | - List<ListenableFuture<Boolean>> futures = new ArrayList<ListenableFuture<Boolean>>(sessions.size()); | |
| 1745 | - for (SessionManager s : sessions) | |
| 1746 | - futures.add(s.maybeAddPool(host, blockingExecutor)); | |
| 1747 | - | |
| 1748 | - // Only mark the node up once all session have added their pool (if the load-balancing | |
| 1749 | - // policy says it should), so that Host.isUp() don't return true before we're reconnected | |
| 1750 | - // to the node. | |
| 1751 | - ListenableFuture<List<Boolean>> f = Futures.allAsList(futures); | |
| 1752 | - Futures.addCallback(f, new FutureCallback<List<Boolean>>() { | |
| 1753 | - public void onSuccess(List<Boolean> poolCreationResults) { | |
| 1754 | - // If any of the creation failed, they will have signaled a connection failure | |
| 1755 | - // which will trigger a reconnection to the node. So don't bother marking UP. | |
| 1756 | - if (Iterables.any(poolCreationResults, Predicates.equalTo(false))) { | |
| 1757 | - logger.debug("Connection pool cannot be created, not marking {} UP", host); | |
| 1758 | - return; | |
| 1759 | - } | |
| 1744 | + boolean locked = host.notificationsLock.tryLock(10, TimeUnit.SECONDS); | |
| 1745 | + if (!locked) { | |
| 1746 | + logger.warn("Could not acquire notifications lock within 10 seconds, ignoring ADD notification for {}", host); | |
| 1747 | + return; | |
| 1748 | + } | |
| 1760 | 1749 | |
| 1750 | + // Adds to the load balancing first and foremost, as doing so might change the decision | |
| 1751 | + // it will make for distance() on that node (not likely but we leave that possibility). | |
| 1752 | + // This does mean the policy may start returning that node for query plan, but as long | |
| 1753 | + // as no pools have been created (below) this will be ignored by RequestHandler so it's fine. | |
| 1754 | + loadBalancingPolicy().onAdd(host); | |
| 1755 | + | |
| 1756 | + // Next, if the host should be ignored, well, ignore it. | |
| 1757 | + if (loadBalancingPolicy().distance(host) == HostDistance.IGNORED) { | |
| 1758 | + // We still mark the node UP though as it should be (and notifiy the listeners). | |
| 1759 | + // We'll mark it down if we have a notification anyway and we've documented that especially | |
| 1760 | + // for IGNORED hosts, the isUp() method was a best effort guess | |
| 1761 | 1761 | host.setUp(); |
| 1762 | - | |
| 1763 | 1762 | for (Host.StateListener listener : listeners) |
| 1764 | 1763 | listener.onAdd(host); |
| 1764 | + return; | |
| 1765 | 1765 | } |
| 1766 | 1766 | |
| 1767 | - public void onFailure(Throwable t) { | |
| 1768 | - // That future is not really supposed to throw unexpected exceptions | |
| 1769 | - if (!(t instanceof InterruptedException)) | |
| 1770 | - logger.error("Unexpected error while adding node: while this shouldn't happen, this shouldn't be critical", t); | |
| 1767 | + try { | |
| 1768 | + prepareAllQueries(host); | |
| 1769 | + } catch (InterruptedException e) { | |
| 1770 | + Thread.currentThread().interrupt(); | |
| 1771 | + // Don't propagate because we don't want to prevent other listener to run | |
| 1772 | + } catch (UnsupportedProtocolVersionException e) { | |
| 1773 | + logUnsupportedVersionProtocol(host); | |
| 1774 | + return; | |
| 1775 | + } catch (ClusterNameMismatchException e) { | |
| 1776 | + logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName); | |
| 1777 | + return; | |
| 1771 | 1778 | } |
| 1772 | - }); | |
| 1773 | 1779 | |
| 1774 | - f.get(); | |
| 1780 | + controlConnection.onAdd(host); | |
| 1781 | + | |
| 1782 | + List<ListenableFuture<Boolean>> futures = new ArrayList<ListenableFuture<Boolean>>(sessions.size()); | |
| 1783 | + for (SessionManager s : sessions) | |
| 1784 | + futures.add(s.maybeAddPool(host, blockingExecutor)); | |
| 1785 | + | |
| 1786 | + // Only mark the node up once all session have added their pool (if the load-balancing | |
| 1787 | + // policy says it should), so that Host.isUp() don't return true before we're reconnected | |
| 1788 | + // to the node. | |
| 1789 | + ListenableFuture<List<Boolean>> f = Futures.allAsList(futures); | |
| 1790 | + Futures.addCallback(f, new FutureCallback<List<Boolean>>() { | |
| 1791 | + public void onSuccess(List<Boolean> poolCreationResults) { | |
| 1792 | + // If any of the creation failed, they will have signaled a connection failure | |
| 1793 | + // which will trigger a reconnection to the node. So don't bother marking UP. | |
| 1794 | + if (Iterables.any(poolCreationResults, Predicates.equalTo(false))) { | |
| 1795 | + logger.debug("Connection pool cannot be created, not marking {} UP", host); | |
| 1796 | + return; | |
| 1797 | + } | |
| 1798 | + | |
| 1799 | + host.setUp(); | |
| 1800 | + | |
| 1801 | + for (Host.StateListener listener : listeners) | |
| 1802 | + listener.onAdd(host); | |
| 1803 | + } | |
| 1804 | + | |
| 1805 | + public void onFailure(Throwable t) { | |
| 1806 | + // That future is not really supposed to throw unexpected exceptions | |
| 1807 | + if (!(t instanceof InterruptedException)) | |
| 1808 | + logger.error("Unexpected error while adding node: while this shouldn't happen, this shouldn't be critical", t); | |
| 1809 | + } | |
| 1810 | + }); | |
| 1775 | 1811 | |
| 1776 | - // Now, check if there isn't pools to create/remove following the addition. | |
| 1777 | - // We do that now only so that it's not called before we've set the node up. | |
| 1778 | - for (SessionManager s : sessions) | |
| 1779 | - s.updateCreatedPools(blockingExecutor); | |
| 1812 | + f.get(); | |
| 1813 | + | |
| 1814 | + // Now, check if there isn't pools to create/remove following the addition. | |
| 1815 | + // We do that now only so that it's not called before we've set the node up. | |
| 1816 | + for (SessionManager s : sessions) | |
| 1817 | + s.updateCreatedPools(blockingExecutor); | |
| 1818 | + | |
| 1819 | + } finally { | |
| 1820 | + host.notificationsLock.unlock(); | |
| 1821 | + } | |
| 1780 | 1822 | } |
| 1781 | 1823 | |
| 1782 | 1824 | public ListenableFuture<?> triggerOnRemove(final Host host) { |
| @@ -1793,16 +1835,26 @@ | ||
| 1793 | 1835 | if (isClosed()) |
| 1794 | 1836 | return; |
| 1795 | 1837 | |
| 1796 | - host.setDown(); | |
| 1838 | + try { | |
| 1839 | + boolean locked = host.notificationsLock.tryLock(10, TimeUnit.SECONDS); | |
| 1840 | + if (!locked) { | |
| 1841 | + logger.warn("Could not acquire notifications lock within 10 seconds, ignoring REMOVE notification for {}", host); | |
| 1842 | + return; | |
| 1843 | + } | |
| 1797 | 1844 | |
| 1798 | - logger.debug("Removing host {}", host); | |
| 1799 | - loadBalancingPolicy().onRemove(host); | |
| 1800 | - controlConnection.onRemove(host); | |
| 1801 | - for (SessionManager s : sessions) | |
| 1802 | - s.onRemove(host); | |
| 1845 | + host.setDown(); | |
| 1803 | 1846 | |
| 1804 | - for (Host.StateListener listener : listeners) | |
| 1805 | - listener.onRemove(host); | |
| 1847 | + logger.debug("Removing host {}", host); | |
| 1848 | + loadBalancingPolicy().onRemove(host); | |
| 1849 | + controlConnection.onRemove(host); | |
| 1850 | + for (SessionManager s : sessions) | |
| 1851 | + s.onRemove(host); | |
| 1852 | + | |
| 1853 | + for (Host.StateListener listener : listeners) | |
| 1854 | + listener.onRemove(host); | |
| 1855 | + } finally { | |
| 1856 | + host.notificationsLock.unlock(); | |
| 1857 | + } | |
| 1806 | 1858 | } |
| 1807 | 1859 | |
| 1808 | 1860 | public boolean signalConnectionFailure(Host host, ConnectionException exception, boolean isHostAddition, boolean markSuspected) { |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +4 −0
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | import java.net.InetAddress; |
| 19 | 19 | import java.net.InetSocketAddress; |
| 20 | 20 | import java.util.concurrent.atomic.AtomicReference; |
| 21 | +import java.util.concurrent.locks.Lock; | |
| 22 | +import java.util.concurrent.locks.ReentrantLock; | |
| 21 | 23 | |
| 22 | 24 | import com.google.common.collect.ImmutableList; |
| 23 | 25 | import com.google.common.util.concurrent.Futures; |
| @@ -39,6 +41,8 @@ | ||
| 39 | 41 | |
| 40 | 42 | enum State { ADDED, DOWN, SUSPECT, UP } |
| 41 | 43 | volatile State state; |
| 44 | + /** Ensures state change notifications for that host are handled serially */ | |
| 45 | + final Lock notificationsLock = new ReentrantLock(); | |
| 42 | 46 | |
| 43 | 47 | private final ConvictionPolicy policy; |
| 44 | 48 | private final Cluster.Manager manager; |