refactor: 부가 기능을 capability 모듈로 포장하고 기본 조립을 web-only로 전환
설계: docs/superpowers/specs/2026-09-25-capability-module-packaging-design.md
계획/진행 기록: docs/superpowers/plans/2026-09-25-capability-module-packaging.md
- Wave 0: 규칙 고정(AGENTS.md capability 완료 기준·테스트 규칙) + 차단 결함 7건
(compose overlay 검사, redis-sdk CI job, 적용된 V9/V13 원복 + V16, @EnableScheduling,
messaging bridge 순서, prod 에러 상세 validator, sample loggers read-only)
- Wave 1: 데이터·보안 Critical 10건 (durable operation 25P02, operation 유스케이스 이동,
outbox 재인코딩, Kafka TLS/SASL, 알림 트랜잭션, SNS 서명, tus checksum, GraphQL fragment,
Redis lane, gRPC FAILED_TERMINAL)
- Wave 2: 리팩터링 잔재·실험용 자리 삭제, outbox v1 fencing(V17), 멱등성 V2 정본화
- Wave 3: 라이브러리형 코드 정리(동작하지 않는 설정·인터페이스·gate 제거, web-advanced 분리)
- Wave 4: capability 11개를 "있으면 켜짐" 모듈로 포장, 끄는 장치 제거, 기본 조립 web-only,
qualificationBootJar 분리
- Wave 5: 빌드 로직 축소(17.2k -> 10.8k줄), sample 테스트 컨벤션 적용
삭제한 코드는 docs/capability-status.md "제거된 코드" 표에 복구 커밋(208077fd)과 함께 기록.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
208077fddf
commit
6d27356e2b
+3
@@ -42,6 +42,9 @@ public enum FileserverErrorCode implements ApiErrorCode {
|
||||
INVALID_PATH(422, Category.VALIDATION, false),
|
||||
PATH_OUTSIDE_NAMESPACE(422, Category.VALIDATION, false),
|
||||
TRANSFER_ADMISSION_REJECTED(429, Category.RATE_LIMIT, true),
|
||||
// tus checksum extension status: one request's bytes did not match the checksum the client sent
|
||||
// for them. The chunk is rolled back before its offset commits, so the upload stays resumable.
|
||||
CHECKSUM_MISMATCH(460, Category.DATA_INTEGRITY, false),
|
||||
PARTIAL_WRITE(500, Category.TRANSIENT_DEPENDENCY, false),
|
||||
// Never retryable by definition: the operation may already have taken effect, and a blind retry
|
||||
// is what turns an ambiguous outcome into a duplicated one.
|
||||
|
||||
+4
-6
@@ -27,7 +27,7 @@ import dev.caskeleton.application.fileserver.cleanup.CleanupQueue;
|
||||
import dev.caskeleton.application.fileserver.cleanup.CleanupRequest;
|
||||
import dev.caskeleton.application.fileserver.cleanup.CleanupType;
|
||||
import dev.caskeleton.application.fileserver.recovery.RecoveryQueue;
|
||||
import dev.caskeleton.application.transaction.PolicyTransactionPort;
|
||||
import dev.caskeleton.application.transaction.TransactionPort;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.Locale;
|
||||
@@ -53,7 +53,7 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
private final CleanupQueue cleanupQueue;
|
||||
private final RecoveryQueue recoveryQueue;
|
||||
private final PublishMode publishMode;
|
||||
private final PolicyTransactionPort transactions;
|
||||
private final TransactionPort transactions;
|
||||
private final Clock clock;
|
||||
|
||||
public DefaultFinalizeUploadService(
|
||||
@@ -65,7 +65,7 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
CleanupQueue cleanupQueue,
|
||||
RecoveryQueue recoveryQueue,
|
||||
PublishMode publishMode,
|
||||
PolicyTransactionPort transactions,
|
||||
TransactionPort transactions,
|
||||
Clock clock) {
|
||||
this.metadataStore = metadataStore;
|
||||
this.sessionStore = sessionStore;
|
||||
@@ -318,9 +318,7 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
// The object is on the volume and the record does not say so — the definition of an ambiguous
|
||||
// completion. Enqueued in its own boundary, because the one that failed took nothing with it.
|
||||
try {
|
||||
FileserverCompensationTransaction.run(
|
||||
transactions,
|
||||
"fileserver-ready-recovery-" + verifying.fileId().value(),
|
||||
transactions.inNew(
|
||||
() -> recoveryQueue.enqueue(verifying.fileId(), "READY_COMMIT_UNCONFIRMED"));
|
||||
} catch (RuntimeException recoveryWriteFailure) {
|
||||
exception.addSuppressed(recoveryWriteFailure);
|
||||
|
||||
+9
-7
@@ -36,7 +36,7 @@ import dev.caskeleton.application.fileserver.observability.FileserverMetricsPort
|
||||
import dev.caskeleton.application.fileserver.observability.SizeBucket;
|
||||
import dev.caskeleton.application.fileserver.quota.TransferAdmissionController;
|
||||
import dev.caskeleton.application.fileserver.quota.TransferPermit;
|
||||
import dev.caskeleton.application.transaction.PolicyTransactionPort;
|
||||
import dev.caskeleton.application.transaction.TransactionPort;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
@@ -72,7 +72,7 @@ public final class DefaultUploadApplicationService implements UploadApplicationS
|
||||
private final UploadIdentifierFactory identifierFactory;
|
||||
private final UploadPolicy uploadPolicy;
|
||||
private final FileserverMetricsPort metrics;
|
||||
private final PolicyTransactionPort transactions;
|
||||
private final TransactionPort transactions;
|
||||
private final Clock clock;
|
||||
|
||||
public DefaultUploadApplicationService(
|
||||
@@ -88,7 +88,7 @@ public final class DefaultUploadApplicationService implements UploadApplicationS
|
||||
UploadIdentifierFactory identifierFactory,
|
||||
UploadPolicy uploadPolicy,
|
||||
FileserverMetricsPort metrics,
|
||||
PolicyTransactionPort transactions,
|
||||
TransactionPort transactions,
|
||||
Clock clock) {
|
||||
this.transactions = transactions;
|
||||
this.metadataStore = metadataStore;
|
||||
@@ -152,9 +152,7 @@ public final class DefaultUploadApplicationService implements UploadApplicationS
|
||||
// Staging sits outside the boundary above because it has to — it is a filesystem call — so
|
||||
// it is the one failure here that still compensates by hand instead of rolling back.
|
||||
try {
|
||||
FileserverCompensationTransaction.run(
|
||||
transactions,
|
||||
"fileserver-upload-create-compensation-" + uploadId.value(),
|
||||
transactions.inNew(
|
||||
() -> {
|
||||
failRecord(created.record(), "UPLOAD_CREATE_FAILED");
|
||||
quotaService.release(created.reservation());
|
||||
@@ -188,6 +186,7 @@ public final class DefaultUploadApplicationService implements UploadApplicationS
|
||||
long expectedOffset,
|
||||
ReadableByteChannel content,
|
||||
long contentLength,
|
||||
Optional<String> expectedChunkSha256,
|
||||
RequestContext context) {
|
||||
UploadSession session = requireActiveSession(uploadId);
|
||||
FileRecord record = requireRecord(session.fileId());
|
||||
@@ -212,8 +211,11 @@ public final class DefaultUploadApplicationService implements UploadApplicationS
|
||||
requireOffsetsAgree(session, handle, expectedOffset);
|
||||
|
||||
Instant startedAt = clock.instant();
|
||||
// The chunk checksum is checked by the store before it returns, so a mismatch never reaches
|
||||
// the offset commit below.
|
||||
AppendResult appended =
|
||||
storageGateway.append(handle, expectedOffset, content, contentLength, fence);
|
||||
storageGateway.append(
|
||||
handle, expectedOffset, content, contentLength, expectedChunkSha256, fence);
|
||||
UploadSession committed =
|
||||
leaseCoordinator.commitOffset(
|
||||
fence.current(), expectedOffset, appended.committedOffset());
|
||||
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
package dev.caskeleton.application.fileserver.upload;
|
||||
|
||||
import dev.caskeleton.application.outbound.CallBudget;
|
||||
import dev.caskeleton.application.transaction.OperationId;
|
||||
import dev.caskeleton.application.transaction.PolicyTransactionPort;
|
||||
import dev.caskeleton.application.transaction.TransactionPolicyId;
|
||||
import dev.caskeleton.application.transaction.TransactionRequest;
|
||||
import dev.caskeleton.application.transaction.TransactionResult;
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Policy-based REQUIRES_NEW boundary for fileserver compensation/recovery writes. */
|
||||
final class FileserverCompensationTransaction {
|
||||
|
||||
private static final Duration BUDGET = Duration.ofSeconds(30);
|
||||
|
||||
private FileserverCompensationTransaction() {}
|
||||
|
||||
static void run(PolicyTransactionPort transactions, String operationId, Runnable action) {
|
||||
Objects.requireNonNull(transactions, "transactions");
|
||||
Objects.requireNonNull(operationId, "operationId");
|
||||
Objects.requireNonNull(action, "action");
|
||||
|
||||
TransactionRequest request =
|
||||
new TransactionRequest(
|
||||
TransactionPolicyId.MAINTENANCE_NEW,
|
||||
CallBudget.fromNow(BUDGET),
|
||||
Optional.empty(),
|
||||
Optional.of(new OperationId(operationId)));
|
||||
|
||||
TransactionResult<Void> result =
|
||||
transactions.inTransaction(
|
||||
request,
|
||||
() -> {
|
||||
action.run();
|
||||
return null;
|
||||
});
|
||||
|
||||
if (result instanceof TransactionResult.Committed<?>) {
|
||||
return;
|
||||
}
|
||||
if (result
|
||||
instanceof TransactionResult.CommittedWithPostCommitFailure<?> committedWithFailure) {
|
||||
throw committedWithFailure.operationalFailure();
|
||||
}
|
||||
if (result instanceof TransactionResult.DeterminateRollback<?> rollback) {
|
||||
throw rollback.failure();
|
||||
}
|
||||
if (result instanceof TransactionResult.Indeterminate<?> indeterminate) {
|
||||
throw new IllegalStateException(
|
||||
"fileserver compensation transaction outcome is indeterminate at "
|
||||
+ indeterminate.lastObservedPhase()
|
||||
+ " for operationId="
|
||||
+ operationId);
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"MAINTENANCE_NEW compensation unexpectedly participated in an outer transaction");
|
||||
}
|
||||
}
|
||||
+19
@@ -3,6 +3,7 @@ package dev.caskeleton.application.fileserver.upload;
|
||||
import dev.caskeleton.application.fileserver.api.UploadId;
|
||||
import dev.caskeleton.application.fileserver.api.security.RequestContext;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* The upload half of the public application surface.
|
||||
@@ -14,13 +15,31 @@ public interface UploadApplicationService {
|
||||
|
||||
UploadSessionView create(CreateUploadRequest request, RequestContext context);
|
||||
|
||||
/**
|
||||
* Appends one request's bytes.
|
||||
*
|
||||
* @param expectedChunkSha256 hex SHA-256 the client asserted for this request's bytes only, never
|
||||
* the whole upload; a mismatch is rejected before the offset commits and leaves no bytes
|
||||
* behind
|
||||
*/
|
||||
AppendUploadResult append(
|
||||
UploadId uploadId,
|
||||
long expectedOffset,
|
||||
ReadableByteChannel content,
|
||||
long contentLength,
|
||||
Optional<String> expectedChunkSha256,
|
||||
RequestContext context);
|
||||
|
||||
/** Appends without a client checksum. */
|
||||
default AppendUploadResult append(
|
||||
UploadId uploadId,
|
||||
long expectedOffset,
|
||||
ReadableByteChannel content,
|
||||
long contentLength,
|
||||
RequestContext context) {
|
||||
return append(uploadId, expectedOffset, content, contentLength, Optional.empty(), context);
|
||||
}
|
||||
|
||||
UploadSessionView status(UploadId uploadId, RequestContext context);
|
||||
|
||||
void cancel(UploadId uploadId, RequestContext context);
|
||||
|
||||
+17
@@ -6,6 +6,7 @@ import dev.caskeleton.application.fileserver.api.content.AppendResult;
|
||||
import dev.caskeleton.application.fileserver.api.content.UploadHandle;
|
||||
import dev.caskeleton.application.fileserver.api.content.WriteFence;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Narrow view of the content store used while an upload is in progress.
|
||||
@@ -26,11 +27,27 @@ public interface UploadStorageGateway {
|
||||
* visible mid-transfer: the storage layer stops on the next buffer instead of at the end of a
|
||||
* transfer whose bytes are already on the volume.
|
||||
*/
|
||||
default AppendResult append(
|
||||
UploadHandle handle,
|
||||
long expectedOffset,
|
||||
ReadableByteChannel source,
|
||||
long contentLength,
|
||||
WriteFence fence) {
|
||||
return append(handle, expectedOffset, source, contentLength, Optional.empty(), fence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends and, when {@code expectedChunkSha256} is present, compares it with the digest of this
|
||||
* call's bytes only. A mismatch is rolled back to {@code expectedOffset} and reported as {@link
|
||||
* dev.caskeleton.application.fileserver.api.error.FileserverErrorCode#CHECKSUM_MISMATCH}, so the
|
||||
* caller never commits an offset for bytes the client disowned.
|
||||
*/
|
||||
AppendResult append(
|
||||
UploadHandle handle,
|
||||
long expectedOffset,
|
||||
ReadableByteChannel source,
|
||||
long contentLength,
|
||||
Optional<String> expectedChunkSha256,
|
||||
WriteFence fence);
|
||||
|
||||
/** Appends with no ownership to lose; see {@link WriteFence#unfenced()}. */
|
||||
|
||||
+2
-1
@@ -15,7 +15,8 @@ import org.junit.jupiter.params.provider.EnumSource;
|
||||
class FileserverExceptionTest {
|
||||
|
||||
private static final Set<Integer> DESIGN_STATUSES =
|
||||
Set.of(400, 401, 403, 404, 409, 410, 411, 412, 413, 415, 416, 422, 429, 500, 503, 504, 507);
|
||||
Set.of(
|
||||
400, 401, 403, 404, 409, 410, 411, 412, 413, 415, 416, 422, 429, 460, 500, 503, 504, 507);
|
||||
|
||||
@Test
|
||||
void ambiguousCompletionCarriesReconciliationFlag() {
|
||||
|
||||
+15
-11
@@ -1,8 +1,6 @@
|
||||
package dev.caskeleton.application.fileserver.testkit;
|
||||
|
||||
import dev.caskeleton.application.transaction.PolicyTransactionPort;
|
||||
import dev.caskeleton.application.transaction.TransactionRequest;
|
||||
import dev.caskeleton.application.transaction.TransactionResult;
|
||||
import dev.caskeleton.application.transaction.TransactionPort;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
@@ -14,11 +12,18 @@ import java.util.function.Supplier;
|
||||
* a failed unit undoes its writes — that is what the real-PostgreSQL readiness suite is for. What
|
||||
* it does prove is the part unit tests can: that a service opens a boundary at all, and how many.
|
||||
* {@link #openedWrites()} lets a test assert that two writes which must commit together were asked
|
||||
* for inside one boundary rather than two.
|
||||
* for inside one boundary rather than two. {@link #failNextNew(RuntimeException)} makes the next
|
||||
* independent boundary fail, which is how a test reaches a compensation that could not be written.
|
||||
*/
|
||||
public final class DirectTransactions implements PolicyTransactionPort {
|
||||
public final class DirectTransactions implements TransactionPort {
|
||||
|
||||
private final List<String> opened = new ArrayList<>();
|
||||
private RuntimeException nextNewFailure;
|
||||
|
||||
/** Makes the next {@link #inNew(Supplier)} fail with {@code failure} instead of running. */
|
||||
public void failNextNew(RuntimeException failure) {
|
||||
this.nextNewFailure = failure;
|
||||
}
|
||||
|
||||
/** The boundary kinds opened so far, in order. */
|
||||
public List<String> openedWrites() {
|
||||
@@ -50,12 +55,11 @@ public final class DirectTransactions implements PolicyTransactionPort {
|
||||
@Override
|
||||
public <T> T inNew(Supplier<T> action) {
|
||||
opened.add("new");
|
||||
RuntimeException failure = nextNewFailure;
|
||||
if (failure != null) {
|
||||
nextNewFailure = null;
|
||||
throw failure;
|
||||
}
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> TransactionResult<T> inTransaction(TransactionRequest request, Supplier<T> action) {
|
||||
opened.add("policy:" + request.policyId().name());
|
||||
return new TransactionResult.Committed<>(action.get(), request.operationId());
|
||||
}
|
||||
}
|
||||
|
||||
+19
-6
@@ -7,6 +7,7 @@ import dev.caskeleton.application.fileserver.api.content.UploadHandle;
|
||||
import dev.caskeleton.application.fileserver.api.content.WriteFence;
|
||||
import dev.caskeleton.application.fileserver.api.error.FileserverErrorCode;
|
||||
import dev.caskeleton.application.fileserver.api.error.FileserverFailureContext;
|
||||
import dev.caskeleton.application.fileserver.api.error.IntegrityMismatchException;
|
||||
import dev.caskeleton.application.fileserver.api.error.StorageUnavailableException;
|
||||
import dev.caskeleton.application.fileserver.upload.UploadStorageGateway;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -19,6 +20,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Hand-rolled staging store that keeps upload bytes in memory. */
|
||||
public final class FakeUploadStorageGateway implements UploadStorageGateway {
|
||||
@@ -66,10 +68,11 @@ public final class FakeUploadStorageGateway implements UploadStorageGateway {
|
||||
long expectedOffset,
|
||||
ReadableByteChannel source,
|
||||
long contentLength,
|
||||
Optional<String> expectedChunkSha256,
|
||||
WriteFence fence) {
|
||||
ByteArrayOutputStream sink = staging.get(handle.uploadId());
|
||||
ByteArrayOutputStream chunk = new ByteArrayOutputStream();
|
||||
ByteBuffer buffer = ByteBuffer.allocate(8192);
|
||||
long appended = 0;
|
||||
try {
|
||||
while (true) {
|
||||
fence.requireStillOwned();
|
||||
@@ -79,15 +82,25 @@ public final class FakeUploadStorageGateway implements UploadStorageGateway {
|
||||
break;
|
||||
}
|
||||
buffer.flip();
|
||||
byte[] chunk = new byte[buffer.remaining()];
|
||||
buffer.get(chunk);
|
||||
sink.write(chunk);
|
||||
appended += chunk.length;
|
||||
byte[] bytes = new byte[buffer.remaining()];
|
||||
buffer.get(bytes);
|
||||
chunk.write(bytes);
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
throw new UncheckedIOException(exception);
|
||||
}
|
||||
return new AppendResult(expectedOffset + appended, appended, sha256Hex(sink.toByteArray()));
|
||||
byte[] received = chunk.toByteArray();
|
||||
// Same rule as the real store: checked against this call's bytes, before anything is kept.
|
||||
if (expectedChunkSha256.isPresent()
|
||||
&& !sha256Hex(received).equalsIgnoreCase(expectedChunkSha256.get())) {
|
||||
throw new IntegrityMismatchException(
|
||||
"chunk checksum mismatch",
|
||||
FileserverFailureContext.forUpload(
|
||||
FileserverErrorCode.CHECKSUM_MISMATCH, handle.uploadId(), false, false, false));
|
||||
}
|
||||
sink.writeBytes(received);
|
||||
return new AppendResult(
|
||||
expectedOffset + received.length, received.length, sha256Hex(sink.toByteArray()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
@@ -89,6 +89,7 @@ class DefaultSingleShotUploadServiceTest {
|
||||
long expectedOffset,
|
||||
ReadableByteChannel content,
|
||||
long contentLength,
|
||||
Optional<String> expectedChunkSha256,
|
||||
RequestContext context) {
|
||||
if (appendFailure != null) {
|
||||
throw appendFailure;
|
||||
|
||||
-122
@@ -1,122 +0,0 @@
|
||||
package dev.caskeleton.application.fileserver.upload;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import dev.caskeleton.application.transaction.PolicyTransactionPort;
|
||||
import dev.caskeleton.application.transaction.TransactionPhase;
|
||||
import dev.caskeleton.application.transaction.TransactionRequest;
|
||||
import dev.caskeleton.application.transaction.TransactionResult;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Supplier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FileserverCompensationTransactionTest {
|
||||
|
||||
@Test
|
||||
void committedCompensationReturnsNormally() {
|
||||
AtomicBoolean invoked = new AtomicBoolean();
|
||||
|
||||
FileserverCompensationTransaction.run(
|
||||
new StubTransactions(ResultMode.COMMITTED),
|
||||
"fileserver-compensation-1",
|
||||
() -> invoked.set(true));
|
||||
|
||||
assertThat(invoked).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void determinateRollbackPropagatesTheRollbackFailure() {
|
||||
IllegalStateException failure = new IllegalStateException("rolled back");
|
||||
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
FileserverCompensationTransaction.run(
|
||||
new StubTransactions(ResultMode.ROLLBACK, failure),
|
||||
"fileserver-compensation-2",
|
||||
() -> {}))
|
||||
.isSameAs(failure);
|
||||
}
|
||||
|
||||
@Test
|
||||
void indeterminateOutcomeIsNeverReportedAsSuccessfulCompensation() {
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
FileserverCompensationTransaction.run(
|
||||
new StubTransactions(ResultMode.INDETERMINATE),
|
||||
"fileserver-compensation-3",
|
||||
() -> {}))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("indeterminate")
|
||||
.hasMessageContaining("fileserver-compensation-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postCommitOperationalFailureIsSurfacedWithoutTreatingTheCommitAsRollback() {
|
||||
IllegalStateException failure = new IllegalStateException("post-commit callback failed");
|
||||
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
FileserverCompensationTransaction.run(
|
||||
new StubTransactions(ResultMode.POST_COMMIT_FAILURE, failure),
|
||||
"fileserver-compensation-4",
|
||||
() -> {}))
|
||||
.isSameAs(failure);
|
||||
}
|
||||
|
||||
private enum ResultMode {
|
||||
COMMITTED,
|
||||
POST_COMMIT_FAILURE,
|
||||
ROLLBACK,
|
||||
INDETERMINATE
|
||||
}
|
||||
|
||||
private static final class StubTransactions implements PolicyTransactionPort {
|
||||
private final ResultMode mode;
|
||||
private final RuntimeException rollbackFailure;
|
||||
|
||||
private StubTransactions(ResultMode mode) {
|
||||
this(mode, new IllegalStateException("rollback"));
|
||||
}
|
||||
|
||||
private StubTransactions(ResultMode mode, RuntimeException rollbackFailure) {
|
||||
this.mode = mode;
|
||||
this.rollbackFailure = rollbackFailure;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> TransactionResult<T> inTransaction(TransactionRequest request, Supplier<T> action) {
|
||||
return switch (mode) {
|
||||
case COMMITTED -> new TransactionResult.Committed<>(action.get(), request.operationId());
|
||||
case POST_COMMIT_FAILURE ->
|
||||
new TransactionResult.CommittedWithPostCommitFailure<>(
|
||||
action.get(), request.operationId(), rollbackFailure);
|
||||
case ROLLBACK -> new TransactionResult.DeterminateRollback<>(rollbackFailure);
|
||||
case INDETERMINATE ->
|
||||
new TransactionResult.Indeterminate<>(
|
||||
request.operationId(), TransactionPhase.COMMIT_REQUESTED, Optional.empty());
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T inWrite(Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T inRootWrite(Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T inRead(Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T inNew(Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -211,7 +211,7 @@ class FinalizeUploadServiceTest {
|
||||
assertThat(recovery.reasons()).containsValue("READY_COMMIT_UNCONFIRMED");
|
||||
assertThat(transactions.openedWrites())
|
||||
.as("recovery must survive the failed ambient READY transaction")
|
||||
.endsWith("policy:MAINTENANCE_NEW");
|
||||
.endsWith("new");
|
||||
assertThat(metadata.find(verifyingRecord.fileId()).orElseThrow().state())
|
||||
.isNotEqualTo(FileState.READY);
|
||||
assertThat(quota.anyCommitted()).isFalse();
|
||||
|
||||
+63
-1
@@ -2,12 +2,15 @@ package dev.caskeleton.application.fileserver.upload;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
import dev.caskeleton.application.fileserver.api.FileState;
|
||||
import dev.caskeleton.application.fileserver.api.UploadId;
|
||||
import dev.caskeleton.application.fileserver.api.error.ConcurrentFileModificationException;
|
||||
import dev.caskeleton.application.fileserver.api.error.FileAccessDeniedException;
|
||||
import dev.caskeleton.application.fileserver.api.error.FileTooLargeException;
|
||||
import dev.caskeleton.application.fileserver.api.error.FileserverErrorCode;
|
||||
import dev.caskeleton.application.fileserver.api.error.IntegrityMismatchException;
|
||||
import dev.caskeleton.application.fileserver.api.error.StorageUnavailableException;
|
||||
import dev.caskeleton.application.fileserver.api.error.UploadExpiredException;
|
||||
import dev.caskeleton.application.fileserver.api.error.UploadOffsetMismatchException;
|
||||
@@ -32,8 +35,11 @@ import java.io.ByteArrayInputStream;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Clock;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -145,11 +151,29 @@ class UploadApplicationServiceTest {
|
||||
assertThat(quota.anyReleased()).isTrue();
|
||||
assertThat(transactions.openedWrites())
|
||||
.as("filesystem-create compensation must commit independently of an ambient transaction")
|
||||
.endsWith("policy:MAINTENANCE_NEW");
|
||||
.endsWith("new");
|
||||
assertThat(metadata.findRecoverable(recoveryQuery()).stream().map(record -> record.state()))
|
||||
.containsOnly(FileState.FAILED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCompensationThatCannotCommitIsAttachedToTheStagingFailure() {
|
||||
// given
|
||||
storage.failNextCreate();
|
||||
IllegalStateException compensationFailure = new IllegalStateException("compensation lost");
|
||||
transactions.failNextNew(compensationFailure);
|
||||
|
||||
// when
|
||||
Throwable thrown =
|
||||
catchThrowable(() -> service.create(createRequest(), FileserverFixtures.context()));
|
||||
|
||||
// then
|
||||
assertThat(thrown)
|
||||
.isInstanceOf(StorageUnavailableException.class)
|
||||
.satisfies(
|
||||
failure -> assertThat(failure.getSuppressed()).containsExactly(compensationFailure));
|
||||
}
|
||||
|
||||
@Test
|
||||
void appendsAccumulateMonotonically() {
|
||||
UploadSessionView created = service.create(createRequest(6), FileserverFixtures.context());
|
||||
@@ -180,6 +204,33 @@ class UploadApplicationServiceTest {
|
||||
.isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aChunkWhoseChecksumDoesNotMatchIsRejectedWithoutAdvancingTheOffset() {
|
||||
// given
|
||||
UploadSessionView created = service.create(createRequest(6), FileserverFixtures.context());
|
||||
service.append(created.uploadId(), 0, channel("abc"), 3, FileserverFixtures.context());
|
||||
|
||||
// when
|
||||
Throwable failure =
|
||||
catchThrowable(
|
||||
() ->
|
||||
service.append(
|
||||
created.uploadId(),
|
||||
3,
|
||||
channel("def"),
|
||||
3,
|
||||
Optional.of(sha256Hex("xyz")),
|
||||
FileserverFixtures.context()));
|
||||
|
||||
// then
|
||||
assertThat(failure)
|
||||
.isInstanceOfSatisfying(
|
||||
IntegrityMismatchException.class,
|
||||
mismatch ->
|
||||
assertThat(mismatch.code()).isEqualTo(FileserverErrorCode.CHECKSUM_MISMATCH));
|
||||
assertThat(sessions.find(created.uploadId()).orElseThrow().committedOffset()).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMetadataOffsetThatDisagreesWithStorageIsNeverSilentlyRepaired() {
|
||||
UploadSessionView created = service.create(createRequest(), FileserverFixtures.context());
|
||||
@@ -313,4 +364,15 @@ class UploadApplicationServiceTest {
|
||||
private static ReadableByteChannel channel(String payload) {
|
||||
return Channels.newChannel(new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
private static String sha256Hex(String payload) {
|
||||
try {
|
||||
return HexFormat.of()
|
||||
.formatHex(
|
||||
MessageDigest.getInstance("SHA-256")
|
||||
.digest(payload.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (NoSuchAlgorithmException impossible) {
|
||||
throw new IllegalStateException("SHA-256 is required by the platform", impossible);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user