You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2026/06/04 22:46:51 UTC

(causeway) 32/42: CAUSEWAY-4010: proposes replay conflict failure handling

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch CAUSEWAY-4010
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 8d2e611d5a63ef0767f5634d09d0471949f1afde
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Thu Jun 4 18:28:51 2026 +0100

    CAUSEWAY-4010: proposes replay conflict failure handling
---
 .../.openspec.yaml                                 |  2 +
 .../design.md                                      | 66 ++++++++++++++++++++++
 .../proposal.md                                    | 34 +++++++++++
 .../specs/command-replay-result-mapping/spec.md    | 25 ++++++++
 .../spec.md                                        | 28 +++++++++
 .../tasks.md                                       | 25 ++++++++
 6 files changed, 180 insertions(+)

diff --git a/openspec/changes/fail-replay-on-conflicting-result-mapping/.openspec.yaml b/openspec/changes/fail-replay-on-conflicting-result-mapping/.openspec.yaml
new file mode 100644
index 00000000000..f617bd1867a
--- /dev/null
+++ b/openspec/changes/fail-replay-on-conflicting-result-mapping/.openspec.yaml
@@ -0,0 +1,2 @@
+schema: spec-driven
+created: 2026-06-04
diff --git a/openspec/changes/fail-replay-on-conflicting-result-mapping/design.md b/openspec/changes/fail-replay-on-conflicting-result-mapping/design.md
new file mode 100644
index 00000000000..5b7ac9b61bb
--- /dev/null
+++ b/openspec/changes/fail-replay-on-conflicting-result-mapping/design.md
@@ -0,0 +1,66 @@
+## Context
+
+Command replay imports recorded command DTOs and re-executes them locally.
+The replay mapping SPI lets applications remap replay inputs and observe mappings from recorded result bookmarks to actual replay result bookmarks.
+
+The current replay flow executes the command in one `REQUIRES_NEW` transaction and then handles replay success or failure in a second `REQUIRES_NEW` transaction.
+Result-mapping listener notification happens in the second transaction, and listener exceptions are caught and logged.
+That means a listener consistency failure cannot fail or roll back the replayed command.
+
+The default listener stores mappings in an in-memory `HashMap<Bookmark, Bookmark>` keyed by recorded result bookmark.
+It currently replaces an existing mapping with the latest actual bookmark.
+For replay consistency, a recorded original result that was already mapped to one actual bookmark must not later be mapped to a different actual bookmark.
+
+## Goals / Non-Goals
+
+**Goals:**
+
+- Detect conflicting result mappings in the default listener.
+- Make repeated identical result mappings idempotent.
+- Keep ignoring identity mappings where recorded and actual bookmarks are equal.
+- Run replay execution, success bookkeeping, and result-mapping listener notification in one transaction.
+- Propagate result-mapping listener exceptions so consistency failures mark replay as failed and prevent successful command execution.
+
+**Non-Goals:**
+
+- Do not persist default listener mappings beyond the existing in-memory listener instance.
+- Do not change the `CommandReplayMappingListener` method signatures.
+- Do not change target or reference parameter remapping exception handling before command execution.
+- Do not introduce distributed coordination for mapping state across JVMs or nodes.
+
+## Decisions
+
+### Use the default listener as the consistency guard
+
+The default listener already owns the in-memory recorded-to-actual result map.
+It will inspect the existing mapping before writing a new non-identity mapping.
+If the recorded result has no existing mapping, it records the actual result.
+If the existing mapping equals the new actual result, it leaves the map unchanged and returns normally.
+If the existing mapping differs from the new actual result, it throws an exception with the conflicting bookmarks.
+
+An alternative was to let the latest mapping continue to win and add a separate audit warning.
+That would not protect downstream remapping from ambiguous original results, so it is rejected.
+
+### Notify result-mapping listeners inside the replay command transaction
+
+`ReplayableCommand` will move successful replay bookkeeping and result-mapping listener notification into the transaction that calls `CommandExecutorService#executeCommand(...)`.
+The command execution result bookmark is available before that transaction returns, so the replay can save success analysis and notify listeners before commit.
+If a listener throws, the transaction fails and the command replay result is reported as a failure.
+
+An alternative was to keep the second transaction and rethrow listener exceptions there.
+That would fail the post-processing transaction but still leave the replayed command execution committed, which does not satisfy the consistency requirement.
+
+### Keep replay failure bookkeeping in a separate transaction
+
+When command execution or listener notification fails, the existing failure bookkeeping still needs to save replay analysis on the imported command log entry.
+That failure analysis should run after the failed command transaction has rolled back, using a separate transaction as today.
+Success bookkeeping does not need a second transaction because it now belongs to the successful command transaction.
+
+## Risks / Trade-offs
+
+- Custom listeners that previously threw without affecting replay will now cause replay failure.
+  → This is intentional for result-mapping notifications; document it in tests and specs.
+- Failure analysis still depends on a follow-up transaction after rollback.
+  → Preserve the existing failure handling pattern for replay errors.
+- The default listener remains in-memory and is not synchronized across application nodes.
+  → This change only strengthens consistency for one listener instance, matching the current non-persistence scope.
diff --git a/openspec/changes/fail-replay-on-conflicting-result-mapping/proposal.md b/openspec/changes/fail-replay-on-conflicting-result-mapping/proposal.md
new file mode 100644
index 00000000000..1dbfd4d55cc
--- /dev/null
+++ b/openspec/changes/fail-replay-on-conflicting-result-mapping/proposal.md
@@ -0,0 +1,34 @@
+## Why
+
+The default command replay mapping listener currently allows a recorded result bookmark to be overwritten with a later, different actual bookmark.
+That can hide replay divergence and allow downstream commands to remap against an ambiguous original result.
+
+Replay also currently handles replay execution and listener notification in separate transactions.
+A listener failure during result mapping therefore cannot make the command replay itself fail, which prevents consistency checks from protecting the replay outcome.
+
+## What Changes
+
+- Update the default command replay mapping listener to reject conflicting result mappings for the same recorded result bookmark.
+- Treat a repeated notification for the same recorded result and the same actual result as idempotent.
+- Keep ignoring identity mappings where recorded and actual bookmarks are equal.
+- Change replay transaction handling so replay execution, success bookkeeping, and result-mapping listener notification run in the same command transaction.
+- Let result-mapping listener exceptions propagate so a failed consistency check causes command replay execution to fail.
+- Continue not notifying result-mapping listeners when replay fails or when recorded or actual result bookmarks are unavailable.
+
+## Capabilities
+
+### New Capabilities
+
+None.
+
+### Modified Capabilities
+
+- `command-replay-result-mapping`: Result-mapping listener failures must fail the replayed command, and listener notification must occur in the same transaction as command execution.
+- `default-command-replay-mapping-listener`: The default listener must reject conflicting actual bookmarks for a recorded result bookmark.
+
+## Impact
+
+- Affects command replay transaction flow in the command log applib replay implementation.
+- Affects `CommandReplayMappingListenerDefault` behavior for repeated mappings.
+- Affects tests for replay listener exception handling and default listener consistency.
+- Existing applications with custom listeners that throw from result mapping will now cause replay command execution to fail instead of only logging a warning.
diff --git a/openspec/changes/fail-replay-on-conflicting-result-mapping/specs/command-replay-result-mapping/spec.md b/openspec/changes/fail-replay-on-conflicting-result-mapping/specs/command-replay-result-mapping/spec.md
new file mode 100644
index 00000000000..141578bf8ef
--- /dev/null
+++ b/openspec/changes/fail-replay-on-conflicting-result-mapping/specs/command-replay-result-mapping/spec.md
@@ -0,0 +1,25 @@
+## MODIFIED Requirements
+
+### Requirement: Replay result mapping SPI is notified after successful replay
+The system SHALL provide a command replay mapping SPI that applications can implement to receive replay result bookmark mappings and provide replay input remappings.
+After a command replay succeeds, the system SHALL notify the SPI when the imported command log entry has a recorded returned object bookmark and replay execution returns an actual result bookmark.
+The notification SHALL include the recorded returned object bookmark and the actual replay result bookmark.
+The system SHALL notify the SPI in the same transaction as replay command execution.
+The system SHALL propagate result-mapping notification exceptions so the replayed command execution fails and rolls back.
+
+#### Scenario: Successful replay produces a different result bookmark
+- **WHEN** command replay succeeds for an imported command whose recorded result bookmark is `demoInvoice:1`
+- **AND** replay execution returns actual result bookmark `demoInvoice:2`
+- **THEN** the system notifies the command replay mapping SPI with recorded bookmark `demoInvoice:1` and actual bookmark `demoInvoice:2`
+
+#### Scenario: Successful replay produces the same result bookmark
+- **WHEN** command replay succeeds for an imported command whose recorded result bookmark is `demoInvoice:1`
+- **AND** replay execution returns actual result bookmark `demoInvoice:1`
+- **THEN** the system notifies the command replay mapping SPI with recorded bookmark `demoInvoice:1` and actual bookmark `demoInvoice:1`
+
+#### Scenario: Result mapping listener rejects the replay result
+- **WHEN** command replay execution succeeds for an imported command whose recorded result bookmark is `demoInvoice:1`
+- **AND** replay execution returns actual result bookmark `demoInvoice:2`
+- **AND** a command replay mapping SPI throws while handling the result mapping notification
+- **THEN** the replayed command execution fails
+- **AND** the replayed command transaction rolls back
diff --git a/openspec/changes/fail-replay-on-conflicting-result-mapping/specs/default-command-replay-mapping-listener/spec.md b/openspec/changes/fail-replay-on-conflicting-result-mapping/specs/default-command-replay-mapping-listener/spec.md
new file mode 100644
index 00000000000..866abd303cd
--- /dev/null
+++ b/openspec/changes/fail-replay-on-conflicting-result-mapping/specs/default-command-replay-mapping-listener/spec.md
@@ -0,0 +1,28 @@
+## MODIFIED Requirements
+
+### Requirement: Default replay mapping listener records result mappings
+The system SHALL provide a default command replay mapping listener that records each replay result mapping by recorded result bookmark.
+When replay notifies the listener with a recorded result bookmark and a different actual result bookmark, the listener SHALL remember the actual bookmark for the recorded bookmark if no mapping already exists.
+When replay notifies the listener with equal recorded and actual result bookmarks, the listener MUST NOT remember a mapping for that notification.
+When the same recorded bookmark is notified more than once with the same actual result bookmark, the listener SHALL treat the notification as idempotent.
+When the same recorded bookmark is notified more than once with different actual result bookmarks, the listener SHALL throw an exception and MUST NOT replace the remembered actual bookmark.
+
+#### Scenario: Different result mapping is recorded
+- **WHEN** command replay notifies the default listener that recorded result bookmark `demoInvoice:1` mapped to actual result bookmark `demoInvoice:2`
+- **THEN** the default listener records `demoInvoice:2` as the actual bookmark for `demoInvoice:1`
+
+#### Scenario: Equal result mapping is not recorded
+- **WHEN** command replay notifies the default listener that recorded result bookmark `demoInvoice:1` mapped to actual result bookmark `demoInvoice:1`
+- **THEN** the default listener records no mapping for `demoInvoice:1`
+
+#### Scenario: Result mapping is repeated with the same actual bookmark
+- **WHEN** command replay notifies the default listener that recorded result bookmark `demoInvoice:1` mapped to actual result bookmark `demoInvoice:2`
+- **AND** command replay later notifies the default listener that recorded result bookmark `demoInvoice:1` mapped to actual result bookmark `demoInvoice:2`
+- **THEN** the default listener keeps `demoInvoice:2` as the actual bookmark for `demoInvoice:1`
+- **AND** no exception is thrown
+
+#### Scenario: Result mapping conflict is rejected
+- **WHEN** command replay notifies the default listener that recorded result bookmark `demoInvoice:1` mapped to actual result bookmark `demoInvoice:2`
+- **AND** command replay later notifies the default listener that recorded result bookmark `demoInvoice:1` mapped to actual result bookmark `demoInvoice:3`
+- **THEN** the default listener throws an exception
+- **AND** the default listener keeps `demoInvoice:2` as the actual bookmark for `demoInvoice:1`
diff --git a/openspec/changes/fail-replay-on-conflicting-result-mapping/tasks.md b/openspec/changes/fail-replay-on-conflicting-result-mapping/tasks.md
new file mode 100644
index 00000000000..dfc813d768a
--- /dev/null
+++ b/openspec/changes/fail-replay-on-conflicting-result-mapping/tasks.md
@@ -0,0 +1,25 @@
+## 1. Default Listener Consistency
+
+- [ ] 1.1 Update `CommandReplayMappingListenerDefault#onReplayResultMapped(...)` to check for an existing non-identity mapping before writing.
+- [ ] 1.2 Throw an exception when a recorded result bookmark is already mapped to a different actual bookmark.
+- [ ] 1.3 Preserve idempotent behavior when a repeated notification uses the same actual bookmark.
+- [ ] 1.4 Preserve no-op behavior when recorded and actual bookmarks are equal.
+
+## 2. Replay Transaction Handling
+
+- [ ] 2.1 Refactor `ReplayableCommand#tryReplay(...)` so command execution and successful result-mapping notification happen inside the same `REQUIRES_NEW` transaction.
+- [ ] 2.2 Preserve separate failure-analysis persistence after the failed command transaction rolls back.
+- [ ] 2.3 Remove exception swallowing for result-mapping notification failures so listener exceptions propagate from the command transaction.
+- [ ] 2.4 Keep target and reference parameter remapping exception handling unchanged.
+
+## 3. Tests
+
+- [ ] 3.1 Update default listener tests to cover conflict rejection, idempotent duplicate notification, and unchanged mapping after conflict.
+- [ ] 3.2 Update replay mapping tests to verify result-mapping listener exceptions fail replay execution.
+- [ ] 3.3 Add or update transaction-service test stubbing to verify successful listener notification happens in the same transaction as command execution.
+- [ ] 3.4 Verify missing recorded or actual result bookmarks still skip result-mapping notification.
+
+## 4. Verification
+
+- [ ] 4.1 Run the command log applib tests that cover `CommandReplayMappingListenerDefaultTest` and `ReplayableCommandMappingTest`.
+- [ ] 4.2 Run `openspec validate fail-replay-on-conflicting-result-mapping --type change --strict`.