Skip to content

Commit

Permalink
optimize: optimize time query conditions in the console (apache#6285)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingfudeshi authored and ptyin committed Feb 4, 2024
1 parent 3ac5fa0 commit bcdf4f6
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 23 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6291](https://github.com/apache/incubator-seata/pull/6291)] seata-server is developed in idea and console support output logs
- [[#6283](https://github.com/apache/incubator-seata/pull/6283)] add a compatible module to support io.seata APIs
- [[#6294](https://github.com/apache/incubator-seata/pull/6294)] split the frontend resource build process into separate profiles
- [[#6285](https://github.com/apache/incubator-seata/pull/6285)] optimize time query conditions in the console
- [[#6297](https://github.com/apache/incubator-seata/pull/6297)] fix problem of `maven-pmd-plugin`
- [[#6298](https://github.com/apache/incubator-seata/pull/6298)] repackage name to org.apache.seata
- [[#6306](https://github.com/apache/incubator-seata/pull/6306)] replace some URL to org/apache/seata
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
- [[#6291](https://github.com/apache/incubator-seata/pull/6291)] 优化seata-server在idea等开发工具运行时,控制台未输出完整日志的问题
- [[#6283](https://github.com/apache/incubator-seata/pull/6283)] 增加兼容模块支持 io.seata APIs
- [[#6294](https://github.com/apache/incubator-seata/pull/6294)] 拆分前端资源打包流程到单独的profile
- [[#6285](https://github.com/apache/incubator-seata/pull/6285)] 优化控台中时间查询条件不准确的问题
- [[#6297](https://github.com/apache/incubator-seata/pull/6297)] 修复 `maven-pmd-plugin` 相关的问题
- [[#6298](https://github.com/apache/incubator-seata/pull/6298)] 重命名包名为 org.apache.seata
- [[#6306](https://github.com/apache/incubator-seata/pull/6306)] 替换一些URL 至 org/apache/seata
Expand Down
38 changes: 38 additions & 0 deletions common/src/main/java/org/apache/seata/common/util/PageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,42 @@ public static void setObject(PreparedStatement ps, List<Object> sqlParamList) th
}
}
}

/**
* get sql for time start
* @param dbType
* @param timeColumnName
* @return java.lang.String
*/
public static String getTimeStartSql(String dbType, String timeColumnName) {
switch (dbType.toLowerCase()) {
case "mysql":
case "oracle":
case "postgresql":
case "sqlserver":
case "dm":
return " and FLOOR(" + timeColumnName + "/1000) >= ? ";
default:
throw new IllegalArgumentException("The DB type :" + dbType + " is not supported yet");
}
}

/**
* get sql for time end
* @param dbType
* @param timeColumnName
* @return java.lang.String
*/
public static String getTimeEndSql(String dbType, String timeColumnName) {
switch (dbType.toLowerCase()) {
case "mysql":
case "oracle":
case "postgresql":
case "sqlserver":
case "dm":
return " and FLOOR(" + timeColumnName + "/1000) <= ? ";
default:
throw new IllegalArgumentException("The DB type :" + dbType + " is not supported yet");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ private String getWhereConditionByParam(GlobalLockParam param, List<Object> sqlP
sqlParamList.add(param.getBranchId());
}
if (param.getTimeStart() != null) {
whereConditionBuilder.append(" and gmt_create >= ? ");
sqlParamList.add(param.getTimeStart());
whereConditionBuilder.append(PageUtil.getTimeStartSql(this.dbType, "gmt_create"));
sqlParamList.add(param.getTimeStart() / 1000);
}
if (param.getTimeEnd() != null) {
whereConditionBuilder.append(" and gmt_create <= ? ");
sqlParamList.add(param.getTimeEnd());
whereConditionBuilder.append(PageUtil.getTimeEndSql(this.dbType, "gmt_create"));
sqlParamList.add(param.getTimeEnd() / 1000);
}
String whereCondition = whereConditionBuilder.toString();
return whereCondition.replaceFirst("and", "where");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

Expand Down Expand Up @@ -149,12 +148,12 @@ private String getWhereConditionByParam(GlobalSessionParam param, List<Object> s
sqlParamList.add(param.getTransactionName());
}
if (param.getTimeStart() != null) {
whereConditionBuilder.append(" and gmt_create >= ? ");
sqlParamList.add(new Date(param.getTimeStart()));
whereConditionBuilder.append(PageUtil.getTimeStartSql(this.dbType, "begin_time"));
sqlParamList.add(param.getTimeStart() / 1000);
}
if (param.getTimeEnd() != null) {
whereConditionBuilder.append(" and gmt_create <= ? ");
sqlParamList.add(new Date(param.getTimeEnd()));
whereConditionBuilder.append(PageUtil.getTimeEndSql(this.dbType, "begin_time"));
sqlParamList.add(param.getTimeEnd() / 1000);
}
String whereCondition = whereConditionBuilder.toString();
return whereCondition.replaceFirst("and", "where");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ private Predicate<? super GlobalSession> obtainGlobalSessionPredicate(GlobalLock

&&
// timeStart
(isNull(param.getTimeStart()) || param.getTimeStart() <= globalSession.getBeginTime())
(isNull(param.getTimeStart()) || param.getTimeStart() / 1000 >= globalSession.getBeginTime() / 1000)

&&
// timeEnd
(isNull(param.getTimeEnd()) || param.getTimeEnd() >= globalSession.getBeginTime());
(isNull(param.getTimeEnd()) || param.getTimeEnd() / 1000 <= globalSession.getBeginTime() / 1000);

};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ private Predicate<? super GlobalSession> obtainPredicate(GlobalSessionParam para

&&
// timeStart
(isNull(param.getTimeStart()) || param.getTimeStart() <= session.getBeginTime())
(isNull(param.getTimeStart()) || param.getTimeStart() / 1000 >= session.getBeginTime() / 1000)

&&
// timeEnd
(isNull(param.getTimeEnd()) || param.getTimeEnd() >= session.getBeginTime());
(isNull(param.getTimeEnd()) || param.getTimeEnd() / 1000 <= session.getBeginTime() / 1000);

};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,20 @@ public void lockQueryTest(GlobalSession globalSessions1, GlobalSession globalSes
param.setTableName(null);
param.setTimeStart(dateFormat.parse("2022-1-1 08:00:01").getTime());
final PageResult<GlobalLockVO> timeTestResult1 = globalLockService.query(param);
Assertions.assertEquals(0, timeTestResult1.getTotal());
Assertions.assertEquals(8, timeTestResult1.getTotal());

param.setTimeStart(dateFormat.parse("2022-1-1 08:00:00").getTime());
final PageResult<GlobalLockVO> timeTestResult2 = globalLockService.query(param);
Assertions.assertEquals(4, timeTestResult2.getTotal());
Assertions.assertEquals(8, timeTestResult2.getTotal());

param.setTimeStart(null);
param.setTimeEnd(dateFormat.parse("2022-1-1 02:59:59").getTime());
final PageResult<GlobalLockVO> timeTestResult3 = globalLockService.query(param);
Assertions.assertEquals(0, timeTestResult3.getTotal());
Assertions.assertEquals(8, timeTestResult3.getTotal());

param.setTimeEnd(dateFormat.parse("2022-1-1 03:00:00").getTime());
final PageResult<GlobalLockVO> timeTestResult4 = globalLockService.query(param);
Assertions.assertEquals(4, timeTestResult4.getTotal());
Assertions.assertEquals(8, timeTestResult4.getTotal());

//test release lock
for (BranchSession branchSession : globalSessions1.getBranchSessions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,26 +348,25 @@ public void findGlobalSessionsWithPageResultTest(List<GlobalSession> globalSessi
Assertions.assertEquals(3, withBranchTestResult.getData().size());

// timeStart and timeEnd
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

globalSessionParam.setWithBranch(false);
Assertions.assertEquals(3, globalSessionService.query(globalSessionParam).getData().size());

globalSessionParam.setTimeStart(DateUtils.addHours(new Date(), 1).getTime());
Assertions.assertEquals(0, globalSessionService.query(globalSessionParam).getData().size());
Assertions.assertEquals(3, globalSessionService.query(globalSessionParam).getData().size());

globalSessionParam.setTimeStart(DateUtils.addHours(new Date(), -1).getTime());
Assertions.assertEquals(3, globalSessionService.query(globalSessionParam).getData().size());
Assertions.assertEquals(0, globalSessionService.query(globalSessionParam).getData().size());


globalSessionParam.setTimeStart(null);
Assertions.assertEquals(3, globalSessionService.query(globalSessionParam).getData().size());

globalSessionParam.setTimeEnd(DateUtils.addHours(new Date(), 1).getTime());
Assertions.assertEquals(3, globalSessionService.query(globalSessionParam).getData().size());
Assertions.assertEquals(0, globalSessionService.query(globalSessionParam).getData().size());

globalSessionParam.setTimeStart(DateUtils.addHours(new Date(), -1).getTime());
Assertions.assertEquals(3, globalSessionService.query(globalSessionParam).getData().size());
Assertions.assertEquals(0, globalSessionService.query(globalSessionParam).getData().size());
} finally {
for (GlobalSession globalSession : globalSessions) {
globalSession.end();
Expand Down

0 comments on commit bcdf4f6

Please sign in to comment.