Improve startup speed

This commit is contained in:
SwallowGG
2023-12-12 23:04:39 +08:00
parent fe1b362af3
commit 3df13fc9bc
6 changed files with 74 additions and 47 deletions

View File

@ -319,29 +319,7 @@ public class TableServiceImpl implements TableService {
long total = 0; long total = 0;
long version = 0L; long version = 0L;
if (param.isRefresh() || versionDO == null) { if (param.isRefresh() || versionDO == null) {
version = getLock(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName(), versionDO); total = addCache(param,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);
}
} else { } else {
if ("2".equals(versionDO.getStatus())) { if ("2".equals(versionDO.getStatus())) {
version = versionDO.getVersion() - 1; version = versionDO.getVersion() - 1;
@ -370,6 +348,37 @@ public class TableServiceImpl implements TableService {
return PageResult.of(tables, total, param); return PageResult.of(tables, total, param);
} }
private long addCache(TablePageQueryParam param,TableCacheVersionDO versionDO){
LambdaQueryWrapper<TableCacheVersionDO> 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 @Override
public ListResult<SimpleTable> queryTables(TablePageQueryParam param) { public ListResult<SimpleTable> queryTables(TablePageQueryParam param) {
LambdaQueryWrapper<TableCacheVersionDO> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<TableCacheVersionDO> queryWrapper = new LambdaQueryWrapper<>();
@ -377,7 +386,7 @@ public class TableServiceImpl implements TableService {
queryWrapper.eq(TableCacheVersionDO::getKey, key); queryWrapper.eq(TableCacheVersionDO::getKey, key);
TableCacheVersionDO versionDO = getVersionMapper().selectOne(queryWrapper); TableCacheVersionDO versionDO = getVersionMapper().selectOne(queryWrapper);
if (versionDO == null) { if (versionDO == null) {
return ListResult.of(Lists.newArrayList()); addCache(param,versionDO);
} }
long version = "2".equals(versionDO.getStatus()) ? versionDO.getVersion() - 1 : versionDO.getVersion(); long version = "2".equals(versionDO.getStatus()) ? versionDO.getVersion() - 1 : versionDO.getVersion();

View File

@ -146,7 +146,9 @@ public class Dbutils {
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev"); String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
if ("dev".equalsIgnoreCase(environment)) { if ("dev".equalsIgnoreCase(environment)) {
dataSource.setJdbcUrl("jdbc:h2:file:~/.chat2db/db/chat2db_dev;MODE=MYSQL"); 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.setJdbcUrl("jdbc:h2:~/.chat2db/db/chat2db;MODE=MYSQL;FILE_LOCK=NO");
} }
dataSource.setDriverClassName("org.h2.Driver"); dataSource.setDriverClassName("org.h2.Driver");

View File

@ -32,7 +32,7 @@ import org.springframework.stereotype.Indexed;
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
ConfigUtils.pid(); //ConfigUtils.pid();
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
} }

View File

@ -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: server:
port: 10822 port: 10822

View File

@ -61,6 +61,14 @@ public class ConfigUtils {
version = StringUtils.trim(FileUtil.readUtf8String(versionFile)); version = StringUtils.trim(FileUtil.readUtf8String(versionFile));
return version; 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() { public static ConfigJson getConfig() {
if (config == null) { if (config == null) {
@ -91,31 +99,42 @@ public class ConfigUtils {
public static void pid() { public static void pid() {
try { try {
log.error("pidinit");
ProcessHandle currentProcess = ProcessHandle.current(); ProcessHandle currentProcess = ProcessHandle.current();
long pid = currentProcess.pid(); long pid = currentProcess.pid();
log.error("pid:{}", pid);
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev"); String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "pid"); File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "pid");
if (!pidFile.exists()) { if (!pidFile.exists()) {
FileUtil.writeUtf8String(String.valueOf(pid), pidFile); FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
} else { } else {
String oldPid = FileUtil.readUtf8String(pidFile); String oldPid = FileUtil.readUtf8String(pidFile);
log.error("oldPid:{}", oldPid);
if (StringUtils.isNotBlank(oldPid)) { if (StringUtils.isNotBlank(oldPid)) {
Optional<ProcessHandle> processHandle = ProcessHandle.of(Long.parseLong(oldPid)); Optional<ProcessHandle> processHandle = ProcessHandle.of(Long.parseLong(oldPid));
log.error("processHandle:{}", JSON.toJSONString(processHandle));
processHandle.ifPresent(handle -> { processHandle.ifPresent(handle -> {
ProcessHandle.Info info = handle.info(); ProcessHandle.Info info = handle.info();
String[] arguments = info.arguments().orElse(null); String[] arguments = info.arguments().orElse(null);
log.error("arguments:{}", JSON.toJSONString(arguments));
if (arguments == null) { if (arguments == null) {
return; return;
} }
for (String argument : arguments) { for (String argument : arguments) {
if (StringUtils.equals("chat2db-server-start.jar", argument)) { if (StringUtils.equals("chat2db-server-start.jar", argument)) {
handle.destroy(); handle.destroy();
log.error("destroy old process--------");
break;
}
if (StringUtils.equals("application", argument)) {
handle.destroy();
log.error("destroy old process--------");
break; break;
} }
} }
}); });
} }
FileUtil.writeUtf8String(String.valueOf(pid), pidFile); FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
} }

View File

@ -53,10 +53,18 @@ public class SystemController {
*/ */
@GetMapping @GetMapping
public DataResult<SystemVO> get() { public DataResult<SystemVO> get() {
ConfigJson configJson = ConfigUtils.getConfig(); String clientVersion = System.getProperty("client.version");
return DataResult.of(SystemVO.builder() String version = ConfigUtils.getLatestLocalVersion();
.systemUuid(configJson.getSystemUuid()) log.error("clientVersion:{},version:{}", clientVersion, version);
.build()); 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"; private static final String UPDATE_TYPE = "client_update_type";
@ -134,11 +142,12 @@ public class SystemController {
if (forceQuit) { if (forceQuit) {
stop(); stop();
} else { } else {
String clientVersion = System.getProperty("client.version"); // String clientVersion = System.getProperty("client.version");
String version = ConfigUtils.getLocalVersion(); // String version = ConfigUtils.getLatestLocalVersion();
if (!StringUtils.equals(clientVersion, version)) { // log.error("clientVersion:{},version:{}", clientVersion, version);
// if (!StringUtils.equals(clientVersion, version)) {
stop(); stop();
} //}
} }
return DataResult.of("ok"); return DataResult.of("ok");
} }