Skip to content

Validate payload before WAL publication, fail-fast with 400 - #371

Merged
em3s merged 10 commits into
kakao:mainfrom
eazyhozy:feat/validate-payload-before-wal
Jun 2, 2026
Merged

em3s merged 10 commits into
kakao:mainfrom
eazyhozy:feat/validate-payload-before-wal

Conversation

@eazyhozy

@eazyhozy eazyhozy commented Jun 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Add payload-deterministic schema validation before WAL publication to fail-fast with HTTP 400 on invalid mutation payloads.

Previously, async tables accepted any payload and returned "QUEUED", with errors silently surfacing only in downstream processor logs. The sync path swallowed validation failures as 200 OK with status: "ERROR".

Scope: Part 1 of #366 only (validate before WAL, fail-fast 400, incl. all-or-nothing barrier). Part 2 (surfacing RMW-stage validation failures as HTTP 400 instead of 200 + status:ERROR) is deferred to a separate follow-up PR.

Closes #366

Changes

  • MutationRequestValidator.kt (new): Internal helper checkNonNullableFields enforcing nullability rules per event type — INSERT requires all non-nullable fields; UPDATE rejects explicit null for non-nullable but allows absent fields; DELETE is a no-op (tombstone).
  • EdgeBulkMutationRequest / MultiEdgeBulkMutationRequest: Call checkNonNullableFields inside createEvent, before any WAL publish.
  • MutationService: Add collectList() barrier after createEvent — by eagerly materializing the entire Flux before any writeWal starts, if even one createEvent call throws (= validation failure), the resulting Mono error propagates immediately and no WAL writes start at all. This prevents partial WAL publication in bulk requests (e.g. 10 mutations, 1 invalid → 0 published, 400).

How to Test

./gradlew :core:test :engine:test :server:test

New test coverage:

  • EdgeBulkMutationRequestTest / MultiEdgeBulkMutationRequestTest: unit tests for INSERT/UPDATE/DELETE × nullable/non-nullable field combinations.
  • MutationServiceSpec: invalid INSERT fails before WAL write; bulk with one invalid item errors without publishing any item (all-or-nothing).
  • EdgeMutationValidationTest: E2E — POST /edges (async) and /edges/sync with invalid payload → 400; valid payload → 200 (regression guard).

Scope & Limitations

  • Type validation is unchanged — this PR adds nullability checks only. Type
    compatibility stays the existing PrimitiveType.cast(...) in createEvent (runs
    before WAL): numeric parse failures already return 400, while lenient coercions
    remain silent ("123"→123, any value→String via toString(), non-"true"→
    false, fractional→integer truncation). Stricter type validation is out of scope.
  • All-or-nothing is at the validation barrier only — collectList() ensures a
    bulk request with any invalid item publishes nothing to the WAL (e.g. 10 mutations,
    1 invalid → 0 published, 400). It does not add cross-key transactional writes; the
    downstream RMW stage keeps its existing per-key semantics.

AI Assistance

  • This PR was written largely with AI assistance.
    • Tool / model: Claude Code (claude-opus-4-8)

- Add PayloadValidator.kt with payload-deterministic nullability rules:
  INSERT requires every non-nullable field present and non-null;
  UPDATE rejects explicit null for non-nullable but allows absent fields;
  DELETE skips property validation (tombstone semantics).
- Call validatePayloadNullability in EdgeBulkMutationRequest and
  MultiEdgeBulkMutationRequest createEvent, before WAL publication.
- Add a collectList() barrier in MutationService.mutate so that any
  validation failure in a bulk request prevents partial WAL publication.
- Discriminate IllegalArgumentException in the sync-path onErrorResume
  so schema violations from the RMW branch surface as HTTP 400 instead
  of being swallowed as per-item ERROR status.

Closes kakao#366

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>
@eazyhozy eazyhozy self-assigned this Jun 2, 2026
@em3s em3s changed the title feat: validate payload before WAL publication, fail-fast with 400 Validate payload before WAL publication, fail-fast with 400 Jun 2, 2026
- Extract tableDdl() to remove duplicated DDL between sync/async tables
- Extract mutateSync()/mutateAsync() helpers to reduce boilerplate
- Remove redundant async valid case (same code path as sync)
- Remove Part 1/2A section comments; test names are self-explanatory

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>
@em3s

em3s commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

@eazyhozy

The name validatePayloadNullability feels a bit too broad. Since the actual scope is validating the mutation request, could we change the name to reflect that?

@eazyhozy

eazyhozy commented Jun 2, 2026 •

Copy link
Copy Markdown
Member Author

@em3s Two reasons why IllegalArgumentException maps to 400:

  1. Code: ExceptionController has @ExceptionHandler(IllegalArgumentException::class) mapped to HttpStatus.BAD_REQUEST. Since require(...) throws IAE on violation, this handler always catches it.
@ExceptionHandler(
    IllegalStateException::class,
    ServerWebInputException::class,
    UnsupportedOperationException::class,
    IllegalArgumentException::class,
)
fun handleClientError(e: Exception) = createErrorResponse(e, HttpStatus.BAD_REQUEST) { ... }
  1. Tests: EdgeMutationValidationTest verifies .isBadRequest for all invalid payload cases (missing non-nullable field, explicit null) on both sync and async endpoints. All passing locally.

eazyhozy and others added 4 commits June 2, 2026 14:47
Reflects em3s's feedback that the name should capture the mutation
request validation scope rather than the nullability implementation detail.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>
@eazyhozy

eazyhozy commented Jun 2, 2026 •

Copy link
Copy Markdown
Member Author

@em3s Updated — renamed validatePayloadNullability → checkNonNullableFields, file → MutationRequestValidator.kt. (Tried validateMutationRequest first but the params are List<StructField> + payload Map, not a request object — checkNonNullableFields is more accurate.)

@eazyhozy

eazyhozy commented Jun 2, 2026 •

Copy link
Copy Markdown
Member Author

@em3s One design question — currently onErrorResume in MutationService propagates only IllegalArgumentException as Mono.error() (→ HTTP 400), while other errors (infra failures, lock timeouts, etc.) still flow as MutationResult with status: ERROR in a 200 response.

Would it be worth moving all error paths to Mono.error() propagation? That would make HTTP status consistently reflect the actual outcome and align better with standard retry semantics. The trade-off is that callers relying on status: ERROR in the response body for partial-failure handling would need to adapt.

Curious whether you'd prefer keeping the current mixed approach or moving toward uniform error propagation.


Update: Scoped this PR down to Part 1 only. The onErrorResume discrimination (and any MutationResult changes) are deferred to a follow-up PR.

@eazyhozy
eazyhozy marked this pull request as ready for review June 2, 2026 06:51
@eazyhozy
eazyhozy requested a review from em3s as a code owner June 2, 2026 06:51
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 2, 2026
… PR)

Restores the original catch-all onErrorResume. Part 2 (surfacing
RMW-stage validation failures as HTTP 400) is a nice-to-have and
will be addressed in a separate follow-up PR.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>
…ATE branches

Split the single loop into separate INSERT and UPDATE branches to mirror
the KDoc contract directly, as suggested in code review.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>
@em3s

em3s commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Since this modifies the write path, we must verify the latency in shadow testing before deploying.

Nice work @eazyhozy

Merging.

@em3s
em3s merged commit 7ed5ceb into kakao:main Jun 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fail-fast schema validation before WAL publication on mutation requests

2 participants