Skip to content

Commit b936d53

Browse files
Zhou-KanVLSMB
andauthored
Feat/143 datasource inspection (#189)
* Step 3: Upgrade Java runtime configuration - Compile: SUCCESS, Tests: all passed Updated Java targets, Docker runtime, CI, and documentation to Java 25. Security: Replaced Alpine runtime image with Ubuntu Noble variant; Docker diagnostic still reports one base-image vulnerability. * feat: add connection testing for saved datasources * feat: display datasource tables and views * chore: exclude unrelated Java upgrade from datasource PR * fix: bound datasource connection tests and validate before creation * fix(datasource): address review feedback and persist test results --------- Co-authored-by: kankanzhou <121850056+kankanzhou@users.noreply.github.com> Co-authored-by: VLSMB <vlsmb@foxmail.com>
1 parent 53ac059 commit b936d53

16 files changed

Lines changed: 770 additions & 20 deletions

File tree

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/DataSourceType.java‎

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,72 @@
1818
package io.github.malonetalk.agent.datasource;
1919

2020
import java.util.Arrays;
21+
import java.util.Map;
2122
import java.util.Optional;
23+
import java.util.StringJoiner;
2224
import lombok.AllArgsConstructor;
2325
import lombok.Getter;
2426

2527
@Getter
2628
@AllArgsConstructor
2729
public enum DataSourceType {
28-
MYSQL("mysql", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://", "com.mysql:mysql-connector-j"),
30+
MYSQL(
31+
"mysql",
32+
"com.mysql.cj.jdbc.Driver",
33+
"jdbc:mysql://",
34+
"com.mysql:mysql-connector-j",
35+
Map.of("connectTimeout", "5000", "socketTimeout", "5000")),
2936
POSTGRESQL(
3037
"postgresql",
3138
"org.postgresql.Driver",
3239
"jdbc:postgresql://",
33-
"org.postgresql:postgresql"),
40+
"org.postgresql:postgresql",
41+
Map.of("connectTimeout", "5", "socketTimeout", "5", "loginTimeout", "10")),
3442
ORACLE(
3543
"oracle",
3644
"oracle.jdbc.OracleDriver",
3745
"jdbc:oracle:thin:@",
38-
"com.oracle.database.jdbc:ojdbc11"),
46+
"com.oracle.database.jdbc:ojdbc11",
47+
Map.of("oracle.net.CONNECT_TIMEOUT", "5000", "oracle.jdbc.ReadTimeout", "5000")),
3948
CLICKHOUSE(
4049
"clickhouse",
4150
"com.clickhouse.jdbc.ClickHouseDriver",
4251
"jdbc:clickhouse://",
43-
"com.clickhouse:clickhouse-jdbc"),
52+
"com.clickhouse:clickhouse-jdbc",
53+
Map.of("connection_timeout", "5000", "socket_timeout", "5000")),
4454
SQLSERVER(
4555
"sqlserver",
4656
"com.microsoft.sqlserver.jdbc.SQLServerDriver",
4757
"jdbc:sqlserver://",
48-
"com.microsoft.sqlserver:mssql-jdbc"),
49-
DAMENG("dameng", "dm.jdbc.driver.DmDriver", "jdbc:dm://", "com.dameng:DmJdbcDriver18"),
58+
"com.microsoft.sqlserver:mssql-jdbc",
59+
Map.of("loginTimeout", "5", "socketTimeout", "5000")),
60+
DAMENG(
61+
"dameng",
62+
"dm.jdbc.driver.DmDriver",
63+
"jdbc:dm://",
64+
"com.dameng:DmJdbcDriver18",
65+
Map.of("connectTimeout", "5000", "socketTimeout", "5000")),
5066
OCEANBASE(
5167
"oceanbase",
5268
"com.oceanbase.jdbc.Driver",
5369
"jdbc:oceanbase://",
54-
"com.oceanbase:oceanbase-client"),
55-
SQLITE("sqlite", "org.sqlite.JDBC", "jdbc:sqlite:", "org.xerial:sqlite-jdbc");
70+
"com.oceanbase:oceanbase-client",
71+
Map.of("connectTimeout", "5000", "socketTimeout", "5000")),
72+
SQLITE(
73+
"sqlite",
74+
"org.sqlite.JDBC",
75+
"jdbc:sqlite:",
76+
"org.xerial:sqlite-jdbc",
77+
Map.of("busy_timeout", "5000"));
5678

