From ba73bb79aa2afd8bdb6fa343b5695943ba770a85 Mon Sep 17 00:00:00 2001 From: ylz Date: Wed, 31 May 2023 13:38:26 +0800 Subject: [PATCH] fix high cpu usage inner 2240 --- .../dble/statistic/stat/UserSqlHighStat.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/actiontech/dble/statistic/stat/UserSqlHighStat.java b/src/main/java/com/actiontech/dble/statistic/stat/UserSqlHighStat.java index e94e1a284d..fd5b3a5301 100644 --- a/src/main/java/com/actiontech/dble/statistic/stat/UserSqlHighStat.java +++ b/src/main/java/com/actiontech/dble/statistic/stat/UserSqlHighStat.java @@ -7,7 +7,6 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; public class UserSqlHighStat { @@ -15,7 +14,6 @@ public class UserSqlHighStat { private Map sqlFrequencyMap = new ConcurrentHashMap<>(); - private ReentrantLock lock = new ReentrantLock(); private StatSqlParser sqlParser = new StatSqlParser(); @@ -24,24 +22,17 @@ public void addSql(String sql, long executeTime, long startTime, long endTime) { String newSql = this.sqlParser.mergeSql(sql); SqlFrequency frequency = this.sqlFrequencyMap.get(newSql); if (frequency == null) { - if (lock.tryLock()) { - try { - frequency = new SqlFrequency(); - frequency.setSql(newSql); - } finally { - lock.unlock(); - } - } else { - while (frequency == null) { - frequency = this.sqlFrequencyMap.get(newSql); - } + frequency = new SqlFrequency(); + frequency.setSql(newSql); + SqlFrequency tmp = sqlFrequencyMap.putIfAbsent(newSql, frequency); + if (tmp != null) { + frequency = tmp; } } frequency.setLastTime(endTime); frequency.incCount(); //TODO setExecuteTime has thread safe problem frequency.setExecuteTime(executeTime); - this.sqlFrequencyMap.put(newSql, frequency); }