refactor: 각 어댑터터별 리펙토링 진행

This commit is contained in:
DongHyeonka
2026-08-24 18:26:40 +09:00
parent 16e33ce986
commit a261df2a2b
100 changed files with 7072 additions and 902 deletions
@@ -0,0 +1,158 @@
package dev.caskeleton.messaging.reliability;
import dev.caskeleton.messaging.api.CausationId;
import dev.caskeleton.messaging.api.CorrelationId;
import dev.caskeleton.messaging.api.ProducerId;
import dev.caskeleton.messaging.api.TenantContext;
import dev.caskeleton.messaging.api.TraceContext;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
/**
* The envelope facts an outbox row carries in its own columns.
*
* <p>They used to live nowhere. A row held identity, type, version, content type, payload and an
* arbitrary header map, so producer, tenant, correlation, causation, trace and schema were either
* invented when the envelope was rebuilt — {@code Optional.empty()} for every one of them — or
* smuggled through the header map under reserved names the platform was supposed to own.
*
* <p>Both routes fail in the same direction. A relay cannot filter, route or diagnose by tenant
* without decoding the payload, so the operational question "which tenant is backed up" has no
* answer; and a message that crossed the outbox arrived at its consumer with a different tenant,
* trace and correlation than the one that was published, which makes the publish path — direct,
* polling or CDC — part of the message's meaning.
*
* <p>Columns rather than a blob, because the point is that the database can answer questions about
* them. A versioned envelope encoding would round-trip just as faithfully and would still leave the
* relay unable to select rows for one tenant.
*
* <p>Every field is optional except the trace context, which has its own empty form. A row written
* before these columns existed has none of them, and a row written by a producer that genuinely has
* no correlation is not distinguishable from it — nor does it need to be, because the reader's
* behaviour is the same: carry what is there and invent nothing.
*
* @param producer the logical producing service that wrote the row
* @param occurredAt the instant the business fact occurred, when it differs from the write
* @param correlationId the workflow correlation
* @param causationId the directly causing message
* @param tenant the tenant this message belongs to
* @param partitionKey the broker distribution key
* @param orderingKey the ordering key
* @param schemaSubject the registry subject the payload was written against
* @param schemaUri the resolvable schema location, when one exists
* @param traceContext the W3C trace values active when the row was written
*/
public record OutboxCanonicalMetadata(
Optional<ProducerId> producer,
Optional<Instant> occurredAt,
Optional<CorrelationId> correlationId,
Optional<CausationId> causationId,
Optional<TenantContext> tenant,
Optional<String> partitionKey,
Optional<String> orderingKey,
Optional<String> schemaSubject,
Optional<String> schemaUri,
TraceContext traceContext) {
public OutboxCanonicalMetadata {
Objects.requireNonNull(producer, "producer must not be null");
Objects.requireNonNull(occurredAt, "occurredAt must not be null");
Objects.requireNonNull(correlationId, "correlationId must not be null");
Objects.requireNonNull(causationId, "causationId must not be null");
Objects.requireNonNull(tenant, "tenant must not be null");
Objects.requireNonNull(partitionKey, "partitionKey must not be null");
Objects.requireNonNull(orderingKey, "orderingKey must not be null");
Objects.requireNonNull(schemaSubject, "schemaSubject must not be null");
Objects.requireNonNull(schemaUri, "schemaUri must not be null");
Objects.requireNonNull(traceContext, "traceContext must not be null");
if (schemaUri.isPresent() && schemaSubject.isEmpty()) {
throw new IllegalArgumentException(
"a schema location without a subject cannot be resolved; a reader would have a URI and "
+ "no way to know what it is a schema for");
}
}
/**
* Returns the metadata of a row that carries none.
*
* <p>Used by callers that write an outbox row from a message with no provenance of its own, and
* by the reader for rows written before these columns existed.
*
* @return empty canonical metadata
*/
public static OutboxCanonicalMetadata none() {
return new OutboxCanonicalMetadata(
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
TraceContext.none());
}
/**
* Returns a copy naming the producing service.
*
* @param producingService the logical producing service
* @return the metadata with its producer set
*/
public OutboxCanonicalMetadata withProducer(ProducerId producingService) {
return new OutboxCanonicalMetadata(
Optional.of(Objects.requireNonNull(producingService, "producer must not be null")),
occurredAt,
correlationId,
causationId,
tenant,
partitionKey,
orderingKey,
schemaSubject,
schemaUri,
traceContext);
}
/**
* Returns a copy bound to a tenant.
*
* @param owner the tenant this message belongs to
* @return the metadata with its tenant set
*/
public OutboxCanonicalMetadata withTenant(TenantContext owner) {
return new OutboxCanonicalMetadata(
producer,
occurredAt,
correlationId,
causationId,
Optional.of(Objects.requireNonNull(owner, "tenant must not be null")),
partitionKey,
orderingKey,
schemaSubject,
schemaUri,
traceContext);
}
/**
* Returns a copy carrying the broker routing keys.
*
* @param distributionKey the partition key, or null for none
* @param ordering the ordering key, or null for none
* @return the metadata with its routing keys set
*/
public OutboxCanonicalMetadata withRoutingKeys(String distributionKey, String ordering) {
return new OutboxCanonicalMetadata(
producer,
occurredAt,
correlationId,
causationId,
tenant,
Optional.ofNullable(distributionKey),
Optional.ofNullable(ordering),
schemaSubject,
schemaUri,
traceContext);
}
}
@@ -23,13 +23,19 @@ import java.util.Optional;
* this row carrying a stable {@code messageId} and the consumer having an Inbox — not from the
* outbox alone.
*
* <p>{@link OutboxCanonicalMetadata} is a separate component rather than more fields here because
* the two halves answer to different owners. Identity, payload, status, attempts and lease are the
* relay's bookkeeping; the metadata is the message's own provenance, and it is the half that has to
* survive the round trip through the database unchanged.
*
* @param messageId the stable logical identity, preserved across every retry
* @param destination the logical destination
* @param messageType the catalog message type
* @param schemaVersion the payload schema revision
* @param contentType the codec media type
* @param payload the encoded payload
* @param headers the reserved and application headers to publish
* @param headers the application headers to publish; reserved names are refused
* @param metadata the canonical envelope facts this row carries in its own columns
* @param createdAt when the business transaction wrote this row
* @param status the current lifecycle state
* @param attempts how many publish attempts have been made
@@ -45,6 +51,7 @@ public record OutboxRecord(
ContentType contentType,
byte[] payload,
Map<String, String> headers,
OutboxCanonicalMetadata metadata,
Instant createdAt,
OutboxStatus status,
int attempts,
@@ -59,6 +66,7 @@ public record OutboxRecord(
Objects.requireNonNull(contentType, "contentType must not be null");
Objects.requireNonNull(payload, "payload must not be null");
Objects.requireNonNull(headers, "headers must not be null");
Objects.requireNonNull(metadata, "metadata must not be null");
Objects.requireNonNull(createdAt, "createdAt must not be null");
Objects.requireNonNull(status, "status must not be null");
Objects.requireNonNull(leaseExpiresAt, "leaseExpiresAt must not be null");
@@ -95,6 +103,7 @@ public record OutboxRecord(
contentType,
payload,
headers,
metadata,
createdAt,
newStatus,
newAttempts,