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

Background tasks for MQTT #120

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Background tasks for MQTT #120

wants to merge 5 commits into from

Conversation

jschlyter
Copy link
Member

@jschlyter jschlyter commented Nov 14, 2024

Enqueue all MQTT message to queue and process in the background (log error in case the queue is full). Also start up the application as part of the lifespan handler.

This creates a single MQTT client for the application, no more reconnected at every request.

Summary by CodeRabbit

  • New Features

    • Introduced asynchronous handling for MQTT message publishing, improving performance.
    • Added a configurable queue_size for MQTT settings, enhancing message processing capabilities.
    • Implemented a new counter to track dropped MQTT messages due to full queues.
  • Bug Fixes

    • Enhanced error handling during aggregate creation and MongoDB interactions for improved reliability.
  • Refactor

    • Updated server lifecycle management and logging for clarity and efficiency.

@jschlyter jschlyter requested a review from a team as a code owner November 14, 2024 07:37
@jschlyter
Copy link
Member Author

@coderabbitai review

Copy link

coderabbitai bot commented Nov 15, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Nov 15, 2024

Walkthrough

The changes introduced in this pull request primarily enhance the handling of MQTT messages and implement asynchronous operations across the aggrec module. Key modifications include the introduction of a queue mechanism for message publishing in aggrec/aggregates.py, an updated AggrecServer class in aggrec/server.py with improved lifecycle management and asynchronous message handling, and the addition of a queue_size parameter in the MqttSettings class in aggrec/settings.py. These changes collectively improve performance, error handling, and configuration options.

Changes

File Change Summary
aggrec/aggregates.py - Modified create_aggregate to use a queue for MQTT message publishing instead of a direct MQTT client.
- Enhanced error handling for MongoDB operations.
- Added aggregates_mqtt_queue_drops counter.
aggrec/server.py - Added lifespan parameter to the AggrecServer constructor.
- Changed connect_mongodb to an instance method.
- Introduced mqtt_publisher method for handling MQTT messages.
- Removed factory method and updated main function for direct initialization.
aggrec/settings.py - Added queue_size field to the MqttSettings class with a default of 1024.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant MQTTQueue
    participant MongoDB

    Client->>Server: Request to create aggregate
    Server->>MQTTQueue: Put new aggregate message
    alt Queue full
        MQTTQueue-->>Server: Raise QueueFull exception
        Server->>MongoDB: Attempt to save metadata
        alt Save successful
            Server-->>Client: Success response
        else Save failed
            MongoDB-->>Server: Raise exception
            Server->>S3: Delete object
            Server-->>Client: HTTP 500 error
        end
    else Queue not full
        Server-->>Client: Success response
    end
Loading

🐇 In the meadow, where bunnies play,
New changes hop in, brightening the day!
With queues for messages, swift as a breeze,
Asynchronous magic, bringing us ease.
So let’s celebrate, with a joyful cheer,
For every new feature, we hold dear!
🐰✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (2)
aggrec/settings.py (1)

28-28: LGTM! Consider adding field documentation.

The addition of queue_size aligns well with the PR's objective of implementing background MQTT message processing.

Consider adding documentation to explain the field's purpose and implications:

-    queue_size: int = Field(default=1024)
+    queue_size: int = Field(
+        default=1024,
+        description="Maximum number of MQTT messages that can be queued for processing. When queue is full, new messages will be dropped."
+    )
aggrec/server.py (1)

88-111: Enhance exception handling in mqtt_publisher

While MqttError and CancelledError are handled, consider catching other potential exceptions to prevent the task from terminating unexpectedly. Including a general exception handler will improve the robustness of the MQTT publisher task.

Apply this diff to add a general exception handler:

 except MqttError as exc:
     _logger.error("MQTT error: %s", str(exc))
+except Exception as exc:
+    _logger.exception("Unhandled exception in MQTT publisher: %s", str(exc))
 except asyncio.exceptions.CancelledError:
     _logger.debug("MQTT publish task cancelled")
     return
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 481df47 and 8e71749.

📒 Files selected for processing (3)
  • aggrec/aggregates.py (2 hunks)
  • aggrec/server.py (5 hunks)
  • aggrec/settings.py (1 hunks)
🔇 Additional comments (4)
aggrec/server.py (3)

35-35: Confirm the use of lifespan parameter in super().__init__

The inclusion of lifespan=self.lifespan in the super().__init__ call modifies the application lifecycle management. Ensure that the lifespan method is correctly defined and that this change is compatible with FastAPI's expected usage to prevent unexpected behavior.


51-51: Verify MQTT queue size configuration

The mqtt_new_aggregate_messages queue is initialized with a maximum size defined by self.settings.mqtt.queue_size. Confirm that this queue size is adequately configured to handle the expected message load without causing QueueFull exceptions, and adjust if necessary.


152-153: Initialize application with updated settings handling

The AggrecServer is now instantiated directly with Settings() without using a factory method. Verify that any previous configurations reliant on the factory method are appropriately adapted to this new initialization approach.

aggrec/aggregates.py (1)

1-1: Addition of import asyncio module is appropriate

The asyncio module is imported to support the asynchronous operations introduced in this file. This is necessary and correctly implemented.

aggrec/settings.py Show resolved Hide resolved
aggrec/server.py Show resolved Hide resolved
aggrec/server.py Outdated Show resolved Hide resolved
aggrec/aggregates.py Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
aggrec/server.py (1)

