fix(backend-java): 收口并发资源与恢复链路

This commit is contained in:
2026-09-05 14:33:07 +08:00
parent d760838e71
commit 40224d8e56
35 changed files with 917 additions and 154 deletions
@@ -65,7 +65,7 @@ public class SimilarAsinLlmLocalVerify {
ossProps.setPublicEndpoint("https://oss.aishufu.top");
ossProps.setBucket("nanri-ai-images");
ossProps.setAccessKeyId("appuser");
ossProps.setAccessKeySecret("AppUser@2024SecureKey");
ossProps.setAccessKeySecret("test-secret");
OssStorageService oss = new OssStorageService(ossProps);
PuzzleImageMerger merger = new PuzzleImageMerger(props);
@@ -14,6 +14,9 @@ import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.SimpleTransactionStatus;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -38,6 +41,49 @@ class TaskScopePayloadStorageServiceTest {
TaskScopeStateEntity.class);
}
@Test
void recoversBufferedPayloadIntoExistingScopeAndRemovesBufferFile() throws Exception {
TaskScopeStateMapper mapper = mock(TaskScopeStateMapper.class);
TransientPayloadStorageService payloadStorage = mock(TransientPayloadStorageService.class);
PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
TaskScopePayloadStorageService service = new TaskScopePayloadStorageService(
mapper, new ObjectMapper(), payloadStorage, transactionManager);
long taskId = 777L;
String moduleType = "PRICE_TRACK";
String scopeHash = "a".repeat(64);
Path buffer = service.getBufferedPayloadRoot()
.resolve(moduleType.toLowerCase())
.resolve(String.valueOf(taskId))
.resolve(scopeHash + ".json");
Files.createDirectories(buffer.getParent());
Files.writeString(buffer, "{\"value\":\"recovered\"}", StandardCharsets.UTF_8);
TaskScopeStateEntity existing = new TaskScopeStateEntity();
existing.setId(9L);
existing.setTaskId(taskId);
existing.setModuleType(moduleType);
existing.setScopeHash(scopeHash);
existing.setStateJson("rustfs:old");
when(mapper.selectOne(any())).thenReturn(existing);
when(mapper.update(any(), any())).thenReturn(1);
when(payloadStorage.storeScopePayloadVersioned(
eq(moduleType), eq(taskId), eq(scopeHash), any(), eq(true)))
.thenReturn("rustfs:new");
when(transactionManager.getTransaction(any(TransactionDefinition.class)))
.thenReturn(new SimpleTransactionStatus());
try {
assertTrue(service.recoverBufferedScopePayload(taskId, moduleType, scopeHash));
assertFalse(Files.exists(buffer), "恢复成功后应删除本地缓冲文件");
verify(mapper).update(any(), any());
verify(payloadStorage).storeScopePayloadVersioned(
eq(moduleType), eq(taskId), eq(scopeHash), eq("{\"value\":\"recovered\"}"), eq(true));
} finally {
Files.deleteIfExists(buffer);
}
}
@Test
void uploadsBeforeOpeningTransactionAndCleansReplacedPayloadAfterCommit() {
TaskScopeStateMapper mapper = mock(TaskScopeStateMapper.class);