-
Notifications
You must be signed in to change notification settings - Fork 44
Implementation of the Keep Alive thread in the transport service. #281
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
base: master
Are you sure you want to change the base?
Conversation
9adb176 to
803ac59
Compare
| # | ||
| # See file LICENSE for full license details. | ||
| # | ||
| from .keep_alive_service import * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'.keep_alive_service.*' imported but unused
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LePailleurThibault Is it a valid comment from hound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, i don't know how this comment is generated and imports in a init file are never really "used".
However, for example, in the transport service there is this line
from wirepas_gateway.keep_alive_service import KeepAliveServiceThread.
The init.py is required but maybe not this line
|
|
||
| Args: | ||
| sink_manager: The sink manager to send sinks the keep alive messages. | ||
| mqtt_wrapper: The mqtt wrapper to get access to queue level of the mqtt broker. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (91 > 79 characters)
| ) | ||
| if self.keep_alive_interval_s is not None: | ||
| buffer += KeepAliveMessage.encode_tlv_item( | ||
| KeepAliveType.KEEP_ALIVE_INTERVAL_TYPE, 2, self.keep_alive_interval_s, "H", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (91 > 79 characters)
| Bits 1-7: Reserved for future use or other status indicators | ||
| rtc_timestamp_ms: Unix epoch timestamp in ms (milliseconds since January 1, 1970). | ||
| timezone_offset_mn: Time zone offset from UTC in minutes (-840 to +720). | ||
| keep_alive_interval_s: Interval in seconds until the next keepalive message is expected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (96 > 79 characters)
| Attributes: | ||
| version: The version number for the keep-alive message. | ||
| gateway_status: The running status of the gateway. | ||
| Bit 0: Backhaul (MQTT broker) Connectivity (0 = Disconnected, 1 = Connected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (88 > 79 characters)
|
Maybe a more general question, but why is the timezone offset needed in the keepalive messages? In locations with daylight saving time, the offset from UTC would change throughout the year. |
|
These messages will be used for example in the dlms app where the application might update the connected meter local time with the UTC time + timezone offset. |
python_transport/wirepas_gateway/keep_alive_service/keep_alive_service.py
Outdated
Show resolved
Hide resolved
python_transport/wirepas_gateway/keep_alive_service/keep_alive_service.py
Outdated
Show resolved
Hide resolved
| KEEP_ALIVE_DST_EP = 67 | ||
|
|
||
| # Timeouts and periods used for the keep alive service. | ||
| WM_MSG_RETRY_PERIOD_S = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 1s enough ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well if a message can't be sent to a sink 3 times in a row in ~2s, there are definitely issue with this sink. So not sending a keep alive message for that sink wouldn't be that bad.
python_transport/wirepas_gateway/keep_alive_service/keep_alive_service.py
Outdated
Show resolved
Hide resolved
| src_ep=KEEP_ALIVE_SRC_EP, | ||
| dst_ep=KEEP_ALIVE_DST_EP, | ||
| qos=0, | ||
| initial_time=0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I recall right, it is already the default value, so could be skipped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This argument is required by the method. I don't think there is any default value for the initial_time.
| """ Main loop that send periodically keep alive message to the network. """ | ||
| while True: | ||
| # Put a timer so that the message are periodic with a good precision | ||
| start_timer = time() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't time.monotonic() be used instead, to avoid issue if clock is changed during the process?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, monotonic would be much safer. How about the nodes, can it be a problem there if the keepalive message has an older timestamp than the previous one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will use a monotonic timer here. The gateway is supposed to be connected to a NTP server when running the keep alive service and the keep alive messages contain the UTC timestamp. This issue will never happen with this timestamp. Now if the application wants to use local time (based on UTC + timezone offset), the application itself should handle the consequence of DST.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more about the following (very unlikely) scenario:
- NTP server(s) becomes unreachable during runtime, but not MQTT
- Host clock drifts forward
- NTP is reconnected and clock is updated
- New keepalive message has an older timestamp than the previous one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Time should not be included in the keep alive packet if the NTP server time is unavailable. However, at this moment, we do not have any NTP service-like API for this kind of issue. So there is no way of knowing if this time is valid without implementing a NTP server nearby.
But at the moment, when using the time of this service, the gateway is supposed to be synchronized with a NTP server.
And if it the issue to happen, the time will not be updated in the current applications if the clock difference is too big between 2 packets.
python_transport/wirepas_gateway/keep_alive_service/keep_alive_service.py
Outdated
Show resolved
Hide resolved
| for sink_id in current_sinks: | ||
| self.send_keep_alive_msg_to_sink(self.sink_manager.get_sink(sink_id)) | ||
|
|
||
| self.wait_for_next_keep_alive_message_iteration(self.keep_alive_interval_s, start_timer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (100 > 79 characters)
|
|
||
| if not current_sinks: | ||
| logging.error("No sinks are detected!") | ||
| self.wait_for_next_keep_alive_message_iteration(self.keep_alive_interval_s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (91 > 79 characters)
|
|
||
| return True | ||
|
|
||
| def wait_for_next_keep_alive_message_iteration(self, time_to_wait, start_timer=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (89 > 79 characters)
| ) | ||
| if res != wmm.GatewayResultCode.GW_RES_OK and retries_left > 0: | ||
| sleep(WM_MSG_RETRY_PERIOD_S) | ||
| logging.debug("Retry sending the keep alive message that couldn't be sent to %s sink: %s. ", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (108 > 79 characters)
| The interval in seconds between keep-alive messages. | ||
| keep_alive_timezone_name (str): Default to "Etc/UTC". | ||
| Time zone name used to set the timezone offset in the keep alive message. | ||
| Check https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (87 > 79 characters)
| keep_alive_interval_s (int): Default to 300 seconds. | ||
| The interval in seconds between keep-alive messages. | ||
| keep_alive_timezone_name (str): Default to "Etc/UTC". | ||
| Time zone name used to set the timezone offset in the keep alive message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (89 > 79 characters)
|
|
||
| class KeepAliveType(IntEnum): | ||
| """Keep alive fields TLV type enumerate.""" | ||
| VERSION_TYPE = 0x01 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Black would make changes.
d52d583 to
f39881b
Compare
6880b52 to
84b3f6d
Compare
84b3f6d to
4e343c2
Compare
No description provided.