Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,72 @@
package io.github.malonetalk.agent.datasource;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum DataSourceType {
MYSQL("mysql", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://", "com.mysql:mysql-connector-j"),
MYSQL(
"mysql",
"com.mysql.cj.jdbc.Driver",
"jdbc:mysql://",
"com.mysql:mysql-connector-j",
Map.of("connectTimeout", "5000", "socketTimeout", "5000")),
POSTGRESQL(
"postgresql",
"org.postgresql.Driver",
"jdbc:postgresql://",
"org.postgresql:postgresql"),
"org.postgresql:postgresql",
Map.of("connectTimeout", "5", "socketTimeout", "5", "loginTimeout", "10")),
ORACLE(
"oracle",
"oracle.jdbc.OracleDriver",
"jdbc:oracle:thin:@",
"com.oracle.database.jdbc:ojdbc11"),
"com.oracle.database.jdbc:ojdbc11",
Map.of("oracle.net.CONNECT_TIMEOUT", "5000", "oracle.jdbc.ReadTimeout", "5000")),
CLICKHOUSE(
"clickhouse",
"com.clickhouse.jdbc.ClickHouseDriver",
"jdbc:clickhouse://",
"com.clickhouse:clickhouse-jdbc"),
"com.clickhouse:clickhouse-jdbc",
Map.of("connection_timeout", "5000", "socket_timeout", "5000")),
SQLSERVER(
"sqlserver",
"com.microsoft.sqlserver.jdbc.SQLServerDriver",
"jdbc:sqlserver://",
"com.microsoft.sqlserver:mssql-jdbc"),
DAMENG("dameng", "dm.jdbc.driver.DmDriver", "jdbc:dm://", "com.dameng:DmJdbcDriver18"),
"com.microsoft.sqlserver:mssql-jdbc",
Map.of("loginTimeout", "5", "socketTimeout", "5000")),
DAMENG(
"dameng",
"dm.jdbc.driver.DmDriver",
"jdbc:dm://",
"com.dameng:DmJdbcDriver18",
Map.of("connectTimeout", "5000", "socketTimeout", "5000")),
OCEANBASE(
"oceanbase",
"com.oceanbase.jdbc.Driver",
"jdbc:oceanbase://",
"com.oceanbase:oceanbase-client"),
SQLITE("sqlite", "org.sqlite.JDBC", "jdbc:sqlite:", "org.xerial:sqlite-jdbc");
"com.oceanbase:oceanbase-client",
Map.of("connectTimeout", "5000", "socketTimeout", "5000")),
SQLITE(
"sqlite",
"org.sqlite.JDBC",
"jdbc:sqlite:",
"org.xerial:sqlite-jdbc",
Map.of("busy_timeout", "5000"));

private final String code;
private final String driverClassName;
private final String urlPrefix;
private final String mavenCoordinates;

/** Driver-specific units: PostgreSQL/login timeouts use seconds, others milliseconds. */
private final Map<String, String> inspectionTimeouts;

