feat/#63 - cleverswarm unidirectional command
Unit test coverage / pytest (push) Has been cancelled
/ build-and-push (push) Has been cancelled
Unit test coverage / pytest (pull_request) Failing after 1m51s

This commit is contained in:
Stanislav Hejny
2025-07-26 15:29:07 +01:00
parent 9491517696
commit 41fbe46129
12 changed files with 148 additions and 86 deletions
+24 -19
View File
@@ -78,6 +78,7 @@ class BackpressureHandler:
self.current_availability = 1 self.current_availability = 1
self.max_load = 1 self.max_load = 1
self.current_load = 0 self.current_load = 0
self.idle_state_detected_time = 0
def increase_current_load(self): def increase_current_load(self):
"""Increase the number of parallel executions, or current_availability load""" """Increase the number of parallel executions, or current_availability load"""
@@ -146,12 +147,12 @@ class BackpressureHandler:
current_time = time.time() current_time = time.time()
last_event_delta: float = current_time - self.last_backpressure_event_time last_event_delta: float = current_time - self.last_backpressure_event_time
logging_info( # logging_info(
"Backpressure: Check conditions, current_availability=%s, max=%s, last_activity=%s", # "Backpressure: Check conditions, current_availability=%s, max=%s, last_activity=%s",
self.current_availability, # self.current_availability,
self.max_availability, # self.max_availability,
last_event_delta, # last_event_delta,
) # )
# Check for OVERLOAD condition - low availability (less than 20% of maximum) # Check for OVERLOAD condition - low availability (less than 20% of maximum)
if self.current_availability <= round(0.1 * self.max_availability): if self.current_availability <= round(0.1 * self.max_availability):
@@ -171,7 +172,7 @@ class BackpressureHandler:
idle_duration = self.config.backpressure.idle_duration idle_duration = self.config.backpressure.idle_duration
# Check if service has been idle for longer than the configured duration # Check if service has been idle for longer than the configured duration
if ( if (
last_event_delta > idle_duration current_time - self.idle_state_detected_time > idle_duration
and self.last_backpressure_event == ScalingRequestAlert.IDLE and self.last_backpressure_event == ScalingRequestAlert.IDLE
): ):
logging_info( logging_info(
@@ -184,19 +185,25 @@ class BackpressureHandler:
await self._handle_backpressure_idle_event() await self._handle_backpressure_idle_event()
# update / reset time-window so that the IDLE is not sent too often # update / reset time-window so that the IDLE is not sent too often
self.last_backpressure_event_time = current_time self.last_backpressure_event_time = current_time
self.idle_state_detected_time = current_time
# send IDLE even only once
self.last_backpressure_event = ScalingRequestAlert.UPDATE
else: else:
# Don't send IDLE right away when the availability increases, but wait for a while, send UPDATE instead # Don't send IDLE right away when the availability increases, but wait for a while, send UPDATE instead
self.last_backpressure_event = ScalingRequestAlert.IDLE self.last_backpressure_event = ScalingRequestAlert.IDLE
_scaling_request: ScaleRequestV1 = ScaleRequestV1( if current_time - self.idle_state_detected_time > idle_duration:
self.swarm_service_id, self.idle_state_detected_time = current_time
self.swarm_task_id, if last_event_delta > self.config.backpressure.time_window:
self.max_availability, _scaling_request: ScaleRequestV1 = ScaleRequestV1(
self.current_availability, # Current availability is passed directly self.swarm_service_id,
ScalingRequestAlert.UPDATE, self.swarm_task_id,
) self.max_availability,
# Address the message to any adapter capable of supporting BACKPRESSURE request self.current_availability, # Current availability is passed directly
await self.publish_backpressure_request(_scaling_request) ScalingRequestAlert.UPDATE,
self.last_backpressure_event_time = current_time )
# Address the message to any adapter capable of supporting BACKPRESSURE request
await self.publish_backpressure_request(_scaling_request)
self.last_backpressure_event_time = current_time
# If neither OVERLOAD nor IDLE, and it's time for an update, send UPDATE event # If neither OVERLOAD nor IDLE, and it's time for an update, send UPDATE event
elif last_event_delta > self.config.backpressure.time_window: elif last_event_delta > self.config.backpressure.time_window:
@@ -212,8 +219,6 @@ class BackpressureHandler:
self.last_backpressure_event_time = current_time self.last_backpressure_event_time = current_time
self.last_backpressure_event = ScalingRequestAlert.UPDATE self.last_backpressure_event = ScalingRequestAlert.UPDATE
self.update_last_backpressure_event_time()
async def _backpressure_monitor(self): async def _backpressure_monitor(self):
"""Periodically monitor the backpressure conditions and trigger events accordingly""" """Periodically monitor the backpressure conditions and trigger events accordingly"""
_monitor_interval = 0.5 # Monitor every 500ms _monitor_interval = 0.5 # Monitor every 500ms
+21 -16
View File
@@ -128,6 +128,11 @@ class DataMessageFactory:
package, class_name, method = fq_method_name.rsplit(".", 2) package, class_name, method = fq_method_name.rsplit(".", 2)
headers = {"Content-Type": ["application/octet-stream"], "Return-Address": [swarm_id]} headers = {"Content-Type": ["application/octet-stream"], "Return-Address": [swarm_id]}
trace_info = {} trace_info = {}
body = python_type_to_json(args).encode(encoding="utf-8") if args else b""
print(
f"Creating RPC message for method: {fq_method_name}, args: {args} body: {body.decode('utf-8') if body else 'None'}"
)
return DataMessage( return DataMessage(
DataMessageMagicByte.RPC_REQUEST.value, DataMessageMagicByte.RPC_REQUEST.value,
self.generate_snowflake_id(), self.generate_snowflake_id(),
@@ -136,7 +141,7 @@ class DataMessageFactory:
method, method,
headers, headers,
trace_info, trace_info,
python_type_to_json(args) if args else b"", body,
) )
@staticmethod @staticmethod
@@ -195,13 +200,13 @@ class DataMessageFactory:
header_length = 1 + len(id_bytes) + len(prolog) header_length = 1 + len(id_bytes) + len(prolog)
prolog_len = header_length.to_bytes(2, byteorder="big") prolog_len = header_length.to_bytes(2, byteorder="big")
msg_length = 2 + header_length + len(message.base64body()) msg_length = 2 + header_length + len(message.base64body())
with BytesIO(bytearray(msg_length)) as baos: baos = BytesIO(bytearray(msg_length))
baos.write(prolog_len) baos.write(prolog_len)
baos.write(DataMessageMagicByte.HTTP_REQUEST.value.encode("utf-8")) baos.write(message.magic().encode("utf-8"))
baos.write(id_bytes) baos.write(id_bytes)
baos.write(prolog) baos.write(prolog)
baos.write(message.base64body()) baos.write(message.base64body())
return baos.getvalue() return baos.getvalue()
@staticmethod @staticmethod
def from_bytes(input_bytes: bytes) -> DataMessage: def from_bytes(input_bytes: bytes) -> DataMessage:
@@ -214,7 +219,7 @@ class DataMessageFactory:
metadata = input_bytes[2 : prolog_len + 2].decode() metadata = input_bytes[2 : prolog_len + 2].decode()
pattern = re.compile( pattern = re.compile(
f"{DataMessageMagicByte.HTTP_REQUEST.value}" r"([AR])"
r"([0-9A-Fa-f]+)\|" r"([0-9A-Fa-f]+)\|"
r"m=([^|]*)\|" r"m=([^|]*)\|"
r"d=([^|]*)\|" r"d=([^|]*)\|"
@@ -230,19 +235,19 @@ class DataMessageFactory:
f"Input string [{metadata}] does not match pattern: {pattern.pattern}" f"Input string [{metadata}] does not match pattern: {pattern.pattern}"
) )
id_str = match.group(1) id_str = match.group(2)
method = match.group(2) method = match.group(3)
domain = match.group(3) domain = match.group(4)
path = match.group(4) path = match.group(5)
headers_str = match.group(5) headers_str = match.group(6)
trace_info_str = match.group(6) trace_info_str = match.group(7)
body_b64 = input_bytes[prolog_len + 2 :] body_b64 = input_bytes[prolog_len + 2 :]
headers = parse_map_list_string(headers_str) headers = parse_map_list_string(headers_str)
trace_info = parse_map_string(trace_info_str) trace_info = parse_map_string(trace_info_str)
return DataMessage( return DataMessage(
DataMessageMagicByte.HTTP_REQUEST.value, match.group(1), # Magic byte (R for RPC, A for HTTP)
SnowflakeId.from_hex(id_str), SnowflakeId.from_hex(id_str),
method, method,
domain, domain,
+7 -3
View File
@@ -39,10 +39,14 @@ def map_with_list_as_string(map_: Dict[str, List[str]]) -> str:
def parse_map_string(input_str: str) -> Dict[str, str]: def parse_map_string(input_str: str) -> Dict[str, str]:
map_ = {} map_ = {}
if input_str.startswith("{") and input_str.endswith("}"): if input_str.startswith("{") and input_str.endswith("}"):
for entry in input_str[1:-1].split(","): for entry in str(input_str[1:-1]).replace("\n", "").split(","):
if len(entry) > 1: if len(entry) > 1:
key, value = entry.split("=") if entry.startswith('"'):
map_[key] = value key, value = entry.split(":")
map_[key.strip('"')] = value.strip('"')
else:
key, value = entry.split("=")
map_[key] = value
return map_ return map_
+6 -1
View File
@@ -50,7 +50,12 @@ class RouterBase:
return self.route_database.find_route(message.domain(), message.path()) return self.route_database.find_route(message.domain(), message.path())
def find_route_by_service(self, service_name: str) -> Optional[AMQRoute]: def find_route_by_service(self, service_name: str) -> Optional[AMQRoute]:
return self.route_database.find_route_by_service(service_name) _route: Optional[AMQRoute] = self.route_database.find_route_by_service(service_name)
if _route is None:
for route in self.own_routes:
if route.component_name == service_name:
_route = route
return _route
def add_routes(self, message: str) -> None: def add_routes(self, message: str) -> None:
try: try:
+20 -13
View File
@@ -256,7 +256,7 @@ class DataMessageHandler:
try: try:
_a_message = AMQMessage(amq_message, self.rabbit_mq_client.connection) _a_message = AMQMessage(amq_message, self.rabbit_mq_client.connection)
_response: DataResponse = await self.service_adapter.on_rpc(_a_message) _response: DataResponse = await self.on_rpc(_a_message)
logging_debug(f"Result in thread: {_response}") logging_debug(f"Result in thread: {_response}")
try: try:
await self.send_reply(message.reply_to, _response) await self.send_reply(message.reply_to, _response)
@@ -301,10 +301,10 @@ class DataMessageHandler:
AttributeError: If the specified class or function/method does not exist. AttributeError: If the specified class or function/method does not exist.
Exception: For any other errors during invocation. Exception: For any other errors during invocation.
""" """
package_name: str = a_message.domain() package_name: str = a_message.method()
class_name: str = a_message.path() class_name: str = a_message.domain()
function_name: str = a_message.method() function_name: str = a_message.path()
kwargs: Dict[str, Any] = parse_map_string(a_message.body().decode("utf-8")) kwargs: Dict[str, Any] = parse_map_string(a_message._base64body.decode("utf-8"))
return await invoke_command( return await invoke_command(
package_name, class_name, function_name, a_message.id().as_string(), kwargs package_name, class_name, function_name, a_message.id().as_string(), kwargs
@@ -490,16 +490,23 @@ class DataMessageHandler:
:param message: DataMessage to be sent :param message: DataMessage to be sent
:return: Future that will be completed with the response payload :return: Future that will be completed with the response payload
""" """
rmq_body = DataMessageFactory.serialize(message)
cid = message.id().as_string()
ctype = (
message.content_type()[0]
if isinstance(message.content_type(), list)
else message.content_type()
)
print(
f"Sending RPC command to route: {route}, message ID: {cid} with body: {rmq_body} correlation ID: {cid}, content type: {ctype}"
)
# Create a Pika message with the correlation ID set to the message ID # Create a Pika message with the correlation ID set to the message ID
pika_message = Message( pika_message = Message(
body=DataMessageFactory.serialize(message), body=rmq_body,
correlation_id=message.id().as_string(), correlation_id=cid,
content_type=( content_type=ctype,
message.content_type()[0]
if isinstance(message.content_type(), list)
else message.content_type()
),
) )
print(f"SENDING RPC COMMAND: {pika_message.body} with ID: {message.id().as_string()}")
# Get the exchange from the route # Get the exchange from the route
rpc_exchange_name = route.exchange rpc_exchange_name = route.exchange
@@ -509,7 +516,7 @@ class DataMessageHandler:
# Publish the message to the exchange with the component name as the routing key # Publish the message to the exchange with the component name as the routing key
await exchange.publish(message=pika_message, routing_key=route.component_name) await exchange.publish(message=pika_message, routing_key=route.component_name)
logging_debug( logging_info(
"###### RPC Request Published to exchg:(%s) routing_key=%s, msgId=%s", "###### RPC Request Published to exchg:(%s) routing_key=%s, msgId=%s",
rpc_exchange_name, rpc_exchange_name,
route.component_name, route.component_name,
+3 -3
View File
@@ -151,13 +151,13 @@ class AMQService:
""" """
route: Optional[AMQRoute] = self.router.find_route_by_service(service_name) route: Optional[AMQRoute] = self.router.find_route_by_service(service_name)
if route is not None: if route is not None:
message: DataMessage = self.data_message_factory.create_rpc_message( m: DataMessage = self.data_message_factory.create_rpc_message(
fq_method_name=fq_method_name, fq_method_name=fq_method_name,
swarm_id=self.amq_configuration.amq_adapter.swarm_task_id, swarm_id=self.amq_configuration.amq_adapter.swarm_task_id,
**kwargs, args=kwargs,
) )
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self.amq_data_message_handler.send_command(route, message), self.amq_data_message_handler.send_command(route, m),
loop=self.backpressure_handler.loop, loop=self.backpressure_handler.loop,
) )
return True return True
+2 -2
View File
@@ -110,8 +110,8 @@ class CleverBragAmqpAdapter(CleverThisServiceAdapter):
amq_configuration, routes, None, "auth_user", extra_init_data=extra_init_data amq_configuration, routes, None, "auth_user", extra_init_data=extra_init_data
) )
self.auth_provider = auth_provider self.auth_provider = auth_provider
_amq_service: AMQService = AMQService(amq_configuration, self) self.amq_service: AMQService = AMQService(amq_configuration, self)
self.thread = Thread(target=_amq_service.run) self.thread = Thread(target=self.amq_service.run)
self.thread.start() self.thread.start()
async def get_current_user(self, token: str) -> User: async def get_current_user(self, token: str) -> User:
+2 -2
View File
@@ -50,8 +50,8 @@ class CleverSwarmAmqpAdapter(CleverThisServiceAdapter):
def __init__(self, amq_configuration: AMQConfiguration, routes: List[APIRoute]): def __init__(self, amq_configuration: AMQConfiguration, routes: List[APIRoute]):
super().__init__(amq_configuration, routes=routes, session=None) super().__init__(amq_configuration, routes=routes, session=None)
_amq_service: AMQService = AMQService(amq_configuration, self) self.amq_service: AMQService = AMQService(amq_configuration, self)
self.thread = Thread(target=_amq_service.run) self.thread = Thread(target=self.amq_service.run)
self.thread.start() self.thread.start()
async def get_current_user(self, token: str) -> UserSchema: async def get_current_user(self, token: str) -> UserSchema:
+32 -21
View File
@@ -1,4 +1,5 @@
import json import json
import logging
import os import os
import random import random
import shutil import shutil
@@ -194,32 +195,42 @@ async def process_document_metadata(metadata: dict):
return {"status": "metadata processed", "received_data": metadata} return {"status": "metadata processed", "received_data": metadata}
@app.get("/api/v3/documents") @app.get("/api/v4/documents")
async def get_documents(): async def get_documents():
# Create a custom span for the logic within this endpoint # Create a custom span for the logic within this endpoint
with tracer.start_as_current_span("get_documents_endpoint_logic"): documents = []
documents = [] try:
# Generate several random documents to emulate a list with tracer.start_as_current_span("get_documents_endpoint_logic"):
for i in range(random.randint(2, 5)): documents = []
documents.append( # Generate several random documents to emulate a list
{ for i in range(random.randint(2, 5)):
"id": f"doc-{random.randint(1000, 9999)}", did = f"doc-{random.randint(1000, 9999)}"
"name": f"Document {random.randint(1, 100)}", documents.append(
"description": f"Description for document {random.randint(1, 1000)}", {
"uploaded_at": time.strftime( "id": did,
"%Y-%m-%dT%H:%M:%SZ", "name": f"Document {random.randint(1, 100)}",
time.gmtime(time.time() - random.randint(0, 86400 * 30)), "description": f"Description for document {random.randint(1, 1000)}",
), "uploaded_at": time.strftime(
} "%Y-%m-%dT%H:%M:%SZ",
) time.gmtime(time.time() - random.randint(0, 86400 * 30)),
),
}
)
otadapter.amq_service.command(
"otdemo-2",
"otdemo.otdemo_backend.__global__.print_document_id_static",
document_id=did,
)
# Increment the documents_retrieved_total metric # Increment the documents_retrieved_total metric
documents_retrieved_counter.add(1) documents_retrieved_counter.add(1)
except Exception as e:
logging.error(f"Error retrieving documents: {e}")
# Simulate some processing time # Simulate some processing time
time.sleep(random.uniform(0.01, 0.1)) time.sleep(random.uniform(0.01, 0.1))
return {"documents": documents} return {"documents": documents}
# Need to place below all route methods, otherwise the routes are incomplete # Need to place below all route methods, otherwise the routes are incomplete
+4 -4
View File
@@ -5,14 +5,14 @@
# CleverMicro AMQ Adapter settings that identify this AMQ Adapter instance to other adapters # CleverMicro AMQ Adapter settings that identify this AMQ Adapter instance to other adapters
# A name to identify this service for the AMQ Adapter # A name to identify this service for the AMQ Adapter
cm.amq-adapter.service-name=cleverswarm cm.amq-adapter.service-name=otdemo
# The CleverMicro AMQ Adapter instance ID, used to identify this instance in the CleverMicro AMQ Adapter cluster # The CleverMicro AMQ Adapter instance ID, used to identify this instance in the CleverMicro AMQ Adapter cluster
cm.amq-adapter.generator-id=1 cm.amq-adapter.generator-id=1
# Route mapping. For route detail description see section 3.1 in Onboarding Guide for Python: # Route mapping. For route detail description see section 3.1 in Onboarding Guide for Python:
# https://docs.cleverthis.com/en/architecture/microservices/onboarding/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) # NOTE: this is a comma-separated list of route mappings (multiple, comma-separated routes are allowed here)
# cm-dev route # cm-dev route
cm.amq-adapter.route-mapping=otdemo:otdemo-exchange:otdemo-queue:api.cm.dev.cleverthis.com.test.python.#:5:0 cm.amq-adapter.route-mapping=otdemo:otdemo-exchange:otdemo-queue-1:api.cm.dev.cleverthis.localhost.test.python.frontend.#:5:0,otdemo-2:otdemo-exchange:otdemo-queue-2:otdemo-2.#:5:0
# local route # local route
#cm.amq-adapter.route-mapping=otdemo::otdemo:api.dev.localhost.otdemo.#:5:0 #cm.amq-adapter.route-mapping=otdemo::otdemo:api.dev.localhost.otdemo.#:5:0
# Indicate if AMQ Adapter should respond with HTTP 401 Unauthorized if the request does not contain valid JWT token # Indicate if AMQ Adapter should respond with HTTP 401 Unauthorized if the request does not contain valid JWT token
@@ -20,7 +20,7 @@ cm.amq-adapter.require-authenticated-user=false
# CleverMicro AMQ Adapter dispatcher, settings related to RabbitMQ connection and optional REST forwarding # CleverMicro AMQ Adapter dispatcher, settings related to RabbitMQ connection and optional REST forwarding
cm.dispatch.use-dlq=true cm.dispatch.use-dlq=true
cm.dispatch.amq-host=rabbitmq cm.dispatch.amq-host=localhost
cm.dispatch.amq-port=5672 cm.dispatch.amq-port=5672
cm.dispatch.amq-port-tls=5671 cm.dispatch.amq-port-tls=5671
cm.dispatch.download-dir=/tmp/downloaded_files cm.dispatch.download-dir=/tmp/downloaded_files
@@ -37,7 +37,7 @@ cm.backpressure.threshold-cpu-overload=90
# Define maximum CPU usage (%) before triggering backpressure IDLE alert # Define maximum CPU usage (%) before triggering backpressure IDLE alert
cm.backpressure.threshold-cpu-idle=10 cm.backpressure.threshold-cpu-idle=10
# Define the time window between the Backpressure reports, in seconds # Define the time window between the Backpressure reports, in seconds
cm.backpressure.time-window=10 cm.backpressure.time-window=15
# Define the time window length, in seconds, during which, if CPU usage is consistently above the threshold, the system will trigger OVERLOAD alert # 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 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 # Define the time window length, in seconds, during which, if no request is made to the Service, the system will trigger IDLE alert
+2 -2
View File
@@ -32,8 +32,8 @@ class OTDemoAmqpAdapter(CleverThisServiceAdapter):
def __init__(self, amq_configuration: AMQConfiguration, routes: List[APIRoute]): def __init__(self, amq_configuration: AMQConfiguration, routes: List[APIRoute]):
super().__init__(amq_configuration, routes=routes, session=None) super().__init__(amq_configuration, routes=routes, session=None)
_amq_service: AMQService = AMQService(amq_configuration, self) self.amq_service: AMQService = AMQService(amq_configuration, self)
self.thread = Thread(target=_amq_service.run) self.thread = Thread(target=self.amq_service.run)
self.thread.start() self.thread.start()
async def get_current_user(self, token: str) -> str: async def get_current_user(self, token: str) -> str:
+25
View File
@@ -0,0 +1,25 @@
def print_document_id_static(document_id: str) -> str:
print(f"STATIC Document ID: {document_id}")
return "STATIC PRINTED!"
class Backend:
def __init__(self, name: str, description: str):
self.name = name
self.description = description
def __repr__(self):
return f"Backend(name={self.name}, description={self.description})"
def __str__(self):
return f"{self.name}: {self.description}"
def __eq__(self, other):
if not isinstance(other, Backend):
return False
return self.name == other.name and self.description == other.description
def print_document_id(self, document_id: str) -> str:
print(f"CLASS Document ID: {document_id}")
return "CLASS PRINTED!"