();
+ Node node = free; // first boundary node
+ do {
+ node = node.next;
+ } while (!node.isBoundaryNode() && minSize-- > 0);
+
+ while (!node.isBoundaryNode() && trimmed.size() < maxTrim) {
+ Node current = node;
+ node = node.next;
+ if (current.pc.shouldTrim(usedSince, createdSince)) {
+ current.remove();
+ freeSize--;
+ trimmed.add(current.pc);
+ }
+ }
+ return trimmed;
+ }
+
+ /**
+ * Node of a linkedlist. The linkedLists always have two empty nodes at the start and end.
+ * (boundary nodes) They are generated with the init() method.
+ *
+ * the first usable node is startNode.next (which could be the end edge)
+ */
+ static final class Node {
+
+ private Node next;
+ private Node prev;
+ final PooledConnection pc;
+
+ private Node(PooledConnection pc) {
+ this.pc = pc;
+ }
+
+ /**
+ * Creates new "list" with two empty boundary nodes
+ */
+ public static Node init() {
+ Node node1 = new Node(null);
+ Node node2 = new Node(null);
+ node1.next = node2;
+ node2.prev = node1;
+ return node1;
+ }
+
+ /**
+ * Retruns true, if this is a boundary node. (start or end node of list)
+ */
+ private boolean isBoundaryNode() {
+ return pc == null;
+ }
+
+ /**
+ * Removes the node from the list. The node can be re-added to an other list
+ */
+ private void remove() {
+ assert pc != null : "called remove a boundary node";
+ assert prev != null && next != null : "not part of a list";
+ next.prev = prev;
+ prev.next = next;
+ prev = null;
+ next = null;
+ }
+
+ /**
+ * Adds this after node.
+ *
+ * Node is in most cases a boundary node (e.g. start of list)
+ */
+ public void addAfter(Node node) {
+ assert !this.isBoundaryNode() : "this is a boundary node";
+ assert next == null & prev == null : "Node already member of a list";
+ next = node.next;
+ prev = node;
+ node.next.prev = this;
+ node.next = this;
+ }
+ }
+}
diff --git a/ebean-datasource/src/main/java/io/ebean/datasource/pool/FreeConnectionBuffer.java b/ebean-datasource/src/main/java/io/ebean/datasource/pool/FreeConnectionBuffer.java
deleted file mode 100644
index 4cc6b93f..00000000
--- a/ebean-datasource/src/main/java/io/ebean/datasource/pool/FreeConnectionBuffer.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package io.ebean.datasource.pool;
-
-import java.util.*;
-
-/**
- * A buffer designed especially to hold free pooled connections.
- *
- * All thread safety controlled externally (by PooledConnectionQueue).
- *
- */
-final class FreeConnectionBuffer {
-
- /**
- * Buffer oriented for add and remove.
- */
- private final LinkedList freeBuffer = new LinkedList<>();
-
- /**
- * Return the number of entries in the buffer.
- */
- int size() {
- return freeBuffer.size();
- }
-
- /**
- * Return true if the buffer is empty.
- */
- boolean isEmpty() {
- return freeBuffer.isEmpty();
- }
-
- /**
- * Add connection to the free list.
- */
- void add(PooledConnection pc) {
- freeBuffer.addFirst(pc);
- }
-
- /**
- * Remove a connection from the free list.
- */
- PooledConnection remove() {
- return freeBuffer.removeFirst();
- }
-
- /**
- * Close all connections in this buffer.
- */
- void closeAll(boolean logErrors) {
- List tempList = new ArrayList<>(freeBuffer);
- freeBuffer.clear();
- if (Log.isLoggable(System.Logger.Level.TRACE)) {
- Log.trace("... closing all {0} connections from the free list with logErrors: {1}", tempList.size(), logErrors);
- }
- for (PooledConnection connection : tempList) {
- connection.closeConnectionFully(logErrors);
- }
- }
-
- /**
- * Trim any inactive connections that have not been used since usedSince.
- */
- List trim(int minSize, long usedSince, long createdSince, int maxTrim) {
- var trimmed = new ArrayList();
- ListIterator iterator = freeBuffer.listIterator(minSize);
- while (iterator.hasNext() && trimmed.size() < maxTrim) {
- PooledConnection pooledConnection = iterator.next();
- if (pooledConnection.shouldTrim(usedSince, createdSince)) {
- iterator.remove();
- trimmed.add(pooledConnection);
- }
- }
- return trimmed;
- }
-}
diff --git a/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java b/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java
index 939fd2d7..730aa22d 100644
--- a/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java
+++ b/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java
@@ -23,7 +23,7 @@ final class PooledConnectionQueue {
/**
* A 'circular' buffer designed specifically for free connections.
*/
- private final FreeConnectionBuffer freeList;
+ private final ConnectionBuffer buffer;
/**
* A 'slots' buffer designed specifically for busy connections.
* Fast add remove based on slot id.
@@ -90,7 +90,7 @@ final class PooledConnectionQueue {
this.maxAgeMillis = pool.maxAgeMillis();
this.validateStaleMillis = pool.validateStaleMillis();
this.busyList = new BusyConnectionBuffer(maxSize, 20);
- this.freeList = new FreeConnectionBuffer();
+ this.buffer = new ConnectionBuffer();
this.lock = new ReentrantLock(false);
this.notEmpty = lock.newCondition();
this.nanoTime = nanoTime;
@@ -98,7 +98,7 @@ final class PooledConnectionQueue {
}
private PoolStatus createStatus() {
- return new Status(minSize, maxSize, freeList.size(), busyList.size(), waitingThreads, highWaterMark,
+ return new Status(minSize, maxSize, buffer.freeSize(), busyList.size(), waitingThreads, highWaterMark,
waitCount, hitCount, totalAcquireNanos, maxAcquireNanos, totalWaitNanos);
}
@@ -138,7 +138,7 @@ PoolStatus collect(boolean delta) {
var collectedHitCount = delta ? hitCount - lastHitCount : hitCount;
var collectedTotalAcquireNanos = delta ? totalAcquireNanos - lastTotalAcquireNanos : totalAcquireNanos;
var collectedTotalWaitNanos = delta ? totalWaitNanos - lastTotalWaitNanos : totalWaitNanos;
- var status = new Status(minSize, maxSize, freeList.size(), busyList.size(), waitingThreads, publishedHighWaterMark,
+ var status = new Status(minSize, maxSize, buffer.freeSize(), busyList.size(), waitingThreads, publishedHighWaterMark,
collectedWaitCount, collectedHitCount, collectedTotalAcquireNanos, publishedMaxAcquireNanos, collectedTotalWaitNanos);
if (delta) {
lastWaitCount = waitCount;
@@ -188,14 +188,14 @@ void setMaxSize(int maxSize) {
}
private int totalConnections() {
- return freeList.size() + busyList.size();
+ return buffer.freeSize() + busyList.size();
}
void createConnections(int numberToAdd) throws SQLException {
lock.lock();
try {
for (int i = 0; i < numberToAdd; i++) {
- freeList.add(pool.createConnectionForQueue(connectionId++));
+ buffer.addFree(pool.createConnectionForQueue(connectionId++));
}
notEmpty.signal();
} finally {
@@ -215,7 +215,7 @@ void returnPooledConnection(PooledConnection c, boolean forceClose) {
if (forceClose || c.shouldTrimOnReturn(lastResetTime, maxAgeMillis)) {
c.closeConnectionFully(false);
} else {
- freeList.add(c);
+ buffer.addFree(c);
notEmpty.signal();
}
} finally {
@@ -224,10 +224,11 @@ void returnPooledConnection(PooledConnection c, boolean forceClose) {
}
private PooledConnection extractFromFreeList() {
- if (freeList.isEmpty()) {
+
+ PooledConnection c = buffer.popFree();
+ if (c == null) {
return null;
}
- final PooledConnection c = freeList.remove();
if (validateStaleMillis > 0 && staleEviction(c)) {
c.closeConnectionFully(false);
return null;
@@ -349,7 +350,7 @@ private PooledConnection createConnectionOutsideLock(int id, long generation) th
} else {
int busySize = registerBusyConnection(connection);
if (Log.isLoggable(DEBUG)) {
- Log.debug("DataSource [{0}] grow; id[{1}] free[{2}] busy[{3}] max[{4}]", name, connection.name(), freeList.size(), busySize, maxSize);
+ Log.debug("DataSource [{0}] grow; id[{1}] free[{2}] busy[{3}] max[{4}]", name, connection.name(), buffer.freeSize(), busySize, maxSize);
}
}
} finally {
@@ -387,7 +388,7 @@ private PooledConnection _obtainConnectionWaitLoop() throws SQLException, Interr
try {
nanos = notEmpty.awaitNanos(nanos);
- if (!freeList.isEmpty()) {
+ if (buffer.hasFreeConnections()) {
// successfully waited
return extractFromFreeList();
}
@@ -459,7 +460,7 @@ void trim(long maxInactiveMillis, long maxAgeMillis) {
lock.lock();
try {
trimmedConnections = trimInactiveConnections(maxInactiveMillis, maxAgeMillis);
- int freeDeficit = minSize - freeList.size();
+ int freeDeficit = minSize - buffer.freeSize();
int capacity = maxSize - totalConnections() - creatingConnections;
add = Math.min(freeDeficit, capacity);
if (add > 0) {
@@ -494,12 +495,12 @@ private void createReservedConnections(int firstConnectionId, int numberToAdd, l
try {
creatingConnections--;
if (connection == null || doingShutdown || generation != resetGeneration
- || freeList.size() >= minSize || totalConnections() >= maxSize) {
+ || buffer.freeSize() >= minSize || totalConnections() >= maxSize) {
close = connection != null;
} else {
- freeList.add(connection);
+ buffer.addFree(connection);
if (Log.isLoggable(DEBUG)) {
- Log.debug("DataSource [{0}] grow reserve; id[{1}] free[{2}] busy[{3}] max[{4}]", name, connection.name(), freeList.size(), busyList.size(), maxSize);
+ Log.debug("DataSource [{0}] grow reserve; id[{1}] free[{2}] busy[{3}] max[{4}]", name, connection.name(), buffer.freeSize(), busyList.size(), maxSize);
}
notEmpty.signal();
}
@@ -519,22 +520,22 @@ private void createReservedConnections(int firstConnectionId, int numberToAdd, l
private List trimInactiveConnections(long maxInactiveMillis, long maxAgeMillis) {
final long createdSince = (maxAgeMillis == 0) ? 0 : System.currentTimeMillis() - maxAgeMillis;
final List trimmedConnections;
- if (freeList.size() > minSize) {
+ if (buffer.freeSize() > minSize) {
// trim on maxInactive and maxAge
long usedSince = System.currentTimeMillis() - maxInactiveMillis;
- int excess = freeList.size() - minSize;
+ int excess = buffer.freeSize() - minSize;
// Progressively reduce excess idle connections rather than closing them all at once.
int maxTrim = Math.max(1, (excess + 4) / 5);
- trimmedConnections = freeList.trim(minSize, usedSince, createdSince, maxTrim);
+ trimmedConnections = buffer.trim(minSize, usedSince, createdSince, maxTrim);
} else if (createdSince > 0) {
// trim only on maxAge
- trimmedConnections = freeList.trim(0, createdSince, createdSince, Integer.MAX_VALUE);
+ trimmedConnections = buffer.trim(0, createdSince, createdSince, Integer.MAX_VALUE);
} else {
trimmedConnections = List.of();
}
if (!trimmedConnections.isEmpty() && Log.isLoggable(DEBUG)) {
Log.debug("DataSource [{0}] trim [{1}] inactive connections. free[{2}] busy[{3}]",
- name, trimmedConnections.size(), freeList.size(), busyList.size());
+ name, trimmedConnections.size(), buffer.freeSize(), busyList.size());
}
return trimmedConnections;
}
@@ -545,7 +546,7 @@ private List trimInactiveConnections(long maxInactiveMillis, l
private void closeFreeConnections(boolean logErrors) {
lock.lock();
try {
- freeList.closeAll(logErrors);
+ buffer.closeAllFree(logErrors);
} finally {
lock.unlock();
}
diff --git a/ebean-datasource/src/test/java/io/ebean/datasource/pool/FreeConnectionBufferTest.java b/ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionBufferTest.java
similarity index 60%
rename from ebean-datasource/src/test/java/io/ebean/datasource/pool/FreeConnectionBufferTest.java
rename to ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionBufferTest.java
index c4ddcb7d..62e00064 100644
--- a/ebean-datasource/src/test/java/io/ebean/datasource/pool/FreeConnectionBufferTest.java
+++ b/ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionBufferTest.java
@@ -9,70 +9,70 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
-class FreeConnectionBufferTest {
+class ConnectionBufferTest {
@Test
void test() {
- FreeConnectionBuffer b = new FreeConnectionBuffer();
+ ConnectionBuffer b = new ConnectionBuffer();
PooledConnection p0 = new PooledConnection("0");
PooledConnection p1 = new PooledConnection("1");
PooledConnection p2 = new PooledConnection("2");
// PooledConnection p3 = new PooledConnection("3");
- assertEquals(0, b.size());
- assertTrue(b.isEmpty());
+ assertEquals(0, b.freeSize());
+ assertFalse(b.hasFreeConnections());
- b.add(p0);
+ b.addFree(p0);
- assertEquals(1, b.size());
- assertFalse(b.isEmpty());
+ assertEquals(1, b.freeSize());
+ assertTrue(b.hasFreeConnections());
- PooledConnection r0 = b.remove();
+ PooledConnection r0 = b.popFree();
assertThat(p0).isSameAs(r0);
- assertEquals(0, b.size());
- assertTrue(b.isEmpty());
+ assertEquals(0, b.freeSize());
+ assertFalse(b.hasFreeConnections());
- b.add(p0);
- b.add(p1);
- b.add(p2);
+ b.addFree(p0);
+ b.addFree(p1);
+ b.addFree(p2);
- assertEquals(3, b.size());
+ assertEquals(3, b.freeSize());
- PooledConnection r1 = b.remove();
+ PooledConnection r1 = b.popFree();
assertSame(p2, r1);
- PooledConnection r2 = b.remove();
+ PooledConnection r2 = b.popFree();
assertSame(p1, r2);
- assertEquals(1, b.size());
- b.add(p2);
- assertEquals(2, b.size());
- PooledConnection r3 = b.remove();
+ assertEquals(1, b.freeSize());
+ b.addFree(p2);
+ assertEquals(2, b.freeSize());
+ PooledConnection r3 = b.popFree();
assertSame(p2, r3);
- assertEquals(1, b.size());
- PooledConnection r4 = b.remove();
+ assertEquals(1, b.freeSize());
+ PooledConnection r4 = b.popFree();
assertSame(p0, r4);
- assertEquals(0, b.size());
+ assertEquals(0, b.freeSize());
- b.add(p2);
- b.add(p1);
- b.add(p0);
+ b.addFree(p2);
+ b.addFree(p1);
+ b.addFree(p0);
- assertEquals(3, b.size());
+ assertEquals(3, b.freeSize());
- PooledConnection r5 = b.remove();
+ PooledConnection r5 = b.popFree();
assertSame(p0, r5);
- assertEquals(2, b.size());
+ assertEquals(2, b.freeSize());
- PooledConnection r6 = b.remove();
+ PooledConnection r6 = b.popFree();
assertSame(p1, r6);
- assertEquals(1, b.size());
+ assertEquals(1, b.freeSize());
- PooledConnection r7 = b.remove();
+ PooledConnection r7 = b.popFree();
assertSame(p2, r7);
- assertEquals(0, b.size());
+ assertEquals(0, b.freeSize());
}