kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 700f2fff0c89f0fb0eae891f80f321913fb7cdad6611f15a0afed5121322366f |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-12-19 19:00 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-12-19 19:00 +01:00 |
| Parents | f69df2b52f63 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +44 −36
| @@ -74,6 +74,8 @@ | ||
| 74 | 74 | |
| 75 | 75 | private static final int DEFAULT_THREAD_KEEP_ALIVE = 30; |
| 76 | 76 | |
| 77 | + private static final int NOTIF_LOCK_TIMEOUT_SECONDS = 60; | |
| 78 | + | |
| 77 | 79 | final Manager manager; |
| 78 | 80 | |
| 79 | 81 | /** |
| @@ -1378,12 +1380,12 @@ | ||
| 1378 | 1380 | return; |
| 1379 | 1381 | } |
| 1380 | 1382 | |
| 1383 | + boolean locked = host.notificationsLock.tryLock(NOTIF_LOCK_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 1384 | + if (!locked) { | |
| 1385 | + logger.warn("Could not acquire notifications lock within {} seconds, ignoring UP notification for {}", NOTIF_LOCK_TIMEOUT_SECONDS, host); | |
| 1386 | + return; | |
| 1387 | + } | |
| 1381 | 1388 | try { |
| 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 | - } | |
| 1387 | 1389 | |
| 1388 | 1390 | // We don't want to use the public Host.isUp() as this would make us skip the rest for suspected hosts |
| 1389 | 1391 | if (host.state == Host.State.UP) |
| @@ -1476,7 +1478,7 @@ | ||
| 1476 | 1478 | }); |
| 1477 | 1479 | } |
| 1478 | 1480 | |
| 1479 | - public void onSuspected(final Host host) { | |
| 1481 | + public void onSuspected(final Host host) throws InterruptedException { | |
| 1480 | 1482 | logger.debug("Host {} is Suspected", host); |
| 1481 | 1483 | |
| 1482 | 1484 | if (isClosed()) |
| @@ -1490,6 +1492,10 @@ | ||
| 1490 | 1492 | return; |
| 1491 | 1493 | } |
| 1492 | 1494 | |
| 1495 | + // If we've already mark the node down/suspected, ignore this | |
| 1496 | + if (host.state == Host.State.SUSPECT || host.reconnectionAttempt.get() != null) | |
| 1497 | + return; | |
| 1498 | + | |
| 1493 | 1499 | // We need to |
| 1494 | 1500 | // 1) mark the node suspect if no-one has bitten us to it |
| 1495 | 1501 | // 2) start the reconnection attempt |
| @@ -1501,15 +1507,14 @@ | ||
| 1501 | 1507 | // once, but we also don't want said threads to return from this method before |
| 1502 | 1508 | // the loadbalancing policy has been informed (otherwise those threads won't |
| 1503 | 1509 | // consider the host suspect but simply ignore it). So we lock. |
| 1510 | + boolean locked = host.notificationsLock.tryLock(NOTIF_LOCK_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 1511 | + if (!locked) { | |
| 1512 | + logger.warn("Could not acquire notifications lock within {} seconds, ignoring SUSPECTED notification for {}", NOTIF_LOCK_TIMEOUT_SECONDS, host); | |
| 1513 | + return; | |
| 1514 | + } | |
| 1504 | 1515 | 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 | - | |
| 1511 | - // If we've already mark the node down/suspected, ignore this | |
| 1512 | - if (!host.setSuspected() || host.reconnectionAttempt.get() != null) | |
| 1516 | + // Again, exit if someone beat us to suspecting the host | |
| 1517 | + if (!host.setSuspected() || host.reconnectionAttempt.get() != null) | |
| 1513 | 1518 | return; |
| 1514 | 1519 | |
| 1515 | 1520 | // Start the initial initial reconnection attempt |
| @@ -1545,8 +1550,6 @@ | ||
| 1545 | 1550 | for (Host.StateListener listener : listeners) |
| 1546 | 1551 | listener.onSuspected(host); |
| 1547 | 1552 | |
| 1548 | - } catch (InterruptedException e) { | |
| 1549 | - Thread.currentThread().interrupt(); | |
| 1550 | 1553 | } finally { |
| 1551 | 1554 | host.notificationsLock.unlock(); |
| 1552 | 1555 | } |
| @@ -1559,12 +1562,12 @@ | ||
| 1559 | 1562 | if (isClosed()) |
| 1560 | 1563 | return; |
| 1561 | 1564 | |
| 1565 | + boolean locked = host.notificationsLock.tryLock(NOTIF_LOCK_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 1566 | + if (!locked) { | |
| 1567 | + logger.warn("Could not acquire notifications lock within {} seconds, ignoring DOWN notification for {}", NOTIF_LOCK_TIMEOUT_SECONDS, host); | |
| 1568 | + return; | |
| 1569 | + } | |
| 1562 | 1570 | 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 | - } | |
| 1568 | 1571 | |
| 1569 | 1572 | // If we're SUSPECT and not the task validating the suspicion, then some other task is |
| 1570 | 1573 | // already checking to verify if the node is really down (or if it's simply that the |
| @@ -1740,12 +1743,12 @@ | ||
| 1740 | 1743 | return; |
| 1741 | 1744 | } |
| 1742 | 1745 | |
| 1746 | + boolean locked = host.notificationsLock.tryLock(NOTIF_LOCK_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 1747 | + if (!locked) { | |
| 1748 | + logger.warn("Could not acquire notifications lock within {} seconds, ignoring ADD notification for {}", NOTIF_LOCK_TIMEOUT_SECONDS, host); | |
| 1749 | + return; | |
| 1750 | + } | |
| 1743 | 1751 | try { |
| 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 | - } | |
| 1749 | 1752 | |
| 1750 | 1753 | // Adds to the load balancing first and foremost, as doing so might change the decision |
| 1751 | 1754 | // it will make for distance() on that node (not likely but we leave that possibility). |
| @@ -1835,12 +1838,12 @@ | ||
| 1835 | 1838 | if (isClosed()) |
| 1836 | 1839 | return; |
| 1837 | 1840 | |
| 1841 | + boolean locked = host.notificationsLock.tryLock(NOTIF_LOCK_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 1842 | + if (!locked) { | |
| 1843 | + logger.warn("Could not acquire notifications lock within {} seconds, ignoring REMOVE notification for {}", NOTIF_LOCK_TIMEOUT_SECONDS, host); | |
| 1844 | + return; | |
| 1845 | + } | |
| 1838 | 1846 | 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 | - } | |
| 1844 | 1847 | |
| 1845 | 1848 | host.setDown(); |
| 1846 | 1849 | |
| @@ -1868,11 +1871,16 @@ | ||
| 1868 | 1871 | if (isHostAddition || !markSuspected) { |
| 1869 | 1872 | triggerOnDown(host, isHostAddition); |
| 1870 | 1873 | } else { |
| 1871 | - // Note that we do want to call onSuspected on the current thread, as the whole point is | |
| 1872 | - // that by the time this method return, the host initialReconnectionAttempt will have been | |
| 1873 | - // set and the load balancing policy informed of the suspection. We know that onSuspected | |
| 1874 | - // does little work (and non blocking one) itself however. | |
| 1875 | - onSuspected(host); | |
| 1874 | + try { | |
| 1875 | + // Note that we do want to call onSuspected on the current thread, as the whole point is | |
| 1876 | + // that by the time this method return, the host initialReconnectionAttempt will have been | |
| 1877 | + // set and the load balancing policy informed of the suspection. We know that onSuspected | |
| 1878 | + // does little work (and non blocking one) itself however. | |
| 1879 | + onSuspected(host); | |
| 1880 | + } catch (InterruptedException e) { | |
| 1881 | + // This is most likely due to shutdown | |
| 1882 | + logger.warn("Interrupted while trying to set host SUSPECT, aborting"); | |
| 1883 | + } | |
| 1876 | 1884 | } |
| 1877 | 1885 | } |
| 1878 | 1886 | return isDown; |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +1 −2
| @@ -18,7 +18,6 @@ | ||
| 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 | 21 | import java.util.concurrent.locks.ReentrantLock; |
| 23 | 22 | |
| 24 | 23 | import com.google.common.collect.ImmutableList; |
| @@ -42,7 +41,7 @@ | ||
| 42 | 41 | enum State { ADDED, DOWN, SUSPECT, UP } |
| 43 | 42 | volatile State state; |
| 44 | 43 | /** Ensures state change notifications for that host are handled serially */ |
| 45 | - final Lock notificationsLock = new ReentrantLock(); | |
| 44 | + final ReentrantLock notificationsLock = new ReentrantLock(true); | |
| 46 | 45 | |
| 47 | 46 | private final ConvictionPolicy policy; |
| 48 | 47 | private final Cluster.Manager manager; |