Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller.Api.MQTT: reconnect on connection failure #2602

Merged
merged 22 commits into from
Jun 16, 2024

Conversation

Sn0w3y
Copy link
Contributor

@Sn0w3y Sn0w3y commented Apr 1, 2024

Add Reconnection Logic to MQTT Controller

This PR introduces reconnection logic to the MQTT Controller, ensuring robustness in the face of connection losses with the MQTT broker. The implementation schedules reconnection attempts with an exponential backoff strategy, aiming to minimize the impact on both the broker and the controller while trying to re-establish a lost connection.

Key Changes:

  1. Scheduled Reconnection Attempts: Utilizes a single-threaded ScheduledExecutorService to manage reconnection attempts, allowing for delayed execution with exponential backoff.

  2. Adaptive Delay Calculation: Implements an adaptive delay calculation using a base delay, a maximum delay cap, and a multiplier to manage the delay between reconnection attempts, preventing aggressive reconnection attempts.

  3. Integration with Component Lifecycle: Hooks the reconnection logic into the component's activation process, ensuring attempts to connect or reconnect are made upon activation. Similarly, ensures proper shutdown of the executor service upon deactivation to clean up resources.

Integration:

This implementation ensures that the MQTT Controller attempts to reconnect to the broker following a connection loss, employing an exponential backoff strategy to balance between prompt reconnection and avoiding overwhelming the broker or the network.

After the initial connection attempt in the activate method, we schedule the attemptConnect method to be called periodically every 60 seconds. This ensures that if the MQTT client becomes disconnected for any reason (e.g., network issues, broker restarts), the system will automatically attempt to re-establish the connection at regular intervals.
Enhanced Lifecycle Management: Modified the component's initialization and shutdown processes. The ScheduledExecutorService, responsible for managing reconnection attempts, is now initialized in the component's constructor and configured in the @activate method. This ensures that the executor is ready for use immediately and adapts to configuration updates.

Graceful Shutdown: Implemented a more graceful shutdown process using ThreadPoolUtils.shutdownAndAwaitTermination in the @deactivate method. This approach ensures that ongoing reconnection attempts are properly completed before the component is deactivated, preventing potential resource leaks.

Configuration Update Handling: Introduced a @Modified method to dynamically handle configuration changes. This method allows the component to adjust its behavior, such as changing the reconnection attempt interval, without needing to be fully deactivated and reactivated.
Dynamic Reconnection Interval: Added a new configuration parameter (reconnectionAttemptInterval) to the Config interface. This parameter allows users to specify the interval between reconnection attempts to the MQTT broker, enhancing the component's adaptability to network conditions or broker availability.
@Sn0w3y Sn0w3y changed the title Patch 3 Reconnect MQTT automatically in case of failure Apr 1, 2024
@Sn0w3y Sn0w3y marked this pull request as draft April 1, 2024 23:02
@Sn0w3y Sn0w3y changed the title Reconnect MQTT automatically in case of failure Update ControllerApiMqttImpl.java to reconnect on Connection Failure Apr 1, 2024
Copy link

github-actions bot commented Apr 1, 2024

Code Coverage

@Sn0w3y Sn0w3y marked this pull request as ready for review April 1, 2024 23:22
Copy link

github-actions bot commented Apr 1, 2024

Code Coverage

1 similar comment
Copy link

github-actions bot commented Apr 1, 2024

Code Coverage

@Sn0w3y
Copy link
Contributor Author

Sn0w3y commented May 11, 2024

Maybe this Feature would be a "nice to have" ?

See Issue #2510:

image

Copy link

Code Coverage

@Sn0w3y Sn0w3y closed this Jun 16, 2024
@Sn0w3y Sn0w3y deleted the patch-3 branch June 16, 2024 12:50
@Sn0w3y Sn0w3y restored the patch-3 branch June 16, 2024 12:50
@Sn0w3y Sn0w3y reopened this Jun 16, 2024
Copy link

Code Coverage

1 similar comment
Copy link

Code Coverage

Copy link

Code Coverage

@sfeilmeier sfeilmeier changed the title Update ControllerApiMqttImpl.java to reconnect on Connection Failure Controller.Api.MQTT: reconnect on connection failure Jun 16, 2024
Copy link

Code Coverage

@sfeilmeier sfeilmeier merged commit 625122c into OpenEMS:develop Jun 16, 2024
2 checks passed
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.

2 participants