Skip to content

feat: Implement Fallback design pattern #2846#3538

Open
priyanshuvishwakarma273403 wants to merge 2 commits into
iluwatar:masterfrom
priyanshuvishwakarma273403:feat/fallback
Open

feat: Implement Fallback design pattern #2846#3538
priyanshuvishwakarma273403 wants to merge 2 commits into
iluwatar:masterfrom
priyanshuvishwakarma273403:feat/fallback

Conversation

@priyanshuvishwakarma273403

Copy link
Copy Markdown

What does this PR do?

This PR implements the Fallback design pattern as a standalone, self-contained Maven module (fallback). The implementation demonstrates a robust resiliency mechanism commonly used in microservices architectures.

Key Components Implemented:

  • RemoteService: A simple interface representing a service/dependency call that might fail.
  • PrimaryService: Implements RemoteService to simulate the main dependency with configurable latency, timeout, and exceptions.
  • FallbackService: Implements RemoteService and represents the fallback strategy, returning cached or degraded responses.
  • SimpleCircuitBreaker: A thread-safe state machine (CLOSED, OPEN, HALF_OPEN) that monitors service health to fast-fail subsequent requests when the primary service is down.
  • FallbackExecutor: Leverages Java 21's Virtual Threads to orchestrate the service call execution, applying timeouts and delegating to the fallback handler when failures, timeouts, or OPEN circuit states are encountered.
  • App: Main application illustrating different runtime scenarios and state transitions (healthy call, exception, timeout, fast-failing during OPEN, and recovery back to CLOSED).
  • Unit & Integration Tests: Fully verified using JUnit 5 (covering all state transitions, timeouts, short-circuits, and recovery). Passed spotless checks.

Fixes #2846

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

PR Summary

Introduces a standalone Maven module fallback that demonstrates the Fallback design pattern with a circuit breaker. Includes a PrimaryService and FallbackService implementing a common RemoteService, a SimpleCircuitBreaker, a FallbackExecutor using virtual threads, and an App demo plus unit/integration tests. Also ships a README and updates the root POM to include the new module.

Changes

File Summary
fallback/README.md Documentation detailing the Fallback pattern, motivation, and Java example usage, including real-world analogies and integration with a circuit breaker and fallback strategy.
fallback/pom.xml Defines the module pom for the standalone fallback module, including dependencies, build configuration and main class for assembly.
fallback/src/main/java/com/iluwatar/fallback/App.java Demo application showing scenarios for the Fallback pattern: healthy primary calls, exceptions, timeouts, OPEN-circuit fast-fail, and recovery sequences.
fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java Orchestrates primary execution with timeout handling, circuit-breaker checks, and fallback delegation; uses a virtual-thread per task executor and clean shutdown.
fallback/src/main/java/com/iluwatar/fallback/FallbackService.java Concrete fallback implementation of RemoteService returning a cached/degraded response.
fallback/src/main/java/com/iluwatar/fallback/PrimaryService.java PrimaryService simulating latency and optional failure to test resiliency patterns.
fallback/src/main/java/com/iluwatar/fallback/RemoteService.java RemoteService interface representing a potentially failing external dependency.
fallback/src/main/java/com/iluwatar/fallback/SimpleCircuitBreaker.java SimpleCircuitBreaker tracking CLOSED, OPEN, and HALF_OPEN states with failure threshold and retry period, including state evaluation logic.
fallback/src/test/java/com/iluwatar/fallback/AppTest.java Test ensuring the App main method runs without throwing errors.
fallback/src/test/java/com/iluwatar/fallback/FallbackPatternTest.java Unit and integration tests covering healthy calls, failures, timeouts, open fast-fail, and recovery behavior of the fallback pattern.
pom.xml Update root POM to include the new fallback module in the multi-module build.

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (2)
  • ab58c76: docs: add README for Fallback design pattern implementation
  • 488c4d7: feat: implement Fallback design pattern with circuit breaker and service monitoring
Files Processed (11)
  • fallback/README.md (1 hunk)
  • fallback/pom.xml (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/App.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/FallbackService.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/PrimaryService.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/RemoteService.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/SimpleCircuitBreaker.java (1 hunk)
  • fallback/src/test/java/com/iluwatar/fallback/AppTest.java (1 hunk)
  • fallback/src/test/java/com/iluwatar/fallback/FallbackPatternTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (7)
  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [46-46]

    possible issue: "Handle potential null responses from primary service."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [66-66]

    behavior: "Circuit breaker fast-fail path."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [81-81]

    best-practice: "Timeout handling."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [86-86]

    best-practice: "Cancel task on timeout to free resources."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [94-94]

    maintainability: "Narrow exception handling in executor."

  • fallback/src/main/java/com/iluwatar/fallback/SimpleCircuitBreaker.java [69-69]

    design: "Circuit breaker state transitions."

  • fallback/pom.xml [47-47]

    best-practice: "Test dependencies: add junit-jupiter-api for API usage in tests."

@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.96491% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.27%. Comparing base (74d2dbe) to head (ab58c76).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...n/java/com/iluwatar/fallback/FallbackExecutor.java 71.87% 9 Missing ⚠️
...va/com/iluwatar/fallback/SimpleCircuitBreaker.java 87.50% 2 Missing and 2 partials ⚠️
...lback/src/main/java/com/iluwatar/fallback/App.java 91.66% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3538      +/-   ##
============================================
+ Coverage     83.24%   83.27%   +0.03%     
- Complexity     4025     4050      +25     
============================================
  Files          1060     1065       +5     
  Lines         14246    14360     +114     
  Branches        686      690       +4     
============================================
+ Hits          11859    11959     +100     
- Misses         2100     2114      +14     
  Partials        287      287              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Fallback design pattern

1 participant