Skip to content

Commit

Permalink
[Optimization-1548][metadata] Optimize datasource registration should…
Browse files Browse the repository at this point in the history
… can save config before validate (#1576)

Co-authored-by: wenmo <[email protected]>
  • Loading branch information
aiwenmo and aiwenmo authored Jan 18, 2023
1 parent 828012d commit 02f2b95
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ public Result checkHeartBeats() {
List<DataBase> 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("状态刷新完成");
}
Expand All @@ -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("数据源连接正常");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,25 @@ public class DataBaseServiceImpl extends SuperServiceImpl<DataBaseMapper, DataBa

@Override
public String testConnect(DataBase dataBase) {
return Driver.build(dataBase.getDriverConfig()).test();
return Driver.buildUnconnected(dataBase.getDriverConfig()).test();
}

@Override
public boolean checkHeartBeat(DataBase dataBase) {
boolean isHealthy = Asserts.isEquals(CommonConstant.HEALTHY, Driver.build(dataBase.getDriverConfig()).test());
dataBase.setStatus(isHealthy);
boolean isHealthy = false;
dataBase.setHeartbeatTime(LocalDateTime.now());
if (isHealthy) {
dataBase.setHealthTime(LocalDateTime.now());
try {
isHealthy = Asserts.isEquals(
CommonConstant.HEALTHY,
Driver.buildUnconnected(dataBase.getDriverConfig()).test());
if (isHealthy) {
dataBase.setHealthTime(LocalDateTime.now());
}
} catch (Exception e) {
isHealthy = false;
throw e;
} finally {
dataBase.setStatus(isHealthy);
}
return isHealthy;
}
Expand All @@ -75,8 +84,11 @@ public boolean saveOrUpdateDataBase(DataBase dataBase) {
return false;
}
if (Asserts.isNull(dataBase.getId())) {
checkHeartBeat(dataBase);
return save(dataBase);
try {
checkHeartBeat(dataBase);
} finally {
return save(dataBase);
}
} else {
DataBase dataBaseInfo = getById(dataBase.getId());
if (Asserts.isNull(dataBase.getUrl())) {
Expand All @@ -88,8 +100,11 @@ public boolean saveOrUpdateDataBase(DataBase dataBase) {
if (Asserts.isNull(dataBase.getPassword())) {
dataBase.setPassword(dataBaseInfo.getPassword());
}
checkHeartBeat(dataBase);
return updateById(dataBase);
try {
checkHeartBeat(dataBase);
} finally {
return updateById(dataBase);
}
}
}

Expand Down Expand Up @@ -205,7 +220,8 @@ public String getEnabledFlinkWithSql() {
public boolean copyDatabase(DataBase database) {
String name = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
database.setId(null);
database.setName((database.getName().length() > 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ static Driver build(DriverConfig config) {
}
}

static Driver buildUnconnected(DriverConfig config) {
String key = config.getName();
synchronized (Driver.class) {
Optional<Driver> 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()) {
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions dlink-web/src/locales/zh-CN/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':'失败,请重试',
Expand Down
8 changes: 4 additions & 4 deletions dlink-web/src/pages/RegistrationCenter/DataBase/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit 02f2b95

Please sign in to comment.