Elffuss BraidHub

kikocis/java-driver public

Remove "suspected" mechanism (JAVA-617).
Changesetdc0b7d27d87e38f88bfa814813fe8350af068be8058a13a1682f23936eaf5b76
Authorolim7t <omichallat+github@gmail.com> · 2015-03-19 17:24 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-04-17 22:59 +02:00
Parents0af82488f207

Browse the files at this changeset

10 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -40,6 +40,7 @@
4040 schema-altering statement (JAVA-669)
4141 - [improvement] Make connection and pool creation fully async (JAVA-692)
4242 - [improvement] Optimize connection use after reconnection (JAVA-505)
43+- [improvement] Remove "suspected" mechanism (JAVA-617)
4344
4445 Merged from 2.0.9_fixes branch:
4546
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +5 −86
@@ -1518,77 +1518,13 @@
15181518 return executor.submit(new ExceptionCatchingRunnable() {
15191519 @Override
15201520 public void runMayThrow() throws InterruptedException, ExecutionException {
1521- onDown(host, isHostAddition, false, startReconnection);
1521+ onDown(host, isHostAddition, startReconnection);
15221522 }
15231523 });
15241524 }
15251525
1526- public void onSuspected(final Host host) {
1527- logger.debug("Host {} is Suspected", host);
1528-
1529- if (isClosed())
1530- return;
1531-
1532- // We shouldn't really get there for IGNORED nodes since we shouldn't have
1533- // connected to one in the first place, but if we ever do, simply hand it
1534- // off to onDown
1535- if (loadBalancingPolicy().distance(host) == HostDistance.IGNORED) {
1536- triggerOnDown(host, true);
1537- return;
1538- }
1539-
1540- // We need to
1541- // 1) mark the node suspect if no-one has bitten us to it
1542- // 2) start the reconnection attempt
1543- // 3) inform the loadbalancing policy
1544- // We must do 2) before 3) as we want the policy to be able to rely
1545- // on the reconnection attempt future.
1546- //
1547- // If multiple threads get there, we want to start reconnection attempts only
1548- // once, but we also don't want said threads to return from this method before
1549- // the loadbalancing policy has been informed (otherwise those threads won't
1550- // consider the host suspect but simply ignore it). So we synchronize.
1551- synchronized (host) {
1552- // If we've already marked the node down/suspected, ignore this
1553- if (!host.setSuspected() || host.reconnectionAttempt.get() != null)
1554- return;
1555-
1556- // Start the initial initial reconnection attempt
1557- host.initialReconnectionAttempt.set(executor.submit(new ExceptionCatchingRunnable() {
1558- @Override
1559- public void runMayThrow() throws InterruptedException, ExecutionException {
1560- boolean success;
1561- try {
1562- Connection connection = connectionFactory.open(host);
1563- // Note that we want to do the pool creation on this thread because we want that
1564- // when onUp return, the host is ready for querying
1565- onUp(host, connection);
1566- // If one of the connections in onUp failed, it signaled the error and triggerd onDown,
1567- // but onDown aborted because this reconnection attempt was in progress (JAVA-577).
1568- // Test the state now to check than onUp succeeded (we know it's up-to-date since onUp was
1569- // executed synchronously).
1570- success = host.state == Host.State.UP;
1571- } catch (Exception e) {
1572- success = false;
1573- }
1574- if (!success)
1575- onDown(host, false, true, true);
1576- }
1577- }));
1578-
1579- loadBalancingPolicy().onSuspected(host);
1580- }
1581-
1582- controlConnection.onSuspected(host);
1583- for (SessionManager s : sessions)
1584- s.onSuspected(host);
1585-
1586- for (Host.StateListener listener : listeners)
1587- listener.onSuspected(host);
1588- }
1589-
15901526 // Use triggerOnDown unless you're sure you want to run this on the current thread.
1591- private void onDown(final Host host, final boolean isHostAddition, final boolean isSuspectedVerification, boolean startReconnection) throws InterruptedException, ExecutionException {
1527+ private void onDown(final Host host, final boolean isHostAddition, boolean startReconnection) throws InterruptedException, ExecutionException {
15921528 logger.debug("Host {} is DOWN", host);
15931529
15941530 if (isClosed())
@@ -1601,14 +1537,6 @@
16011537 }
16021538 try {
16031539
1604- // If we're SUSPECT and not the task validating the suspicion, then some other task is
1605- // already checking to verify if the node is really down (or if it's simply that the
1606- // connections where broken). So just skip this in that case.
1607- if (!isSuspectedVerification && host.state == Host.State.SUSPECT) {
1608- logger.debug("Aborting onDown because a reconnection is running on SUSPECT host {}", host);
1609- return;
1610- }
1611-
16121540 // Note: we don't want to skip that method if !host.isUp() because we set isUp
16131541 // late in onUp, and so we can rely on isUp if there is an error during onUp.
16141542 // But if there is a reconnection attempt in progress already, then we know
@@ -1889,24 +1817,15 @@
18891817 }
18901818 }
18911819
1892- public boolean signalConnectionFailure(Host host, ConnectionException exception, boolean isHostAddition, boolean markSuspected) {
1820+ public boolean signalConnectionFailure(Host host, ConnectionException exception, boolean isHostAddition) {
18931821 // Don't signal failure until we've fully initialized the controlConnection as this might mess up with
18941822 // the protocol detection
18951823 if (!isFullyInit || isClosed())
18961824 return true;
18971825
18981826 boolean isDown = host.signalConnectionFailure(exception);
1899- if (isDown) {
1900- if (isHostAddition || !markSuspected) {
1901- triggerOnDown(host, isHostAddition, true);
1902- } else {
1903- // Note that we do want to call onSuspected on the current thread, as the whole point is
1904- // that by the time this method return, the host initialReconnectionAttempt will have been
1905- // set and the load balancing policy informed of the suspection. We know that onSuspected
1906- // does little work (and non blocking one) itself however.
1907- onSuspected(host);
1908- }
1909- }
1827+ if (isDown)
1828+ triggerOnDown(host, isHostAddition, true);
19101829 return isDown;
19111830 }
19121831
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +1 −13
@@ -390,23 +390,11 @@
390390 ? (ConnectionException)e
391391 : new ConnectionException(address, "Connection problem", e);
392392
393- // We need to signal the connection failure before erroring out handlers to make
394- // sure the "suspected" mechanism work as expected
395393 Host host = factory.manager.metadata.getHost(address);
396394 if (host != null) {
397-
398- // If the host was reconnecting, and this error happens right after we opened a connection pool, but
399- // before we could mark the node UP, we don't want to go through the SUSPECTED state, because that can
400- // lead to a race condition that leaves the node UP with a closed pool.
401- HostConnectionPool pool = this.poolRef.get();
402- boolean belongsToReconnectingPool = host.state != Host.State.UP &&
403- (pool == null || !pool.isClosed());
404-
405- boolean markSuspected = isInitialized && !belongsToReconnectingPool;
406-
407395 // This will trigger onDown, including when the defunct Connection is part of a reconnection attempt, which is redundant.
408396 // This is not too much of a problem since calling onDown on a node that is already down has no effect.
409- boolean isDown = factory.manager.signalConnectionFailure(host, ce, host.wasJustAdded(), markSuspected);
397+ boolean isDown = factory.manager.signalConnectionFailure(host, ce, host.wasJustAdded());
410398 notifyOwnerWhenDefunct(isDown);
411399 }
412400
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +11 −16
@@ -41,7 +41,7 @@
4141
4242 private final InetSocketAddress address;
4343
44- enum State { ADDED, DOWN, SUSPECT, UP }
44+ enum State { ADDED, DOWN, UP }
4545 volatile State state;
4646 /** Ensures state change notifications for that host are handled serially */
4747 final ReentrantLock notificationsLock = new ReentrantLock(true);
@@ -49,9 +49,6 @@
4949 private final ConvictionPolicy policy;
5050 private final Cluster.Manager manager;
5151
52- // Tracks the first "immediate" reconnection attempt when a node get suspected.
53- final AtomicReference<ListenableFuture<?>> initialReconnectionAttempt = new AtomicReference<ListenableFuture<?>>(MoreFutures.VOID_SUCCESS);
54-
5552 // Tracks later reconnection attempts to that host so we avoid adding multiple tasks.
5653 final AtomicReference<ListenableFuture<?>> reconnectionAttempt = new AtomicReference<ListenableFuture<?>>();
5754
@@ -188,9 +185,7 @@
188185 * @return whether the node is considered up.
189186 */
190187 public boolean isUp() {
191- // Consider a suspected host UP until proved otherwise to avoid
192- // having the status flapping if it turns out the host is not really down.
193- return state == State.UP || state == State.SUSPECT;
188+ return state == State.UP;
194189 }
195190
196191 /**
@@ -214,9 +209,13 @@
214209 * we are trying suspected nodes.
215210 *
216211 * @return the future.
212+ *
213+ * @deprecated the suspicion mechanism has been disabled. This will always return
214+ * a completed future.
217215 */
216+ @Deprecated
218217 public ListenableFuture<?> getInitialReconnectionAttemptFuture() {
219- return initialReconnectionAttempt.get();
218+ return Futures.immediateFuture(null);
220219 }
221220
222221 /**
@@ -284,14 +283,6 @@
284283 state = State.UP;
285284 }
286285
287- boolean setSuspected() {
288- if (state != State.UP)
289- return false;
290-
291- state = State.SUSPECT;
292- return true;
293- }
294-
295286 boolean signalConnectionFailure(ConnectionException exception) {
296287 return policy.addFailure(exception);
297288 }
@@ -341,7 +332,11 @@
341332 * that is suspected down turns out to be truly down (that is, the driver
342333 * cannot successfully connect to it right away), then {@link onDown} will
343334 * be called.
335+ *
336+ * @deprecated the suspicion mechanism has been disabled. This will never
337+ * get called.
344338 */
339+ @Deprecated
345340 public void onSuspected(Host host);
346341
347342 /**
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +0 −3
@@ -423,9 +423,6 @@
423423 updateCreatedPools();
424424 }
425425
426- void onSuspected(Host host) {
427- }
428-
429426 void onRemove(Host host) throws InterruptedException, ExecutionException {
430427 onDown(host);
431428 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +19 −78
@@ -65,8 +65,6 @@
6565 @VisibleForTesting
6666 volatile String localDc;
6767
68- private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcSuspectedHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>();
69-
7068 private final int usedHostsPerRemoteDc;
7169 private final boolean dontHopForLocalCL;
7270
@@ -281,51 +279,23 @@
281279 private int idx = startIdx;
282280 private int remainingLocal = hosts.size();
283281
284- private Iterator<Host> localSuspected;
285-
286282 // For remote Dcs
287283 private Iterator<String> remoteDcs;
288284 private List<Host> currentDcHosts;
289285 private int currentDcRemaining;
290- private Iterator<Host> currentDcSuspected;
291286
292287 @Override
293288 protected Host computeNext() {
294- if (remainingLocal > 0) {
295- remainingLocal--;
296- int c = idx++ % hosts.size();
297- if (c < 0) {
298- c += hosts.size();
289+ while (true) {
290+ if (remainingLocal > 0) {
291+ remainingLocal--;
292+ int c = idx++ % hosts.size();
293+ if (c < 0) {
294+ c += hosts.size();
295+ }
296+ return hosts.get(c);
299297 }
300- return hosts.get(c);
301- }
302-
303- if (localSuspected == null) {
304- List<Host> l = perDcSuspectedHosts.get(localDc);
305- localSuspected = l == null ? Collections.<Host>emptySet().iterator() : l.iterator();
306- }
307-
308- while (localSuspected.hasNext()) {
309- Host h = localSuspected.next();
310- waitOnReconnection(h);
311- if (h.isUp())
312- return h;
313- }
314-
315- ConsistencyLevel cl = statement.getConsistencyLevel() == null
316- ? configuration.getQueryOptions().getConsistencyLevel()
317- : statement.getConsistencyLevel();
318-
319- if (dontHopForLocalCL && cl.isDCLocal())
320- return endOfData();
321-
322- if (remoteDcs == null) {
323- Set<String> copy = new HashSet<String>(perDcLiveHosts.keySet());
324- copy.remove(localDc);
325- remoteDcs = copy.iterator();
326- }
327298
328- while (true) {
329299 if (currentDcHosts != null && currentDcRemaining > 0) {
330300 currentDcRemaining--;
331301 int c = idx++ % currentDcHosts.size();
@@ -335,13 +305,17 @@
335305 return currentDcHosts.get(c);
336306 }
337307
338- if (currentDcSuspected != null) {
339- while (currentDcSuspected.hasNext()) {
340- Host h = currentDcSuspected.next();
341- waitOnReconnection(h);
342- if (h.isUp())
343- return h;
344- }
308+ ConsistencyLevel cl = statement.getConsistencyLevel() == null
309+ ? configuration.getQueryOptions().getConsistencyLevel()
310+ : statement.getConsistencyLevel();
311+
312+ if (dontHopForLocalCL && cl.isDCLocal())
313+ return endOfData();
314+
315+ if (remoteDcs == null) {
316+ Set<String> copy = new HashSet<String>(perDcLiveHosts.keySet());
317+ copy.remove(localDc);
318+ remoteDcs = copy.iterator();
345319 }
346320
347321 if (!remoteDcs.hasNext())
@@ -355,27 +329,12 @@
355329 currentDcHosts = dcHosts.subList(0, Math.min(dcHosts.size(), usedHostsPerRemoteDc));
356330 currentDcRemaining = currentDcHosts.size();
357331 }
358- List<Host> suspectedList = perDcSuspectedHosts.get(nextRemoteDc);
359- currentDcSuspected = suspectedList == null ? null : suspectedList.iterator();
360332 }
361333 return endOfData();
362334 }
363335 };
364336 }
365337
366- private void waitOnReconnection(Host h) {
367- try {
368- h.getInitialReconnectionAttemptFuture().get(configuration.getSocketOptions().getConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
369- } catch (InterruptedException e) {
370- Thread.currentThread().interrupt();
371- } catch (ExecutionException e) {
372- throw new AssertionError(e);
373- } catch (TimeoutException e) {
374- // Shouldn't really happen but isn't really a huge deal
375- logger.debug("Timeout while waiting on initial reconnection future for " + h, e);
376- }
377- }
378-
379338 @Override
380339 public void onUp(Host host) {
381340 String dc = dc(host);
@@ -395,24 +354,10 @@
395354 return;
396355 }
397356 dcHosts.addIfAbsent(host);
398-
399- CopyOnWriteArrayList<Host> dcSuspected = perDcSuspectedHosts.get(dc(host));
400- if (dcSuspected != null)
401- dcSuspected.remove(host);
402357 }
403358
404359 @Override
405360 public void onSuspected(Host host) {
406- String dc = dc(host);
407- CopyOnWriteArrayList<Host> dcSuspected = perDcSuspectedHosts.get(dc);
408- if (dcSuspected == null) {
409- CopyOnWriteArrayList<Host> newMap = new CopyOnWriteArrayList<Host>(Collections.singletonList(host));
410- dcSuspected = perDcSuspectedHosts.putIfAbsent(dc, newMap);
411- // If we've successfully put our new host, we're good, otherwise we've been beaten so continue
412- if (dcSuspected == null)
413- return;
414- }
415- dcSuspected.addIfAbsent(host);
416361 }
417362
418363 @Override
@@ -420,10 +365,6 @@
420365 CopyOnWriteArrayList<Host> dcHosts = perDcLiveHosts.get(dc(host));
421366 if (dcHosts != null)
422367 dcHosts.remove(host);
423-
424- CopyOnWriteArrayList<Host> dcSuspected = perDcSuspectedHosts.get(dc(host));
425- if (dcSuspected != null)
426- dcSuspected.remove(host);
427368 }
428369
429370 @Override
modified driver-core/src/main/java/com/datastax/driver/core/policies/RoundRobinPolicy.java +1 −31
@@ -55,8 +55,6 @@
5555 private final CopyOnWriteArrayList<Host> liveHosts = new CopyOnWriteArrayList<Host>();
5656 private final AtomicInteger index = new AtomicInteger();
5757
58- private final CopyOnWriteArrayList<Host> suspectedHosts = new CopyOnWriteArrayList<Host>();
59-
6058 private volatile Configuration configuration;
6159 private volatile boolean hasLoggedLocalCLUse;
6260
@@ -132,23 +130,11 @@
132130
133131 private int idx = startIdx;
134132 private int remaining = hosts.size();
135- private Iterator<Host> suspected;
136133
137134 @Override
138135 protected Host computeNext() {
139- if (remaining <= 0) {
140-
141- if (suspected == null)
142- suspected = suspectedHosts.iterator();
143-
144- while (suspected.hasNext()) {
145- Host h = suspected.next();
146- waitOnReconnection(h);
147- if (h.isUp())
148- return h;
149- }
136+ if (remaining <= 0)
150137 return endOfData();
151- }
152138
153139 remaining--;
154140 int c = idx++ % hosts.size();
@@ -159,34 +145,18 @@
159145 };
160146 }
161147
162- private void waitOnReconnection(Host h) {
163- try {
164- h.getInitialReconnectionAttemptFuture().get(configuration.getSocketOptions().getConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
165- } catch (InterruptedException e) {
166- Thread.currentThread().interrupt();
167- } catch (ExecutionException e) {
168- throw new AssertionError(e);
169- } catch (TimeoutException e) {
170- // Shouldn't really happen but isn't really a huge deal
171- logger.debug("Timeout while waiting only host initial reconnection future", e);
172- }
173- }
174-
175148 @Override
176149 public void onUp(Host host) {
177150 liveHosts.addIfAbsent(host);
178- suspectedHosts.remove(host);
179151 }
180152
181153 @Override
182154 public void onSuspected(Host host) {
183- suspectedHosts.addIfAbsent(host);
184155 }
185156
186157 @Override
187158 public void onDown(Host host) {
188159 liveHosts.remove(host);
189- suspectedHosts.remove(host);
190160 }
191161
192162 @Override
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +0 −7
@@ -53,13 +53,6 @@
5353 return this;
5454 }
5555
56- public HostAssert isNotReconnectingFromSuspected() {
57- assertThat(actual.getInitialReconnectionAttemptFuture() != null && !actual.getInitialReconnectionAttemptFuture().isDone())
58- .isFalse();
59- return this;
60-
61- }
62-
6356 public HostAssert comesUpWithin(long duration, TimeUnit unit) {
6457 final CountDownLatch upSignal = new CountDownLatch(1);
6558 StateListener upListener = new StateListenerBase() {
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +2 −4
@@ -111,8 +111,7 @@
111111 // Node 1 should now be DOWN with no reconnection attempt
112112 assertThat(mainCluster).host(1)
113113 .hasState(DOWN)
114- .isNotReconnectingFromDown()
115- .isNotReconnectingFromSuspected();
114+ .isNotReconnectingFromDown();
116115 }
117116
118117 @Test(groups = "long")
@@ -135,8 +134,7 @@
135134 // Node 1 should now be DOWN with no reconnection attempt
136135 assertThat(mainCluster).host(1)
137136 .hasState(DOWN)
138- .isNotReconnectingFromDown()
139- .isNotReconnectingFromSuspected();
137+ .isNotReconnectingFromDown();
140138 }
141139
142140 @AfterMethod(groups = "long")
modified driver-core/src/test/java/com/datastax/driver/core/policies/LimitingLoadBalancingPolicy.java +0 −7
@@ -133,13 +133,6 @@
133133 onAdd(host);
134134 }
135135
136- @Override
137- public void onSuspected(Host host) {
138- // If a node gets suspected, it means the driver was connected to it, so it was a chosen node.
139- // Keep it as chosen for now, but notify the child policy which might reorder the query plan accordingly.
140- delegate.onSuspected(host);
141- }
142-
143136 @Override
144137 public void onDown(Host host) {
145138 delegate.onDown(host);