Browse Source

2026-11-18

优化
master
fengyuan_yang 2 days ago
parent
commit
9ac9e60b40
  1. 133
      src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java
  2. 15
      src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java
  3. 10
      src/main/resources/mapper/schedule/SchedulingMapper.xml

133
src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java

@ -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;
}
}

15
src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java

@ -11,7 +11,6 @@ import com.gaotao.common.utils.R;
import com.gaotao.modules.inboundNotification.service.impl.InboundNotificationServiceImpl;
import com.gaotao.modules.rollNoReview.data.SFDCMaterialHistOutData;
import com.gaotao.modules.schedule.CreateSplitRollTiming;
import com.gaotao.modules.schedule.SplitRollWaitProbe;
import com.gaotao.modules.schedule.data.*;
import com.gaotao.modules.schedule.data.dto.ScheduleDateShifDto;
import com.gaotao.modules.schedule.data.dto.SfdcNewsfdcDto;
@ -59,7 +58,6 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
@ -94,8 +92,6 @@ public class ScheduleServiceImpl implements ScheduleService {
private static final String TODO_DETAIL_STAGE_TRIGGER = "TRIGGER";
private static final String TODO_DETAIL_STAGE_REPORT = "REPORT";
@Autowired
private DataSource dataSource;
@Autowired
private ScheduleMapper scheduleMapper;
@Autowired
@ -5011,15 +5007,8 @@ public class ScheduleServiceImpl implements ScheduleService {
tempData.setRollNo(rollNoArr[i]);
//查询当前的打印的数据
stepStart = System.currentTimeMillis();
StringBuilder waitInfo = new StringBuilder();
Map<String, Object> printMap = SplitRollWaitProbe.call(dataSource,
() -> schedulingMapper.getSfdcFlowLabelData(tempData), waitInfo);
long labelMs = System.currentTimeMillis() - stepStart;
String labelStep = "查询流转标签数据";
if (labelMs >= 3000) {
labelStep = labelStep + " " + (waitInfo.length() > 0 ? waitInfo : "未采到等待信息");
}
CreateSplitRollTiming.record(labelStep, labelMs);
Map<String, Object> printMap = schedulingMapper.getSfdcFlowLabelData(tempData);
CreateSplitRollTiming.record("查询流转标签数据", System.currentTimeMillis() - stepStart);
//添加打印的数据
printList.add(printMap);

10
src/main/resources/mapper/schedule/SchedulingMapper.xml

@ -463,15 +463,15 @@
pt.DefaultLocationID locationId,
st.SiteName siteName
FROM SFDC_Rolls sfr
INNER JOIN ShopOrder so ON sfr.Site = so.Site AND sfr.OrderNo = so.OrderNo
INNER JOIN Part pt ON so.Site = pt.Site AND so.PartNo = pt.PartNo
INNER JOIN ShopOrder so ON so.Site = sfr.Site AND so.OrderNo = sfr.OrderNo
INNER JOIN Part pt ON pt.Site = so.Site AND pt.PartNo = CONVERT(varchar(30), so.PartNo)
LEFT JOIN Site st ON st.SiteID = pt.Site
<where>
AND sfr.Site = #{site}
AND sfr.Site = CONVERT(varchar(4), #{site})
<if test="orderNo != null and orderNo != ''">
AND sfr.OrderNo = #{orderNo}
AND sfr.OrderNo = CONVERT(varchar(20), #{orderNo})
</if>
AND sfr.RollNo = #{rollNo}
AND sfr.RollNo = CONVERT(varchar(50), #{rollNo})
AND sfr.VirtualRollFlag = 'N'
</where>
</select>

Loading…
Cancel
Save