5779
private final String code;
5880
private final String driverClassName;
5981
private final String urlPrefix;
6082
private final String mavenCoordinates;
6183

84+
/** Driver-specific units: PostgreSQL/login timeouts use seconds, others milliseconds. */
85+
private final Map<String, String> inspectionTimeouts;
86+
6287
public static Optional<DataSourceType> fromCode(String code) {
6388
if (code == null) {
6489
return Optional.empty();
@@ -68,6 +93,22 @@ public static Optional<DataSourceType> fromCode(String code) {
6893
.findFirst();
6994
}
7095

96+
String inspectionJdbcUrl(String jdbcUrl) {
97+
if (this != POSTGRESQL) return jdbcUrl;
98+
// PostgreSQL 的 URL 参数优先于 Properties;只改临时测试 URL,保留其他参数原文。
99+
String[] parts = jdbcUrl.split("\\?", 2);
100+
Map<String, String> timeouts = getInspectionTimeouts();
101+
StringJoiner query = new StringJoiner("&");
102+
if (parts.length == 2) {
103+
for (String parameter : parts[1].split("&")) {
104+
String key = parameter.split("=", 2)[0];
105+
if (!parameter.isEmpty() && !timeouts.containsKey(key)) query.add(parameter);
106+
}
107+
}
108+
timeouts.forEach((key, value) -> query.add(key + "=" + value));
109+
return parts[0] + "?" + query;
110+
}
111+
71112
public String buildJdbcUrl(String host, int port, String databaseName) {
72113
return switch (this) {
73114
case MYSQL, POSTGRESQL, CLICKHOUSE, DAMENG, OCEANBASE ->

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/DynamicDataSourceManager.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import io.github.malonetalk.model.po.DatasourcePo;
2525
import io.opentelemetry.instrumentation.jdbc.datasource.JdbcTelemetry;
2626
import jakarta.annotation.PreDestroy;
27+
import java.sql.Connection;
28+
import java.sql.SQLException;
2729
import java.util.concurrent.ConcurrentHashMap;
2830
import javax.sql.DataSource;
2931
import lombok.RequiredArgsConstructor;
@@ -48,11 +50,18 @@ public class DynamicDataSourceManager {
4850
public DataSource getOrCreateDataSource(DatasourcePo datasource) {
4951
HikariDataSource pool =
5052
dataSourcePool.computeIfAbsent(
51-
datasource.getId(), id -> createDataSource(datasource));
53+
datasource.getId(), id -> createDataSource(datasource, false));
5254
return jdbcTelemetry.wrap(pool);
5355
}
5456

55-
private HikariDataSource createDataSource(DatasourcePo datasource) {
57+
public boolean testConnection(DatasourcePo datasource) throws SQLException {
58+
try (HikariDataSource pool = createDataSource(datasource, true);
59+
Connection connection = pool.getConnection()) {
60+
return connection.isValid(5);
61+
}
62+
}
63+
64+
private HikariDataSource createDataSource(DatasourcePo datasource, boolean inspection) {
5665
DataSourceType type =
5766
DataSourceType.fromCode(datasource.getType())
5867
.orElseThrow(
@@ -67,7 +76,7 @@ private HikariDataSource createDataSource(DatasourcePo datasource) {
6776
// 注意:HikariConfig.setDriverClassName() 会同步加载驱动类,缺驱动时在此即抛异常,
6877
// 因此 config 构建与池创建必须同在一个 try 内,才能被转译为可操作的缺驱动提示。
6978
try {
70-
HikariConfig config = getHikariConfig(datasource, jdbcUrl, type);
79+
HikariConfig config = getHikariConfig(datasource, jdbcUrl, type, inspection);
7180

7281
log.info(
7382
"Creating datasource pool for [{}] type={} url={}",
@@ -112,7 +121,7 @@ private RuntimeException translateInitializationFailure(
112121
}
113122

114123
private static HikariConfig getHikariConfig(
115-
DatasourcePo datasource, String jdbcUrl, DataSourceType type) {
124+
DatasourcePo datasource, String jdbcUrl, DataSourceType type, boolean inspection) {
116125
HikariConfig config = new HikariConfig();
117126
config.setJdbcUrl(jdbcUrl);
118127
config.setUsername(datasource.getUsername());
@@ -125,6 +134,13 @@ private static HikariConfig getHikariConfig(
125134
config.setConnectionTimeout(CONNECTION_TIMEOUT);
126135
config.setMaxLifetime(MAX_LIFETIME);
127136

137+
if (inspection) {
138+
// Pool acquisition/isValid timeouts do not bound the driver's initial handshake.
139+
config.setJdbcUrl(type.inspectionJdbcUrl(jdbcUrl));
140+
type.getInspectionTimeouts().forEach(config::addDataSourceProperty);
141+
config.setMaximumPoolSize(1);
142+
}
143+
128144
config.setPoolName("ds-" + datasource.getId() + "-" + type.getCode());
129145
return config;
130146
}

‎data-agent-backend/src/main/java/io/github/malonetalk/controller/DatasourceController.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
import io.github.malonetalk.model.vo.DatasourceColumnsVo;
3232
import io.github.malonetalk.model.vo.DatasourceObjectVo;
3333
import io.github.malonetalk.model.vo.DatasourceOptionVo;
34+
import io.github.malonetalk.model.vo.DatasourceTestVo;
3435
import io.github.malonetalk.model.vo.DatasourceVo;
3536
import io.github.malonetalk.model.vo.ListVo;
37+
import io.github.malonetalk.model.vo.PhysicalTableInfoVo;
3638
import io.github.malonetalk.service.DataPermissionService;
39+
import io.github.malonetalk.service.DatasourceInspectionService;
3740
import io.github.malonetalk.service.DatasourceService;
3841
import jakarta.validation.Valid;
3942
import java.util.List;
@@ -55,6 +58,7 @@
5558
public class DatasourceController {
5659

5760
private final DatasourceService dataSourceService;
61+
private final DatasourceInspectionService datasourceInspectionService;
5862
private final DataPermissionService dataPermissions;
5963
private final UserContextHolder holder;
6064
private final DatasourceConverter datasourceConverter;
@@ -201,4 +205,22 @@ public ListVo<DatasourceOptionVo> options() {
201205
ds.getStatus()))
202206
.toList());
203207
}
208+
209+
@PostMapping("/test")
210+
@RequirePermission(PermissionEnum.DATA_DATASOURCE_CREATE)
211+
public DatasourceTestVo testConfiguration(@Valid @RequestBody DatasourceDto request) {
212+
return datasourceInspectionService.testConfiguration(request);
213+
}
214+
215+
@PostMapping("/{id}/test")
216+
@RequirePermission(PermissionEnum.DATA_DATASOURCE_UPDATE)
217+
public DatasourceTestVo testConnection(@PathVariable Integer id) {
218+
return datasourceInspectionService.testConnection(id);
219+
}
220+
221+
@GetMapping("/{id}/physical-tables")
222+
@RequirePermission(PermissionEnum.DATA_DATASOURCE_UPDATE)
223+
public ListVo<PhysicalTableInfoVo> listPhysicalTables(@PathVariable Integer id) {
224+
return new ListVo<>(datasourceInspectionService.listPhysicalTables(id));
225+
}
204226
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
package io.github.malonetalk.enums;
19+
20+
public enum DatasourceTestStatus {
21+
SUCCESS,
22+
FAILED
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
package io.github.malonetalk.model.vo;
19+
20+
public record DatasourceTestVo(boolean success, String message) implements BaseVo {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
package io.github.malonetalk.model.vo;
19+
20+
public record PhysicalTableInfoVo(String tableName, String objectType, String remarks)
21+
implements BaseVo {}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
package io.github.malonetalk.service;
19+
20+
import io.github.malonetalk.agent.datasource.DynamicDataSourceManager;
21+
import io.github.malonetalk.agent.datasource.SchemaReader;
22+
import io.github.malonetalk.enums.DatasourceTestStatus;
23+
import io.github.malonetalk.exception.BusinessException;
24+
import io.github.malonetalk.model.converter.DatasourceConverter;
25+
import io.github.malonetalk.model.dto.DatasourceDto;
26+
import io.github.malonetalk.model.po.DatasourcePo;
27+
import io.github.malonetalk.model.vo.DatasourceTestVo;
28+
import io.github.malonetalk.model.vo.PhysicalTableInfoVo;
29+
import java.sql.SQLException;
30+
import java.util.Comparator;
31+
import java.util.List;
32+
import lombok.RequiredArgsConstructor;
33+
import lombok.extern.slf4j.Slf4j;
34+
import org.springframework.stereotype.Service;
35+
36+
@Slf4j
37+
@Service
38+
@RequiredArgsConstructor
39+
public class DatasourceInspectionService {
40+
41+
private final DatasourceService datasourceService;
42+
private final DynamicDataSourceManager dynamicDataSourceManager;
43+
private final SchemaReader schemaReader;
44+
private final DatasourceConverter converter;
45+
46+
public DatasourceTestVo testConnection(Integer id) {
47+
DatasourcePo datasource = datasourceService.requireById(id);
48+
DatasourceTestVo result;
49+
try {
50+
result = testConnection(datasource);
51+
} catch (BusinessException e) {
52+
datasourceService.updateTestStatus(id, DatasourceTestStatus.FAILED);
53+
throw e;
54+
}
55+
datasourceService.updateTestStatus(
56+
id, result.success() ? DatasourceTestStatus.SUCCESS : DatasourceTestStatus.FAILED);
57+
return result;
58+
}
59+
60+
public DatasourceTestVo testConfiguration(DatasourceDto request) {
61+
return testConnection(converter.toPo(request));
62+
}
63+
64+
private DatasourceTestVo testConnection(DatasourcePo datasource) {
65+
try {
66+
boolean success = dynamicDataSourceManager.testConnection(datasource);
67+
68+
return new DatasourceTestVo(success, success ? "连接成功" : "连接验证失败,请检查数据库是否可用");
69+
} catch (BusinessException e) {
70+
throw e;
71+
} catch (SQLException | RuntimeException e) {
72+
log.warn("Datasource connection test failed, datasourceId={}", datasource.getId(), e);
73+
74+
return new DatasourceTestVo(false, "连接失败,请检查地址、端口、数据库名、账号密码及网络");
75+
}
76+
}
77+
78+
public List<PhysicalTableInfoVo> listPhysicalTables(Integer id) {
79+
DatasourcePo datasource = datasourceService.requireById(id);
80+
81+
return schemaReader.getTables(datasource).stream()
82+
.map(
83+
table ->
84+
new PhysicalTableInfoVo(
85+
table.tableName(), table.objectType(), table.remarks()))
86+
.sorted(
87+
Comparator.comparing(
88+
PhysicalTableInfoVo::tableName, String.CASE_INSENSITIVE_ORDER))
89+
.toList();
90+
}
91+
}

‎data-agent-backend/src/main/java/io/github/malonetalk/service/DatasourceService.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package io.github.malonetalk.service;
1919

20+
import io.github.malonetalk.enums.DatasourceTestStatus;
2021
import io.github.malonetalk.enums.Status;
2122
import io.github.malonetalk.exception.BusinessException;
2223
import io.github.malonetalk.exception.ErrorCode;
@@ -53,5 +54,7 @@ default DatasourcePo requireById(Integer id) {
5354

5455
List<DatasourcePo> findByType(String type);
5556

57+
void updateTestStatus(Integer id, DatasourceTestStatus status);
58+
5659
void updateStatus(Integer id, Status status);
5760
}

0 commit comments

Comments
 (0)