Fix Race Condition in EVCS Component Initialization and OCPP Server Startup #2912
+89
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request addresses a race condition in the OpenEMS Edge project, where charging stations may attempt to connect to the OCPP server before the corresponding EVCS components are fully initialized. The solution implements an exponential backoff retry mechanism with optional randomness to handle these cases gracefully.
Problem
When the OCPP server starts, it begins accepting connections from charging stations immediately. However, the EVCS components may not be fully initialized at that time. If a charging station connects before its corresponding EVCS component is ready, the server fails to associate the session with the EVCS, leading to errors and unsuccessful connections.
Solution
MyJsonServer
class to handle new sessions when the EVCS component is not yet initialized.Changes Made
1. Modified
newSession
MethodIn
MyJsonServer.java
, thenewSession
method now checks if the EVCS component is initialized. If not, it stores the session in apendingSessions
map and initiates the retry mechanism.2. Implemented
retryNewSession
Method with Exponential BackoffAdded a new method
retryNewSession
that schedules retries using a ScheduledExecutorService. The delay between retries increases exponentially, capped at a maximum value.3. Added Necessary Data Structures
Introduced maps to keep track of retry attempts and total retry time per
ocppIdentifier
.4. Moved pendingSessions to EvcsOcppServer Class
To maintain consistency with other session management fields, moved
pendingSessions
to theEvcsOcppServer
class.5. Properly Shutdown Scheduled Executor Service
Ensured that the
ScheduledExecutorService
is properly shut down when the server is deactivated to prevent resource leaks.Related Issues
Issue #2909: Race Condition in Component Initialization and OCPP Server Startup