fix(临时载荷): 收口删除守卫 + chunk 读兜底,修「对象被误删→任务永久失败」

线上任务 28459(外观专利)三个分片的载荷对象被删、DB 行仍指向已删的确定性
key,组装读不到 → 整单 FAILED。数据其实还在同槽位的版本化对象里,是行指错了。

三条收口:
- 上传补偿删除加准入:只有末段带 UUID 的版本化 key(本次写入独占)才允许入队。
  确定性 key 会被重传复用,删它就删掉了行仍在引用的对象;而删除队列
  (deleteObjectFromRetry -> removeObject)本身不做引用反查,准入必须卡在入队处。
- 引用守卫阈值 >1 收紧为 >0:原来把「恰好 1 行引用」当作调用方自己那行而放行,
  但调用方无法证明归属。物理删除本就约定在 DB 行删除之后执行,正常路径引用数
  必为 0;宁可留孤儿(有保留期清理兜底),也不删可能仍被引用的对象。
- chunk 读兜底:指针对象确已缺失(NoSuchKey)时回退同槽位版本化对象 chunk-N-*。
  仅限 chunk 槽位 + 确为缺失两个条件,避免误配无关对象或掩盖真实故障。

测试:新增 31 个用例(补偿删除准入 / 引用守卫 / 兄弟对象兜底),均先确认 RED
再实现;另更新 3 个断言旧行为的既有用例。全量 mvn test 3082 个 0 失败。
This commit is contained in:
2026-09-17 10:42:53 +08:00
parent a75b843937
commit c9be8f87d4
7 changed files with 812 additions and 15 deletions
@@ -297,16 +297,19 @@ class RustfsObjectStorageServiceTest {
RustfsObjectStorageService service = new RustfsObjectStorageService(
properties, emptyProvider(), provider(retryService), () -> client);
// 补偿删除只对版本化 key(本次写入独占)生效,故这里用 versioned key 覆盖该路径
String firstKey = "task/a-11111111-2222-3333-4444-555555555555.json";
String secondKey = "task/b-66666666-7777-8888-9999-000000000000.json";
IllegalStateException firstFailure = assertThrows(IllegalStateException.class,
() -> service.uploadText("task/a.json", "{}", true));
() -> service.uploadText(firstKey, "{}", true));
IllegalStateException secondFailure = assertThrows(IllegalStateException.class,
() -> service.uploadText("task/b.json", "{}", true));
() -> service.uploadText(secondKey, "{}", true));
assertTrue(firstFailure.getMessage().contains("not visible"));
assertTrue(secondFailure.getMessage().contains("not visible"));
verify(client, times(5)).statObject(any(StatObjectArgs.class));
verify(retryService).enqueue(eq("task/a.json"), same(firstFailure));
verify(retryService).enqueue(eq("task/b.json"), same(secondFailure));
verify(retryService).enqueue(eq(firstKey), same(firstFailure));
verify(retryService).enqueue(eq(secondKey), same(secondFailure));
IllegalStateException rejected = assertThrows(IllegalStateException.class,
() -> service.uploadText("task/c.json", "{}", false));
assertTrue(rejected.getMessage().contains("cooldown active"));
@@ -360,6 +363,9 @@ class RustfsObjectStorageServiceTest {
@Test
void completedUploadIsEnqueuedWhenDeadlineExpiresAfterPut() throws Exception {
// 只有版本化 key(本次写入独占)才允许补偿删除;共享 key 的守卫见
// RustfsUploadCompensationGuardTest#deterministicKeyDerivedFromShapeDoesNotEnqueueCompensation
String versionedKey = "task/chunk-1-0ffc254b-afad-4279-aab2-e85e3ff955e9.json";
TransientStorageProperties properties = configuredProperties();
properties.setOperationTimeoutSeconds(1);
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
@@ -372,11 +378,11 @@ class RustfsObjectStorageServiceTest {
properties, emptyProvider(), provider(retryService), () -> client);
IllegalStateException failure = assertThrows(IllegalStateException.class,
() -> service.uploadText("task/a.json", "{}", false));
() -> service.uploadText(versionedKey, "{}", false));
assertTrue(failure.getMessage().contains("operation timeout"));
verify(client).putObject(any(PutObjectArgs.class));
verify(retryService).enqueue(eq("task/a.json"), same(failure));
verify(retryService).enqueue(eq(versionedKey), same(failure));
}
private static TransientStorageProperties configuredProperties() {
@@ -0,0 +1,217 @@
package com.nanri.aiimage.modules.file.service.object;
import com.nanri.aiimage.config.TransientStorageProperties;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.StatObjectArgs;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectProvider;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 上传失败补偿删除的准入守卫(2026-09-17 线上任务 28459 事故)。
*
* <p>事故形态:客户端重传复用**确定性 key**({@code chunk-462.json})。第一次上传 put 已成功、
* 但随后的 {@code verifyObjectVisible} 抖动 → 旧代码无条件
* {@code enqueueDeleteRetry(objectKey)};而删除重试队列对该 key 不再做任何引用校验
* ({@code RustfsDeleteRetryService} → {@code deleteObjectFromRetry} → {@code removeObject}),
* 于是把重传成功后 DB 行仍指向的对象删掉,下游读 chunk 直接 404,整个任务组装失败。
*
* <p>口径:只有**本次写入独占的对象**(版本化 key,末段以 UUID 结尾)才允许补偿删除;
* 确定性 key 会被重写复用,删它就可能删掉别的行正在引用的对象。
*/
class RustfsUploadCompensationGuardTest {
// ===== 正常路径 =====
/** 唯一 key(版本化)+ 验证失败 + 显式允许 → 补偿删除入队。 */
@Test
void uniqueKeyVerifyFailureEnqueuesCompensation() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
RustfsObjectStorageService service = new RustfsObjectStorageService(
configuredProperties(), emptyProvider(), provider(retryService), RustfsUploadCompensationGuardTest::putOkButStatFails);
assertThrows(IllegalStateException.class,
() -> service.uploadBytes("task/chunk-462-0ffc254b-afad-4279-aab2-e85e3ff955e9.json",
"{}".getBytes(), true, true));
verify(retryService).enqueue(eq("task/chunk-462-0ffc254b-afad-4279-aab2-e85e3ff955e9.json"),
any(Throwable.class));
}
/** 三参重载按 key 形态判定:版本化 key 自动允许补偿删除。 */
@Test
void versionedKeyDerivedFromShapeEnqueuesCompensation() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
RustfsObjectStorageService service = new RustfsObjectStorageService(
configuredProperties(), emptyProvider(), provider(retryService), RustfsUploadCompensationGuardTest::putOkButStatFails);
assertThrows(IllegalStateException.class,
() -> service.uploadBytes("task/chunk-77-d03bf3b0-abbb-469d-9bb8-a7c01c309477.json",
"{}".getBytes(), true));
verify(retryService).enqueue(anyString(), any(Throwable.class));
}
// ===== 事故根因:共享 key 必须不删 =====
/** 确定性(共享)key + 显式禁止 → 不入队(异常仍照旧抛出,失败归属不变)。 */
@Test
void sharedKeyVerifyFailureDoesNotEnqueueCompensation() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
RustfsObjectStorageService service = new RustfsObjectStorageService(
configuredProperties(), emptyProvider(), provider(retryService), RustfsUploadCompensationGuardTest::putOkButStatFails);
assertThrows(IllegalStateException.class,
() -> service.uploadBytes("task/chunk-462.json", "{}".getBytes(), true, false));
verify(retryService, never()).enqueue(anyString(), any(Throwable.class));
}
/** 三参重载按 key 形态判定:确定性 key 自动禁止补偿删除(本次事故的直接修复点)。 */
@Test
void deterministicKeyDerivedFromShapeDoesNotEnqueueCompensation() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
RustfsObjectStorageService service = new RustfsObjectStorageService(
configuredProperties(), emptyProvider(), provider(retryService), RustfsUploadCompensationGuardTest::putOkButStatFails);
assertThrows(IllegalStateException.class,
() -> service.uploadBytes("task/chunk-462.json", "{}".getBytes(), true));
verify(retryService, never()).enqueue(anyString(), any(Throwable.class));
}
/** verify=false 上传即成功,不得产生任何补偿删除。 */
@Test
void verifyDisabledNeverEnqueuesCompensation() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
RustfsObjectStorageService service = new RustfsObjectStorageService(
configuredProperties(), emptyProvider(), provider(retryService), RustfsUploadCompensationGuardTest::putOkButStatFails);
String key = service.uploadBytes("task/chunk-462-uuid-x.json", "{}".getBytes(), false, true);
assertEquals("task/chunk-462-uuid-x.json", key);
verify(retryService, never()).enqueue(anyString(), any(Throwable.class));
}
// ===== 异常路径 =====
/** put 从未成功过:对象本就不存在,不该产生删除动作(与开关无关)。 */
@Test
void putNeverCompletedDoesNotEnqueueCompensation() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
MinioClient client = mock(MinioClient.class);
when(client.putObject(any(PutObjectArgs.class))).thenThrow(new IOException("put failed"));
TransientStorageProperties properties = configuredProperties();
properties.setUploadMaxRetries(1);
RustfsObjectStorageService service = new RustfsObjectStorageService(
properties, emptyProvider(), provider(retryService), () -> client);
assertThrows(RuntimeException.class,
() -> service.uploadBytes("task/chunk-462-uuid-x.json", "{}".getBytes(), true, true));
verify(retryService, never()).enqueue(anyString(), any(Throwable.class));
}
/** 补偿删除入队本身失败:不得把上传失败改写成入队异常,原异常必须继续上抛。 */
@Test
void enqueueFailureDoesNotMaskUploadFailure() throws Exception {
RustfsDeleteRetryService retryService = mock(RustfsDeleteRetryService.class);
org.mockito.Mockito.doThrow(new IllegalStateException("queue down"))
.when(retryService).enqueue(anyString(), any(Throwable.class));
RustfsObjectStorageService service = new RustfsObjectStorageService(
configuredProperties(), emptyProvider(), provider(retryService), RustfsUploadCompensationGuardTest::putOkButStatFails);
IllegalStateException ex = assertThrows(IllegalStateException.class,
() -> service.uploadBytes("task/chunk-9-11111111-2222-3333-4444-555555555555.json",
"{}".getBytes(), true, true));
assertFalse(ex.getMessage() == null || ex.getMessage().contains("queue down"),
"入队异常不应成为对外错误;实际=" + ex.getMessage());
}
// ===== key 形态判定的边界 =====
@Test
void versionedKeyShapeIsRecognized() {
assertTrue(RustfsObjectStorageService.isVersionedObjectKey(
"task-chunk/appearance_patent/28459/hash/chunk-462-0ffc254b-afad-4279-aab2-e85e3ff955e9.json"));
assertTrue(RustfsObjectStorageService.isVersionedObjectKey(
"task-chunk/x/1/hash/0ffc254b-afad-4279-aab2-e85e3ff955e9.json"),
"整个 entryKey 就是 UUID 时也应识别为唯一 key");
assertTrue(RustfsObjectStorageService.isVersionedObjectKey(
"task-chunk/x/1/hash/chunk-1-0ffc254b-afad-4279-aab2-e85e3ff955e9"),
"无 .json 后缀但末段是 UUID,同样视为唯一");
}
@Test
void deterministicKeyShapeIsNotRecognized() {
assertFalse(RustfsObjectStorageService.isVersionedObjectKey("task-chunk/x/1/hash/chunk-462.json"));
assertFalse(RustfsObjectStorageService.isVersionedObjectKey("task-parsed/x/1/hash/latest.json"));
assertFalse(RustfsObjectStorageService.isVersionedObjectKey("task-result-payload/x/1/hash/28459.json"),
"提交号这类 key 会被重写复用,不能当成唯一 key");
assertFalse(RustfsObjectStorageService.isVersionedObjectKey(
"task-chunk/x/1/0ffc254b-afad-4279-aab2-e85e3ff955e9/chunk-462.json"),
"UUID 出现在中间段不算唯一——重写复用的判据只看末段");
}
@Test
void malformedKeyShapeDoesNotThrowAndIsTreatedAsShared() {
assertFalse(RustfsObjectStorageService.isVersionedObjectKey(null));
assertFalse(RustfsObjectStorageService.isVersionedObjectKey(""));
assertFalse(RustfsObjectStorageService.isVersionedObjectKey(" "));
assertFalse(RustfsObjectStorageService.isVersionedObjectKey("chunk-462.json"));
assertFalse(RustfsObjectStorageService.isVersionedObjectKey("-afad-4279-aab2-e85e3ff955e9.json".substring(0, 20)));
}
// ===== 辅助 =====
private static MinioClient putOkButStatFails() {
MinioClient client = mock(MinioClient.class);
try {
when(client.putObject(any(PutObjectArgs.class))).thenReturn(null);
when(client.statObject(any(StatObjectArgs.class))).thenThrow(new IOException("stat 抖动"));
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
return client;
}
private static TransientStorageProperties configuredProperties() {
TransientStorageProperties properties = new TransientStorageProperties();
properties.setEndpoint("http://127.0.0.1:9000");
properties.setBucket("bucket");
properties.setAccessKeyId("ak");
properties.setAccessKeySecret("sk");
properties.setBaseRetryDelayMillis(0);
properties.setRetryJitterMillis(0);
return properties;
}
private static <T> ObjectProvider<T> provider(T value) {
@SuppressWarnings("unchecked")
ObjectProvider<T> provider = mock(ObjectProvider.class);
when(provider.getIfAvailable()).thenReturn(value);
return provider;
}
private static <T> ObjectProvider<T> emptyProvider() {
@SuppressWarnings("unchecked")
ObjectProvider<T> provider = mock(ObjectProvider.class);
when(provider.getIfAvailable()).thenReturn(null);
return provider;
}
}