From 3df13fc9bcb0792ff6271703fcc91c36a37f1f6f Mon Sep 17 00:00:00 2001
From: SwallowGG <1558143046@qq.com>
Date: Tue, 12 Dec 2023 23:04:39 +0800
Subject: [PATCH] Improve startup speed
---
.../domain/core/impl/TableServiceImpl.java | 57 +++++++++++--------
.../server/domain/repository/Dbutils.java | 4 +-
.../ai/chat2db/server/start/Application.java | 2 +-
.../src/main/resources/application-test.yml | 12 ----
.../server/tools/common/util/ConfigUtils.java | 21 ++++++-
.../controller/system/SystemController.java | 25 +++++---
6 files changed, 74 insertions(+), 47 deletions(-)
diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java
index 85b20d0b6..1e57dcd82 100644
--- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java
+++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/TableServiceImpl.java
@@ -319,29 +319,7 @@ public PageResult
pageQuery(TablePageQueryParam param, TableSelector sele
long total = 0;
long version = 0L;
if (param.isRefresh() || versionDO == null) {
- version = getLock(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName(), versionDO);
- if (version == -1) {
- int n = 0;
- while (n < 100) {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- versionDO = getVersionMapper().selectOne(queryWrapper);
- if (versionDO != null && "1".equals(versionDO.getStatus())) {
- version = versionDO.getVersion();
- total = versionDO.getTableCount();
- break;
- }
- n++;
- }
- } else {
- total = addDBCache(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName(), version);
- TableCacheVersionDO versionDO1 = new TableCacheVersionDO();
- versionDO1.setStatus("1");
- versionDO1.setTableCount(total);
- getVersionMapper().update(versionDO1, queryWrapper);
- }
+ total = addCache(param,versionDO);
} else {
if ("2".equals(versionDO.getStatus())) {
version = versionDO.getVersion() - 1;
@@ -370,6 +348,37 @@ public PageResult pageQuery(TablePageQueryParam param, TableSelector sele
return PageResult.of(tables, total, param);
}
+ private long addCache(TablePageQueryParam param,TableCacheVersionDO versionDO){
+ LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
+ String key = getTableKey(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName());
+ queryWrapper.eq(TableCacheVersionDO::getKey, key);
+ long total = 0;
+ long version = getLock(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName(), versionDO);
+ if (version == -1) {
+ int n = 0;
+ while (n < 100) {
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ }
+ versionDO = getVersionMapper().selectOne(queryWrapper);
+ if (versionDO != null && "1".equals(versionDO.getStatus())) {
+ version = versionDO.getVersion();
+ total = versionDO.getTableCount();
+ break;
+ }
+ n++;
+ }
+ } else {
+ total = addDBCache(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName(), version);
+ TableCacheVersionDO versionDO1 = new TableCacheVersionDO();
+ versionDO1.setStatus("1");
+ versionDO1.setTableCount(total);
+ getVersionMapper().update(versionDO1, queryWrapper);
+ }
+ return total;
+ }
+
@Override
public ListResult queryTables(TablePageQueryParam param) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
@@ -377,7 +386,7 @@ public ListResult queryTables(TablePageQueryParam param) {
queryWrapper.eq(TableCacheVersionDO::getKey, key);
TableCacheVersionDO versionDO = getVersionMapper().selectOne(queryWrapper);
if (versionDO == null) {
- return ListResult.of(Lists.newArrayList());
+ addCache(param,versionDO);
}
long version = "2".equals(versionDO.getStatus()) ? versionDO.getVersion() - 1 : versionDO.getVersion();
diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository/Dbutils.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository/Dbutils.java
index d71babc2d..a780b13fc 100644
--- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository/Dbutils.java
+++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository/Dbutils.java
@@ -146,7 +146,9 @@ private static DataSource initDataSource() {
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
if ("dev".equalsIgnoreCase(environment)) {
dataSource.setJdbcUrl("jdbc:h2:file:~/.chat2db/db/chat2db_dev;MODE=MYSQL");
- } else {
+ } if ("test".equalsIgnoreCase(environment)) {
+ dataSource.setJdbcUrl("jdbc:h2:file:~/.chat2db/db/chat2db_test;MODE=MYSQL");
+ }else {
dataSource.setJdbcUrl("jdbc:h2:~/.chat2db/db/chat2db;MODE=MYSQL;FILE_LOCK=NO");
}
dataSource.setDriverClassName("org.h2.Driver");
diff --git a/chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/Application.java b/chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/Application.java
index b363705ea..118baaa25 100644
--- a/chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/Application.java
+++ b/chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/Application.java
@@ -32,7 +32,7 @@
public class Application {
public static void main(String[] args) {
- ConfigUtils.pid();
+ //ConfigUtils.pid();
SpringApplication.run(Application.class, args);
}
}
diff --git a/chat2db-server/chat2db-server-start/src/main/resources/application-test.yml b/chat2db-server/chat2db-server-start/src/main/resources/application-test.yml
index 7e7aae15d..344076cd4 100644
--- a/chat2db-server/chat2db-server-start/src/main/resources/application-test.yml
+++ b/chat2db-server/chat2db-server-start/src/main/resources/application-test.yml
@@ -1,15 +1,3 @@
-spring:
- datasource:
- # 配置自带数据库的相对路径
- url: jdbc:h2:~/.chat2db/db/chat2db_test;MODE=MYSQL
- driver-class-name: org.h2.Driver
- h2:
- console:
- enabled: true
- path: /h2
- settings:
- trace: true
- web-allow-others: true
# 端口号
server:
port: 10822
diff --git a/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/ConfigUtils.java b/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/ConfigUtils.java
index 545dc0e7b..7ad4e6868 100644
--- a/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/ConfigUtils.java
+++ b/chat2db-server/chat2db-server-tools/chat2db-server-tools-common/src/main/java/ai/chat2db/server/tools/common/util/ConfigUtils.java
@@ -61,6 +61,14 @@ public static String getLocalVersion() {
version = StringUtils.trim(FileUtil.readUtf8String(versionFile));
return version;
}
+ public static String getLatestLocalVersion() {
+ if (versionFile == null) {
+ log.warn("VERSION_FILE is null");
+ return null;
+ }
+ return StringUtils.trim(FileUtil.readUtf8String(versionFile));
+ }
+
public static ConfigJson getConfig() {
if (config == null) {
@@ -91,31 +99,42 @@ private static String getAppPath() {
public static void pid() {
try {
-
+ log.error("pidinit");
ProcessHandle currentProcess = ProcessHandle.current();
long pid = currentProcess.pid();
+ log.error("pid:{}", pid);
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "pid");
if (!pidFile.exists()) {
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
} else {
String oldPid = FileUtil.readUtf8String(pidFile);
+ log.error("oldPid:{}", oldPid);
if (StringUtils.isNotBlank(oldPid)) {
Optional processHandle = ProcessHandle.of(Long.parseLong(oldPid));
+ log.error("processHandle:{}", JSON.toJSONString(processHandle));
processHandle.ifPresent(handle -> {
ProcessHandle.Info info = handle.info();
String[] arguments = info.arguments().orElse(null);
+ log.error("arguments:{}", JSON.toJSONString(arguments));
if (arguments == null) {
return;
}
for (String argument : arguments) {
if (StringUtils.equals("chat2db-server-start.jar", argument)) {
handle.destroy();
+ log.error("destroy old process--------");
+ break;
+ }
+ if (StringUtils.equals("application", argument)) {
+ handle.destroy();
+ log.error("destroy old process--------");
break;
}
}
});
}
+
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
}
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/system/SystemController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/system/SystemController.java
index 2a17b5079..99e8153ed 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/system/SystemController.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/system/SystemController.java
@@ -53,10 +53,18 @@ public class SystemController {
*/
@GetMapping
public DataResult get() {
- ConfigJson configJson = ConfigUtils.getConfig();
- return DataResult.of(SystemVO.builder()
- .systemUuid(configJson.getSystemUuid())
- .build());
+ String clientVersion = System.getProperty("client.version");
+ String version = ConfigUtils.getLatestLocalVersion();
+ log.error("clientVersion:{},version:{}", clientVersion, version);
+ if (!StringUtils.equals(clientVersion, version)) {
+ stop();
+ return null;
+ } else {
+ ConfigJson configJson = ConfigUtils.getConfig();
+ return DataResult.of(SystemVO.builder()
+ .systemUuid(configJson.getSystemUuid())
+ .build());
+ }
}
private static final String UPDATE_TYPE = "client_update_type";
@@ -134,11 +142,12 @@ public DataResult stop(boolean forceQuit) {
if (forceQuit) {
stop();
} else {
- String clientVersion = System.getProperty("client.version");
- String version = ConfigUtils.getLocalVersion();
- if (!StringUtils.equals(clientVersion, version)) {
+// String clientVersion = System.getProperty("client.version");
+// String version = ConfigUtils.getLatestLocalVersion();
+// log.error("clientVersion:{},version:{}", clientVersion, version);
+// if (!StringUtils.equals(clientVersion, version)) {
stop();
- }
+ //}
}
return DataResult.of("ok");
}