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

issue 3238 - fix cpu 100% #3569

Merged
merged 2 commits into from
Dec 28, 2022
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,41 +7,29 @@

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();

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