-
Notifications
You must be signed in to change notification settings - Fork 38
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
Run Limits Responses in Thread to Prevent Blocking Decom #1901
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright 2024 OpenC3, Inc. | ||
# Copyright 2025 OpenC3, Inc. | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can modify and/or redistribute it | ||
|
@@ -18,6 +18,8 @@ | |
import sys | ||
import time | ||
import json | ||
import threading | ||
import queue | ||
from openc3.microservices.microservice import Microservice | ||
from openc3.system.system import System | ||
from openc3.topics.topic import Topic | ||
|
@@ -29,7 +31,54 @@ | |
handle_build_cmd, | ||
handle_inject_tlm, | ||
) | ||
from openc3.top_level import kill_thread | ||
|
||
class LimitsResponseThread: | ||
def __init__(self, microservice_name, queue, logger, metric, scope): | ||
self.microservice_name = microservice_name | ||
self.queue = queue | ||
self.logger = logger | ||
self.metric = metric | ||
self.scope = scope | ||
self.count = 0 | ||
self.error_count = 0 | ||
self.metric.set(name="limits_response_total", value=self.count, type="counter") | ||
self.metric.set(name="limits_response_error_total", value=self.error_count, type="counter") | ||
|
||
def start(self): | ||
self.thread = threading.Thread(target=self.run, daemon=True) | ||
self.thread.start() | ||
return self.thread | ||
|
||
def stop(self): | ||
if self.thread: | ||
kill_thread(self, self.thread) | ||
self.thread = None | ||
|
||
def graceful_kill(self): | ||
self.queue.put([None, None, None]) | ||
|
||
def run(self): | ||
try: | ||
while True: | ||
packet, item, old_limits_state = self.queue.get() | ||
if packet is None: | ||
break | ||
|
||
try: | ||
item.limits.response.call(packet, item, old_limits_state) | ||
except Exception as error: | ||
self.error_count += 1 | ||
self.metric.set(name='limits_response_error_total', value=self.error_count, type='counter') | ||
self.logger.error(f"{packet.target_name} {packet.packet_name} {item.name} Limits Response Exception!") | ||
self.logger.error(f"Called with old_state = {old_limits_state}, new_state = {item.limits.state}") | ||
self.logger.error(repr(error)) | ||
|
||
self.count += 1 | ||
self.metric.set(name='limits_response_total', value=self.count, type='counter') | ||
except Exception as error: | ||
self.logger.error(f"{self.microservice_name}: Limits Response thread died: {repr(error)}") | ||
raise error | ||
|
||
class DecomMicroservice(Microservice): | ||
LIMITS_STATE_INDEX = { "RED_LOW": 0, "YELLOW_LOW": 1, "YELLOW_HIGH": 2, "RED_HIGH": 3, "GREEN_LOW": 4, "GREEN_HIGH": 5 } | ||
|
@@ -46,8 +95,13 @@ def __init__(self, *args): | |
self.error_count = 0 | ||
self.metric.set(name="decom_total", value=self.count, type="counter") | ||
self.metric.set(name="decom_error_total", value=self.error_count, type="counter") | ||
self.limits_response_queue = queue.Queue() | ||
self.limits_response_thread = None | ||
|
||
def run(self): | ||
self.limits_response_thread = LimitsResponseThread(microservice_name=self.name, queue=self.limits_response_queue, logger=self.logger, metric=self.metric, scope=self.scope) | ||
self.limits_response_thread.start() | ||
|
||
self.setup_microservice_topic() | ||
while True: | ||
if self.cancel_thread: | ||
|
@@ -78,6 +132,9 @@ def run(self): | |
self.error = error | ||
self.logger.error(f"Decom error {repr(error)}") | ||
|
||
self.limits_response_thread.stop() | ||
self.limits_response_thread = None | ||
|
||
def decom_packet(self, topic, msg_id, msg_hash, _redis): | ||
# OpenC3.in_span("decom_packet") do | ||
msgid_seconds_from_epoch = int(msg_id.split("-")[0]) / 1000.0 | ||
|
@@ -179,15 +236,9 @@ def limits_change_callback(self, packet, item, old_limits_state, value, log_chan | |
LimitsEventTopic.write(event, scope=self.scope) | ||
|
||
if item.limits.response is not None: | ||
try: | ||
# TODO: The limits response is user code and should be run as a separate thread / process | ||
# If this code blocks it will delay TelemetryDecomTopic.write_packet | ||
item.limits.response.call(packet, item, old_limits_state) | ||
except Exception as error: | ||
self.error = error | ||
self.logger.error(f"{packet.target_name} {packet.packet_name} {item.name} Limits Response Exception!") | ||
self.logger.error(f"Called with old_state = {old_limits_state}, new_state = {item.limits.state}") | ||
self.logger.error(repr(error)) | ||
copied_packet = packet.deep_copy() | ||
copied_item = packet.items[item.name] | ||
self.limits_response_queue.put([copied_packet, copied_item, old_limits_state]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a reference right? Should it be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the packet.deep_copy it is a reference to the correct new item. |
||
|
||
|
||
if os.path.basename(__file__) == os.path.basename(sys.argv[0]): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
Python already has
deepcopy
: https://docs.python.org/3/library/copy.html#copy.deepcopy. Should this just bepacket.deepcopy
?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.
No. Its a "mostly" deep copy. At least in Ruby can't do a real deep copy because that would marshal Mutex which isn't allowed.