53-63: Consider adding connection timeout and error handling

While the MongoDB connection logic is sound, it lacks timeout configuration and error handling for connection failures.

 def connect_mongodb(self):
     if mongodb_host := str(self.settings.mongodb.server):
         params = {"host": mongodb_host}
+        params["serverSelectionTimeoutMS"] = 5000  # 5 second timeout
         if "host" in params and params["host"].startswith("mongomock://"):
             import mongomock
             params["host"] = params["host"].replace("mongomock://", "mongodb://")
             params["mongo_client_class"] = mongomock.MongoClient
         logger.info("Connecting to MongoDB %s", params)
-        mongoengine.connect(**params, tz_aware=True)
+        try:
+            mongoengine.connect(**params, tz_aware=True)
+            logger.info("MongoDB connected")
+        except mongoengine.connection.MongoEngineConnectionError as e:
+            logger.error("Failed to connect to MongoDB: %s", str(e))
+            raise
-        logger.info("MongoDB connected")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8e71749 and d183b22.

📒 Files selected for processing (2)
  • aggrec/aggregates.py (3 hunks)
  • aggrec/server.py (5 hunks)
🔇 Additional comments (6)
aggrec/server.py (4)

2-4: LGTM: Import changes align with new async functionality

The new imports support the async MQTT handling and error management. The tracer setup is appropriately placed at the module level.

Also applies to: 11-11, 28-29


112-125: LGTM: Well-implemented lifecycle management

The lifespan implementation properly manages application lifecycle, including graceful startup and shutdown of background tasks.


153-154: LGTM: Simplified server initialization

The direct initialization with settings is cleaner than the previous factory method approach.


51-51: Verify queue size configuration in settings

The queue size is obtained from settings.mqtt.queue_size. Ensure this configuration exists and has appropriate bounds.

✅ Verification successful

Queue size configuration is properly defined with default value

The queue_size configuration is correctly defined in MqttSettings class with a default value of 1024 using Pydantic's Field. The configuration is properly typed as int and uses Pydantic's validation system.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for queue_size definition in settings
rg -A 5 "queue_size" --type python

# Search for any validation on queue_size
ast-grep --pattern 'class MqttSettings:
  $$$
  queue_size: $_
  $$$'

Length of output: 137


Script:

#!/bin/bash
# Search for queue_size definition in settings using ripgrep
rg -l "queue_size"

# Search for MqttSettings class definition
rg -A 10 "class MqttSettings"

# Try finding settings files
fd "settings.py|config.py"

Length of output: 736

aggrec/aggregates.py (2)

46-49: LGTM! Well-structured metric for monitoring queue health

The new OpenTelemetry counter follows best practices with a clear name and description, providing valuable insights into queue overflow incidents.


346-351: ⚠️ Potential issue

Critical: Implement robust message handling to prevent data loss

The current implementation drops messages when the queue is full, which could lead to data inconsistency. While tracking dropped messages is good, preventing message loss should be prioritized given the PR's objective of improving robustness.

Consider these approaches in order of preference:

  1. Implement backpressure mechanism:
-    request.app.mqtt_new_aggregate_messages.put_nowait(
-        json.dumps(get_new_aggregate_event_message(metadata, request.app.settings))
-    )
-except asyncio.QueueFull:
-    aggregates_mqtt_queue_drops.add(1)
-    logger.warning("MQTT queue full, message dropped")
+    message = json.dumps(get_new_aggregate_event_message(metadata, request.app.settings))
+    try:
+        # Try non-blocking put first
+        request.app.mqtt_new_aggregate_messages.put_nowait(message)
+    except asyncio.QueueFull:
+        # If queue is full, wait with timeout
+        try:
+            await asyncio.wait_for(
+                request.app.mqtt_new_aggregate_messages.put(message),
+                timeout=2.0  # Adjust timeout as needed
+            )
+        except asyncio.TimeoutError:
+            aggregates_mqtt_queue_drops.add(1)
+            logger.error("MQTT queue full, message dropped after timeout")
+            # Consider raising HTTP 503 Service Unavailable here
+            raise HTTPException(
+                status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
+                detail="Service temporarily unable to handle request"
+            )
  1. Use blocking put with timeout (simpler alternative):
-    request.app.mqtt_new_aggregate_messages.put_nowait(
-        json.dumps(get_new_aggregate_event_message(metadata, request.app.settings))
-    )
-except asyncio.QueueFull:
-    aggregates_mqtt_queue_drops.add(1)
-    logger.warning("MQTT queue full, message dropped")
+    try:
+        await asyncio.wait_for(
+            request.app.mqtt_new_aggregate_messages.put(
+                json.dumps(get_new_aggregate_event_message(metadata, request.app.settings))
+            ),
+            timeout=2.0
+        )
+    except asyncio.TimeoutError:
+        aggregates_mqtt_queue_drops.add(1)
+        logger.error("MQTT queue full, message dropped after timeout")
+        raise HTTPException(
+            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
+            detail="Service temporarily unable to handle request"
+        )

Both approaches:

  • Prevent immediate message drops
  • Provide time for queue to process messages
  • Signal upstream systems when service is overwhelmed
  • Allow for monitoring via the existing counter

aggrec/server.py Show resolved Hide resolved
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.

1 participant