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 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 class TableServiceImpl implements TableService {
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
public ListResult<SimpleTable> queryTables(TablePageQueryParam param) {
LambdaQueryWrapper<TableCacheVersionDO> queryWrapper = new LambdaQueryWrapper<>();
@ -377,7 +386,7 @@ public class TableServiceImpl implements TableService {
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();

View File

@ -146,7 +146,9 @@ public class Dbutils {
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");

View File

@ -32,7 +32,7 @@ import org.springframework.stereotype.Indexed;
public class Application {
public static void main(String[] args) {
ConfigUtils.pid();
//ConfigUtils.pid();
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:
port: 10822

View File

@ -61,6 +61,14 @@ public class ConfigUtils {
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 @@ public class ConfigUtils {
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 = 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);
}

View File

@ -53,10 +53,18 @@ public class SystemController {
*/
@GetMapping
public DataResult<SystemVO> 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 class SystemController {
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");
}