MQTT-Based Event Notification System for Order Processing
System Overview
The MQTT-based event notification system allows Waldur to communicate changes to resources, orders, and user roles to the waldur-site-agent
that runs on a remote cluster. This eliminates the need for constant polling and enables immediate reactions to events.
The key components include:
-
MQTT Publisher (Waldur side): Located in the
waldur_mastermind/marketplace_slurm_remote/handlers.py
andutils.py
files, this component publishes messages to MQTT topics when specific events occur. -
Event Subscription Service: Manages subscriptions to events by creating unique topics for each type of notification.
-
MQTT Consumer (Agent side): The
waldur-site-agent
running on the resource provider's infrastructure that subscribes to these topics and processes incoming messages.
Event Flow
- An event occurs in Waldur (e.g., a new order is created, a user role changes, or a resource is updated)
- Waldur publishes a message to the appropriate MQTT topic
- The site agent receives the message and processes it based on the event type
- The agent communicates with the backend (e.g., SLURM) to execute the necessary actions
Message Types
The system handles three primary types of events:
- Order Messages: Notifications about marketplace orders (create, update, terminate)
- User Role Messages: Changes to user permissions in projects
- Resource Messages: Updates to resource configuration or status
Implementation Details
Publishing Messages (Waldur Side)
When events like order creation occur, Waldur prepares and publishes MQTT messages:
1 2 3 4 5 |
|
These messages are then sent via:
1 |
|
Subscription Management (Agent Side)
The EventSubscriptionManager
class handles creation of event subscriptions and setup of MQTT consumers:
1 2 3 |
|
1 2 3 4 5 |
|
Message Processing (Agent Side)
When a message arrives, it's routed to the appropriate handler based on the event type:
1 2 3 |
|
1 2 3 |
|
1 2 3 |
|
Technical Components
- WebSocket Transport: The system uses MQTT over WebSockets for communication
- TLS Security: Connections can be secured with TLS
- User Authentication: Each subscription has its own credentials
- Topic Structure: Topics follow the pattern
subscription/{sub_uuid}/offering/{offering_uuid}/{affected_object}
Error Handling and Resilience
The system includes:
- Graceful connection handling
- Signal handlers for proper shutdown
- Retry mechanisms for order processing
- Error logging and optional Sentry integration
Benefits of the MQTT Approach
- Real-time Processing: Actions are triggered immediately when events occur
- Reduced Network Traffic: No constant polling needed
- Decoupling: The agent doesn't need direct access to Waldur's database
- Scalability: Multiple agents can subscribe to different events
- Reliability: The MQTT protocol provides QoS options to ensure message delivery
This event-driven architecture significantly improves the responsiveness and efficiency of the order processing system compared to traditional polling approaches.