Skip to content

Commit

Permalink
Merge pull request #948 from chat2db/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
shanhexi authored Dec 12, 2023
2 parents a8c4024 + 9cc07b6 commit 3b5056a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,29 +319,7 @@ public PageResult<Table> 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;
Expand Down Expand Up @@ -370,14 +348,45 @@ public PageResult<Table> pageQuery(TablePageQueryParam param, TableSelector sele
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<>();
String key = getTableKey(param.getDataSourceId(), param.getDatabaseName(), param.getSchemaName());
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class Application {

public static void main(String[] args) {
ConfigUtils.pid();
//ConfigUtils.pid();
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -134,11 +142,12 @@ public DataResult<String> 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");
}
Expand Down

0 comments on commit 3b5056a

Please sign in to comment.