-
Notifications
You must be signed in to change notification settings - Fork 8.9k
feature: add SqlMonitor and SlowSqlEntry for SQL execution monitoring #7491
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
Open
Oscarcheng0312
wants to merge
3
commits into
apache:2.x
Choose a base branch
from
Oscarcheng0312:feature/sql-monitor-common
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
common/src/main/java/org/apache/seata/common/monitor/SlowSqlEntry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.common.monitor; | ||
|
||
import java.time.Instant; | ||
|
||
public class SlowSqlEntry { | ||
|
||
private final String sql; | ||
private final long executionTimeMillis; | ||
private final Instant timestamp; | ||
|
||
public SlowSqlEntry(String sql, long executionTimeMillis, Instant timestamp) { | ||
this.sql = sql; | ||
this.executionTimeMillis = executionTimeMillis; | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getSql() { | ||
return sql; | ||
} | ||
|
||
public long getExecutionTimeMillis() { | ||
return executionTimeMillis; | ||
} | ||
|
||
public Instant getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SlowSqlEntry{" + "sql='" | ||
+ sql + '\'' + ", executionTimeMillis=" | ||
+ executionTimeMillis + ", timestamp=" | ||
+ timestamp + '}'; | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
common/src/main/java/org/apache/seata/common/monitor/SqlMonitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.common.monitor; | ||
|
||
import java.time.Instant; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class SqlMonitor { | ||
|
||
private static final SqlMonitor INSTANCE = new SqlMonitor(); | ||
private volatile long slowThreshold = 1000; | ||
private volatile int maxSlowEntries = 50; | ||
private final Deque<SlowSqlEntry> slowSqlQueue = new ConcurrentLinkedDeque<>(); | ||
private final Lock slowLock = new ReentrantLock(); | ||
private final Map<String, Integer> txnHistogram = new ConcurrentHashMap<>(); | ||
private final Map<String, Integer> holdHistogram = new ConcurrentHashMap<>(); | ||
|
||
private SqlMonitor() {} | ||
|
||
public static SqlMonitor getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public void record(String sql, long execMs, long holdMs) { | ||
// slow sql | ||
if (execMs > slowThreshold) { | ||
slowLock.lock(); | ||
try { | ||
slowSqlQueue.addLast(new SlowSqlEntry(sql, execMs, Instant.now())); | ||
if (slowSqlQueue.size() > maxSlowEntries) { | ||
slowSqlQueue.removeFirst(); | ||
} | ||
} finally { | ||
slowLock.unlock(); | ||
} | ||
} | ||
|
||
// transaction histogram | ||
String bin = chooseTxnBucket(execMs); | ||
txnHistogram.merge(bin, 1, Integer::sum); | ||
|
||
// connection hold time histogram | ||
String bin2 = chooseHoldBucket(holdMs); | ||
holdHistogram.merge(bin2, 1, Integer::sum); | ||
} | ||
|
||
/** | ||
* Set the execution time threshold for slow SQL. | ||
* Intended for use by dynamic configuration (e.g. Nacos or application.yml binding). | ||
*/ | ||
public void setSlowThreshold(long threshold) { | ||
this.slowThreshold = threshold; | ||
} | ||
|
||
/** | ||
* Set the max entries for slow SQL | ||
* Intended for use by dunamic configuration (e.g. Nacos or application.yml binding) | ||
*/ | ||
public void setMaxSlowEntries(int maxEntries) { | ||
this.maxSlowEntries = maxEntries; | ||
} | ||
|
||
public List<SlowSqlEntry> getSlowSqlList() { | ||
return new ArrayList<>(slowSqlQueue); | ||
} | ||
|
||
public Map<String, Integer> getTxnHistogram() { | ||
return new LinkedHashMap<>(txnHistogram); | ||
} | ||
|
||
public Map<String, Integer> getHoldHistogram() { | ||
return new LinkedHashMap<>(holdHistogram); | ||
} | ||
|
||
private String chooseTxnBucket(long ms) { | ||
if (ms <= 50) { | ||
return "0-50ms"; | ||
} else if (ms <= 200) { | ||
return "50-200ms"; | ||
} else if (ms <= 500) { | ||
return "200-500ms"; | ||
} else if (ms <= 1000) { | ||
return "500ms-1s"; | ||
} else if (ms <= 3000) { | ||
return "1s-3s"; | ||
} else { | ||
return "3s+"; | ||
} | ||
} | ||
|
||
private String chooseHoldBucket(long ms) { | ||
if (ms <= 50) { | ||
return "0-50ms"; | ||
} else if (ms <= 200) { | ||
return "50-200ms"; | ||
} else if (ms <= 500) { | ||
return "200-500ms"; | ||
} else if (ms <= 1000) { | ||
return "500ms-1s"; | ||
} else { | ||
return "1s+"; | ||
} | ||
} | ||
|
||
/** | ||
* Reset all internal states, only for testing purpose. | ||
*/ | ||
public void resetForTest() { | ||
slowLock.lock(); | ||
try { | ||
slowSqlQueue.clear(); | ||
} finally { | ||
slowLock.unlock(); | ||
} | ||
txnHistogram.clear(); | ||
holdHistogram.clear(); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
common/src/test/java/org/apache/seata/common/monitor/SqlMonitorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.common.monitor; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class SqlMonitorTest { | ||
private SqlMonitor monitor; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
monitor = SqlMonitor.getInstance(); | ||
monitor.resetForTest(); | ||
} | ||
|
||
@Test | ||
public void testRecordSlowSqlEntry() { | ||
monitor.record("SELECT * FROM users", 1500, 100); | ||
List<SlowSqlEntry> slowSqlList = monitor.getSlowSqlList(); | ||
assertEquals(1, slowSqlList.size()); | ||
SlowSqlEntry entry = slowSqlList.get(0); | ||
assertEquals("SELECT * FROM users", entry.getSql()); | ||
assertTrue(entry.getExecutionTimeMillis() >= 1500); | ||
} | ||
|
||
@Test | ||
public void testRecordMultiSlowEntry() { | ||
monitor.record("SELECT * FROM student", 1200, 100); | ||
monitor.record("SELECT * FROM school", 1300, 100); | ||
List<SlowSqlEntry> slowSqlList = monitor.getSlowSqlList(); | ||
assertEquals(2, slowSqlList.size()); | ||
SlowSqlEntry entry1 = slowSqlList.get(0); | ||
SlowSqlEntry entry2 = slowSqlList.get(1); | ||
assertEquals("SELECT * FROM student", entry1.getSql()); | ||
assertEquals("SELECT * FROM school", entry2.getSql()); | ||
} | ||
|
||
@Test | ||
public void testMaxSlowSqlQueueSize() { | ||
// maxSlowEntries = 50 by default | ||
for (int i = 0; i < 55; i++) { | ||
monitor.record("SELECT * FROM orders WHERE id = " + i, 1500, 200); | ||
} | ||
|
||
List<SlowSqlEntry> slowSqlList = monitor.getSlowSqlList(); | ||
assertEquals(50, slowSqlList.size()); | ||
|
||
// Should not contain the first 5 entries | ||
for (int i = 0; i < 5; i++) { | ||
int finalI = i; | ||
assertFalse( | ||
slowSqlList.stream() | ||
.map(SlowSqlEntry::getSql) | ||
.anyMatch(sql -> sql.equals("SELECT * FROM orders WHERE id = " + finalI)), | ||
"Entry with id = " + finalI + " should have been evicted"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRecordForFastSql() { | ||
monitor.record("SELECT 1", 100, 50); | ||
List<SlowSqlEntry> slowSqlList = monitor.getSlowSqlList(); | ||
assertTrue(slowSqlList.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testTxnHistogramBuckets() { | ||
monitor.record("SELECT * FROM t1", 30, 0); | ||
monitor.record("SELECT * FROM t2", 150, 0); | ||
monitor.record("SELECT * FROM t3", 300, 0); | ||
monitor.record("SELECT * FROM t4", 800, 0); | ||
monitor.record("SELECT * FROM t5", 2000, 0); | ||
monitor.record("SELECT * FROM t6", 4000, 0); | ||
|
||
Map<String, Integer> histogram = monitor.getTxnHistogram(); | ||
assertEquals(1, histogram.get("0-50ms")); | ||
assertEquals(1, histogram.get("50-200ms")); | ||
assertEquals(1, histogram.get("200-500ms")); | ||
assertEquals(1, histogram.get("500ms-1s")); | ||
assertEquals(1, histogram.get("1s-3s")); | ||
assertEquals(1, histogram.get("3s+")); | ||
} | ||
|
||
@Test | ||
public void testHoldHistogramBuckets() { | ||
monitor.record("SELECT * FROM hold1", 0, 30); | ||
monitor.record("SELECT * FROM hold2", 0, 150); | ||
monitor.record("SELECT * FROM hold3", 0, 300); | ||
monitor.record("SELECT * FROM hold4", 0, 800); | ||
monitor.record("SELECT * FROM hold5", 0, 2000); | ||
|
||
Map<String, Integer> histogram = monitor.getHoldHistogram(); | ||
assertEquals(1, histogram.get("0-50ms")); | ||
assertEquals(1, histogram.get("50-200ms")); | ||
assertEquals(1, histogram.get("200-500ms")); | ||
assertEquals(1, histogram.get("500ms-1s")); | ||
assertEquals(1, histogram.get("1s+")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a formed partial function, so the significance of the review is not great. You can first merge it into your own repository for development or merge it into the GSoC-conn branch of this repository. The time slice defined here is too rigid. Statistical indicators should be calculated in the console. The client side only needs to report the original values; otherwise, it will lead to distortion of data accuracy.