SIENTIAPDE-1084
Enhance documentation and improve shutdown methods for DataManager and IngestorManager - Updated README.md to clarify persistent storage capabilities. - Enhanced docstrings in DataManager and IngestorManager for shutdown processes, detailing resource cleanup and error handling. - Improved comments in various methods to provide clearer explanations of functionality and side effects.
This commit is contained in:
@@ -164,6 +164,17 @@ class IngestorManager(BaseActivity):
|
||||
return manager
|
||||
|
||||
async def shutdown(self):
|
||||
"""
|
||||
Gracefully shuts down the IngestorManager and all its components.
|
||||
|
||||
This method ensures proper cleanup of:
|
||||
- All OPC manager instances and their connections
|
||||
- Data manager connections and resources
|
||||
- Active subscriptions and server connections
|
||||
|
||||
The shutdown process is performed asynchronously to allow proper cleanup
|
||||
of all managed resources before termination.
|
||||
"""
|
||||
for _server_name, server in self.opc_managers.items():
|
||||
await server.shutdown()
|
||||
self.data_manager.shutdown()
|
||||
@@ -252,6 +263,17 @@ class IngestorManager(BaseActivity):
|
||||
def check_opc_servers_integrity(self):
|
||||
"""
|
||||
Checks the integrity of the OPC servers and updates the OPC servers if necessary.
|
||||
|
||||
This method performs health checks on all managed OPC servers by:
|
||||
- Checking cycle counts for data reception
|
||||
- Monitoring connection health and data flow
|
||||
- Triggering reconnection for lost servers
|
||||
- Updating metrics for active OPC managers
|
||||
|
||||
Side Effects:
|
||||
- Updates cycle monitoring for all nodes
|
||||
- Removes lost servers from managed tags
|
||||
- Updates OPC manager metrics
|
||||
"""
|
||||
for server, opc_manager in self.opc_managers.items():
|
||||
opc_manager.check_cycles()
|
||||
@@ -272,8 +294,14 @@ class IngestorManager(BaseActivity):
|
||||
def declare_active(self):
|
||||
"""
|
||||
Declares the ingestor as active by sending a heartbeat signal to the resource manager.
|
||||
|
||||
This method ensures that the ingestor is marked as active by invoking the
|
||||
`ingestor_heartbeat` method of the associated resource manager.
|
||||
`ingestor_heartbeat` method of the associated resource manager. The heartbeat
|
||||
mechanism enables load balancers and monitoring systems to track active instances.
|
||||
|
||||
Side Effects:
|
||||
- Updates Redis with current instance heartbeat
|
||||
- Enables load balancing and health monitoring
|
||||
"""
|
||||
|
||||
self.resource_manager.ingestor_heartbeat()
|
||||
@@ -281,10 +309,15 @@ class IngestorManager(BaseActivity):
|
||||
def get_active_ingestors(self) -> List[str]:
|
||||
"""
|
||||
Retrieve a list of active ingestors.
|
||||
|
||||
This method fetches all ingestors from the resource manager and returns them.
|
||||
If no ingestors are found, an empty list is returned.
|
||||
|
||||
Returns:
|
||||
List[str]: A list of active ingestor names, or an empty list if none are found.
|
||||
|
||||
The method queries Redis for all active ingestor heartbeats and extracts
|
||||
the pod identifiers for load balancing and coordination purposes.
|
||||
"""
|
||||
|
||||
ingestors = self.resource_manager.get_all_ingestors()
|
||||
@@ -293,10 +326,16 @@ class IngestorManager(BaseActivity):
|
||||
def get_number_of_leases(self) -> int:
|
||||
"""
|
||||
Retrieves the number of leases managed by the resource manager.
|
||||
|
||||
This method fetches all available leases from the resource manager,
|
||||
calculates their count, and updates the `number_of_slots` attribute.
|
||||
|
||||
Returns:
|
||||
int: The total number of leases. Returns 0 if no leases are available.
|
||||
|
||||
Side Effects:
|
||||
- Updates internal slot count tracking
|
||||
- Updates Prometheus metrics for total leases
|
||||
"""
|
||||
|
||||
leases = self.resource_manager.get_all_leases()
|
||||
@@ -307,10 +346,16 @@ class IngestorManager(BaseActivity):
|
||||
def get_number_of_slots(self) -> int:
|
||||
"""
|
||||
Retrieves the number of slots managed by the resource manager.
|
||||
|
||||
This method fetches all available slots from the resource manager,
|
||||
calculates their count, and updates the `number_of_slots` attribute.
|
||||
|
||||
Returns:
|
||||
int: The total number of slots. Returns 0 if no slots are available.
|
||||
|
||||
Side Effects:
|
||||
- Updates internal slot count tracking
|
||||
- Updates Prometheus metrics for total slots
|
||||
"""
|
||||
|
||||
slots = self.resource_manager.get_all_slots()
|
||||
@@ -321,22 +366,29 @@ class IngestorManager(BaseActivity):
|
||||
def get_slot_leases(self, max_slots: int = 1) -> Dict:
|
||||
"""
|
||||
Acquires a specified number of resource slots by leasing them from the resource manager.
|
||||
|
||||
This method implements the slot acquisition logic for load balancing:
|
||||
- Iterates through available slots and attempts to lease them
|
||||
- Logs the leasing of each slot
|
||||
- Updates the `managed_tags` attribute with the acquired slots
|
||||
- Stops leasing once the specified `max_slots` are acquired
|
||||
|
||||
Args:
|
||||
max_slots (int): The maximum number of slots to lease. Defaults to 1.
|
||||
|
||||
Returns:
|
||||
Dict: A dictionary where the keys are the slot identifiers (as strings)
|
||||
and the values are the leased slot details.
|
||||
|
||||
Behavior:
|
||||
- Iterates through available slots and attempts to lease
|
||||
them using the resource manager.
|
||||
- Logs the leasing of each slot.
|
||||
- Updates the `managed_tags` attribute with the acquired slots.
|
||||
- Stops leasing once the specified `max_slots` are acquired.
|
||||
- If unable to acquire the requested number of slots,
|
||||
logs a warning and returns the slots that were leased.
|
||||
- Attempts to lease slots sequentially starting from slot 1
|
||||
- Skips slots that cannot be retrieved after leasing
|
||||
- Logs warnings if unable to acquire the requested number of slots
|
||||
- Updates metrics for acquired slots and managed slots count
|
||||
|
||||
Notes:
|
||||
- If a slot is leased but its details cannot be retrieved
|
||||
(i.e., `get_tag_slot` returns None), that slot is skipped.
|
||||
(i.e., `get_tag_slot` returns None), that slot is skipped.
|
||||
"""
|
||||
|
||||
acquired = {}
|
||||
@@ -367,10 +419,20 @@ class IngestorManager(BaseActivity):
|
||||
async def unsubscribe_slot(self, slot: str):
|
||||
"""
|
||||
Unsubscribes a specific slot from all associated OPC servers.
|
||||
|
||||
This method removes all subscriptions for a given slot across all
|
||||
OPC servers that were managing it. It ensures clean cleanup of
|
||||
resources when slots are released or reconfigured.
|
||||
|
||||
Args:
|
||||
slot (str): The name of the slot to unsubscribe.
|
||||
|
||||
Raises:
|
||||
KeyError: If the specified slot does not exist in the managed tags.
|
||||
|
||||
Side Effects:
|
||||
- Removes subscriptions from all OPC servers for the specified slot
|
||||
- Cleans up subscription resources on the OPC servers
|
||||
"""
|
||||
|
||||
for server in self.managed_tags[slot].keys():
|
||||
@@ -381,6 +443,7 @@ class IngestorManager(BaseActivity):
|
||||
"""
|
||||
Updates the configuration of managed slots by renewing their leases,
|
||||
fetching the latest configurations, and handling any changes or removals.
|
||||
|
||||
This method performs the following steps:
|
||||
1. Renews the lease for each managed slot using the resource manager.
|
||||
2. Fetches the latest configuration for each slot.
|
||||
@@ -389,15 +452,18 @@ class IngestorManager(BaseActivity):
|
||||
5. Unsubscribes and re-subscribes to slots with updated configurations.
|
||||
6. Removes slots from the managed tags if they are no longer valid.
|
||||
7. Updates the OPC servers after processing all slots.
|
||||
|
||||
Side Effects:
|
||||
- Modifies the `managed_tags` dictionary to reflect the latest slot configurations.
|
||||
- Updates OPC server subscriptions based on the current state of managed slots.
|
||||
- Modifies the `managed_tags` dictionary to reflect the latest slot configurations.
|
||||
- Updates OPC server subscriptions based on the current state of managed slots.
|
||||
|
||||
Raises:
|
||||
- None explicitly, but relies on the behavior of `resource_manager` and
|
||||
other dependencies for error handling.
|
||||
None explicitly, but relies on the behavior of `resource_manager` and
|
||||
other dependencies for error handling.
|
||||
|
||||
Logging:
|
||||
- Logs warnings for removed slots.
|
||||
- Logs informational messages for updated slot configurations.
|
||||
- Logs warnings for removed slots.
|
||||
- Logs informational messages for updated slot configurations.
|
||||
"""
|
||||
|
||||
removed_slots = []
|
||||
@@ -417,14 +483,22 @@ class IngestorManager(BaseActivity):
|
||||
def drop_slot_leases(self, ids: List[str]) -> None:
|
||||
"""
|
||||
Releases the leases associated with the specified slot IDs.
|
||||
|
||||
This method iterates through a list of slot IDs and calls the
|
||||
`drop_tag_lease` method of the `resource_manager` to release
|
||||
the lease for each ID.
|
||||
the lease for each ID. It's used during load balancing and
|
||||
graceful shutdown scenarios.
|
||||
|
||||
Args:
|
||||
ids (List[str]): A list of slot IDs for which the leases
|
||||
should be released.
|
||||
|
||||
Returns:
|
||||
None
|
||||
|
||||
Side Effects:
|
||||
- Releases Redis-based leases for specified slots
|
||||
- Updates metrics for released slots count
|
||||
"""
|
||||
for lease_id in ids:
|
||||
self.resource_manager.drop_tag_lease(lease_id)
|
||||
@@ -433,28 +507,38 @@ class IngestorManager(BaseActivity):
|
||||
async def manage_server(self, slot: str, server: str, server_config: dict, tags: dict) -> int:
|
||||
"""
|
||||
Manages the subscription of tags to a specified OPC server and slot.
|
||||
|
||||
This method ensures that the specified server and slot have an active subscription
|
||||
for the provided tags. If the server or slot is not properly configured, or if
|
||||
subscription fails, appropriate error handling is performed.
|
||||
|
||||
Args:
|
||||
slot (str): The slot identifier for the subscription.
|
||||
server (str): The name of the OPC server.
|
||||
server_config (dict): Configuration dictionary for the server, which includes
|
||||
the tags to be subscribed under the key 'tags'.
|
||||
tags (dict): A dictionary of tags to be subscribed.
|
||||
|
||||
Returns:
|
||||
int: Status code indicating the result of the operation:
|
||||
- 0: Subscription was successful.
|
||||
- 1: Server not found in `opc_managers`.
|
||||
- 2: Subscription creation or tag subscription failed.
|
||||
|
||||
Logs:
|
||||
- Logs informational messages about the subscription process.
|
||||
- Logs errors if the server is not found, subscription creation fails, or
|
||||
tag subscription fails.
|
||||
- Logs a warning if a subscription is removed due to failure.
|
||||
|
||||
Raises:
|
||||
Exception: Any unexpected exceptions during subscription creation or tag
|
||||
subscription are logged but not propagated.
|
||||
|
||||
Side Effects:
|
||||
- Creates or updates OPC subscriptions
|
||||
- Manages tag subscriptions on OPC servers
|
||||
- Updates error metrics and notifications
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
@@ -511,19 +595,27 @@ class IngestorManager(BaseActivity):
|
||||
async def subscribe_to_tags(self, tags: Dict) -> None:
|
||||
"""
|
||||
Subscribes to a set of tags and manages their configurations.
|
||||
|
||||
This method processes a dictionary of tags, iterating through each slot and server
|
||||
configuration. It attempts to manage the server configurations and removes any
|
||||
servers that return a specific response code.
|
||||
|
||||
Args:
|
||||
tags (Dict): A dictionary containing tag configurations. The structure is
|
||||
expected to be {slot: {server: server_config}}.
|
||||
|
||||
Side Effects:
|
||||
- Logs the provided tags for debugging purposes.
|
||||
- Updates the `managed_tags` attribute by removing servers that meet the
|
||||
removal criteria.
|
||||
- Establishes OPC subscriptions for all configured tags.
|
||||
|
||||
Removal Criteria:
|
||||
- If the `manage_server` method returns a response code of 2 for a given
|
||||
slot and server, that server is removed from the `managed_tags` attribute.
|
||||
|
||||
The method ensures that only successfully configured servers remain in the
|
||||
managed tags, maintaining system stability and preventing subscription errors.
|
||||
"""
|
||||
|
||||
to_remove = []
|
||||
|
||||
Reference in New Issue
Block a user