Skip to content

Commit 1289f83

Browse files
committed
fix: drop per-shard pending borrow counter
1 parent 8ed26d8 commit 1289f83

2 files changed

Lines changed: 9 additions & 43 deletions

File tree

driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ class HostConnectionPool implements Connection.Owner {
9191
@VisibleForTesting Set<Connection>[] trash;
9292

9393
private Queue<PendingBorrow>[] pendingBorrows;
94-
private AtomicInteger[] pendingBorrowCountByShard;
9594
final AtomicInteger pendingBorrowCount = new AtomicInteger();
9695

9796
private AtomicInteger[] scheduledForCreation;
@@ -211,12 +210,10 @@ public void tempBlockAdvShardAwareness(long millis) {
211210
}
212211

213212
int pendingBorrowCountForShard(int shardId) {
214-
if (pendingBorrowCountByShard == null
215-
|| shardId < 0
216-
|| shardId >= pendingBorrowCountByShard.length) {
213+
if (pendingBorrows == null || shardId < 0 || shardId >= pendingBorrows.length) {
217214
return OperationTimedOutException.UNAVAILABLE;
218215
}
219-
return pendingBorrowCountByShard[shardId].get();
216+
return pendingBorrows[shardId].size();
220217
}
221218

222219
int openConnectionCountForShard(int shardId) {
@@ -331,13 +328,11 @@ ListenableFuture<Void> initAsyncWithConnection(Connection reusedConnection) {
331328
open = new AtomicInteger[shardsCount];
332329
trash = new Set[shardsCount];
333330
pendingBorrows = new Queue[shardsCount];
334-
pendingBorrowCountByShard = new AtomicInteger[shardsCount];
335331
for (int i = 0; i < shardsCount; ++i) {
336332
this.connections[i] = new CopyOnWriteArrayList<Connection>();
337333
scheduledForCreation[i] = new AtomicInteger();
338334
open[i] = new AtomicInteger();
339335
trash[i] = new CopyOnWriteArraySet<Connection>();
340-
pendingBorrowCountByShard[i] = new AtomicInteger();
341336
pendingBorrows[i] = new ConcurrentLinkedQueue<PendingBorrow>();
342337
}
343338

@@ -653,7 +648,6 @@ private ListenableFuture<Connection> enqueue(
653648
}
654649

655650
PendingBorrow pendingBorrow = new PendingBorrow(timeout, unit, timeoutsExecutor);
656-
pendingBorrowCountByShard[shardId].incrementAndGet();
657651
pendingBorrows[shardId].add(pendingBorrow);
658652

659653
// If we raced with shutdown, make sure the future will be completed. This has no effect if it
@@ -718,7 +712,6 @@ private void dequeue(final Connection connection) {
718712
connection.inFlight.decrementAndGet();
719713
} else {
720714
pendingBorrowCount.decrementAndGet();
721-
pendingBorrowCountByShard[connection.shardId()].decrementAndGet();
722715
// Ensure that the keyspace set on the connection is the one set on the pool state, in the
723716
// general case it will be.
724717
ListenableFuture<Connection> setKeyspaceFuture =

driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTimeoutDiagnosticsTest.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
import static org.testng.Assert.assertEquals;
1919

2020
import java.lang.reflect.Field;
21-
import java.util.AbstractQueue;
22-
import java.util.Iterator;
21+
import java.util.ArrayDeque;
2322
import java.util.Queue;
24-
import java.util.concurrent.atomic.AtomicInteger;
2523
import org.objenesis.ObjenesisStd;
2624
import org.testng.annotations.Test;
2725

2826
public class HostConnectionPoolTimeoutDiagnosticsTest {
2927

3028
@Test(groups = "unit")
31-
public void should_use_constant_time_pending_borrow_count_per_shard() throws Exception {
29+
public void should_report_pending_borrow_queue_size_per_shard() throws Exception {
3230
HostConnectionPool pool = allocateInstance(HostConnectionPool.class);
33-
setField(pool, "pendingBorrows", new Queue[] {new ThrowingSizeQueue()});
34-
setField(pool, "pendingBorrowCountByShard", new AtomicInteger[] {new AtomicInteger(6)});
31+
Queue<Object> pendingBorrows = new ArrayDeque<Object>();
32+
for (int i = 0; i < 6; i++) {
33+
pendingBorrows.add(new Object());
34+
}
35+
setField(pool, "pendingBorrows", new Queue[] {pendingBorrows});
3536

3637
assertEquals(pool.pendingBorrowCountForShard(0), 6);
3738
}
@@ -55,32 +56,4 @@ private static void setField(Object target, String name, Object value) throws Ex
5556
}
5657
throw new NoSuchFieldException(name);
5758
}
58-
59-
private static class ThrowingSizeQueue extends AbstractQueue<Object> {
60-
61-
@Override
62-
public Iterator<Object> iterator() {
63-
throw new UnsupportedOperationException();
64-
}
65-
66-
@Override
67-
public int size() {
68-
throw new AssertionError("pendingBorrowCountForShard must not traverse the queue");
69-
}
70-
71-
@Override
72-
public boolean offer(Object o) {
73-
throw new UnsupportedOperationException();
74-
}
75-
76-
@Override
77-
public Object poll() {
78-
throw new UnsupportedOperationException();
79-
}
80-
81-
@Override
82-
public Object peek() {
83-
throw new UnsupportedOperationException();
84-
}
85-
}
8659
}

0 commit comments

Comments
 (0)