REQ-3284: 查询数量过大告警

This commit is contained in:
yanglin 2024-12-02 11:34:30 +08:00
parent edc918a2fd
commit cb7a4c741f
3 changed files with 14 additions and 23 deletions

View File

@ -36,7 +36,7 @@ public class DiagnosisProps implements EnvironmentAware {
public static class RowCount {
private int warningThreshold = 6000;
private boolean enable = true;
private int resultSetWarnTimes = 3;
private int maxWarnTimes = 3;
/**
* For test purpose

View File

@ -43,7 +43,6 @@ class ProxyResultSet implements ResultSet {
@Override public void close() throws SQLException {
delegate.close();
stmt.resultSetClosed();
}
@Override public boolean wasNull() throws SQLException {

View File

@ -46,8 +46,7 @@ class ProxyStatement extends StatementWrapper {
private final Set<String> phaseWarned = new HashSet<>();
private volatile String sql;
private int rowCount = 0;
private int resultSetCount = 0;
private int resultSetWarnTimes = 0;
private int periodWarnTimes = 0;
public ProxyStatement(PreparedStatement rawStatement,
DiagnosisProps props,
@ -60,21 +59,7 @@ class ProxyStatement extends StatementWrapper {
void rowAdvanced() {
rowCount++;
if (rowCountAboveThreshold() && setWarnedForPhase("Threshold"))
warnFor("Threshold");
}
public void resultSetClosed() {
if (rowCountAboveThreshold() && shouldWarnOnResultSetClosed()) {
String phase = String.format("ResultSetClose-%d-%d", resultSetWarnTimes, resultSetCount);
warnFor(phase);
resultSetWarnTimes++;
}
}
private boolean shouldWarnOnResultSetClosed() {
int maxWarnTimes = props.getRowCount().getResultSetWarnTimes();
return rowCount % 2000 == 0 && resultSetWarnTimes <= maxWarnTimes;
tryWarnFor("Threshold", true);
}
@Override
@ -87,18 +72,20 @@ class ProxyStatement extends StatementWrapper {
public ResultSet getResultSet() throws SQLException {
ResultSet resultSet = super.getResultSet();
if (resultSet == null) return null;
resultSetCount++;
return new ProxyResultSet(this, resultSet);
}
@Override
public void close() throws SQLException {
super.close();
if (rowCountAboveThreshold() && setWarnedForPhase("StatementClose"))
warnFor("StatementClose");
tryWarnFor("StatementClose", false);
}
private void warnFor(String phase) {
private void tryWarnFor(String phase, boolean periodically) {
if (!rowCountAboveThreshold()) return;
if (periodically && !canWarnPeriodically()) return;
if (periodically)
periodWarnTimes++;
try {
// make a copy
int rowCount = this.rowCount;
@ -121,6 +108,11 @@ class ProxyStatement extends StatementWrapper {
return rowCount > props.getRowCount().determineWarningThreshold();
}
private boolean canWarnPeriodically() {
int maxWarnTimes = props.getRowCount().getMaxWarnTimes();
return rowCount % 2000 == 0 && periodWarnTimes <= maxWarnTimes;
}
private void sendWarn(String phase, int currentCount, String traceId) {
DingTalkService dingTalkService = beanFactory
.getBeanProvider(DingTalkService.class).getIfAvailable();