Compare commits

...

15 Commits

Author SHA1 Message Date
Stanislav Hejny 08f71aa207 #12 - add comments to properties to complete the documentation of AMQ Adapter
Unit test coverage / pytest (push) Failing after 55s
Unit test coverage / pytest (pull_request) Failing after 1m9s
2025-05-05 13:24:22 +01:00
stanislav.hejny 9ceee90f07 Merge pull request '#25 - monitor CPU usage to determine backpressure events' (#38) from monitor-cpu-usage into develop
Unit test coverage / pytest (push) Failing after 1m13s
CI for pypl publish / publish-lib (push) Successful in 1m15s
Reviewed-on: #38
2025-05-05 12:07:59 +00:00
Stanislav Hejny c772a2844a #25 - monitor CPU usage to determine backpressure events
Unit test coverage / pytest (push) Failing after 59s
Unit test coverage / pytest (pull_request) Failing after 1m18s
2025-05-05 13:05:51 +01:00
stanislav.hejny d40e8319be Merge pull request '#28 - stop using Thread as CleverSwarm methods are not thread-safe' (#37) from single-thread into develop
Unit test coverage / pytest (push) Failing after 1m16s
CI for pypl publish / publish-lib (push) Successful in 1m20s
Reviewed-on: #37
2025-05-02 09:08:11 +00:00
Stanislav Hejny e29b7339f9 #28 - stop using Thread as CleverSwarm methods are not thread-safe
Unit test coverage / pytest (push) Failing after 59s
Unit test coverage / pytest (pull_request) Failing after 1m20s
2025-05-02 10:05:36 +01:00
stanislav.hejny d681b8ae0f Merge pull request '#35 - move CLI tools to dedicated directory so it is not part of the distributable code' (#36) from cli_tools into develop
Unit test coverage / pytest (push) Failing after 1m24s
CI for pypl publish / publish-lib (push) Successful in 1m28s
Reviewed-on: #36
2025-05-01 17:28:59 +00:00
Stanislav Hejny 4fc81edc62 #35 - move CLI tools to dedicated directory so it is not partr of the distributable code
Unit test coverage / pytest (push) Failing after 57s
Unit test coverage / pytest (pull_request) Failing after 1m23s
2025-05-01 18:26:09 +01:00
stanislav.hejny 8163542388 Merge pull request '#29 - CleverSwarm integration - fix PUT multipart/form-data message error' (#30) from fix_put_message into develop
Unit test coverage / pytest (push) Failing after 1m6s
CI for pypl publish / publish-lib (push) Failing after 1m9s
Reviewed-on: #30
2025-05-01 17:09:52 +00:00
Stanislav Hejny 317a68d0c4 bug #29 - CleverSwarm integration - fix PUT multipart/form-data message error
Unit test coverage / pytest (push) Failing after 1m9s
Unit test coverage / pytest (pull_request) Failing after 1m7s
2025-05-01 18:08:15 +01:00
Stanislav Hejny 21c062ed29 Revert "#29 - CleverSwarm integration - fix PUT multipart/form-data message error"
Unit test coverage / pytest (push) Failing after 1m10s
Unit test coverage / pytest (pull_request) Failing after 1m9s
This reverts commit c6b7b2ad7e.
2025-05-01 17:59:30 +01:00
Stanislav Hejny cf40c7d7e4 Revert "Revert "#29 - CleverSwarm integration - fix PUT multipart/form-data message error""
This reverts commit 1540335750.
2025-05-01 17:59:03 +01:00
Stanislav Hejny 1540335750 Revert "#29 - CleverSwarm integration - fix PUT multipart/form-data message error"
This reverts commit c6b7b2ad7e.
2025-05-01 17:00:34 +01:00
stanislav.hejny 10a6c93b8d Merge pull request '#32 add the RabbitMQ exchange initialization tool' (#33) from create_tools_exchg into develop
Unit test coverage / pytest (push) Failing after 1m9s
CI for pypl publish / publish-lib (push) Successful in 1m12s
Reviewed-on: #33
2025-05-01 15:56:07 +00:00
Stanislav Hejny 7453bb8860 #32 add the RabbitMQ exchange initialization tool
Unit test coverage / pytest (push) Failing after 56s
Unit test coverage / pytest (pull_request) Failing after 59s
2025-05-01 01:08:26 +01:00
Stanislav Hejny c6b7b2ad7e #29 - CleverSwarm integration - fix PUT multipart/form-data message error
Unit test coverage / pytest (push) Failing after 1m2s
Unit test coverage / pytest (pull_request) Failing after 58s
2025-05-01 00:40:59 +01:00
8 changed files with 224 additions and 49 deletions
+92 -30
View File
@@ -4,6 +4,7 @@ import time
from asyncio import AbstractEventLoop
from threading import Thread
import psutil
from aio_pika import Message
from aio_pika.abc import AbstractRobustChannel
@@ -46,6 +47,40 @@ class ScaleRequestV1:
)
class RunningAverage:
def __init__(self, num_items):
self.buffer = [0] * num_items # Fixed-size array
self.pointer = 0 # Current position in array
self.num_items = num_items # Maximum number of items to store
self.is_filled = False # Track if buffer is fully filled
def add(self, value):
# Store the value at current pointer position
self.buffer[self.pointer] = value
# Move pointer to next position with wrap-around
self.pointer += 1
if self.pointer >= self.num_items:
self.pointer = 0
self.is_filled = True
def get_average(self):
# Determine how many items we should average
count = self.num_items if self.is_filled else self.pointer
if count == 0:
return 0.0 # Avoid division by zero
return sum(self.buffer) / count
def get_current_values(self):
"""Returns the values in chronological order (oldest first)"""
if not self.is_filled:
return self.buffer[: self.pointer]
return self.buffer[self.pointer :] + self.buffer[: self.pointer]
class BackpressureHandler:
# Track the number of messages currently being processed
current_parallel_executions = 0
@@ -69,6 +104,7 @@ class BackpressureHandler:
self.swarm_service_id = self.config.amq_adapter.swarm_service_id
self.swarm_task_id = self.config.amq_adapter.swarm_task_id
self.parallel_workers = self.config.backpressure.threshold_threads
self.average_cpu_usage = None
def increase_parallel_executions(self):
"""Increase the number of parallel executions"""
@@ -119,67 +155,93 @@ class BackpressureHandler:
self.last_backpressure_event = ScalingRequestAlert.OVERLOAD
def backpressure_monitor_loop(self):
_time_window_sec = self.config.backpressure.time_window
_idle_duration_millis = 1000 * self.config.backpressure.idle_duration
_time_window_millis = self.config.backpressure.time_window
_idle_duration_millis = self.config.backpressure.idle_duration
_overload_duration_millis = self.config.backpressure.idle_duration
_loop = asyncio.new_event_loop()
_monitor_interval = 0.2 # Monitor every 200ms
CPU_IDLE, CPU_ACTIVE, CPU_OVERLOAD = 0, 1, 2
IDLE_THRESHOLD, OVERLOAD_THRESHOLD = 15, 90
self.average_cpu_usage = RunningAverage(
int(
max(_overload_duration_millis, _idle_duration_millis)
// _monitor_interval
)
)
async def _backpressure_monitor():
"""Monitor the backpressure conditions and trigger events accordingly"""
_cpu_usage_state = CPU_ACTIVE
_cpu_usage_changed = time.time()
while True:
_current_cpu_usage = psutil.cpu_percent(interval=None)
self.average_cpu_usage.add(_current_cpu_usage)
_average_cpu_usage = self.average_cpu_usage.get_average()
_now = time.time()
logging_info(
"Backpressure: Monitor loop, current=%s",
self.current_parallel_executions,
)
logging_info(
"Backpressure: Last data message time=%s, eventTime=%s",
"Backpressure: current CPU[pct]=%s, avg=%s, state=%s, last dataMsg=%s, last event=%s, last eventTime=%s",
_current_cpu_usage,
_average_cpu_usage,
_cpu_usage_state,
self.last_data_message_time,
self.last_backpressure_event,
self.last_backpressure_event_time,
)
logging_info(
"Backpressure: Last backpressure event=%s",
self.last_backpressure_event,
)
if _average_cpu_usage < IDLE_THRESHOLD:
if _cpu_usage_state != CPU_IDLE:
_cpu_usage_changed = _now
_cpu_usage_state = CPU_IDLE
elif _average_cpu_usage > OVERLOAD_THRESHOLD:
if _cpu_usage_state != CPU_OVERLOAD:
_cpu_usage_changed = _now
_cpu_usage_state = CPU_OVERLOAD
else:
if _cpu_usage_state != CPU_ACTIVE:
_cpu_usage_changed = _now
_cpu_usage_state = CPU_ACTIVE
# Check if the current time exceeds the last backpressure event time
if (
self.current_parallel_executions >= self.parallel_workers
and time.time() - self.last_backpressure_event_time
> _time_window_sec
and self.last_backpressure_event != ScalingRequestAlert.OVERLOAD
_cpu_usage_state == CPU_OVERLOAD
and _now - _cpu_usage_changed > _overload_duration_millis
and (
_now - self.last_backpressure_event_time > _time_window_millis
or self.last_backpressure_event != ScalingRequestAlert.OVERLOAD
)
):
# Trigger the overload event
await self.handle_backpressure_overload_event()
self.last_backpressure_event_time = time.time()
self.last_backpressure_event_time = _now
self.last_backpressure_event = ScalingRequestAlert.OVERLOAD
elif (
self.current_parallel_executions == 0
and time.time() - self.last_data_message_time
>= _idle_duration_millis
and self.last_backpressure_event != ScalingRequestAlert.IDLE
_cpu_usage_state == CPU_IDLE
and _now - _cpu_usage_changed > _idle_duration_millis
and _now - self.last_data_message_time > _idle_duration_millis
and (
_now - self.last_backpressure_event_time > _time_window_millis
or self.last_backpressure_event != ScalingRequestAlert.IDLE
)
):
# Trigger the idle event
await self._handle_backpressure_idle_event()
self.last_backpressure_event = ScalingRequestAlert.IDLE
self.last_backpressure_event_time = time.time()
self.last_backpressure_event_time = _now
else:
# Trigger update event
if (
self.current_parallel_executions > 0
and time.time() - self.last_backpressure_event_time
> _time_window_sec
):
if _now - self.last_backpressure_event_time > _time_window_millis:
# Trigger the update event
_scaling_request: ScaleRequestV1 = ScaleRequestV1(
self.swarm_service_id,
self.swarm_task_id,
self.parallel_workers,
self.parallel_workers - self.current_parallel_executions,
100,
_average_cpu_usage,
ScalingRequestAlert.UPDATE,
)
# Address the message to any adapter capable of supporting BACKPRESSURE request
await self.publish_backpressure_request(_scaling_request)
self.last_backpressure_event_time = time.time()
self.last_backpressure_event_time = _now
self.last_backpressure_event = ScalingRequestAlert.UPDATE
await asyncio.sleep(_time_window_sec)
await asyncio.sleep(_monitor_interval)
_loop.run_until_complete(_backpressure_monitor())
+21 -10
View File
@@ -2,30 +2,41 @@
# CleverMicro AMQ Adapter settings
# ====================================================================
[CleverMicro-AMQ]
cm.amq-adapter.service-name=amq-adapter-python
cm.amq-adapter.generator-id=164
cm.amq-adapter.route-mapping=amq-cleverswarm::amq-cleverswarm:cleverswarm.localhost.#:5:0
# CleverMicro AMQ Adapter settings that identify this AMQ Adapter instance to other adapters
# A name to identify this service for the AMQ Adapter
cm.amq-adapter.service-name=cleverthis-service-name
# The CleverMicro AMQ Adapter instance ID, used to identify this instance in the CleverMicro AMQ Adapter cluster
cm.amq-adapter.generator-id=1
# Route mapping. For route detail description see section 3.1 in Onboarding Guide for Python:
# https://docs.cleverthis.com/en/architecture/microservices/onboarding/python
# NOTE: this is a comma-separated list of route mappings (multiple, comma-separated routes are allowed here)
cm.amq-adapter.route-mapping=
# Indicate if AMQ Adapter should respond with HTTP 401 Unauthorized if the request does not contain valid JWT token
cm.amq-adapter.require-authenticated-user=false
# CleverMicro AMQ Adapter dispatcher, settings related to RabbitMQ connection and optional REST forwarding
cm.dispatch.use-dlq=true
cm.dispatch.use-confirms=false
cm.dispatch.amq-host=rabbitmq
cm.dispatch.amq-port=5672
cm.dispatch.amq-port-tls=5671
cm.dispatch.download-dir=/tmp/downloaded_files
# Not used for CleverSwarm nor CleverBRAG or in tight coupling mode.
# Not used for CleveSwarm nor CleverBRAG or in tight coupling mode.
# applicable only in loose coupling mode, specify the destination where to forward the REST request
#cm.dispatch.service-host=cleverswarm.localhost
#cm.dispatch.service-host=cleverthis-service-name.localhost
#cm.dispatch.service-port=8080
#
# Backpressure monitor settings
# Define maximum number of messages of being processed in parallel before triggering backpressure OVERLOAD alert
cm.backpressure.threshold=5
# Define maximum CPU usage before triggering backpressure OVERLOAD alert
cm.backpressure.threshold-cpu=90
# Define maximum CPU usage (%) before triggering backpressure OVERLOAD alert
cm.backpressure.threshold-cpu-overload=90
# Define maximum CPU usage (%) before triggering backpressure IDLE alert
cm.backpressure.threshold-cpu-idle=10
# Define the time window between the Backpressure reports, in seconds
cm.backpressure.time-window=10
# Define the time window length, in seconds, during which, if CPU usage is consistently above the threshold, the system will trigger OVERLOAD alert
cm.backpressure.cpu-overload-duration=30
# Define the time window length, in seconds, during which, if no request is made to the Service, the system will trigger IDLE alert
cm.backpressure.idle-duration=30
cm.backpressure.cpu-idle-duration=30
+12 -3
View File
@@ -75,12 +75,21 @@ class DataMessageHandler:
self.file_handler: FileHandler = file_handler
self.backpressure_handler: BackpressureHandler = backpressure_handler
async def inbound_data_message_callback(self, message: AbstractIncomingMessage):
async def inbound_data_message_callback_as_thread(
self, message: AbstractIncomingMessage
):
"""
The callback method that is called when a DataMessage is received from RabbitMQ, starts new Thread.
"""
threading.Thread(
target=lambda: asyncio.run(self.run_callback_thread(message))
target=lambda: asyncio.run(
self.inbound_data_message_callback_no_thread(message)
)
).start()
async def run_callback_thread(self, message: AbstractIncomingMessage):
async def inbound_data_message_callback_no_thread(
self, message: AbstractIncomingMessage
):
"""
Receives the DataMessage as bytes from RabbitMQ queue and deserializes it to the DataMessage object
Ensures Jaeger trace-id propagation.
+1 -1
View File
@@ -107,7 +107,7 @@ class AMQService:
try:
await self.rabbit_mq_client.register_inbound_routes(
route_mapping,
self.amq_data_message_handler.inbound_data_message_callback,
self.amq_data_message_handler.inbound_data_message_callback_no_thread,
)
return await self.rabbit_mq_client.register_data_reply_callback(
self.amq_data_message_handler.reply_received_callback
+4 -2
View File
@@ -252,11 +252,13 @@ class CleverSwarmAmqpAdapter(CleverThisServiceAdapter):
for _body_param in _route.dependant.body_params:
if _body_param.name in _form_data:
_file_data = _form_data[_body_param.name]
actual_filepath = message.extra_data[_body_param.name]
_form_data[_body_param.name] = (
_body_param.type_.__name__ == "UploadFile"
and message.extra_data.get(_body_param.name)
and UploadFile(
file=pathlib.Path(actual_filepath).open("rb"),
file=pathlib.Path(
message.extra_data.get(_body_param.name)
).open("rb"),
size=_file_data["size"],
filename=_file_data["filename"],
headers=Headers(
+4 -3
View File
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
[project]
name = "amq_adapter"
version = "0.2.23-dev"
description = "The Python implementation of the AMQ Adaptor for CleverMicro"
version = "0.2.27"
description = "The Python implementation of the AMQ Adapter for CleverMicro"
readme = "README.md"
authors = [
{ name = "Stanislav Hejny", email = "stanislav.hejny@cleverthis.com" }
@@ -21,13 +21,14 @@ requires-python = ">=3.11"
dependencies = [
"aio-pika~=9.5.5",
"aiohttp~=3.11.11",
"aio_pika~=9.5.5",
"fastapi==0.114.2",
"opentelemetry-api",
"opentelemetry-sdk",
"opentelemetry-exporter-otlp",
"opentelemetry-exporter-prometheus",
"opentelemetry-instrumentation-pika",
"pika~=1.3.2",
"psutil",
"python_multipart"
]
+90
View File
@@ -0,0 +1,90 @@
import asyncio
import aio_pika
import os
import argparse
import sys
from aio_pika.abc import AbstractRobustExchange
async def create_exchange(loop, rabbit_user, rabbit_password, rabbit_host):
"""
Asynchronously connects to RabbitMQ, declares a direct exchange,
and prints the result. Handles potential connection errors.
"""
exchange_name = "cleverthis.clevermicro.management"
connection = None # Keep track of the connection
try:
# Construct the connection URI from environment variables
connection_uri = f"amqp://{rabbit_user}:{rabbit_password}@{rabbit_host}/"
# Attempt to establish the connection
connection = await aio_pika.connect_robust(connection_uri, loop=loop)
print(f"Connected to RabbitMQ: {connection_uri}")
# Create a channel
channel = await connection.channel()
# Declare the exchange
exchange: AbstractRobustExchange = await channel.declare_exchange(
exchange_name, aio_pika.ExchangeType.DIRECT
)
print(f"Exchange '{exchange.name}' created successfully.")
except aio_pika.exceptions.AMQPConnectionError as e:
print(f"Error: Could not connect to RabbitMQ. Details: {e}")
sys.exit(1) # Exit with an error code
except aio_pika.exceptions.AMQPChannelError as e:
print(f"Error creating channel or exchange: {e}")
sys.exit(1)
except Exception as e:
print(f"An unexpected error occurred: {e}")
sys.exit(1)
finally:
# Ensure the connection is closed, even if errors occur
if connection:
await connection.close()
print("Connection to RabbitMQ closed.")
def main():
"""
Main function to parse command-line arguments, retrieve RabbitMQ
credentials from environment variables, and run the
async exchange creation.
"""
# Set up argument parsing
parser = argparse.ArgumentParser(description="Create a RabbitMQ exchange.")
parser.add_argument(
"--host",
default="localhost",
help="RabbitMQ host (default: localhost)",
)
# Parse the arguments
args = parser.parse_args()
rabbit_host = args.host
# Get RabbitMQ credentials from environment variables
rabbit_user = os.environ.get("RABBIT_MQ_USER")
rabbit_password = os.environ.get("RABBIT_MQ_PASSWORD")
if not rabbit_user or not rabbit_password:
print(
"Error: RabbitMQ credentials not found in environment variables.\n"
"Please set RABBIT_MQ_USER and RABBIT_MQ_PASSWORD."
)
sys.exit(1) # Exit with an error code
# Create the event loop
loop = asyncio.get_event_loop()
# Run the asynchronous exchange creation
loop.run_until_complete(create_exchange(loop, rabbit_user, rabbit_password, rabbit_host))
# Close the loop
loop.close()
if __name__ == "__main__":
main()