Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package io.ebean.datasource.pool;

import java.util.*;

/**
* A buffer designed especially to hold pooled connections (free and busy ones)
* <p>
* All thread safety controlled externally (by PooledConnectionQueue).
* </p>
*/
final class ConnectionBuffer {


private final Node free = Node.init();

int freeSize = 0;

/**
* Return the number of entries in the buffer.
*/
int freeSize() {
return freeSize;
}

/**
* Return true if the buffer is empty.
*/
boolean hasFreeConnections() {
return freeSize > 0;
}

/**
* Add connection to the free list.
*/
void addFree(PooledConnection pc) {
new Node(pc).addAfter(free);
freeSize++;
}

/**
* Remove a connection from the free list. Returns <code>null</code> if there is not any.
*/
PooledConnection popFree () {
Node node = free.next;
if (node.isBoundaryNode()) {
return null;
}
node.remove();
freeSize--;
return node.pc;
}

/**
* Close all connections in the free list.
*/
void closeAllFree(boolean logErrors) {
List<PooledConnection> tempList = new ArrayList<>();
PooledConnection c = popFree();
while (c != null) {
tempList.add(c);
c = popFree();
}

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<PooledConnection> trim(int minSize, long usedSince, long createdSince, int maxTrim) {
var trimmed = new ArrayList<PooledConnection>();
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.
* <p>
* 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 <code>this</code> after <code>node</code>.
* <p>
* 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;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -90,15 +90,15 @@ 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;
this.lastMaxResetNanos = nanoTime.getAsLong() - MAX_METRICS_WINDOW_NANOS;
}

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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand All @@ -519,22 +520,22 @@ private void createReservedConnections(int firstConnectionId, int numberToAdd, l
private List<PooledConnection> trimInactiveConnections(long maxInactiveMillis, long maxAgeMillis) {
final long createdSince = (maxAgeMillis == 0) ? 0 : System.currentTimeMillis() - maxAgeMillis;
final List<PooledConnection> 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;
}
Expand All @@ -545,7 +546,7 @@ private List<PooledConnection> trimInactiveConnections(long maxInactiveMillis, l
private void closeFreeConnections(boolean logErrors) {
lock.lock();
try {
freeList.closeAll(logErrors);
buffer.closeAllFree(logErrors);
} finally {
lock.unlock();
}
Expand Down
Loading
Loading