Просмотр исходного кода

fix(video): 观看代次截止时间纳秒归一化并以SQL精度回归

Kingbase 的 start_deadline 为 timestamp(0),首次保存后截止时间被舍入,
后续 status/stop 与提供方同一来源严格校验不符,业务会话停在 CLEANUP_PENDING。
新代次在首次保存与首次下发前 withNano(0) 归一化;既有代次沿用原值,
不改提供方 sameSource 校验,截止预算不变、不延长已发出期限。

回归在 H2 SQL 适配层使用 TIMESTAMP(0) 与固定时钟,经公开会话接口
完成启动、读库后状态查询、停止与新会话重播。
离线验证 reactor 实跑 24 项通过(0 失败/错误/跳过)。
Kazerin 2 недель назад
Родитель
Сommit
efdbf11902

+ 31 - 0
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/VideoPreparedSqlTransactionTest.java

@@ -135,6 +135,37 @@ class VideoPreparedSqlTransactionTest {
         SecurityContextHolder.clearContext();
     }
 
+    @Test void secondPrecisionStoragePreservesDeadlineAcrossStatusStopAndReplay() throws Exception {
+        // Reproduce the deployed timestamp precision at the real SQL adapter, not in business mocks.
+        sql.execute("ALTER TABLE app_user.video_play_session ALTER COLUMN start_deadline TIMESTAMP(0)");
+        context.getBean(MutableClock.class).now = Instant.parse("2026-11-10T08:00:00.584Z");
+        when(media.post(eq("/internal/video/lifecycle/status"), any())).thenAnswer(call -> {
+            Map<String, Object> body = call.getArgument(1);
+            assertEquals(start.get().get("generation"), body.get("generation"));
+            assertEquals(start.get().get("startDeadline"), body.get("startDeadline"),
+                    "SQL round-trip must not change the immutable provider deadline");
+            return Map.of("generation", body.get("generation"), "status", "ACTIVE", "media",
+                    Map.of("stream", "sql_stream", "mediaServerId", "node-sql", "flv", "https://media.invalid/sql.flv"));
+        });
+        when(media.post(eq("/internal/video/lifecycle/cancel"), any())).thenAnswer(call -> {
+            Map<String, Object> body = call.getArgument(1); cancel.set(new LinkedHashMap<>(body));
+            assertEquals(start.get().get("startDeadline"), body.get("startDeadline"),
+                    "stop must use the deadline originally dispatched to the provider");
+            return Map.of("generation", body.get("generation"), "status", "STOPPED");
+        });
+        JSONObject first = prepare();
+        action(first, "live").andExpect(status().isOk()).andExpect(jsonPath("$.data.status").value("ACTIVE"));
+        String firstGeneration = (String) start.get().get("generation");
+        action(first, "status").andExpect(status().isOk()).andExpect(jsonPath("$.data.status").value("ACTIVE"));
+        action(first, "stop").andExpect(status().isOk()).andExpect(jsonPath("$.data.status").value("STOPPED"));
+        JSONObject second = prepare();
+        action(second, "live").andExpect(status().isOk()).andExpect(jsonPath("$.data.status").value("ACTIVE"));
+        assertNotEquals(firstGeneration, start.get().get("generation"));
+        action(second, "status").andExpect(status().isOk());
+        action(second, "stop").andExpect(status().isOk()).andExpect(jsonPath("$.data.status").value("STOPPED"));
+        assertEquals(Instant.parse("2026-11-10T08:00:30Z").toEpochMilli(), start.get().get("startDeadline"));
+    }
+
     @Test void activeSqlFailureCompensatesTheAlreadyStartedGenerationBeforeReturning() throws Exception {
         JSONObject viewer = prepare();
         // This is a real failed SQL statement AFTER identity/STARTING commit and AFTER media start.

+ 3 - 1
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/video/VideoLiveLifecycle.java

@@ -149,7 +149,9 @@ public final class VideoLiveLifecycle {
         if (generations.size() > 1 || deadlines.size() > 1 || streams.size() > 1 || keys.size() > 1 || nodes.size() > 1)
             throw new ServiceException("现存视频代次或句柄不一致,需人工核查");
         String generation = generations.isEmpty() ? UUID.randomUUID().toString() : generations.iterator().next();
-        LocalDateTime deadline = deadlines.isEmpty() ? LocalDateTime.ofInstant(clock.instant().plusSeconds(30), ZoneOffset.UTC)
+        // Canonicalize before both SQL persistence and dispatch: timestamp(0) otherwise rounds the
+        // first deadline, making every later status/cancel fail the provider's exact identity check.
+        LocalDateTime deadline = deadlines.isEmpty() ? LocalDateTime.ofInstant(clock.instant().plusSeconds(30), ZoneOffset.UTC).withNano(0)
                 : deadlines.iterator().next();
         for (VideoSession value : resources) if (value.getMediaGeneration() == null) {
             value.setMediaGeneration(generation); value.setStartDeadline(deadline); update(value);