feat(agent): add shared JDBC pooling and metadata sessions

This commit is contained in:
skyler authored and GitHub committed 2026-07-30 21:10:01 +08:00
1 parent 07ff185573
commit a7a0b69605
37 files changed
+2867 -398

No files matched your search

@@ -19,9 +19,16 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
private static final String GAUSSDB_COMPATIBILITY_SQL =
"SELECT datcompatibility FROM pg_catalog.pg_database WHERE datname = current_database()";
private Connection connection;
private volatile Connection connection;
private String configuredDatabase = "";
private String identifierQuote = "";
private JdbcConnectionPoolRegistry poolRegistry;
private JdbcConnectionPoolRegistry.Lease pooledLease;
private ConnectParams connectParams;
private String poolIdentity;
private boolean requestActive;
private boolean leasePinnedAtRequestStart;
private boolean sessionAffinity;
@Override
public final Connection getConnection() {
@@ -36,11 +43,37 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
@Override
public final void connect(ConnectParams params) {
uncheckedVoid(() -> {
closeCurrentConnection();
sessionAffinity = false;
requestActive = false;
leasePinnedAtRequestStart = false;
loadDriver(params);
connection = openConnection(params);
configuredDatabase = params.getDatabase();
afterConnect(params, connection);
identifierQuote = resolveIdentifierQuote(params, connection);
connectParams = params;
poolIdentity = buildPoolIdentity(params);
if (poolRegistry == null) {
connection = openInitializedConnection(params);
afterConnect(params, connection);
identifierQuote = resolveIdentifierQuote(params, connection);
return;
}
JdbcConnectionPoolRegistry.Lease lease = borrowPooledConnection();
connection = lease.connection();
try {
afterConnect(params, connection);
identifierQuote = resolveIdentifierQuote(params, connection);
} catch (Exception error) {
lease.evict();
throw error;
} finally {
if (!preparePooledConnectionForReturn()) {
lease.evict();
} else {
lease.close();
}
connection = null;
}
});
}
@@ -53,8 +86,8 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
public final Map<String, Object> testConnectionWithInfo(ConnectParams params) {
return unchecked(() -> {
loadDriver(params);
try (Connection conn = openConnection(params)) {
boolean valid = conn.isValid(5);
try (Connection conn = openTestConnection(params)) {
boolean valid = conn != null && conn.isValid(5);
Map<String, Object> result = new LinkedHashMap<>();
result.put("ok", valid);
if (valid) {
@@ -73,7 +106,7 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
return JdbcDatabaseInfo.from(getConnection());
}
private void loadDriver(ConnectParams params) throws Exception {
protected void loadDriver(ConnectParams params) throws Exception {
List<String> driverPaths = params.getJdbc_driver_paths();
String driverClass = params.getJdbc_driver_class();
if (driverClass == null || driverClass.isEmpty()) {
@@ -214,16 +247,90 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
}
@Override
public void disconnect() {
public synchronized void disconnect() {
uncheckedVoid(() -> {
if (connection != null) {
connection.close();
}
connection = null;
closeCurrentConnection();
afterDisconnect();
connectParams = null;
poolIdentity = null;
sessionAffinity = false;
requestActive = false;
leasePinnedAtRequestStart = false;
identifierQuote = "";
});
}
final synchronized void attachConnectionPoolRegistry(JdbcConnectionPoolRegistry registry) {
if (connectParams != null || connection != null || pooledLease != null) {
throw new IllegalStateException("JDBC pool registry must be attached before connecting");
}
poolRegistry = registry;
}
final synchronized boolean usesConnectionPool() {
return poolRegistry != null;
}
final synchronized void beginPooledRequest() throws Exception {
if (poolRegistry == null) {
return;
}
if (connectParams == null) {
throw new IllegalStateException("Not connected");
}
requestActive = true;
leasePinnedAtRequestStart = pooledLease != null;
if (pooledLease == null) {
try {
pooledLease = borrowPooledConnection();
} catch (Exception error) {
requestActive = false;
throw error;
}
}
connection = pooledLease.connection();
}
final synchronized void finishPooledRequest(
JdbcExecutor executor,
boolean succeeded,
boolean requiresSessionAffinity,
boolean evictAfterRequest
) {
if (poolRegistry == null) {
return;
}
requestActive = false;
if (succeeded && requiresSessionAffinity) {
sessionAffinity = true;
JdbcSchemaSwitcher.forget(connection);
}
if (pooledLease == null) {
connection = null;
return;
}
if (evictAfterRequest && leasePinnedAtRequestStart) {
sessionAffinity = true;
} else if (evictAfterRequest || (!succeeded && !leasePinnedAtRequestStart)) {
releasePooledConnection(true);
return;
}
if (sessionAffinity || executor.hasOpenSessions() || executor.hasActiveStatements()) {
return;
}
releasePooledConnection(!preparePooledConnectionForReturn());
}
final synchronized void releaseIdlePooledConnection(JdbcExecutor executor) {
if (poolRegistry == null || requestActive || sessionAffinity || pooledLease == null) {
return;
}
if (executor.hasOpenSessions() || executor.hasActiveStatements()) {
return;
}
releasePooledConnection(!preparePooledConnectionForReturn());
}
private static String resolveIdentifierQuote(ConnectParams params, Connection connection) {
if (isPostgresCompatibleJdbc(params)) {
try (Statement statement = connection.createStatement()) {
@@ -301,16 +408,46 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
return DriverManager.getConnection(buildJdbcUrl(params), params.getUsername(), params.getPassword());
}
protected Connection openTestConnection(ConnectParams params) throws Exception {
return openConnection(params);
}
protected void afterPhysicalConnect(ConnectParams params, Connection connection) throws Exception {
}
protected void afterConnect(ConnectParams params, Connection connection) throws Exception {
}
protected void afterDisconnect() throws Exception {
}
protected void beforeQueryExecution(Connection connection, int timeoutSecs) throws Exception {
}
protected void beforePooledConnectionReturn(Connection connection) throws Exception {
}
protected final void applySchemaContext(Connection connection, String schema) throws Exception {
JdbcSchemaSwitcher.apply(connection, schema, this::setSchemaSQL, this::resetSchemaSQL);
}
protected String getConfiguredDatabase() {
return configuredDatabase;
}
protected static <T> T unwrapConnection(Connection connection, Class<T> connectionType) {
if (connectionType.isInstance(connection)) {
return connectionType.cast(connection);
}
try {
if (connection.isWrapperFor(connectionType)) {
return connection.unwrap(connectionType);
}
} catch (Exception ignored) {
}
return null;
}
protected Object resultValue(ResultSet rs, int index, int sqlType) {
return unchecked(() -> {
Object value;
@@ -349,4 +486,103 @@ public abstract class AbstractJdbcAgent extends BaseDatabaseAgent {
protected JdbcExecutor.ResultValueReader resultValueReader() {
return this::resultValue;
}
private Connection openInitializedConnection(ConnectParams params) throws Exception {
Connection opened = openConnection(params);
boolean initialized = false;
try {
afterPhysicalConnect(params, opened);
initialized = true;
return opened;
} finally {
if (!initialized) {
try {
opened.close();
} catch (Exception ignored) {
}
}
}
}
private JdbcConnectionPoolRegistry.Lease borrowPooledConnection() throws Exception {
ConnectParams params = connectParams;
if (params == null || poolIdentity == null || poolRegistry == null) {
throw new IllegalStateException("Not connected");
}
return poolRegistry.borrow(poolIdentity, () -> openInitializedConnection(params));
}
private void closeCurrentConnection() throws Exception {
if (pooledLease != null) {
boolean evict = sessionAffinity || !preparePooledConnectionForReturn();
releasePooledConnection(evict);
} else if (connection != null) {
connection.close();
connection = null;
}
}
private boolean preparePooledConnectionForReturn() {
try {
beforePooledConnectionReturn(connection);
} catch (Exception ignored) {
return false;
}
return JdbcSchemaSwitcher.resetBeforeReturn(connection);
}
private void releasePooledConnection(boolean evict) {
JdbcConnectionPoolRegistry.Lease lease = pooledLease;
pooledLease = null;
connection = null;
leasePinnedAtRequestStart = false;
if (lease == null) {
return;
}
if (evict) {
lease.evict();
} else {
lease.close();
}
}
private String buildPoolIdentity(ConnectParams params) {
StringBuilder identity = new StringBuilder();
appendPoolIdentity(identity, "agentClass", getClass().getName());
appendPoolIdentity(identity, "driverClass", effectiveDriverClass(params));
appendPoolIdentity(identity, "jdbcUrl", buildJdbcUrl(params));
appendPoolIdentity(identity, "host", params.getHost());
appendPoolIdentity(identity, "port", Integer.toString(params.getPort()));
appendPoolIdentity(identity, "database", params.getDatabase());
appendPoolIdentity(identity, "username", params.getUsername());
appendPoolIdentity(identity, "password", params.getPassword());
appendPoolIdentity(identity, "urlParams", params.getUrl_params());
appendPoolIdentity(identity, "connectionString", params.getConnection_string());
appendPoolIdentity(identity, "portExplicit", Boolean.toString(params.isPort_explicit()));
appendPoolIdentity(identity, "mysqlCompatMode", Boolean.toString(params.isMysql_compat_mode()));
appendPoolIdentity(identity, "ssl", Boolean.toString(params.isSsl()));
appendPoolIdentity(identity, "caCertPath", params.getCa_cert_path());
appendPoolIdentity(identity, "clientCertPath", params.getClient_cert_path());
appendPoolIdentity(identity, "clientKeyPath", params.getClient_key_path());
appendPoolIdentity(identity, "gbaseServer", params.getGbase_server());
appendPoolIdentity(identity, "informixServer", params.getInformix_server());
List<String> driverPaths = params.getJdbc_driver_paths();
if (driverPaths != null) {
for (int index = 0; index < driverPaths.size(); index++) {
appendPoolIdentity(identity, "driverPath" + index, driverPaths.get(index));
}
}
return identity.toString();
}
private String effectiveDriverClass(ConnectParams params) {
String configured = params.getJdbc_driver_class();
return configured == null || configured.isEmpty() ? driverClass() : configured;
}
private static void appendPoolIdentity(StringBuilder identity, String key, String value) {
String normalized = value == null ? "" : value;
identity.append(key.length()).append(':').append(key)
.append('=').append(normalized.length()).append(':').append(normalized).append(';');
}
}
@@ -0,0 +1,177 @@
package com.dbx.agent;
import java.util.Locale;
import java.util.regex.Pattern;
final class JdbcConnectionAffinity {
private static final Pattern STATEFUL_STATEMENT = Pattern.compile(
"(?is)(?:^|;)\\s*(?:BEGIN(?:\\s+(?:WORK|TRANSACTION))?\\b|START\\s+TRANSACTION\\b|SET\\b|RESET\\b|UNSET\\b|"
+ "USE\\b|DATABASE\\b|ALTER\\s+SESSION\\b|CREATE\\s+(?:(?:GLOBAL|LOCAL)\\s+)?TEMP(?:ORARY)?\\b|"
+ "CREATE\\s+(?:(?:SET|MULTISET)\\s+)?VOLATILE\\b|CREATE\\s+TABLE\\s+(?:#{1,2}|\\[#{1,2})|"
+ "SELECT\\s+.+?\\s+INTO\\s+(?:(?:(?:GLOBAL|LOCAL)\\s+)?TEMP(?:ORARY)?\\b|#{1,2}|\\[#{1,2})|"
+ "DECLARE\\b|PREPARE\\b|DEALLOCATE\\b|ATTACH\\b|DETACH\\b|PRAGMA\\b|CALL\\b|EXEC(?:UTE)?\\b|DO\\b|"
+ "LISTEN\\b|UNLISTEN\\b|LOCK\\s+TABLES?\\b|UNLOCK\\s+TABLES?\\b|LOAD\\b|INSTALL\\b|"
+ "ADD\\s+(?:JAR|FILE|ARCHIVE)\\b|DELETE\\s+(?:JAR|FILE|ARCHIVE)\\b|CACHE\\s+TABLE\\b|UNCACHE\\s+TABLE\\b)"
);
private static final Pattern STATEFUL_FUNCTION = Pattern.compile(
"(?is)\\b(?:SET_CONFIG|PG_ADVISORY_(?:XACT_)?LOCK|GET_LOCK|RELEASE_LOCK|SP_GETAPPLOCK|DBMS_LOCK)\\s*\\("
);
private static final Pattern USER_VARIABLE_ASSIGNMENT = Pattern.compile("(?is)(?:SET\\s+)?@[A-Z0-9_$]+\\s*(?::=|=)");
private static final Pattern TEMPORARY_TABLE_REFERENCE = Pattern.compile(
"(?is)(?:^|[^A-Z0-9_$])(?:#{1,2}|\\[#{1,2})[A-Z0-9_$]+"
);
private JdbcConnectionAffinity() {
}
static boolean requiresSessionAffinity(String sql) {
if (sql == null) {
return false;
}
String normalized = sanitizeSql(sql).trim().toUpperCase(Locale.ROOT);
if (normalized.isEmpty()) {
return false;
}
return STATEFUL_STATEMENT.matcher(normalized).find()
|| STATEFUL_FUNCTION.matcher(normalized).find()
|| USER_VARIABLE_ASSIGNMENT.matcher(normalized).find()
|| TEMPORARY_TABLE_REFERENCE.matcher(normalized).find();
}
private static String sanitizeSql(String sql) {
StringBuilder sanitized = new StringBuilder(sql.length());
int index = 0;
while (index < sql.length()) {
char current = sql.charAt(index);
if (current == '-' && index + 1 < sql.length() && sql.charAt(index + 1) == '-') {
index = sanitizeLineComment(sql, sanitized, index);
} else if (current == '#' && isLineCommentStart(sql, index)) {
index = sanitizeLineComment(sql, sanitized, index);
} else if (current == '/' && index + 1 < sql.length() && sql.charAt(index + 1) == '*') {
index = sanitizeBlockComment(sql, sanitized, index);
} else if (current == '\'' || current == '"' || current == '`') {
index = sanitizeQuoted(sql, sanitized, index, current);
} else if (current == '[') {
index = sanitizeQuoted(sql, sanitized, index, ']');
} else if (current == '$') {
String delimiter = dollarQuoteDelimiter(sql, index);
if (delimiter == null) {
sanitized.append(current);
index += 1;
} else {
int closing = sql.indexOf(delimiter, index + delimiter.length());
if (closing < 0) {
sanitized.append(current);
index += 1;
} else {
int end = closing + delimiter.length();
appendSanitized(sanitized, sql, index, end);
index = end;
}
}
} else {
sanitized.append(current);
index += 1;
}
}
return sanitized.toString();
}
private static int sanitizeLineComment(String sql, StringBuilder sanitized, int start) {
int end = start + (sql.charAt(start) == '#' ? 1 : 2);
while (end < sql.length() && sql.charAt(end) != '\n' && sql.charAt(end) != '\r') {
end += 1;
}
appendSanitized(sanitized, sql, start, end);
return end;
}
private static int sanitizeBlockComment(String sql, StringBuilder sanitized, int start) {
int closing = sql.indexOf("*/", start + 2);
int end = closing < 0 ? sql.length() : closing + 2;
int executableStart = executableCommentContentStart(sql, start, closing);
if (executableStart >= 0) {
appendSanitized(sanitized, sql, start, executableStart);
sanitized.append(sanitizeSql(sql.substring(executableStart, closing)));
appendSanitized(sanitized, sql, closing, end);
return end;
}
appendSanitized(sanitized, sql, start, end);
return end;
}
private static boolean isLineCommentStart(String sql, int index) {
for (int previous = index - 1; previous >= 0; previous--) {
char current = sql.charAt(previous);
if (current == '\n' || current == '\r') {
return true;
}
if (!Character.isWhitespace(current)) {
return current == ';';
}
}
return true;
}
private static int executableCommentContentStart(String sql, int start, int closing) {
if (closing < 0) {
return -1;
}
int contentStart;
if (sql.startsWith("/*!", start)) {
contentStart = start + 3;
} else if (sql.startsWith("/*M!", start) || sql.startsWith("/*m!", start)) {
contentStart = start + 4;
} else {
return -1;
}
while (contentStart < closing && Character.isDigit(sql.charAt(contentStart))) {
contentStart += 1;
}
return contentStart;
}
private static int sanitizeQuoted(String sql, StringBuilder sanitized, int start, char closing) {
int index = start + 1;
while (index < sql.length()) {
char current = sql.charAt(index);
if (current == closing) {
if (index + 1 < sql.length() && sql.charAt(index + 1) == closing) {
index += 2;
continue;
}
index += 1;
break;
}
index += 1;
}
if (closing == ']' && start + 1 < sql.length() && sql.charAt(start + 1) == '#') {
sanitized.append(sql, start, index);
} else {
appendSanitized(sanitized, sql, start, index);
}
return index;
}
private static String dollarQuoteDelimiter(String sql, int start) {
int end = start + 1;
while (end < sql.length()) {
char current = sql.charAt(end);
if (current == '$') {
return sql.substring(start, end + 1);
}
if (!(Character.isLetterOrDigit(current) || current == '_')) {
return null;
}
end += 1;
}
return null;
}
private static void appendSanitized(StringBuilder sanitized, String sql, int start, int end) {
for (int index = start; index < end; index++) {
char current = sql.charAt(index);
sanitized.append(current == '\n' || current == '\r' ? current : ' ');
}
}
}
@@ -0,0 +1,516 @@
package com.dbx.agent;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
final class JdbcConnectionPoolRegistry implements AutoCloseable {
private static final int DEFAULT_MAXIMUM_POOL_SIZE = 8;
private static final int DEFAULT_MINIMUM_IDLE = 0;
private static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 30_000L;
private static final long DEFAULT_VALIDATION_TIMEOUT_MILLIS = 5_000L;
private static final long DEFAULT_IDLE_TIMEOUT_MILLIS = 120_000L;
private static final long DEFAULT_MAX_LIFETIME_MILLIS = 1_800_000L;
private static final long DEFAULT_POOL_RETIRE_MILLIS = 300_000L;
private final Map<String, PoolEntry> pools = new ConcurrentHashMap<>();
private final PoolSettings settings;
private final AtomicBoolean closed = new AtomicBoolean();
JdbcConnectionPoolRegistry() {
this(PoolSettings.fromEnvironment());
}
JdbcConnectionPoolRegistry(PoolSettings settings) {
this.settings = Objects.requireNonNull(settings, "settings");
}
Lease borrow(String identity, ConnectionFactory connectionFactory) throws Exception {
String key = digest(identity);
while (true) {
if (closed.get()) {
throw new IllegalStateException("JDBC connection pool registry is closed");
}
PoolEntry entry;
try {
entry = pools.computeIfAbsent(key, ignored -> createPoolEntry(key, connectionFactory));
} catch (PoolCreationException error) {
throw error.unwrap();
}
try {
return entry.borrow();
} catch (PoolRetiredException ignored) {
pools.remove(key, entry);
}
}
}
int poolCount() {
return pools.size();
}
boolean isEnabled() {
return settings.enabled;
}
private PoolEntry createPoolEntry(String key, ConnectionFactory connectionFactory) {
ConnectionFactoryDataSource factoryDataSource = null;
try {
Connection initialConnection = Objects.requireNonNull(
connectionFactory.open(),
"JDBC connection factory returned null"
);
factoryDataSource = new ConnectionFactoryDataSource(connectionFactory, initialConnection);
HikariConfig config = new HikariConfig();
config.setPoolName("dbx-jdbc-" + key.substring(0, 12));
config.setDataSource(factoryDataSource);
config.setMaximumPoolSize(settings.maximumPoolSize);
config.setMinimumIdle(settings.minimumIdle);
config.setConnectionTimeout(settings.connectionTimeoutMillis);
config.setValidationTimeout(settings.validationTimeoutMillis);
config.setIdleTimeout(settings.idleTimeoutMillis);
config.setMaxLifetime(settings.maxLifetimeMillis);
config.setInitializationFailTimeout(-1L);
config.setIsolateInternalQueries(true);
config.setThreadFactory(daemonThreadFactory("dbx-jdbc-pool-" + key.substring(0, 8)));
return new PoolEntry(new HikariDataSource(config), factoryDataSource, settings.poolRetireMillis);
} catch (Exception error) {
if (factoryDataSource != null) {
factoryDataSource.closeUnusedInitialConnection();
}
throw new PoolCreationException(error);
}
}
void retireUnusedPools() {
if (closed.get()) {
return;
}
long now = System.currentTimeMillis();
List<PoolEntry> retired = new ArrayList<>();
for (String key : pools.keySet()) {
pools.computeIfPresent(key, (ignored, entry) -> {
if (entry.tryRetire(now)) {
retired.add(entry);
return null;
}
return entry;
});
}
for (PoolEntry entry : retired) {
entry.closeRetired();
}
}
@Override
public void close() {
if (!closed.compareAndSet(false, true)) {
return;
}
for (PoolEntry entry : pools.values()) {
entry.close();
}
pools.clear();
}
private static String digest(String identity) {
try {
byte[] hash = MessageDigest.getInstance("SHA-256").digest(identity.getBytes(StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder(hash.length * 2);
for (byte value : hash) {
result.append(Character.forDigit((value >>> 4) & 0x0f, 16));
result.append(Character.forDigit(value & 0x0f, 16));
}
return result.toString();
} catch (Exception error) {
throw new IllegalStateException("SHA-256 is unavailable", error);
}
}
private static ThreadFactory daemonThreadFactory(String name) {
return runnable -> {
Thread thread = new Thread(runnable, name);
thread.setDaemon(true);
return thread;
};
}
@FunctionalInterface
interface ConnectionFactory {
Connection open() throws Exception;
}
static final class Lease implements AutoCloseable {
private final PoolEntry entry;
private final Connection connection;
private final AtomicBoolean closed = new AtomicBoolean();
private Lease(PoolEntry entry, Connection connection) {
this.entry = entry;
this.connection = connection;
}
Connection connection() {
return connection;
}
void evict() {
if (closed.compareAndSet(false, true)) {
entry.release(connection, true);
}
}
@Override
public void close() {
if (closed.compareAndSet(false, true)) {
entry.release(connection, false);
}
}
}
static final class PoolSettings {
private final boolean enabled;
private final int maximumPoolSize;
private final int minimumIdle;
private final long connectionTimeoutMillis;
private final long validationTimeoutMillis;
private final long idleTimeoutMillis;
private final long maxLifetimeMillis;
private final long poolRetireMillis;
PoolSettings(
int maximumPoolSize,
int minimumIdle,
long connectionTimeoutMillis,
long validationTimeoutMillis,
long idleTimeoutMillis,
long maxLifetimeMillis,
long poolRetireMillis
) {
this(
true,
maximumPoolSize,
minimumIdle,
connectionTimeoutMillis,
validationTimeoutMillis,
idleTimeoutMillis,
maxLifetimeMillis,
poolRetireMillis
);
}
PoolSettings(
boolean enabled,
int maximumPoolSize,
int minimumIdle,
long connectionTimeoutMillis,
long validationTimeoutMillis,
long idleTimeoutMillis,
long maxLifetimeMillis,
long poolRetireMillis
) {
this.enabled = enabled;
this.maximumPoolSize = maximumPoolSize;
this.minimumIdle = minimumIdle;
this.connectionTimeoutMillis = connectionTimeoutMillis;
this.validationTimeoutMillis = validationTimeoutMillis;
this.idleTimeoutMillis = idleTimeoutMillis;
this.maxLifetimeMillis = maxLifetimeMillis;
this.poolRetireMillis = poolRetireMillis;
}
static PoolSettings fromEnvironment() {
int maximumPoolSize = intSetting(
"dbx.agent.jdbc.pool.maximumPoolSize",
"DBX_AGENT_JDBC_POOL_MAXIMUM_POOL_SIZE",
DEFAULT_MAXIMUM_POOL_SIZE,
1,
32
);
int minimumIdle = intSetting(
"dbx.agent.jdbc.pool.minimumIdle",
"DBX_AGENT_JDBC_POOL_MINIMUM_IDLE",
DEFAULT_MINIMUM_IDLE,
0,
maximumPoolSize
);
long connectionTimeoutMillis = longSetting(
"dbx.agent.jdbc.pool.connectionTimeoutMillis",
"DBX_AGENT_JDBC_POOL_CONNECTION_TIMEOUT_MILLIS",
DEFAULT_CONNECTION_TIMEOUT_MILLIS,
250L
);
long validationTimeoutMillis = Math.min(
connectionTimeoutMillis,
longSetting(
"dbx.agent.jdbc.pool.validationTimeoutMillis",
"DBX_AGENT_JDBC_POOL_VALIDATION_TIMEOUT_MILLIS",
DEFAULT_VALIDATION_TIMEOUT_MILLIS,
250L
)
);
return new PoolSettings(
booleanSetting(
"dbx.agent.jdbc.pool.enabled",
"DBX_AGENT_JDBC_POOL_ENABLED",
true
),
maximumPoolSize,
minimumIdle,
connectionTimeoutMillis,
validationTimeoutMillis,
longSetting(
"dbx.agent.jdbc.pool.idleTimeoutMillis",
"DBX_AGENT_JDBC_POOL_IDLE_TIMEOUT_MILLIS",
DEFAULT_IDLE_TIMEOUT_MILLIS,
10_000L
),
longSetting(
"dbx.agent.jdbc.pool.maxLifetimeMillis",
"DBX_AGENT_JDBC_POOL_MAX_LIFETIME_MILLIS",
DEFAULT_MAX_LIFETIME_MILLIS,
30_000L
),
longSetting(
"dbx.agent.jdbc.pool.retireMillis",
"DBX_AGENT_JDBC_POOL_RETIRE_MILLIS",
DEFAULT_POOL_RETIRE_MILLIS,
60_000L
)
);
}
private static int intSetting(String property, String environment, int defaultValue, int minimum, int maximum) {
String value = setting(property, environment);
if (value == null) {
return defaultValue;
}
try {
return Math.max(minimum, Math.min(maximum, Integer.parseInt(value.trim())));
} catch (NumberFormatException ignored) {
return defaultValue;
}
}
private static long longSetting(String property, String environment, long defaultValue, long minimum) {
String value = setting(property, environment);
if (value == null) {
return defaultValue;
}
try {
return Math.max(minimum, Long.parseLong(value.trim()));
} catch (NumberFormatException ignored) {
return defaultValue;
}
}
private static boolean booleanSetting(String property, String environment, boolean defaultValue) {
String value = setting(property, environment);
if (value == null) {
return defaultValue;
}
String normalized = value.trim();
if ("true".equalsIgnoreCase(normalized) || "1".equals(normalized) || "yes".equalsIgnoreCase(normalized)) {
return true;
}
if ("false".equalsIgnoreCase(normalized) || "0".equals(normalized) || "no".equalsIgnoreCase(normalized)) {
return false;
}
return defaultValue;
}
private static String setting(String property, String environment) {
String value = System.getProperty(property);
return value == null || value.trim().isEmpty() ? System.getenv(environment) : value;
}
}
private static final class PoolEntry implements AutoCloseable {
private final HikariDataSource dataSource;
private final ConnectionFactoryDataSource factoryDataSource;
private final long retireMillis;
private int activeLeases;
private boolean retired;
private boolean dataSourceClosed;
private volatile long lastReleasedAtMillis = System.currentTimeMillis();
private PoolEntry(
HikariDataSource dataSource,
ConnectionFactoryDataSource factoryDataSource,
long retireMillis
) {
this.dataSource = dataSource;
this.factoryDataSource = factoryDataSource;
this.retireMillis = retireMillis;
}
private Lease borrow() throws SQLException {
synchronized (this) {
if (retired) {
throw new PoolRetiredException();
}
activeLeases += 1;
}
try {
return new Lease(this, dataSource.getConnection());
} catch (SQLException | RuntimeException error) {
synchronized (this) {
activeLeases -= 1;
lastReleasedAtMillis = System.currentTimeMillis();
}
throw error;
}
}
private void release(Connection connection, boolean evict) {
try {
if (evict) {
dataSource.evictConnection(connection);
}
connection.close();
} catch (Exception ignored) {
} finally {
synchronized (this) {
lastReleasedAtMillis = System.currentTimeMillis();
activeLeases -= 1;
}
}
}
private synchronized boolean tryRetire(long nowMillis) {
if (retired || activeLeases != 0 || nowMillis - lastReleasedAtMillis < retireMillis) {
return false;
}
retired = true;
return true;
}
private void closeRetired() {
synchronized (this) {
if (dataSourceClosed) {
return;
}
dataSourceClosed = true;
}
factoryDataSource.closeUnusedInitialConnection();
dataSource.close();
}
@Override
public void close() {
synchronized (this) {
retired = true;
}
closeRetired();
}
}
private static final class PoolRetiredException extends SQLException {
private PoolRetiredException() {
super("JDBC connection pool was retired");
}
}
private static final class ConnectionFactoryDataSource implements DataSource {
private final ConnectionFactory connectionFactory;
private final AtomicReference<Connection> initialConnection;
private ConnectionFactoryDataSource(ConnectionFactory connectionFactory, Connection initialConnection) {
this.connectionFactory = connectionFactory;
this.initialConnection = new AtomicReference<>(initialConnection);
}
@Override
public Connection getConnection() throws SQLException {
Connection opened = initialConnection.getAndSet(null);
if (opened != null) {
return opened;
}
try {
return connectionFactory.open();
} catch (SQLException error) {
throw error;
} catch (Exception error) {
throw new SQLException("Failed to open JDBC connection", error);
}
}
private void closeUnusedInitialConnection() {
Connection opened = initialConnection.getAndSet(null);
if (opened == null) {
return;
}
try {
opened.close();
} catch (Exception ignored) {
}
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return getConnection();
}
@Override
public PrintWriter getLogWriter() {
return null;
}
@Override
public void setLogWriter(PrintWriter out) {
}
@Override
public void setLoginTimeout(int seconds) {
}
@Override
public int getLoginTimeout() {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return Logger.getLogger("com.dbx.agent.jdbc.pool");
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return iface.cast(this);
}
throw new SQLException("Not a wrapper for " + iface.getName());
}
@Override
public boolean isWrapperFor(Class<?> iface) {
return iface.isInstance(this);
}
}
private static final class PoolCreationException extends RuntimeException {
private PoolCreationException(Exception cause) {
super(cause);
}
private Exception unwrap() {
Throwable cause = getCause();
return cause instanceof Exception ? (Exception) cause : this;
}
}
}
@@ -254,6 +254,7 @@ public final class JdbcExecutor {
applySchema(conn, schema, setSchemaSql, resetSchemaSql);
Statement stmt = conn.createStatement();
QuerySession createdSession = null;
activeStatements.add(stmt);
try {
applyQueryTimeout(stmt, options.getTimeoutSecs());
@@ -303,9 +304,13 @@ public final class JdbcExecutor {
Math.max(options.getMaxRows(), 1),
valueReader
);
createdSession = session;
targetSessions.put(sessionId, session);
return readSessionPage(targetSessions, session, options.getPageSize(), elapsed);
} catch (Exception e) {
if (createdSession != null) {
closeSession(targetSessions, createdSession.id);
}
activeStatements.remove(stmt);
try {
stmt.close();
@@ -356,6 +361,23 @@ public final class JdbcExecutor {
closeAllTableReadSessions();
}
public boolean hasOpenSessions() {
return !sessions.isEmpty() || !tableReadSessions.isEmpty();
}
public boolean hasActiveStatements() {
return !activeStatements.isEmpty();
}
public int expireIdleResources() {
return expireIdleQuerySessions() + expireIdleTableReadSessions();
}
int expireIdleResources(long nowMillis, long idleTimeoutMillis) {
return expireIdleQuerySessions(nowMillis, idleTimeoutMillis)
+ expireIdleTableReadSessions(nowMillis, idleTimeoutMillis);
}
public int expireIdleQuerySessions() {
return expireIdleQuerySessions(System.currentTimeMillis(), QUERY_SESSION_IDLE_TIMEOUT_MILLIS);
}
@@ -516,7 +538,12 @@ public final class JdbcExecutor {
throw new IllegalArgumentException(missingMessage);
}
synchronized (session) {
return readSessionPage(targetSessions, session, pageSize, 0L);
try {
return readSessionPage(targetSessions, session, pageSize, 0L);
} catch (RuntimeException | Error error) {
closeSession(targetSessions, sessionId);
throw error;
}
}
}
@@ -10,7 +10,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
final class JdbcSchemaSwitcher {
private static final Map<Connection, String> RESET_SQL_BY_CONNECTION =
private static final Map<Connection, ResetState> RESET_STATE_BY_CONNECTION =
Collections.synchronizedMap(new WeakHashMap<>());
private JdbcSchemaSwitcher() {
@@ -27,37 +27,33 @@ final class JdbcSchemaSwitcher {
Supplier<String> resetSchemaSql
) throws Exception {
if (schema == null || schema.trim().isEmpty()) {
String resetSql = RESET_SQL_BY_CONNECTION.remove(conn);
if (resetSql == null) {
return;
}
SchemaSqlResult resetResult = applySchemaSql(conn, () -> resetSql);
if (resetResult.attempted && resetResult.error != null) {
RESET_SQL_BY_CONNECTION.put(conn, resetSql);
throw resetResult.error;
if (!restore(conn, true)) {
throw new SQLException("Failed to restore the original JDBC schema context");
}
return;
}
ResetState resetState = resetState(conn);
SchemaSqlResult schemaSqlResult = applySchemaSql(conn, () -> setSchemaSql.apply(schema));
Exception schemaSqlError = schemaSqlResult.error;
if (schemaSqlError == null) {
rememberResetSql(conn, resetSchemaSql);
resetState.mode = ResetMode.SQL;
rememberResetSql(resetState, resetSchemaSql);
return;
}
try {
conn.setSchema(schema);
rememberResetSql(conn, resetSchemaSql);
resetState.mode = ResetMode.SCHEMA;
return;
} catch (SQLException | AbstractMethodError ignored) {
} catch (SQLException | UnsupportedOperationException | AbstractMethodError ignored) {
// Some JDBC drivers only expose schema switching through SQL.
}
try {
conn.setCatalog(schema);
rememberResetSql(conn, resetSchemaSql);
resetState.mode = ResetMode.CATALOG;
return;
} catch (SQLException | AbstractMethodError ignored) {
} catch (SQLException | UnsupportedOperationException | AbstractMethodError ignored) {
// Last fallback failed as well; surface the SQL-switch error below.
}
@@ -66,11 +62,58 @@ final class JdbcSchemaSwitcher {
}
}
private static void rememberResetSql(Connection conn, Supplier<String> resetSchemaSql) {
static boolean resetBeforeReturn(Connection conn) {
return restore(conn, false);
}
static void forget(Connection conn) {
RESET_STATE_BY_CONNECTION.remove(conn);
}
private static boolean restore(Connection conn, boolean preserveOnFailure) {
ResetState resetState = RESET_STATE_BY_CONNECTION.remove(conn);
if (resetState == null) {
return true;
}
try {
boolean restored = resetState.restore(conn);
if (!restored && preserveOnFailure) {
RESET_STATE_BY_CONNECTION.put(conn, resetState);
}
return restored;
} catch (Exception ignored) {
if (preserveOnFailure) {
RESET_STATE_BY_CONNECTION.put(conn, resetState);
}
return false;
}
}
private static ResetState resetState(Connection conn) {
synchronized (RESET_STATE_BY_CONNECTION) {
return RESET_STATE_BY_CONNECTION.computeIfAbsent(conn, JdbcSchemaSwitcher::captureResetState);
}
}
private static ResetState captureResetState(Connection conn) {
JdbcValue<String> schema = readJdbcValue(conn, true);
JdbcValue<String> catalog = readJdbcValue(conn, false);
return new ResetState(schema, catalog);
}
private static JdbcValue<String> readJdbcValue(Connection conn, boolean schema) {
try {
return new JdbcValue<>(true, schema ? conn.getSchema() : conn.getCatalog());
} catch (SQLException | UnsupportedOperationException | AbstractMethodError ignored) {
return new JdbcValue<>(false, null);
}
}
private static void rememberResetSql(ResetState resetState, Supplier<String> resetSchemaSql) {
try {
String sql = resetSchemaSql.get();
if (sql != null && !sql.trim().isEmpty()) {
RESET_SQL_BY_CONNECTION.put(conn, sql);
resetState.resetSql = sql;
}
} catch (RuntimeException ignored) {
// Drivers without a reset command keep the legacy no-op behavior.
@@ -95,6 +138,78 @@ final class JdbcSchemaSwitcher {
}
}
private enum ResetMode {
SQL,
SCHEMA,
CATALOG
}
private static final class JdbcValue<T> {
private final boolean supported;
private final T value;
private JdbcValue(boolean supported, T value) {
this.supported = supported;
this.value = value;
}
}
private static final class ResetState {
private final JdbcValue<String> schema;
private final JdbcValue<String> catalog;
private ResetMode mode;
private String resetSql;
private ResetState(JdbcValue<String> schema, JdbcValue<String> catalog) {
this.schema = schema;
this.catalog = catalog;
}
private boolean restore(Connection conn) throws Exception {
if (mode == ResetMode.SQL && resetSql != null) {
SchemaSqlResult result = applySchemaSql(conn, () -> resetSql);
return result.attempted && result.error == null;
}
if (mode == ResetMode.SCHEMA) {
return restoreSchema(conn);
}
if (mode == ResetMode.CATALOG) {
return restoreCatalog(conn);
}
if (mode == ResetMode.SQL) {
if (schema.supported && schema.value != null && restoreSchema(conn)) {
return true;
}
return catalog.supported && catalog.value != null && restoreCatalog(conn);
}
return true;
}
private boolean restoreSchema(Connection conn) {
if (!schema.supported) {
return false;
}
try {
conn.setSchema(schema.value);
return true;
} catch (SQLException | UnsupportedOperationException | AbstractMethodError ignored) {
return false;
}
}
private boolean restoreCatalog(Connection conn) {
if (!catalog.supported) {
return false;
}
try {
conn.setCatalog(catalog.value);
return true;
} catch (SQLException | UnsupportedOperationException | AbstractMethodError ignored) {
return false;
}
}
}
private static final class SchemaSqlResult {
private final boolean attempted;
private final Exception error;
@@ -89,11 +89,53 @@ public final class JsonRpcServer {
}
Object dispatchForRuntime(String method, JsonObject params) throws Exception {
return AgentExecutionContext.withJdbcExecutor(jdbcExecutor, () -> dispatch(method, params));
return AgentExecutionContext.withJdbcExecutor(jdbcExecutor, () -> {
AbstractJdbcAgent jdbcAgent = pooledJdbcAgent();
boolean manageConnection = jdbcAgent != null && requiresConnectedConnection(method);
if (manageConnection) {
jdbcAgent.beginPooledRequest();
}
boolean succeeded = false;
try {
Object result = dispatch(method, params);
succeeded = true;
return result;
} finally {
if (manageConnection) {
jdbcAgent.finishPooledRequest(
jdbcExecutor,
succeeded,
requiresSessionAffinity(method, params),
evictAfterRequest(method)
);
}
}
});
}
void cancelActiveStatements() {
jdbcExecutor.cancelActiveStatements();
AbstractJdbcAgent jdbcAgent = pooledJdbcAgent();
if (jdbcAgent != null) {
jdbcAgent.releaseIdlePooledConnection(jdbcExecutor);
}
}
void expireIdleResources() {
jdbcExecutor.expireIdleResources();
releaseIdlePooledConnection();
}
void expireIdleResources(long nowMillis, long idleTimeoutMillis) {
jdbcExecutor.expireIdleResources(nowMillis, idleTimeoutMillis);
releaseIdlePooledConnection();
}
private void releaseIdlePooledConnection() {
AbstractJdbcAgent jdbcAgent = pooledJdbcAgent();
if (jdbcAgent != null) {
jdbcAgent.releaseIdlePooledConnection(jdbcExecutor);
}
}
private Object dispatch(String method, JsonObject params) throws Exception {
@@ -101,8 +143,9 @@ public final class JsonRpcServer {
return AgentProtocol.handshakeResult();
}
if (AgentProtocol.METHOD_CONNECT.equals(method)) {
lastConnectParams = gson.fromJson(params, ConnectParams.class);
agent.connect(lastConnectParams);
ConnectParams connectParams = gson.fromJson(params, ConnectParams.class);
agent.connect(connectParams);
lastConnectParams = connectParams;
lastConnectionValidationTimeMillis = 0L;
return Collections.singletonMap("ok", true);
}
@@ -302,6 +345,10 @@ public final class JsonRpcServer {
if (lastConnectParams == null || !shouldValidateConnection(method)) {
return;
}
AbstractJdbcAgent jdbcAgent = pooledJdbcAgent();
if (jdbcAgent != null) {
return;
}
Connection conn = agent.getConnection();
if (conn == null) {
return;
@@ -344,6 +391,51 @@ public final class JsonRpcServer {
&& !AgentProtocol.METHOD_SHUTDOWN.equals(method);
}
private AbstractJdbcAgent pooledJdbcAgent() {
if (agent instanceof AbstractJdbcAgent jdbcAgent && jdbcAgent.usesConnectionPool()) {
return jdbcAgent;
}
return null;
}
private static boolean requiresConnectedConnection(String method) {
return !AgentProtocol.METHOD_HANDSHAKE.equals(method)
&& !AgentProtocol.METHOD_CONNECT.equals(method)
&& !AgentProtocol.METHOD_TEST_CONNECTION.equals(method)
&& !AgentProtocol.METHOD_DISCONNECT.equals(method)
&& !AgentProtocol.METHOD_SHUTDOWN.equals(method);
}
private boolean requiresSessionAffinity(String method, JsonObject params) {
try {
if (AgentProtocol.METHOD_EXECUTE_QUERY.equals(method)
|| AgentProtocol.METHOD_EXECUTE_QUERY_PAGE.equals(method)
|| AgentProtocol.METHOD_START_TABLE_READ.equals(method)) {
return JdbcConnectionAffinity.requiresSessionAffinity(stringOrNull(params, "sql"));
}
if (AgentProtocol.METHOD_EXECUTE_TRANSACTION.equals(method)
|| AgentProtocol.METHOD_EXECUTE_BATCH.equals(method)) {
JsonElement statements = params.get("statements");
if (statements == null || !statements.isJsonArray()) {
return false;
}
for (JsonElement statement : statements.getAsJsonArray()) {
if (!statement.isJsonNull()
&& JdbcConnectionAffinity.requiresSessionAffinity(statement.getAsString())) {
return true;
}
}
}
} catch (RuntimeException ignored) {
return false;
}
return false;
}
private static boolean evictAfterRequest(String method) {
return AgentProtocol.METHOD_GET_EXPLAIN_INFO.equals(method);
}
private static JsonObject paramsObject(JsonObject req) {
JsonElement params = req.get("params");
if (params == null || params instanceof JsonNull || !params.isJsonObject()) {
@@ -7,31 +7,58 @@ import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
public final class MultiSessionJsonRpcServer {
public final class MultiSessionJsonRpcServer implements AutoCloseable {
private static final String LEGACY_SESSION_ID = "__legacy__";
private static final int MAX_SESSIONS = 256;
private static final long MAINTENANCE_INTERVAL_MILLIS = 60_000L;
private final Supplier<? extends DatabaseAgent> agentFactory;
private final Map<String, Session> sessions = new ConcurrentHashMap<>();
private final ExecutorService requests = Executors.newCachedThreadPool();
private final JdbcConnectionPoolRegistry poolRegistry;
private final Gson gson = new Gson();
private final PrintStream protocolOutput = System.out;
private final Object outputLock = new Object();
private final Object maintenanceLock = new Object();
private final AtomicBoolean closed = new AtomicBoolean();
private ScheduledExecutorService maintenance;
public MultiSessionJsonRpcServer(Supplier<? extends DatabaseAgent> agentFactory) {
this(agentFactory, new JdbcConnectionPoolRegistry());
}
MultiSessionJsonRpcServer(
Supplier<? extends DatabaseAgent> agentFactory,
JdbcConnectionPoolRegistry.PoolSettings poolSettings
) {
this(agentFactory, new JdbcConnectionPoolRegistry(poolSettings));
}
private MultiSessionJsonRpcServer(
Supplier<? extends DatabaseAgent> agentFactory,
JdbcConnectionPoolRegistry poolRegistry
) {
this.agentFactory = agentFactory;
this.poolRegistry = poolRegistry;
}
public void run() {
synchronized (outputLock) {
System.out.println("{\"ready\":true}");
System.out.flush();
protocolOutput.println("{\"ready\":true}");
protocolOutput.flush();
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String line;
@@ -40,15 +67,14 @@ public final class MultiSessionJsonRpcServer {
String method = request.get("method").getAsString();
if (AgentProtocol.METHOD_SHUTDOWN.equals(method)) {
writeResponse(handleRequest(request));
closeAllSessions();
requests.shutdown();
return;
}
requests.submit(() -> writeResponse(handleRequest(request)));
}
} catch (Exception e) {
closeAllSessions();
throw new RuntimeException(e);
} finally {
close();
}
}
@@ -105,7 +131,12 @@ public final class MultiSessionJsonRpcServer {
if (sessions.size() >= MAX_SESSIONS && !sessions.containsKey(sessionId)) {
throw new IllegalStateException("Agent session limit reached: " + MAX_SESSIONS);
}
Session session = new Session(new JsonRpcServer(agentFactory.get()));
DatabaseAgent agent = agentFactory.get();
if (poolRegistry.isEnabled() && agent instanceof AbstractJdbcAgent jdbcAgent) {
jdbcAgent.attachConnectionPoolRegistry(poolRegistry);
ensureMaintenanceStarted();
}
Session session = new Session(new JsonRpcServer(agent));
Session existing = sessions.putIfAbsent(sessionId, session);
if (existing != null) {
throw new IllegalStateException("Agent session already exists: " + sessionId);
@@ -141,6 +172,71 @@ public final class MultiSessionJsonRpcServer {
}
}
void runMaintenance() {
for (Session session : sessions.values()) {
try {
session.expireIdleResources();
} catch (Exception ignored) {
}
}
poolRegistry.retireUnusedPools();
}
void runMaintenance(long nowMillis, long idleTimeoutMillis) {
for (Session session : sessions.values()) {
try {
session.expireIdleResources(nowMillis, idleTimeoutMillis);
} catch (Exception ignored) {
}
}
poolRegistry.retireUnusedPools();
}
private void ensureMaintenanceStarted() {
synchronized (maintenanceLock) {
if (maintenance != null || closed.get()) {
return;
}
maintenance = Executors.newSingleThreadScheduledExecutor(daemonThreadFactory("dbx-jdbc-maintenance"));
maintenance.scheduleWithFixedDelay(
this::runMaintenanceSafely,
MAINTENANCE_INTERVAL_MILLIS,
MAINTENANCE_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS
);
}
}
private void runMaintenanceSafely() {
try {
runMaintenance();
} catch (Throwable ignored) {
}
}
@Override
public void close() {
if (!closed.compareAndSet(false, true)) {
return;
}
synchronized (maintenanceLock) {
if (maintenance != null) {
maintenance.shutdownNow();
}
}
closeAllSessions();
requests.shutdown();
poolRegistry.close();
}
private static ThreadFactory daemonThreadFactory(String name) {
return runnable -> {
Thread thread = new Thread(runnable, name);
thread.setDaemon(true);
return thread;
};
}
private static String requiredSessionId(JsonObject params) {
if (!params.has("agentSessionId") || params.get("agentSessionId").getAsString().trim().isEmpty()) {
throw new IllegalArgumentException("agentSessionId is required");
@@ -150,31 +246,62 @@ public final class MultiSessionJsonRpcServer {
private void writeResponse(JsonObject response) {
synchronized (outputLock) {
System.out.println(gson.toJson(response));
System.out.flush();
protocolOutput.println(gson.toJson(response));
protocolOutput.flush();
}
}
private static final class Session {
private final JsonRpcServer server;
private final ReentrantLock lock = new ReentrantLock();
private Session(JsonRpcServer server) {
this.server = server;
}
private synchronized Object handle(String method, JsonObject params) throws Exception {
return server.dispatchForRuntime(method, params);
private Object handle(String method, JsonObject params) throws Exception {
lock.lock();
try {
return server.dispatchForRuntime(method, params);
} finally {
lock.unlock();
}
}
private synchronized void close() {
private void close() {
lock.lock();
try {
server.dispatchForRuntime(AgentProtocol.METHOD_DISCONNECT, new JsonObject());
} catch (Exception ignored) {
} finally {
lock.unlock();
}
}
private void cancel() {
server.cancelActiveStatements();
}
private void expireIdleResources() {
if (!lock.tryLock()) {
return;
}
try {
server.expireIdleResources();
} finally {
lock.unlock();
}
}
private void expireIdleResources(long nowMillis, long idleTimeoutMillis) {
if (!lock.tryLock()) {
return;
}
try {
server.expireIdleResources(nowMillis, idleTimeoutMillis);
} finally {
lock.unlock();
}
}
}
}