-
Notifications
You must be signed in to change notification settings - Fork 4
Internal heartbeat Timer, use Virtual Thread for Java 21 #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+294
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.ebean</groupId> | ||
<artifactId>ebean-datasource-parent</artifactId> | ||
<version>9.6</version> | ||
</parent> | ||
|
||
<artifactId>blackbox-tests</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.release>21</maven.compiler.release> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.ebean</groupId> | ||
<artifactId>ebean-datasource</artifactId> | ||
<version>9.6</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.avaje</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>1.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.ebean</groupId> | ||
<artifactId>ebean-test-containers</artifactId> | ||
<version>7.8</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>42.7.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.5.17</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-jdk-platform-logging</artifactId> | ||
<version>2.0.17</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.avaje</groupId> | ||
<artifactId>avaje-slf4j-jpl</artifactId> | ||
<version>1.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
64 changes: 64 additions & 0 deletions
64
blackbox-tests/src/test/java/org/example/tests/Java21TrimAndShutdownTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.example.tests; | ||
|
||
import io.ebean.datasource.DataSourceBuilder; | ||
import io.ebean.datasource.DataSourcePool; | ||
import io.ebean.test.containers.PostgresContainer; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
class Java21TrimAndShutdownTest { | ||
|
||
private static Logger log = LoggerFactory.getLogger(Java21TrimAndShutdownTest.class); | ||
|
||
@BeforeAll | ||
static void before() { | ||
PostgresContainer.builder("15") | ||
.port(9999) | ||
.containerName("pool_test") | ||
.dbName("app") | ||
.user("db_owner") | ||
.build() | ||
.startWithDropCreate(); | ||
} | ||
|
||
@Test | ||
void test() throws InterruptedException, SQLException { | ||
Properties clientInfo = new Properties(); | ||
clientInfo.setProperty("ApplicationName", "my-test"); | ||
|
||
DataSourcePool pool = DataSourceBuilder.create() | ||
.url("jdbc:postgresql://127.0.0.1:9999/app") | ||
.username("db_owner") | ||
.password("test") | ||
.clientInfo(clientInfo) | ||
.maxInactiveTimeSecs(2) | ||
.heartbeatFreqSecs(1) | ||
.trimPoolFreqSecs(1) | ||
.build(); | ||
|
||
List<Connection> connectionList = new ArrayList<>(); | ||
for (int i = 0; i < 50; i++) { | ||
connectionList.add(pool.getConnection()); | ||
} | ||
|
||
// close them slowly to allow multiple trims | ||
for (Connection connection : connectionList) { | ||
connection.rollback(); | ||
connection.close(); | ||
Thread.sleep(200); | ||
} | ||
|
||
log.info("----------- Sleep allowing trim -------------"); | ||
Thread.sleep(9_000); | ||
log.info("----------- Shutdown pool -------------"); | ||
pool.shutdown(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<configuration scan="true" scanPeriod="10 seconds"> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="java.lang" level="WARN"/> | ||
<logger name="io.ebean" level="INFO"/> | ||
<logger name="io.avaje.config" level="TRACE"/> | ||
<logger name="io.ebean.docker" level="DEBUG"/> | ||
<logger name="io.ebean.test" level="TRACE"/> | ||
<logger name="io.ebean.datasource" level="TRACE"/> | ||
|
||
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 55 additions & 1 deletion
56
ebean-datasource/src/main/java/io/ebean/datasource/pool/ExecutorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,65 @@ | ||
package io.ebean.datasource.pool; | ||
|
||
import io.ebean.datasource.pool.ConnectionPool.Heartbeat; | ||
|
||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
final class ExecutorFactory { | ||
|
||
static ExecutorService newExecutor() { | ||
return Executors.newSingleThreadExecutor(); | ||
return Executors.newSingleThreadExecutor(factory()); | ||
} | ||
|
||
private static ThreadFactory factory() { | ||
return runnable -> { | ||
Thread thread = new Thread(runnable); | ||
thread.setName("datasource.reaper"); | ||
return thread; | ||
}; | ||
} | ||
|
||
/** | ||
* Return a new Heartbeat for the pool. | ||
*/ | ||
static Heartbeat newHeartBeat(ConnectionPool pool, int freqMillis) { | ||
final Timer timer = new Timer(nm(pool.name()), true); | ||
timer.scheduleAtFixedRate(new HeartbeatTask(pool), freqMillis, freqMillis); | ||
return new TimerHeartbeat(timer); | ||
} | ||
|
||
private static String nm(String poolName) { | ||
return poolName.isEmpty() ? "datasource.heartbeat" : "datasource." + poolName + ".heartbeat"; | ||
} | ||
|
||
private static final class TimerHeartbeat implements Heartbeat { | ||
|
||
private final Timer timer; | ||
|
||
private TimerHeartbeat(Timer timer) { | ||
this.timer = timer; | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
timer.cancel(); | ||
} | ||
} | ||
|
||
private static final class HeartbeatTask extends TimerTask { | ||
|
||
private final ConnectionPool pool; | ||
|
||
private HeartbeatTask(ConnectionPool pool) { | ||
this.pool = pool; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
pool.heartbeat(); | ||
} | ||
} | ||
} |
58 changes: 57 additions & 1 deletion
58
ebean-datasource/src/main/java21/io/ebean/datasource/pool/ExecutorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,67 @@ | ||
package io.ebean.datasource.pool; | ||
|
||
import io.ebean.datasource.pool.ConnectionPool.Heartbeat; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
final class ExecutorFactory { | ||
|
||
static ExecutorService newExecutor() { | ||
return Executors.newVirtualThreadPerTaskExecutor(); | ||
ThreadFactory factory = Thread.ofVirtual().name("datasource.reaper").factory(); | ||
return Executors.newThreadPerTaskExecutor(factory); | ||
} | ||
|
||
static Heartbeat newHeartBeat(ConnectionPool pool, int freqMillis) { | ||
return new VTHeartbeat(pool, freqMillis).start(); | ||
} | ||
|
||
private static final class VTHeartbeat implements Heartbeat { | ||
|
||
private final AtomicBoolean running = new AtomicBoolean(false); | ||
private final ConnectionPool pool; | ||
private final int freqMillis; | ||
private final Thread thread; | ||
|
||
private VTHeartbeat(ConnectionPool pool, int freqMillis) { | ||
this.pool = pool; | ||
this.freqMillis = freqMillis; | ||
this.thread = Thread.ofVirtual() | ||
.name(nm(pool.name())) | ||
.unstarted(this::run); | ||
} | ||
|
||
private static String nm(String poolName) { | ||
return poolName.isEmpty() ? "datasource.heartbeat" : "datasource." + poolName + ".heartbeat"; | ||
} | ||
|
||
private void run() { | ||
while (running.get()) { | ||
try { | ||
Thread.sleep(freqMillis); | ||
pool.heartbeat(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
break; | ||
} catch (Exception e) { | ||
// continue heartbeat | ||
Log.warn("Error during heartbeat", e); | ||
} | ||
} | ||
} | ||
|
||
private Heartbeat start() { | ||
running.set(true); | ||
thread.start(); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
running.set(false); | ||
thread.interrupt(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool: I didn't know this method until now:
Rolands note: Classes will be located in
META-INF/versions/21
https://docs.oracle.com/javase/10/docs/specs/jar/jar.html#multi-release-jar-files