feat(agent): add shared JDBC pooling and metadata sessions
This commit is contained in:
1 parent
07ff185573
commit
a7a0b69605
37 files changed
+2867
-398
No files matched your search
@@ -1,6 +1,6 @@
|
||||
package com.dbx.agent.tdengine;
|
||||
|
||||
import com.dbx.agent.BaseDatabaseAgent;
|
||||
import com.dbx.agent.AbstractJdbcAgent;
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.ConnectParams;
|
||||
import com.dbx.agent.DatabaseInfo;
|
||||
@@ -46,7 +46,7 @@ import java.util.concurrent.TimeoutException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
public final class TDengineAgent extends AbstractJdbcAgent {
|
||||
private static final long TABLE_CACHE_TTL_MILLIS = 10_000L;
|
||||
private static final DateTimeFormatter TDENGINE_TIMESTAMP_FORMAT =
|
||||
new DateTimeFormatterBuilder()
|
||||
@@ -62,32 +62,63 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
private static final Pattern COMPOSITE_KEY_PATTERN =
|
||||
Pattern.compile("(?i)\\bCOMPOSITE\\s+KEY\\b");
|
||||
|
||||
private Connection connection;
|
||||
private final Object tableCacheLock = new Object();
|
||||
private String tableCacheSchema = "";
|
||||
private long tableCacheTimeMillis;
|
||||
private List<TableInfo> tableCache = Collections.emptyList();
|
||||
private Connection restfulStateConnection;
|
||||
private String restfulOriginalCatalog;
|
||||
private String restfulOriginalDatabase;
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
protected String driverClass() {
|
||||
return TDengineTransport.WEBSOCKET.driverClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(ConnectParams params) {
|
||||
uncheckedVoid(() -> {
|
||||
connection = TDengineConnectionFactory.open(params);
|
||||
clearTableCache();
|
||||
});
|
||||
protected String buildJdbcUrl(ConnectParams params) {
|
||||
String connectionString = params.getConnection_string() == null ? "" : params.getConnection_string().trim();
|
||||
if (!connectionString.isBlank()) {
|
||||
return TDengineJdbcUrl.sanitizeConnectionString(connectionString);
|
||||
}
|
||||
TDengineJdbcUrl.TransportPreference preference = TDengineJdbcUrl.transportPreference(params.getUrl_params());
|
||||
TDengineTransport transport = preference == TDengineJdbcUrl.TransportPreference.REST
|
||||
? TDengineTransport.REST
|
||||
: TDengineTransport.WEBSOCKET;
|
||||
return TDengineJdbcUrl.from(params, transport);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testConnection(ConnectParams params) {
|
||||
return unchecked(() -> {
|
||||
try (Connection conn = TDengineConnectionFactory.open(params)) {
|
||||
return conn.isValid(5);
|
||||
}
|
||||
});
|
||||
protected void loadDriver(ConnectParams params) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection openConnection(ConnectParams params) throws Exception {
|
||||
return TDengineConnectionFactory.open(params);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterConnect(ConnectParams params, Connection connection) {
|
||||
clearTableCache();
|
||||
clearRestfulState();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void beforePooledConnectionReturn(Connection connection) throws Exception {
|
||||
if (restfulStateConnection != connection) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
restoreRestfulConnectionState(connection, restfulOriginalCatalog, restfulOriginalDatabase);
|
||||
} finally {
|
||||
clearRestfulState();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterDisconnect() {
|
||||
clearTableCache();
|
||||
clearRestfulState();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -215,7 +246,7 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
options.getMaxRows(),
|
||||
options.getFetchSize(),
|
||||
options.getTimeoutSecs(),
|
||||
this::tdengineResultValue
|
||||
this::resultValue
|
||||
);
|
||||
if (mayChangeMetadata(sql)) {
|
||||
clearTableCache();
|
||||
@@ -231,7 +262,7 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
prepareExecutionSchema(schema),
|
||||
this::setSchemaSQL,
|
||||
options,
|
||||
this::tdengineResultValue
|
||||
this::resultValue
|
||||
);
|
||||
}
|
||||
|
||||
@@ -243,7 +274,7 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
prepareExecutionSchema(schema),
|
||||
this::setSchemaSQL,
|
||||
options,
|
||||
this::tdengineResultValue
|
||||
this::resultValue
|
||||
);
|
||||
}
|
||||
|
||||
@@ -271,11 +302,22 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
}
|
||||
|
||||
private String prepareExecutionSchema(String schema) {
|
||||
return unchecked(() -> prepareExecutionSchema(requireConnected(), schema));
|
||||
return unchecked(() -> {
|
||||
Connection connection = requireConnected();
|
||||
if (schema != null
|
||||
&& !schema.trim().isEmpty()
|
||||
&& unwrapConnection(connection, RestfulConnection.class) != null
|
||||
&& restfulStateConnection != connection) {
|
||||
restfulStateConnection = connection;
|
||||
restfulOriginalCatalog = connection.getCatalog();
|
||||
restfulOriginalDatabase = connection.getClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME);
|
||||
}
|
||||
return prepareExecutionSchema(connection, schema);
|
||||
});
|
||||
}
|
||||
|
||||
static String prepareExecutionSchema(Connection connection, String schema) throws SQLException {
|
||||
if (!(connection instanceof RestfulConnection) || schema == null || schema.trim().isEmpty()) {
|
||||
if (unwrapConnection(connection, RestfulConnection.class) == null || schema == null || schema.trim().isEmpty()) {
|
||||
return schema;
|
||||
}
|
||||
|
||||
@@ -286,15 +328,13 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
uncheckedVoid(() -> {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
connection = null;
|
||||
clearTableCache();
|
||||
});
|
||||
static void restoreRestfulConnectionState(Connection connection, String catalog, String database) throws SQLException {
|
||||
connection.setCatalog(catalog);
|
||||
if (database == null) {
|
||||
connection.getClientInfo().remove(TSDBDriver.PROPERTY_KEY_DBNAME);
|
||||
} else {
|
||||
connection.setClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME, database);
|
||||
}
|
||||
}
|
||||
|
||||
private List<TableInfo> queryTables(String sql, String tableType, boolean includesStableName) throws Exception {
|
||||
@@ -342,6 +382,12 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
}
|
||||
}
|
||||
|
||||
private void clearRestfulState() {
|
||||
restfulStateConnection = null;
|
||||
restfulOriginalCatalog = null;
|
||||
restfulOriginalDatabase = null;
|
||||
}
|
||||
|
||||
private static boolean cacheFresh(long cachedAtMillis) {
|
||||
return cachedAtMillis > 0L && System.currentTimeMillis() - cachedAtMillis <= TABLE_CACHE_TTL_MILLIS;
|
||||
}
|
||||
@@ -452,7 +498,8 @@ public final class TDengineAgent extends BaseDatabaseAgent {
|
||||
}
|
||||
}
|
||||
|
||||
private Object tdengineResultValue(ResultSet rs, int index, int sqlType) {
|
||||
@Override
|
||||
protected Object resultValue(ResultSet rs, int index, int sqlType) {
|
||||
return unchecked(() -> {
|
||||
Object value = switch (sqlType) {
|
||||
case Types.BIGINT -> rs.getLong(index);
|
||||
|
||||
+67
-1
@@ -12,6 +12,8 @@ import com.dbx.agent.test.JdbcMetadataSqlFake;
|
||||
import com.dbx.agent.test.TestSupport;
|
||||
import com.taosdata.jdbc.TSDBDriver;
|
||||
import com.taosdata.jdbc.rs.RestfulConnection;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -22,6 +24,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -354,6 +357,49 @@ class TDengineAgentMetadataTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void restExecutionSupportsPooledWrappersAndRestoresOriginalDatabase() throws Exception {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, "original");
|
||||
try (RestfulConnection physical = restfulConnection(properties)) {
|
||||
physical.setCatalog("original");
|
||||
Connection pooled = wrapperConnection(physical);
|
||||
|
||||
Assertions.assertNull(TDengineAgent.prepareExecutionSchema(pooled, "power"));
|
||||
Assertions.assertEquals("power", pooled.getCatalog());
|
||||
Assertions.assertEquals("power", pooled.getClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME));
|
||||
|
||||
TDengineAgent.restoreRestfulConnectionState(pooled, "original", "original");
|
||||
Assertions.assertEquals("original", pooled.getCatalog());
|
||||
Assertions.assertEquals("original", pooled.getClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void hikariCanManageRestfulConnectionsWithoutLosingNativeAccess() throws Exception {
|
||||
RestfulConnection physical = restfulConnection();
|
||||
DataSource dataSource = proxy(DataSource.class, (proxy, method, args) -> {
|
||||
if ("getConnection".equals(method.getName())) {
|
||||
return physical;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setPoolName("tdengine-rest-test");
|
||||
config.setDataSource(dataSource);
|
||||
config.setMaximumPoolSize(1);
|
||||
config.setMinimumIdle(0);
|
||||
config.setInitializationFailTimeout(-1L);
|
||||
|
||||
try (HikariDataSource pool = new HikariDataSource(config);
|
||||
Connection connection = pool.getConnection()) {
|
||||
Assertions.assertTrue(connection.isWrapperFor(RestfulConnection.class));
|
||||
Assertions.assertSame(physical, connection.unwrap(RestfulConnection.class));
|
||||
Assertions.assertNull(TDengineAgent.prepareExecutionSchema(connection, "power"));
|
||||
Assertions.assertEquals("power", connection.getCatalog());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void websocketExecutionKeepsSchemaForUseSqlSwitching() throws Exception {
|
||||
Connection connection = JdbcAgentFake.connection();
|
||||
@@ -441,10 +487,14 @@ class TDengineAgentMetadataTest {
|
||||
}
|
||||
|
||||
private static RestfulConnection restfulConnection() {
|
||||
return restfulConnection(new Properties());
|
||||
}
|
||||
|
||||
private static RestfulConnection restfulConnection(Properties properties) {
|
||||
return new RestfulConnection(
|
||||
"127.0.0.1",
|
||||
"6041",
|
||||
new Properties(),
|
||||
properties,
|
||||
"",
|
||||
"jdbc:TAOS-RS://127.0.0.1:6041/",
|
||||
null,
|
||||
@@ -453,4 +503,20 @@ class TDengineAgentMetadataTest {
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
private static Connection wrapperConnection(Connection physical) {
|
||||
return proxy(Connection.class, (proxy, method, args) -> {
|
||||
if ("isWrapperFor".equals(method.getName())) {
|
||||
return ((Class<?>) args[0]).isInstance(physical);
|
||||
}
|
||||
if ("unwrap".equals(method.getName())) {
|
||||
return ((Class<?>) args[0]).cast(physical);
|
||||
}
|
||||
try {
|
||||
return method.invoke(physical, args);
|
||||
} catch (java.lang.reflect.InvocationTargetException error) {
|
||||
throw error.getCause();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user