Since this feature is a simulator for testing failure modes, it does not introduce new persistent business entities. It introduces transient configuration entities used during the request lifecycle.
## Transient Entities
### FaultConfiguration
Represented as a set of headers in the HTTP request.
**Input**: Feature specification from `/specs/003-fault-simulator/spec.md` and specific redesign requirements for `FaultInjector`.
## Summary
The goal is to redesign the `FaultInjector` to support four distinct fault modes (Lost Request, Server Crash Before Execution, Response Loss, and Delay). These faults will be activated on a per-request basis via an HTTP header or a dedicated test endpoint, utilizing a one-shot injection counter managed by the client. The simulator will be integrated directly into the `RequestHandler` to trigger faults at specific lifecycle stages: before body reading (for lost requests) and before database commit (for crashes).
## Technical Context
**Language/Version**: Java 27
**Primary Dependencies**: JDK HTTP Server API, Jackson (JSON)
**Storage**: Embedded H2 via JDBC (for existing banking state); no persistent storage for simulator state (handled via per-request headers).
**Performance Goals**: Minimal overhead for non-faulty requests.
**Constraints**: No new libraries; must preserve existing Response Loss and Delay behaviors.
**Scale/Scope**: Single-user simulation for verification of fault-tolerance principles.
## Constitution Check
*GATE: Must pass before Phase 0 research. Re-check after Phase 1 design.*
-[x]**Effectively-Once Execution**: The simulator specifically tests this principle by inducing failures that force client retries. The implementation must ensure these faults don't break the existing idempotency logic. (Compliant)
-[x]**Atomic State Transitions**: The "Server Crash Before Execution" fault MUST be triggered before the DB commit to verify that no partial state is persisted. (Compliant)
-[x]**Deterministic Fault Tolerance**: The redesign expands the system's ability to explicitly handle and test request loss and crashes. (Compliant)
-[x]**Monetary Precision**: No new financial calculations are introduced in the simulator. (Compliant)
-[x]**Specification-Driven Development**: This plan is derived from the Fault Simulator specification. (Compliant)
## Project Structure
### Documentation (this feature)
```text
specs/003-fault-simulator/
├── plan.md # This file
├── research.md # Phase 0 output
├── data-model.md # Phase 1 output
├── quickstart.md # Phase 1 output
└── contracts/ # Phase 1 output
```
### Source Code (repository root)
```text
src/main/java/com/banking/
├── server/
│ ├── FaultInjector.java # Redesigned as an Enum-based simulator
│ └── RequestHandler.java # Integration points for fault injection
└── persistence/
└── [Existing Repositories] # No changes expected here
```
**Structure Decision**: Single project. The `FaultInjector` logic is modified, and `RequestHandler` is updated to invoke the simulator at the correct stages.
**Decision**: Use a Java `enum` for `FaultMode` to represent the four supported modes.
-`NONE`: Normal processing.
-`LOST_REQUEST`: Request is dropped before any server-side processing.
-`SERVER_CRASH`: Server terminates before database commit.
-`RESPONSE_LOSS`: Transaction commits, but the response is not sent (existing).
-`DELAY`: Response is delayed (existing).
**Rationale**: Enums provide type safety and clear mapping for the `X-Fault-Mode` header.
### 2. Per-Request Activation
**Decision**: Implement a `FaultContext` object extracted from the `HttpExchange` headers.
- Header: `X-Fault-Mode` (matches Enum name).
- Header: `X-Fault-Count` (the current attempt number provided by the client).
**Rationale**: This avoids global state and allows concurrent tests with different fault profiles.
### 3. Injection Points in RequestHandler
**Decision**:
-**Lost Request**: Inject at the very beginning of the `handle()` method, before `request.getBody()` is called. The simulator will simply return/close the connection without responding.
-**Server Crash**: Inject inside the transaction block, after the business logic has computed the result but **immediately before**`connection.commit()`. The simulator will trigger a `System.exit(1)` or throw a non-recoverable `RuntimeException` that causes the server to stop/restart in the test environment.
**Rationale**: This precisely mirrors the specified failure modes: the first happens before the server "sees" the intent, the second happens after the "intent" is processed but before it's "permanent".
### 4. One-Shot Injection Logic
**Decision**: The `FaultInjector` will compare the `X-Fault-Count` header against the intended `injectionCount` (provided in the header or spec). If `count < limit`, the fault is triggered.
**Rationale**: Since the client manages the count (as decided in clarification), the server simply obeys the header's current state.
## Alternatives Considered
-**Global Static Flag**: Rejected because it prevents parallel testing and is too blunt.
-**Persistence of Fault State**: Rejected per user clarification; client-side tracking via headers is simpler and more robust against server crashes.