3 changed files with 7 additions and 151 deletions
-
133src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java
-
15src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java
-
10src/main/resources/mapper/schedule/SchedulingMapper.xml
@ -1,133 +0,0 @@ |
|||
package com.gaotao.modules.schedule; |
|||
|
|||
import org.springframework.jdbc.datasource.DataSourceUtils; |
|||
|
|||
import javax.sql.DataSource; |
|||
import java.sql.Connection; |
|||
import java.sql.PreparedStatement; |
|||
import java.sql.ResultSet; |
|||
import java.sql.Statement; |
|||
import java.util.concurrent.Callable; |
|||
import java.util.concurrent.Executors; |
|||
import java.util.concurrent.ScheduledExecutorService; |
|||
import java.util.concurrent.ScheduledFuture; |
|||
import java.util.concurrent.TimeUnit; |
|||
import java.util.concurrent.atomic.AtomicReference; |
|||
|
|||
/** |
|||
* 创建分卷的查询若超过 1 秒,用另一条连接记下当前会话在等什么。 |
|||
* 业务连接自己被堵住时,看不到自己的等待。 |
|||
*/ |
|||
public final class SplitRollWaitProbe { |
|||
|
|||
private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(2, runnable -> { |
|||
Thread thread = new Thread(runnable, "split-roll-wait-probe"); |
|||
thread.setDaemon(true); |
|||
return thread; |
|||
}); |
|||
|
|||
private static final String WAIT_SQL = |
|||
"SELECT TOP 1 r.wait_type, r.wait_time, r.blocking_session_id, w.wait_resource, " + |
|||
"LEFT(REPLACE(REPLACE(ISNULL(bt.text, N''), CHAR(13), N' '), CHAR(10), N' '), 180) AS blocking_sql " + |
|||
"FROM sys.dm_exec_requests r " + |
|||
"LEFT JOIN sys.dm_os_waiting_tasks w ON w.session_id = r.session_id " + |
|||
"LEFT JOIN sys.dm_exec_requests b ON b.session_id = NULLIF(r.blocking_session_id, 0) " + |
|||
"OUTER APPLY sys.dm_exec_sql_text(b.sql_handle) bt " + |
|||
"WHERE r.session_id = ? " + |
|||
"ORDER BY CASE WHEN r.blocking_session_id > 0 THEN 0 ELSE 1 END, r.wait_time DESC"; |
|||
|
|||
private SplitRollWaitProbe() { |
|||
} |
|||
|
|||
public static <T> T call(DataSource dataSource, Callable<T> action, StringBuilder waitInfo) { |
|||
int spid = currentSpid(dataSource); |
|||
AtomicReference<Sample> sample = new AtomicReference<>(); |
|||
ScheduledFuture<?> future = null; |
|||
if (spid > 0 && dataSource != null) { |
|||
future = EXECUTOR.scheduleWithFixedDelay(() -> capture(dataSource, spid, sample), 1, 1, TimeUnit.SECONDS); |
|||
} |
|||
try { |
|||
return action.call(); |
|||
} catch (RuntimeException ex) { |
|||
throw ex; |
|||
} catch (Exception ex) { |
|||
throw new IllegalStateException(ex); |
|||
} finally { |
|||
if (future != null) { |
|||
future.cancel(false); |
|||
} |
|||
Sample found = sample.get(); |
|||
if (waitInfo != null && found != null && found.text != null && !found.text.isEmpty()) { |
|||
waitInfo.append(found.text); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private static int currentSpid(DataSource dataSource) { |
|||
if (dataSource == null) { |
|||
return 0; |
|||
} |
|||
Connection connection = null; |
|||
try { |
|||
connection = DataSourceUtils.getConnection(dataSource); |
|||
try (Statement statement = connection.createStatement(); |
|||
ResultSet rs = statement.executeQuery("SELECT @@SPID")) { |
|||
return rs.next() ? rs.getInt(1) : 0; |
|||
} |
|||
} catch (Exception ex) { |
|||
return 0; |
|||
} finally { |
|||
DataSourceUtils.releaseConnection(connection, dataSource); |
|||
} |
|||
} |
|||
|
|||
private static void capture(DataSource dataSource, int spid, AtomicReference<Sample> sample) { |
|||
try (Connection connection = dataSource.getConnection(); |
|||
PreparedStatement statement = connection.prepareStatement(WAIT_SQL)) { |
|||
statement.setInt(1, spid); |
|||
try (ResultSet rs = statement.executeQuery()) { |
|||
if (!rs.next()) { |
|||
return; |
|||
} |
|||
int waitTime = rs.getInt("wait_time"); |
|||
Sample previous = sample.get(); |
|||
if (previous != null && previous.waitTime >= waitTime) { |
|||
return; |
|||
} |
|||
String waitType = rs.getString("wait_type"); |
|||
int blockingSessionId = rs.getInt("blocking_session_id"); |
|||
String waitResource = rs.getString("wait_resource"); |
|||
String blockingSql = rs.getString("blocking_sql"); |
|||
StringBuilder text = new StringBuilder(); |
|||
text.append("等待=").append(waitType == null ? "" : waitType.trim()); |
|||
text.append(" 等待时长=").append(waitTime).append("毫秒"); |
|||
if (blockingSessionId > 0) { |
|||
text.append(" 阻塞会话=").append(blockingSessionId); |
|||
} |
|||
if (waitResource != null && !waitResource.trim().isEmpty()) { |
|||
text.append(" 等待资源=").append(waitResource.trim()); |
|||
} |
|||
if (blockingSql != null && !blockingSql.trim().isEmpty()) { |
|||
text.append(" 阻塞语句=").append(blockingSql.trim()); |
|||
} |
|||
Sample next = new Sample(); |
|||
next.waitTime = waitTime; |
|||
next.text = text.toString(); |
|||
sample.set(next); |
|||
} |
|||
} catch (Exception ex) { |
|||
Sample previous = sample.get(); |
|||
if (previous == null) { |
|||
Sample failed = new Sample(); |
|||
failed.waitTime = 0; |
|||
failed.text = "等待信息读取失败=" + ex.getClass().getSimpleName(); |
|||
sample.set(failed); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private static final class Sample { |
|||
private int waitTime; |
|||
private String text; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue