Commit 99762c08 authored by Shouib's avatar Shouib

docs: plan fault injector redesign

parent f9221ad7
# Interface Contracts: Fault Simulator
## Request Headers
The fault simulator is controlled via custom HTTP headers added to the `POST /withdraw` request.
| Header | Required | Description | Example |
|--------|----------|-------------|---------|
| `X-Fault-Mode` | No | The type of fault to simulate. Defaults to `NONE`. | `SERVER_CRASH` |
| `X-Fault-Count` | No | The current attempt number for the transaction. Defaults to `1`. | `1` |
## Fault Mode Behavior
| Mode | Effect | Response |
|------|--------|----------|
| `NONE` | Normal processing | Standard JSON response |
| `LOST_REQUEST` | Connection closed immediately before body read | No response (TCP Reset/Close) |
| `SERVER_CRASH` | Process terminates before DB commit | No response (Connection dropped) |
| `RESPONSE_LOSS` | Transaction commits, response not sent | No response (Connection dropped) |
| `DELAY` | Response delayed by X ms | Standard JSON response (delayed) |
# Data Model: Fault Simulator
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.
| Attribute | Type | Description | Source |
|------------|------|-------------|--------|
| `faultMode` | Enum | `NONE`, `LOST_REQUEST`, `SERVER_CRASH`, `RESPONSE_LOSS`, `DELAY` | `X-Fault-Mode` header |
| `injectionCount` | Integer | The current attempt number for this specific transaction | `X-Fault-Count` header |
| `targetIdentity` | String | The unique sequence key of the transaction | Request Body |
## Validation Rules
- `X-Fault-Mode` MUST match one of the defined `FaultMode` enum values.
- `X-Fault-Count` MUST be a non-negative integer.
- If `X-Fault-Mode` is provided but `X-Fault-Count` is missing, it defaults to 1.
# Implementation Plan: Fault Simulator Extension
**Branch**: `003-fault-simulator` | **Date**: 2026-11-03 | **Spec**: [spec.md](/mnt/c/Users/Shouib/IdeaProjects/banking-sdd/specs/003-fault-simulator/spec.md)
**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).
**Testing**: JUnit 5, Manual Browser/Curl testing
**Target Platform**: Linux/Windows (Server)
**Project Type**: web-service (extending existing banking system)
**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.
## Complexity Tracking
No violations found.
# Quickstart Validation Guide: Fault Simulator
This guide describes how to verify the four fault modes of the simulator.
## Prerequisites
- Server running in test mode.
- A bank account with an initial balance (e.g., `ACC-1` with $100).
## Validation Scenarios
### 1. Request Delivery Failure (LOST_REQUEST)
**Setup**: Use a tool like `curl` to send a request with the fault header.
- **Command**: `curl -H "X-Fault-Mode: LOST_REQUEST" -H "X-Fault-Count: 1" -d '{"accountId":"ACC-1", ...}' http://localhost:8080/withdraw`
- **Expected Outcome**: The connection is closed immediately. No response is received.
- **Verification**: Check database; balance should still be $100.
- **Retry**: Send same request with `X-Fault-Count: 2` (or `X-Fault-Mode: NONE`).
- **Expected Outcome**: Request succeeds; balance becomes $80.
### 2. Server Crash Before Execution (SERVER_CRASH)
**Setup**: Send request with crash header.
- **Command**: `curl -H "X-Fault-Mode: SERVER_CRASH" -H "X-Fault-Count: 1" -d '{"accountId":"ACC-1", ...}' http://localhost:8080/withdraw`
- **Expected Outcome**: Connection dropped. Server process terminates.
- **Verification**: Restart server. Check database; balance should still be $100 (no partial commit).
- **Retry**: Send same request with `X-Fault-Count: 2` (or `X-Fault-Mode: NONE`).
- **Expected Outcome**: Request succeeds; balance becomes $80.
### 3. Response Loss (RESPONSE_LOSS)
**Setup**: Send request with response loss header.
- **Command**: `curl -H "X-Fault-Mode: RESPONSE_LOSS" -H "X-Fault-Count: 1" -d '{"accountId":"ACC-1", ...}' http://localhost:8080/withdraw`
- **Expected Outcome**: Connection dropped.
- **Verification**: Check database; balance should be $80 (transaction committed).
- **Retry**: Send same request with `X-Fault-Count: 2`.
- **Expected Outcome**: Server returns existing SUCCESS result; balance remains $80.
### 4. Latency (DELAY)
**Setup**: Send request with delay header.
- **Command**: `curl -H "X-Fault-Mode: DELAY" -H "X-Fault-Count: 1" -d '{"accountId":"ACC-1", ...}' http://localhost:8080/withdraw`
- **Expected Outcome**: Standard JSON response received after a noticeable delay.
- **Verification**: Balance updated to $80.
# Research: Fault Simulator Redesign
## Technical Context & Decisions
### 1. Fault Mode Definition
**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.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment