Skip to content

Commit 7ed715a

Browse files
committed
Avoid process being occupied and optimize database initialization speed
1 parent 09f04f7 commit 7ed715a

5 files changed

Lines changed: 37 additions & 28 deletions

File tree

  • chat2db-server
    • chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository
    • chat2db-server-start/src/main/java/ai/chat2db/server/start
    • chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util
    • chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb

chat2db-server/chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository/Dbutils.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package ai.chat2db.server.domain.repository;
22

3-
import ai.chat2db.server.domain.repository.entity.DataSourceDO;
4-
import ai.chat2db.server.domain.repository.mapper.DataSourceMapper;
53
import ai.chat2db.server.tools.common.model.ConfigJson;
64
import ai.chat2db.server.tools.common.util.ConfigUtils;
7-
import cn.hutool.core.lang.UUID;
8-
import com.alibaba.fastjson2.JSON;
95
import com.baomidou.mybatisplus.annotation.DbType;
106
import com.baomidou.mybatisplus.core.MybatisConfiguration;
117
import com.baomidou.mybatisplus.core.config.GlobalConfig;

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/Application.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ai.chat2db.server.start;
22

33
import ai.chat2db.server.domain.repository.Dbutils;
4+
import ai.chat2db.server.tools.common.util.ConfigUtils;
45
import lombok.extern.slf4j.Slf4j;
56
import org.springframework.boot.SpringApplication;
67
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -27,7 +28,7 @@
2728
public class Application {
2829

2930
public static void main(String[] args) {
30-
//ConfigUtils.pid();
31+
ConfigUtils.initProcess();
3132
CompletableFuture.runAsync(() -> {
3233
Dbutils.init();
3334
});

chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/ConfigUtils.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ai.chat2db.server.tools.common.model.ConfigJson;
44
import cn.hutool.core.io.FileUtil;
5+
import cn.hutool.core.lang.UUID;
56
import com.alibaba.fastjson2.JSON;
67
import lombok.extern.slf4j.Slf4j;
78
import org.apache.commons.lang3.StringUtils;
@@ -25,6 +26,9 @@ public class ConfigUtils {
2526
public static File configFile;
2627
private static ConfigJson config = null;
2728

29+
public static File clientIdFile;
30+
private static String clientId = null;
31+
2832
static {
2933
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
3034
if (APP_PATH != null) {
@@ -39,6 +43,14 @@ public class ConfigUtils {
3943
if (!configFile.exists()) {
4044
FileUtil.writeUtf8String(JSON.toJSONString(new ConfigJson()), configFile);
4145
}
46+
47+
clientIdFile = new File(
48+
CONFIG_BASE_PATH + File.separator + "config" + File.separator + "client_uuid");
49+
if (!clientIdFile.exists()) {
50+
String uuid = UUID.fastUUID().toString(true);
51+
FileUtil.writeUtf8String(uuid, clientIdFile);
52+
clientId = uuid;
53+
}
4254
}
4355

4456
public static void updateVersion(String version) {
@@ -61,6 +73,7 @@ public static String getLocalVersion() {
6173
version = StringUtils.trim(FileUtil.readUtf8String(versionFile));
6274
return version;
6375
}
76+
6477
public static String getLatestLocalVersion() {
6578
if (versionFile == null) {
6679
log.warn("VERSION_FILE is null");
@@ -77,6 +90,13 @@ public static ConfigJson getConfig() {
7790
return config;
7891
}
7992

93+
public static String getClientId() {
94+
if (clientId == null) {
95+
clientId = StringUtils.trim(FileUtil.readUtf8String(clientIdFile));
96+
}
97+
return clientId;
98+
}
99+
80100
public static void setConfig(ConfigJson config) {
81101
String stringConfigJson = JSON.toJSONString(config);
82102
FileUtil.writeUtf8String(stringConfigJson, configFile);
@@ -87,48 +107,43 @@ public static void setConfig(ConfigJson config) {
87107
private static String getAppPath() {
88108
try {
89109
String jarPath = System.getProperty("project.path");
90-
// log.info("user home: {}", System.getProperty("user.home"));
91-
// log.info("project.path: {}", System.getProperty("project.path"));
92-
// log.info("jarPath: {}", jarPath);
93110
return FileUtil.getParent(jarPath, 4);
94111
} catch (Exception e) {
95112
log.error("getAppPath error", e);
96113
return null;
97114
}
98115
}
99116

100-
public static void pid() {
117+
public static void initProcess() {
101118
try {
102-
log.error("pidinit");
103119
ProcessHandle currentProcess = ProcessHandle.current();
104120
long pid = currentProcess.pid();
105-
log.error("pid:{}", pid);
106121
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
107-
File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "pid");
122+
File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "app.pid");
108123
if (!pidFile.exists()) {
109124
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
110125
} else {
111126
String oldPid = FileUtil.readUtf8String(pidFile);
112-
log.error("oldPid:{}", oldPid);
127+
log.info("oldPid:{}", oldPid);
113128
if (StringUtils.isNotBlank(oldPid)) {
114129
Optional<ProcessHandle> processHandle = ProcessHandle.of(Long.parseLong(oldPid));
115-
log.error("processHandle:{}", JSON.toJSONString(processHandle));
130+
//log.error("processHandle:{}", JSON.toJSONString(processHandle));
116131
processHandle.ifPresent(handle -> {
117132
ProcessHandle.Info info = handle.info();
118133
String[] arguments = info.arguments().orElse(null);
119-
log.error("arguments:{}", JSON.toJSONString(arguments));
134+
log.info("arguments:{}", JSON.toJSONString(arguments));
120135
if (arguments == null) {
121136
return;
122137
}
123138
for (String argument : arguments) {
124139
if (StringUtils.equals("chat2db-server-start.jar", argument)) {
125140
handle.destroy();
126-
log.error("destroy old process--------");
141+
log.info("destroy old process--------");
127142
break;
128143
}
129-
if (StringUtils.equals("application", argument)) {
144+
if (argument.contains("Application")) {
130145
handle.destroy();
131-
log.error("destroy old process--------");
146+
log.info("destroy old process--------");
132147
break;
133148
}
134149
}
@@ -138,8 +153,8 @@ public static void pid() {
138153
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
139154
}
140155

141-
}catch (Exception e){
142-
log.error("updatePid error",e);
156+
} catch (Exception e) {
157+
log.error("updatePid error", e);
143158
}
144159

145160
}

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import ai.chat2db.server.tools.base.enums.DataSourceTypeEnum;
1616
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
1717
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
18+
import ai.chat2db.server.tools.common.util.ConfigUtils;
1819
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
1920
import ai.chat2db.server.web.api.controller.ai.chat2db.client.Chat2dbAIClient;
2021
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
@@ -70,7 +71,7 @@ public ListResult<ExecuteResultVO> manage(@RequestBody DmlRequest request) {
7071
ListResult<ExecuteResult> resultDTOListResult = dlTemplateService.execute(param);
7172
List<ExecuteResultVO> resultVOS = rdbWebConverter.dto2vo(resultDTOListResult.getData());
7273
String type = Chat2DBContext.getConnectInfo().getDbType();
73-
String clientId = getClientId(request.getClientId());
74+
String clientId = getClientId();
7475
String sqlContent = request.getSql();
7576
executorService.submit(() -> {
7677
try {
@@ -102,11 +103,11 @@ private void addOperationLog(String clientId, String sqlType, String sqlContent,
102103
*
103104
* @return
104105
*/
105-
private String getClientId(String clientId) {
106+
private String getClientId() {
106107
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
107108
Config keyConfig = configService.find(Chat2dbAIClient.CHAT2DB_OPENAI_KEY).getData();
108109
if (Objects.isNull(keyConfig) || StringUtils.isBlank(keyConfig.getContent())) {
109-
return clientId;
110+
return ConfigUtils.getClientId();
110111
}
111112
return keyConfig.getContent();
112113
}
@@ -149,7 +150,7 @@ public DataResult<ExecuteResultVO> executeSelectResultUpdate(@RequestBody DmlReq
149150
ExecuteResultVO executeResultVO = rdbWebConverter.dto2vo(result.getData());
150151
String type = Chat2DBContext.getConnectInfo().getDbType();
151152
String sqlContent = request.getSql();
152-
String clientId = getClientId(request.getClientId());
153+
String clientId = getClientId();
153154
executorService.submit(() -> {
154155
try {
155156
addOperationLog(clientId, type, sqlContent, result.getErrorMessage(), result.getSuccess(), Lists.newArrayList(executeResultVO));

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/request/DmlRequest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,4 @@ public class DmlRequest extends DataSourceBaseRequest implements DataSourceConso
4545
*/
4646
private Boolean pageSizeAll;
4747

48-
/**
49-
* 客户端id
50-
*/
51-
private String clientId;
5248
}

0 commit comments

Comments
 (0)