feat: jpa, messaging, notification, mongo, graphql 어댑터터 구현체 추가

This commit is contained in:
DongHyeonka
2026-08-15 13:01:58 +09:00
parent e23ed9486e
commit f56dd3e4eb
174 changed files with 10338 additions and 1274 deletions
@@ -0,0 +1,114 @@
package dev.caskeleton.messaging.schema;
import dev.caskeleton.messaging.api.error.MessageTooLargeException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Objects;
/**
* A byte sink that refuses the byte after the limit instead of the buffer after the fact.
*
* <p>Every codec here used to serialize into an unbounded buffer and compare {@code bytes.length}
* to the configured maximum afterwards. That makes the maximum a reporting threshold rather than an
* allocation bound: a payload whose graph expands to hundreds of megabytes exhausts the heap while
* being written, and the check that would have rejected it never runs. Under a broker consumer that
* is a process-wide outage caused by one message.
*
* <p>This sink fails on the write that would cross the limit, so peak allocation is bounded by the
* limit itself plus whatever the encoder buffers. The exception is the same {@link
* MessageTooLargeException} the after-the-fact check raised, carrying the codec's own error code,
* so callers and evidence records see no behavioural difference for payloads that merely exceed the
* bound.
*
* <p>Not thread-safe, and not meant to be: an instance belongs to a single encode call.
*/
public final class BoundedByteSink extends OutputStream {
private final int maxBytes;
private final String errorCode;
private final ByteArrayOutputStream buffer;
private int written;
private BoundedByteSink(int maxBytes, String errorCode) {
if (maxBytes < 1) {
throw new IllegalArgumentException("maxBytes must be positive");
}
this.maxBytes = maxBytes;
this.errorCode = Objects.requireNonNull(errorCode, "errorCode must not be null");
// Sized to the limit only when the limit is small: a 1 GiB bound must not pre-allocate 1 GiB.
this.buffer = new ByteArrayOutputStream(Math.min(maxBytes, 8_192));
}
/**
* Creates a sink that refuses to accumulate more than {@code maxBytes}.
*
* @param maxBytes the inclusive byte limit
* @param errorCode the code the codec reports for an oversized payload
* @return a bounded sink
*/
public static BoundedByteSink of(int maxBytes, String errorCode) {
return new BoundedByteSink(maxBytes, errorCode);
}
@Override
public void write(int b) {
reserve(1);
buffer.write(b);
}
@Override
public void write(byte[] bytes, int offset, int length) {
Objects.checkFromIndexSize(offset, length, bytes.length);
reserve(length);
buffer.write(bytes, offset, length);
}
/**
* Checks a size the caller can compute before serializing, without consuming the budget.
*
* <p>Protobuf knows its serialized size exactly, so the whole encode can be refused before the
* first byte is written. The subsequent writes are still bounded — this is a cheaper refusal, not
* a replacement for the bound.
*
* @param size the number of bytes the caller is about to produce
*/
public void requireFits(int size) {
if (size < 0) {
throw new IllegalArgumentException("size must not be negative");
}
refuseIfBeyondLimit(size);
}
private void reserve(int size) {
refuseIfBeyondLimit(size);
written += size;
}
private void refuseIfBeyondLimit(int size) {
if (size > maxBytes - written) {
throw new MessageTooLargeException(
errorCode,
("the encoded payload exceeds the %d byte limit; it was refused at byte %d rather than"
+ " buffered in full")
.formatted(maxBytes, written + size));
}
}
/**
* The bytes accumulated so far.
*
* @return a copy of the buffered bytes
*/
public byte[] toByteArray() {
return buffer.toByteArray();
}
/**
* How many bytes have been admitted.
*
* @return the running total
*/
public int written() {
return written;
}
}
@@ -0,0 +1,49 @@
package dev.caskeleton.messaging.schema;
import dev.caskeleton.messaging.api.MessageType;
import dev.caskeleton.messaging.api.SchemaVersion;
import java.util.Objects;
/**
* The identity a codec registry is keyed on: a message type at a specific schema version.
*
* <p>Keying on the message type alone is what let an unregistered version decode. The version
* travels in the envelope and in {@link SchemaReference}, so a consumer receiving {@code
* order.created v999} would look up {@code order.created}, find the v1 class or parser, decode
* against it, and then keep the v999 label on the result. Nothing failed, and every downstream
* compatibility gate and audit record then described a version that was never registered.
*
* <p>With the version in the key that lookup misses, which is the only honest outcome: the platform
* has no contract for that version and must say so rather than guess one.
*
* @param type the catalog message type
* @param version the schema version the contract is registered for
*/
public record MessageContractKey(MessageType type, SchemaVersion version) {
/** Validates both components. */
public MessageContractKey {
Objects.requireNonNull(type, "messageType must not be null");
Objects.requireNonNull(version, "schemaVersion must not be null");
}
/**
* Creates a key from raw components.
*
* @param type the catalog message type
* @param version the schema version
* @return the key
*/
public static MessageContractKey of(String type, int version) {
return new MessageContractKey(new MessageType(type), new SchemaVersion(version));
}
/**
* Renders the key the way error messages and audit records name a contract.
*
* @return for example {@code order.created v1}
*/
public String describe() {
return type.value() + " v" + version.value();
}
}
@@ -0,0 +1,87 @@
package dev.caskeleton.messaging.schema;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import dev.caskeleton.messaging.api.error.MessageTooLargeException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* The boundary the codecs delegate to.
*
* <p>Each codec used to serialize into an unbounded buffer and compare the finished length to the
* limit. That check reports an oversized payload; it does not prevent one. The difference only
* shows up for a payload whose serialized form is orders of magnitude larger than the bound, which
* is precisely the case that matters — the consumer is out of heap before the comparison runs.
*/
class BoundedByteSinkTest {
private static final int MAX = 64;
@Test
@DisplayName("one byte under, exactly at, and one byte over the limit")
void theLimitIsInclusiveAndTheNextByteIsRefused() {
assertThatCode(() -> fill(MAX - 1)).doesNotThrowAnyException();
assertThatCode(() -> fill(MAX)).doesNotThrowAnyException();
assertThatThrownBy(() -> fill(MAX + 1))
.isInstanceOf(MessageTooLargeException.class)
.hasMessageContaining("refused at byte 65");
}
@Test
@DisplayName("a much larger stream stops at the limit rather than after allocation")
void aMuchLargerStreamStopsAtTheLimit() {
BoundedByteSink sink = BoundedByteSink.of(MAX, "PAYLOAD_TOO_LARGE");
assertThatThrownBy(
() -> {
// Ten mebibytes offered one kibibyte at a time. A buffer-then-check codec would have
// held all of it; this refuses the chunk that crosses the bound.
for (int chunk = 0; chunk < 10_240; chunk++) {
sink.write(new byte[1_024], 0, 1_024);
}
})
.isInstanceOf(MessageTooLargeException.class);
assertThat(sink.written())
.as("nothing beyond the bound was ever admitted")
.isLessThanOrEqualTo(MAX);
assertThat(sink.toByteArray()).isEmpty();
}
@Test
@DisplayName("a pre-flight size check does not consume the budget")
void aPreFlightCheckDoesNotConsumeTheBudget() {
BoundedByteSink sink = BoundedByteSink.of(MAX, "PAYLOAD_TOO_LARGE");
sink.requireFits(MAX);
assertThat(sink.written()).isZero();
assertThatCode(() -> sink.write(new byte[MAX], 0, MAX)).doesNotThrowAnyException();
}
@Test
@DisplayName("a pre-flight check refuses a size that cannot fit")
void aPreFlightCheckRefusesASizeThatCannotFit() {
BoundedByteSink sink = BoundedByteSink.of(MAX, "AVRO_PAYLOAD_TOO_LARGE");
assertThatThrownBy(() -> sink.requireFits(MAX + 1))
.isInstanceOf(MessageTooLargeException.class)
.asInstanceOf(
org.assertj.core.api.InstanceOfAssertFactories.type(MessageTooLargeException.class))
.extracting(failure -> failure.failure().code())
.as("the sink reports the codec's own code, not a generic one")
.isEqualTo("AVRO_PAYLOAD_TOO_LARGE");
}
private static byte[] fill(int size) {
BoundedByteSink sink = BoundedByteSink.of(MAX, "PAYLOAD_TOO_LARGE");
for (int index = 0; index < size; index++) {
sink.write('a');
}
return sink.toByteArray();
}
}