Skip to content

Commit

Permalink
Remove the ConnectionPool warning size feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Jun 14, 2024
1 parent 5a40a35 commit 2e48870
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ public interface DataSourceAlert {
*/
void dataSourceDown(DataSource dataSource, SQLException reason);

/**
* Send an alert to say the dataSource is getting close to its max size.
*/
void dataSourceWarning(DataSource dataSource, String msg);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,4 @@ default SQLException getDataSourceDownReason() {
*/
void setMaxSize(int max);

/**
* Deprecated - looking to remove.
* <p>
* Set a new maximum size. The pool should respect this new warning level immediately
* and not require a restart. We may want to increase the maxConnections if the
* pool gets large and hits the warning levels.
*/
@Deprecated(forRemoval = true)
void setWarningSize(int warningSize);

/**
* Deprecated - looking to remove.
* <p>
* Return the warning size. When the pool hits this size it can send a
* warning message to an administrator.
*/
@Deprecated(forRemoval = true)
int getWarningSize();

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ final class ConnectionPool implements DataSourcePool {
*/
private final AtomicBoolean dataSourceUp = new AtomicBoolean(false);
private SQLException dataSourceDownReason;
private final AtomicBoolean inWarningMode = new AtomicBoolean();
private final int minConnections;
private int maxConnections;
private int warningSize;
private final int waitTimeoutMillis;
private final int pstmtCacheSize;
private final PooledConnectionQueue queue;
Expand Down Expand Up @@ -276,19 +274,6 @@ public SQLException dataSourceDownReason() {
return dataSourceDownReason;
}

/**
* Called when the pool hits the warning level.
*/
void notifyWarning(String msg) {
if (inWarningMode.compareAndSet(false, true)) {
// send an Error to the event log...
Log.warn(msg);
if (notify != null) {
notify.dataSourceWarning(this, msg);
}
}
}

private void notifyDataSourceIsDown(SQLException reason) {
if (dataSourceUp.get()) {
reset();
Expand Down Expand Up @@ -448,17 +433,6 @@ int minSize() {
return minConnections;
}

@Override
public void setWarningSize(int warningSize) {
queue.setWarningSize(warningSize);
this.warningSize = warningSize;
}

@Override
public int getWarningSize() {
return warningSize;
}

/**
* Return the time in millis that threads will wait when the pool has hit
* the max size. These threads wait for connections to be returned by the
Expand Down Expand Up @@ -587,7 +561,6 @@ PooledConnection createConnectionForQueue(int connId) throws SQLException {
*/
private void reset() {
queue.reset(leakTimeMinutes);
inWarningMode.set(false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ final class PooledConnectionQueue {
private final Condition notEmpty;
private int connectionId;
private final long waitTimeoutMillis;
private final long leakTimeMinutes;
private final long maxAgeMillis;
private final int minSize;
private int warningSize;
private int maxSize;
/**
* Number of threads in the wait queue.
Expand Down Expand Up @@ -72,9 +70,7 @@ final class PooledConnectionQueue {
this.name = pool.name();
this.minSize = pool.minSize();
this.maxSize = pool.maxSize();
this.warningSize = pool.getWarningSize();
this.waitTimeoutMillis = pool.waitTimeoutMillis();
this.leakTimeMinutes = pool.leakTimeMinutes();
this.maxAgeMillis = pool.maxAgeMillis();
this.validateStaleMillis = pool.validateStaleMillis();
this.busyList = new BusyConnectionBuffer(maxSize, 20);
Expand Down Expand Up @@ -128,18 +124,6 @@ void setMaxSize(int maxSize) {
}
}

void setWarningSize(int warningSize) {
lock.lock();
try {
if (warningSize > this.maxSize) {
throw new IllegalArgumentException("warningSize " + warningSize + " > maxSize " + this.maxSize);
}
this.warningSize = warningSize;
} finally {
lock.unlock();
}
}

private int totalConnections() {
return freeList.size() + busyList.size();
}
Expand Down Expand Up @@ -253,7 +237,6 @@ private PooledConnection _obtainConnection() throws InterruptedException, SQLExc
if (Log.isLoggable(DEBUG)) {
Log.debug("DataSource [{0}] grow; id[{1}] busy[{2}] max[{3}]", name, c.name(), busySize, maxSize);
}
checkForWarningSize();
return c;
}
}
Expand Down Expand Up @@ -422,23 +405,6 @@ void closeBusyConnections(long leakTimeMinutes) {
}
}

/**
* As the pool grows it gets closer to the maxConnections limit. We can send
* an Alert (or warning) as we get close to this limit and hence an
* Administrator could increase the pool size if desired.
* <p>
* This is called whenever the pool grows in size (towards the max limit).
*/
private void checkForWarningSize() {
// the total number of connections that we can add
// to the pool before it hits the maximum
int availableGrowth = (maxSize - totalConnections());
if (availableGrowth < warningSize) {
closeBusyConnections(leakTimeMinutes);
pool.notifyWarning("DataSource [" + name + "] is [" + availableGrowth + "] connections from its maximum size.");
}
}

String getBusyConnectionInformation() {
return getBusyConnectionInformation(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ public class ConnectionPoolDbOutageTest implements DataSourceAlert, WaitFor {
private static final Logger log = LoggerFactory.getLogger(ConnectionPoolDbOutageTest.class);
private String connString;
private Connection h2Conn;

private int up;
private int down;
private int warn;



@AfterEach
void afterAll() throws SQLException {
h2Conn.close();
}

/**
* Changes the password and closes all exisitng sessions.
*/
Expand All @@ -47,11 +45,11 @@ void setPassword(String pw) throws SQLException {
sessions.add(rs.getInt(1));
}
}

try (Statement stmt = h2Conn.createStatement()) {
stmt.execute("alter user sa set password '" + pw + "'");
}

h2Conn.close();
// reopen connection with new credentials and kill old session
h2Conn = DriverManager.getConnection(connString, "sa", pw);
Expand Down Expand Up @@ -82,11 +80,11 @@ private DataSourceConfig config() {
public void testOfflineFromStart() throws InterruptedException, SQLException {
connString = "jdbc:h2:mem:testOfflineFromStart";
h2Conn = DriverManager.getConnection(connString, "sa", "unknown");

DataSourceConfig config = config();
assertThat(down).isEqualTo(0);
ConnectionPool pool = new ConnectionPool("mem:testOfflineFromStart", config);
//

waitFor(() -> {
assertThat(pool.isOnline()).isFalse();
assertThat(up).isEqualTo(0);
Expand All @@ -104,14 +102,13 @@ public void testOfflineFromStart() throws InterruptedException, SQLException {
assertThat(down).isEqualTo(1);
assertThat(pool.size()).isEqualTo(1);
});

}

@Test
public void testOfflineDuringRun() throws InterruptedException, SQLException {
connString = "jdbc:h2:mem:testOfflineDuringRun";
h2Conn = DriverManager.getConnection(connString, "sa", "sa");

DataSourceConfig config = config();
ConnectionPool pool = new ConnectionPool("testOfflineDuringRun", config);
waitFor(()-> {
Expand All @@ -121,15 +118,15 @@ public void testOfflineDuringRun() throws InterruptedException, SQLException {
assertThat(pool.size()).isEqualTo(3);
});
log.info("pool created ");

// simulate an outage
setPassword("outage");
waitFor(()-> {
assertThat(pool.isOnline()).isFalse();
assertThat(up).isEqualTo(1);
assertThat(down).isEqualTo(1);
});

// recover from outage
setPassword("sa");
waitFor(()-> {
Expand All @@ -148,9 +145,4 @@ public void dataSourceUp(DataSource dataSource) {
public void dataSourceDown(DataSource dataSource, SQLException reason) {
down++;
}

@Override
public void dataSourceWarning(DataSource dataSource, String msg) {
warn++;
}
}

0 comments on commit 2e48870

Please sign in to comment.