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

This commit is contained in:
yanglin 2024-11-29 10:39:07 +08:00
parent 1f16cef438
commit e456e36323

View File

@ -24,6 +24,7 @@ import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
/**
* @author yanglin
@ -56,16 +57,18 @@ class ProxyStatement extends StatementWrapper {
}
void rowAdvanced() {
int currentRowCount = rowCount++;
if (tryWarnForPhase("Threshold"))
maybeWarn("Threshold", currentRowCount);
rowCount++;
warnFor("Threshold", () -> shouldWarnForPhase("Threshold"));
}
public void resultSetClosed() {
if (rowCount % 2000 == 0 && resetSetWarnTimes <= props.getRowCount().getResultSetWarnTimes()) {
maybeWarn(String.format("ResultSetClose-%d-%d", resetSetWarnTimes, resultSetCount), rowCount);
String phase = String.format("ResultSetClose-%d-%d", resetSetWarnTimes, resultSetCount);
if (warnFor(phase, this::shouldWarnOnResultSetClosed))
resetSetWarnTimes++;
}
}
private boolean shouldWarnOnResultSetClosed() {
return rowCount % 2000 == 0 && resetSetWarnTimes <= props.getRowCount().getResultSetWarnTimes();
}
@Override
@ -84,12 +87,12 @@ class ProxyStatement extends StatementWrapper {
@Override
public void close() throws SQLException {
super.close();
if (tryWarnForPhase("StatementClose"))
maybeWarn("StatementClose", rowCount);
warnFor("StatementClose", () -> shouldWarnForPhase("StatementClose"));
}
private void maybeWarn(String phase, int rowCount) {
if (rowCount < props.getRowCount().getWarningThreshold()) return;
private boolean warnFor(String phase, Supplier<Boolean> guard) {
if (rowCount <= props.getRowCount().getWarningThreshold()) return false;
if (!guard.get()) return false;
try {
String traceId = TraceUtils.getOrCreateTraceId();
EXECUTOR.execute(() -> {
@ -99,10 +102,13 @@ class ProxyStatement extends StatementWrapper {
log.warn("[Task] Error sending warning", e);
}
});
return true;
} catch (RejectedExecutionException e) {
log.warn("[Submit] Error sending warning, task rejected");
return false;
} catch (Exception e) {
log.warn("[Submit] Error sending warning", e);
return false;
}
}
@ -153,10 +159,7 @@ class ProxyStatement extends StatementWrapper {
}
}
/**
* 不要和 {@link #maybeWarn} 合并在一起
*/
private boolean tryWarnForPhase(String phase) {
private boolean shouldWarnForPhase(String phase) {
if (phaseWarned.contains(phase)) return false;
phaseWarned.add(phase);
return true;