From 02f2b95734de3cf2db8c3f6b8c107bd112743ab8 Mon Sep 17 00:00:00 2001 From: aiwenmo <32723967+aiwenmo@users.noreply.github.com> Date: Thu, 19 Jan 2023 00:09:40 +0800 Subject: [PATCH] [Optimization-1548][metadata] Optimize datasource registration should can save config before validate (#1576) Co-authored-by: wenmo <32723967+wenmo@users.noreply.github.com> --- .../dlink/controller/DataBaseController.java | 19 +++++++--- .../service/impl/DataBaseServiceImpl.java | 36 +++++++++++++------ .../com/dlink/metadata/driver/Driver.java | 28 +++++++++++---- dlink-web/src/locales/zh-CN/request.ts | 4 +-- .../RegistrationCenter/DataBase/service.ts | 8 ++--- 5 files changed, 68 insertions(+), 27 deletions(-) diff --git a/dlink-admin/src/main/java/com/dlink/controller/DataBaseController.java b/dlink-admin/src/main/java/com/dlink/controller/DataBaseController.java index 31f50ae344..92bb115cfe 100644 --- a/dlink-admin/src/main/java/com/dlink/controller/DataBaseController.java +++ b/dlink-admin/src/main/java/com/dlink/controller/DataBaseController.java @@ -149,8 +149,11 @@ public Result checkHeartBeats() { List dataBases = databaseService.listEnabledAll(); for (int i = 0; i < dataBases.size(); i++) { DataBase dataBase = dataBases.get(i); - databaseService.checkHeartBeat(dataBase); - databaseService.updateById(dataBase); + try { + databaseService.checkHeartBeat(dataBase); + } finally { + databaseService.updateById(dataBase); + } } return Result.succeed("状态刷新完成"); } @@ -162,9 +165,17 @@ public Result checkHeartBeats() { public Result checkHeartBeatById(@RequestParam Integer id) { DataBase dataBase = databaseService.getById(id); Asserts.checkNotNull(dataBase, "该数据源不存在!"); - databaseService.checkHeartBeat(dataBase); + String error = ""; + try { + databaseService.checkHeartBeat(dataBase); + } catch (Exception e) { + error = e.getMessage(); + } databaseService.updateById(dataBase); - return Result.succeed(dataBase, "状态刷新完成"); + if (Asserts.isNotNullString(error)) { + return Result.failed(error); + } + return Result.succeed("数据源连接正常"); } /** diff --git a/dlink-admin/src/main/java/com/dlink/service/impl/DataBaseServiceImpl.java b/dlink-admin/src/main/java/com/dlink/service/impl/DataBaseServiceImpl.java index 89669aee59..e49fef4988 100644 --- a/dlink-admin/src/main/java/com/dlink/service/impl/DataBaseServiceImpl.java +++ b/dlink-admin/src/main/java/com/dlink/service/impl/DataBaseServiceImpl.java @@ -55,16 +55,25 @@ public class DataBaseServiceImpl extends SuperServiceImpl 10 ? database.getName().substring(0, 10) : database.getName()) + "_" + name); + database.setName((database.getName().length() > 10 ? database.getName().substring(0, 10) : database.getName()) + + "_" + name); database.setCreateTime(null); return this.save(database); } diff --git a/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java b/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java index 678977c453..f7b8ce1465 100644 --- a/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java +++ b/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java @@ -70,6 +70,18 @@ static Driver build(DriverConfig config) { } } + static Driver buildUnconnected(DriverConfig config) { + String key = config.getName(); + synchronized (Driver.class) { + Optional optionalDriver = Driver.get(config); + if (!optionalDriver.isPresent()) { + throw new MetaDataException( + "缺少数据源类型【" + config.getType() + "】的依赖,请在 lib 下添加对应的扩展依赖"); + } + return optionalDriver.get(); + } + } + static Driver getHealthDriver(String key) { Driver driver = DriverPool.get(key); if (driver.isHealth()) { @@ -165,13 +177,15 @@ static Driver build(String connector, String url, String username, String passwo String generateCreateTableSql(Table table); - /* boolean insert(Table table, JsonNode data); - - boolean update(Table table, JsonNode data); - - boolean delete(Table table, JsonNode data); - - SelectResult select(String sql);*/ + /* + * boolean insert(Table table, JsonNode data); + * + * boolean update(Table table, JsonNode data); + * + * boolean delete(Table table, JsonNode data); + * + * SelectResult select(String sql); + */ boolean execute(String sql) throws Exception; diff --git a/dlink-web/src/locales/zh-CN/request.ts b/dlink-web/src/locales/zh-CN/request.ts index 91ab329e6d..0d7fd16e96 100644 --- a/dlink-web/src/locales/zh-CN/request.ts +++ b/dlink-web/src/locales/zh-CN/request.ts @@ -39,8 +39,8 @@ export default { 'app.request.failed': '请求失败,请重试', 'app.request.test.connection': '正在测试连接', 'app.request.heartbeat.connection': '正在检测心跳', - 'app.request.heartbeat.connection.success': '数据源心跳正常,检测时间为: {time}', - 'app.request.heartbeat.connection.failed': '数据源心跳异常,检测时间为: {time}', + 'app.request.heartbeat.connection.success': '数据源连接正常', + 'app.request.heartbeat.connection.failed': '数据源连接异常,原因为: {error}', 'app.request.upload.failed': '上传失败。', 'app.request.update.setting.success': '修改配置成功!', 'app.request.error.try':'失败,请重试', diff --git a/dlink-web/src/pages/RegistrationCenter/DataBase/service.ts b/dlink-web/src/pages/RegistrationCenter/DataBase/service.ts index 279ed55ab4..aee959786f 100644 --- a/dlink-web/src/pages/RegistrationCenter/DataBase/service.ts +++ b/dlink-web/src/pages/RegistrationCenter/DataBase/service.ts @@ -42,12 +42,12 @@ export async function testDatabaseConnect(databse: DataBaseItem) { export async function checkHeartBeat(id: number) { const hide = message.loading(l('app.request.heartbeat.connection')); try { - const {datas} = await getInfoById('/api/database/checkHeartBeatById',id); + const {code, msg} = await getInfoById('/api/database/checkHeartBeatById', id); hide(); - datas.status==1?message.success( - l('app.request.heartbeat.connection.success','',{time :datas.heartbeatTime })) + code == 0 ? message.success( + l('app.request.heartbeat.connection.success','')) : - message.error(l('app.request.heartbeat.connection.failed','',{time :datas.heartbeatTime })); + message.error(l('app.request.heartbeat.connection.failed', '', {error: msg})); } catch (error) { hide(); message.error(l('app.request.failed'));