Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix high cpu usage inner 2240 #3715

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class UserSqlHighStat {

private static final int CAPACITY_SIZE = 1024;

private Map<String, SqlFrequency> sqlFrequencyMap = new ConcurrentHashMap<>();

private ReentrantLock lock = new ReentrantLock();


private StatSqlParser sqlParser = new StatSqlParser();
Expand All @@ -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);
}


Expand Down