public static Optional<DataSourceType> fromCode(String code) {
if (code == null) {
return Optional.empty();
Expand All @@ -68,6 +93,22 @@ public static Optional<DataSourceType> fromCode(String code) {
.findFirst();
}

String inspectionJdbcUrl(String jdbcUrl) {
if (this != POSTGRESQL) return jdbcUrl;
// PostgreSQL 的 URL 参数优先于 Properties;只改临时测试 URL,保留其他参数原文。
String[] parts = jdbcUrl.split("\\?", 2);
Map<String, String> timeouts = getInspectionTimeouts();
StringJoiner query = new StringJoiner("&");
if (parts.length == 2) {
for (String parameter : parts[1].split("&")) {
String key = parameter.split("=", 2)[0];
if (!parameter.isEmpty() && !timeouts.containsKey(key)) query.add(parameter);
}
}
timeouts.forEach((key, value) -> query.add(key + "=" + value));
return parts[0] + "?" + query;
}

public String buildJdbcUrl(String host, int port, String databaseName) {
return switch (this) {
case MYSQL, POSTGRESQL, CLICKHOUSE, DAMENG, OCEANBASE ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.github.malonetalk.model.po.DatasourcePo;
import io.opentelemetry.instrumentation.jdbc.datasource.JdbcTelemetry;
import jakarta.annotation.PreDestroy;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import lombok.RequiredArgsConstructor;
Expand All @@ -48,11 +50,18 @@ public class DynamicDataSourceManager {
public DataSource getOrCreateDataSource(DatasourcePo datasource) {
HikariDataSource pool =
dataSourcePool.computeIfAbsent(
datasource.getId(), id -> createDataSource(datasource));
datasource.getId(), id -> createDataSource(datasource, false));
return jdbcTelemetry.wrap(pool);
}

private HikariDataSource createDataSource(DatasourcePo datasource) {
public boolean testConnection(DatasourcePo datasource) throws SQLException {
try (HikariDataSource pool = createDataSource(datasource, true);
Connection connection = pool.getConnection()) {
return connection.isValid(5);
}
}

private HikariDataSource createDataSource(DatasourcePo datasource, boolean inspection) {
DataSourceType type =
DataSourceType.fromCode(datasource.getType())
.orElseThrow(
Expand All @@ -67,7 +76,7 @@ private HikariDataSource createDataSource(DatasourcePo datasource) {
// 注意:HikariConfig.setDriverClassName() 会同步加载驱动类,缺驱动时在此即抛异常,
// 因此 config 构建与池创建必须同在一个 try 内,才能被转译为可操作的缺驱动提示。
try {
HikariConfig config = getHikariConfig(datasource, jdbcUrl, type);
HikariConfig config = getHikariConfig(datasource, jdbcUrl, type, inspection);

log.info(
"Creating datasource pool for [{}] type={} url={}",
Expand Down Expand Up @@ -112,7 +121,7 @@ private RuntimeException translateInitializationFailure(
}

private static HikariConfig getHikariConfig(
DatasourcePo datasource, String jdbcUrl, DataSourceType type) {
DatasourcePo datasource, String jdbcUrl, DataSourceType type, boolean inspection) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcUrl);
config.setUsername(datasource.getUsername());
Expand All @@ -125,6 +134,13 @@ private static HikariConfig getHikariConfig(
config.setConnectionTimeout(CONNECTION_TIMEOUT);
config.setMaxLifetime(MAX_LIFETIME);

if (inspection) {
// Pool acquisition/isValid timeouts do not bound the driver's initial handshake.
config.setJdbcUrl(type.inspectionJdbcUrl(jdbcUrl));
type.getInspectionTimeouts().forEach(config::addDataSourceProperty);
config.setMaximumPoolSize(1);
}

config.setPoolName("ds-" + datasource.getId() + "-" + type.getCode());
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
import io.github.malonetalk.model.vo.DatasourceColumnsVo;
import io.github.malonetalk.model.vo.DatasourceObjectVo;
import io.github.malonetalk.model.vo.DatasourceOptionVo;
import io.github.malonetalk.model.vo.DatasourceTestVo;
import io.github.malonetalk.model.vo.DatasourceVo;
import io.github.malonetalk.model.vo.ListVo;
import io.github.malonetalk.model.vo.PhysicalTableInfoVo;
import io.github.malonetalk.service.DataPermissionService;
import io.github.malonetalk.service.DatasourceInspectionService;
import io.github.malonetalk.service.DatasourceService;
import jakarta.validation.Valid;
import java.util.List;
Expand All @@ -55,6 +58,7 @@
public class DatasourceController {

private final DatasourceService dataSourceService;
private final DatasourceInspectionService datasourceInspectionService;
private final DataPermissionService dataPermissions;
private final UserContextHolder holder;
private final DatasourceConverter datasourceConverter;
Expand Down Expand Up @@ -201,4 +205,22 @@ public ListVo<DatasourceOptionVo> options() {
ds.getStatus()))
.toList());
}

@PostMapping("/test")
@RequirePermission(PermissionEnum.DATA_DATASOURCE_CREATE)
public DatasourceTestVo testConfiguration(@Valid @RequestBody DatasourceDto request) {
return datasourceInspectionService.testConfiguration(request);
}

@PostMapping("/{id}/test")
@RequirePermission(PermissionEnum.DATA_DATASOURCE_UPDATE)
public DatasourceTestVo testConnection(@PathVariable Integer id) {
return datasourceInspectionService.testConnection(id);
}

@GetMapping("/{id}/physical-tables")
@RequirePermission(PermissionEnum.DATA_DATASOURCE_UPDATE)
public ListVo<PhysicalTableInfoVo> listPhysicalTables(@PathVariable Integer id) {
return new ListVo<>(datasourceInspectionService.listPhysicalTables(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.enums;

public enum DatasourceTestStatus {
SUCCESS,
FAILED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.model.vo;

public record DatasourceTestVo(boolean success, String message) implements BaseVo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.model.vo;

public record PhysicalTableInfoVo(String tableName, String objectType, String remarks)
implements BaseVo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.service;

import io.github.malonetalk.agent.datasource.DynamicDataSourceManager;
import io.github.malonetalk.agent.datasource.SchemaReader;
import io.github.malonetalk.enums.DatasourceTestStatus;
import io.github.malonetalk.exception.BusinessException;
import io.github.malonetalk.model.converter.DatasourceConverter;
import io.github.malonetalk.model.dto.DatasourceDto;
import io.github.malonetalk.model.po.DatasourcePo;
import io.github.malonetalk.model.vo.DatasourceTestVo;
import io.github.malonetalk.model.vo.PhysicalTableInfoVo;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class DatasourceInspectionService {

private final DatasourceService datasourceService;
private final DynamicDataSourceManager dynamicDataSourceManager;
private final SchemaReader schemaReader;
private final DatasourceConverter converter;

public DatasourceTestVo testConnection(Integer id) {
DatasourcePo datasource = datasourceService.requireById(id);
DatasourceTestVo result;
try {
result = testConnection(datasource);
} catch (BusinessException e) {
datasourceService.updateTestStatus(id, DatasourceTestStatus.FAILED);
throw e;
}
datasourceService.updateTestStatus(
id, result.success() ? DatasourceTestStatus.SUCCESS : DatasourceTestStatus.FAILED);
return result;
}

public DatasourceTestVo testConfiguration(DatasourceDto request) {
return testConnection(converter.toPo(request));
}

private DatasourceTestVo testConnection(DatasourcePo datasource) {
try {
boolean success = dynamicDataSourceManager.testConnection(datasource);

return new DatasourceTestVo(success, success ? "连接成功" : "连接验证失败,请检查数据库是否可用");
} catch (BusinessException e) {
throw e;
} catch (SQLException | RuntimeException e) {
log.warn("Datasource connection test failed, datasourceId={}", datasource.getId(), e);

return new DatasourceTestVo(false, "连接失败,请检查地址、端口、数据库名、账号密码及网络");
}
}

public List<PhysicalTableInfoVo> listPhysicalTables(Integer id) {
DatasourcePo datasource = datasourceService.requireById(id);

return schemaReader.getTables(datasource).stream()
.map(
table ->
new PhysicalTableInfoVo(
table.tableName(), table.objectType(), table.remarks()))
.sorted(
Comparator.comparing(
PhysicalTableInfoVo::tableName, String.CASE_INSENSITIVE_ORDER))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package io.github.malonetalk.service;

import io.github.malonetalk.enums.DatasourceTestStatus;
import io.github.malonetalk.enums.Status;
import io.github.malonetalk.exception.BusinessException;
import io.github.malonetalk.exception.ErrorCode;
Expand Down Expand Up @@ -53,5 +54,7 @@ default DatasourcePo requireById(Integer id) {

List<DatasourcePo> findByType(String type);

void updateTestStatus(Integer id, DatasourceTestStatus status);

void updateStatus(Integer id, Status status);
}
Loading
Loading