Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
45941de6b1
|
@@ -1,7 +0,0 @@
|
|||||||
# Note: You can use any Debian/Ubuntu based image you want.
|
|
||||||
FROM mcr.microsoft.com/devcontainers/base:bullseye
|
|
||||||
|
|
||||||
# [Optional] Uncomment this section to install additional OS packages.
|
|
||||||
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
|
||||||
&& apt-get -y install --no-install-recommends \
|
|
||||||
bash curl jq
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
|
||||||
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-outside-of-docker-compose
|
|
||||||
{
|
|
||||||
"name": "Docker from Docker Compose",
|
|
||||||
"dockerComposeFile": "docker-compose.yml",
|
|
||||||
"service": "app",
|
|
||||||
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
|
|
||||||
// Use this environment variable if you need to bind mount your local source code into a new container.
|
|
||||||
"remoteEnv": {
|
|
||||||
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}",
|
|
||||||
"WORKSPACE_FOLDER": "/workspaces/${localWorkspaceFolderBasename}"
|
|
||||||
},
|
|
||||||
"features": {
|
|
||||||
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
|
|
||||||
"version": "latest",
|
|
||||||
"enableNonRootDocker": "true",
|
|
||||||
"moby": "false"
|
|
||||||
},
|
|
||||||
"ghcr.io/devcontainers/features/python:1": {
|
|
||||||
"version": "3.11"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
|
||||||
"forwardPorts": [
|
|
||||||
"rabbitmq:15672",
|
|
||||||
"jaeger:4317",
|
|
||||||
"jaeger:4318",
|
|
||||||
"jaeger:16686",
|
|
||||||
],
|
|
||||||
"postCreateCommand": "/bin/bash /workspaces/${localWorkspaceFolderBasename}/.devcontainer/setup.sh",
|
|
||||||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
|
||||||
// "remoteUser": "root"
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
version: '3'
|
|
||||||
|
|
||||||
services:
|
|
||||||
app:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
# Forwards the local Docker socket to the container.
|
|
||||||
- /var/run/docker.sock:/var/run/docker-host.sock
|
|
||||||
# Update this to wherever you want VS Code to mount the folder of your project
|
|
||||||
- ../..:/workspaces:cached
|
|
||||||
|
|
||||||
# Overrides default command so things don't shut down after the process ends.
|
|
||||||
entrypoint: /usr/local/share/docker-init.sh
|
|
||||||
command: sleep infinity
|
|
||||||
|
|
||||||
# Uncomment the next four lines if you will use a ptrace-based debuggers like C++, Go, and Rust.
|
|
||||||
# cap_add:
|
|
||||||
# - SYS_PTRACE
|
|
||||||
# security_opt:
|
|
||||||
# - seccomp:unconfined
|
|
||||||
|
|
||||||
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
|
|
||||||
# (Adding the "ports" property to this file will not forward from a Codespace.)
|
|
||||||
depends_on:
|
|
||||||
- rabbitmq
|
|
||||||
- jaeger
|
|
||||||
|
|
||||||
rabbitmq:
|
|
||||||
image: rabbitmq:4-management
|
|
||||||
hostname: "my-rabbit"
|
|
||||||
environment:
|
|
||||||
RABBITMQ_DEFAULT_USER: springuser
|
|
||||||
RABBITMQ_DEFAULT_PASS: TheCleverWho
|
|
||||||
volumes:
|
|
||||||
- "rabbitmq-data:/var/lib/rabbitmq"
|
|
||||||
|
|
||||||
jaeger:
|
|
||||||
image: "jaegertracing/jaeger:2.2.0"
|
|
||||||
command:
|
|
||||||
- "--config=file:/config/jaeger.yml"
|
|
||||||
volumes:
|
|
||||||
- "jaeger-data:/tmp" # This is the only folder can be used without permission issue
|
|
||||||
- "./jaeger-config.yaml:/config/jaeger.yml"
|
|
||||||
- "./jaeger-config-ui.json:/config/jaeger-config-ui.json"
|
|
||||||
environment:
|
|
||||||
- COLLECTOR_OTLP_ENABLED=true
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
rabbitmq-data:
|
|
||||||
jaeger-data:
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
python3 -m pip install --upgrade pip
|
|
||||||
pip install -e .[dev]
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
name: Build and Publish Docker Image
|
|
||||||
|
|
||||||
#on:
|
|
||||||
# push:
|
|
||||||
# branches: [ main, master ]
|
|
||||||
# tags: [ 'v*' ]
|
|
||||||
# pull_request:
|
|
||||||
# branches: [ main, master ]
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master # publish release on master
|
|
||||||
- develop # publish snapshot on develop
|
|
||||||
- feat-58-backpressure-reactive-streams
|
|
||||||
workflow_dispatch:
|
|
||||||
# allow manual trigger
|
|
||||||
|
|
||||||
env:
|
|
||||||
REGISTRY_URL: "git.cleverthis.com"
|
|
||||||
REPOSITORY: "clevermicro/amq-adapter-python-demo"
|
|
||||||
DOCKER_HOST: "tcp://dind:2375"
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-and-push:
|
|
||||||
runs-on: general
|
|
||||||
services:
|
|
||||||
dind:
|
|
||||||
image: docker:dind
|
|
||||||
cmd:
|
|
||||||
- dockerd
|
|
||||||
- -H
|
|
||||||
- tcp://0.0.0.0:2375
|
|
||||||
- --tls=false
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: ${{ github.ref_name }}
|
|
||||||
|
|
||||||
# setup docker
|
|
||||||
- name: set up docker cli
|
|
||||||
run: |
|
|
||||||
apt-get update
|
|
||||||
apt-get install ca-certificates curl
|
|
||||||
install -m 0755 -d /etc/apt/keyrings
|
|
||||||
curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
|
|
||||||
chmod a+r /etc/apt/keyrings/docker.asc
|
|
||||||
echo \
|
|
||||||
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
|
|
||||||
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
|
|
||||||
tee /etc/apt/sources.list.d/docker.list > /dev/null
|
|
||||||
apt-get update
|
|
||||||
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v2
|
|
||||||
|
|
||||||
- name: Login to Docker Registry
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ${{ env.REGISTRY_URL }}
|
|
||||||
username: ${{ secrets.REGISTRY_USER }}
|
|
||||||
password: ${{ secrets.REGISTRY_PASSWORD }}
|
|
||||||
if: github.event_name != 'pull_request'
|
|
||||||
|
|
||||||
- name: Extract metadata for Docker
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v4
|
|
||||||
with:
|
|
||||||
images: ${{ env.REGISTRY_URL }}/clevermicro/amq-adapter-python-demo
|
|
||||||
tags: |
|
|
||||||
type=ref,event=branch
|
|
||||||
type=ref,event=pr
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
type=sha,format=short
|
|
||||||
|
|
||||||
- name: Build and push Docker image
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: ./Dockerfile
|
|
||||||
# push: ${{ github.event_name != 'pull_request' }}
|
|
||||||
push: true
|
|
||||||
tags: ${{ env.REGISTRY_URL }}/${{ env.REPOSITORY }}:latest
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
@@ -38,4 +38,4 @@ jobs:
|
|||||||
file: ./Dockerfile
|
file: ./Dockerfile
|
||||||
no-cache: true
|
no-cache: true
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ env.REGISTRY_URL }}/${{ env.REPOSITORY }}:20250709-01
|
tags: ${{ env.REGISTRY_URL }}/${{ env.REPOSITORY }}:20250715-01
|
||||||
|
|||||||
@@ -3,16 +3,18 @@ import json
|
|||||||
import time
|
import time
|
||||||
from asyncio import AbstractEventLoop
|
from asyncio import AbstractEventLoop
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from aio_pika import Message
|
from aio_pika import Message
|
||||||
from aio_pika.abc import AbstractRobustChannel
|
from aio_pika.abc import AbstractRobustChannel
|
||||||
|
|
||||||
from amqp.adapter.logging_utils import logging_info, logging_warning
|
from amqp.adapter.logging_utils import logging_info
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
from amqp.model.model import ScalingRequestAlert
|
|
||||||
from amqp.router.utils import await_future, await_result
|
from amqp.router.utils import await_future, await_result
|
||||||
|
|
||||||
|
CPU_IDLE, CPU_ACTIVE, CPU_OVERLOAD = 0, 1, 2
|
||||||
|
IDLE_THRESHOLD, OVERLOAD_THRESHOLD = 0.15, 0.90
|
||||||
|
RESOURCE_UNSET, RESOURCE_IDLE, RESOURCE_ACTIVE, RESOURCE_OVERLOAD = -1, 0, 1, 2
|
||||||
|
|
||||||
|
|
||||||
class ScaleRequestV1:
|
class ScaleRequestV1:
|
||||||
|
|
||||||
@@ -20,16 +22,12 @@ class ScaleRequestV1:
|
|||||||
self,
|
self,
|
||||||
serviceId: str,
|
serviceId: str,
|
||||||
taskId: str,
|
taskId: str,
|
||||||
max_availability: int,
|
currentAvailability: int,
|
||||||
current_availability: int,
|
|
||||||
requestType: ScalingRequestAlert,
|
|
||||||
):
|
):
|
||||||
self.version = 1 # Version of the request, currently 1
|
self.version = 1 # Version of the request, currently 1
|
||||||
self.serviceId = serviceId
|
self.serviceId = serviceId
|
||||||
self.taskId = taskId
|
self.taskId = taskId
|
||||||
self.max_availability = max_availability
|
self.currentAvailability = currentAvailability
|
||||||
self.current_availability = current_availability
|
|
||||||
self.requestType = requestType
|
|
||||||
self.thread = None
|
self.thread = None
|
||||||
|
|
||||||
def to_json(self) -> str:
|
def to_json(self) -> str:
|
||||||
@@ -39,21 +37,60 @@ class ScaleRequestV1:
|
|||||||
"version": self.version,
|
"version": self.version,
|
||||||
"serviceId": self.serviceId,
|
"serviceId": self.serviceId,
|
||||||
"taskId": self.taskId,
|
"taskId": self.taskId,
|
||||||
"maxAvailability": self.max_availability,
|
"currentAvailability": self.currentAvailability,
|
||||||
"currentAvailability": self.current_availability,
|
|
||||||
"requestType": self.requestType.value, # Using .value for Enum serialization
|
|
||||||
},
|
},
|
||||||
indent=2,
|
indent=2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RunningAverage:
|
||||||
|
"""
|
||||||
|
A class to maintain a running average of the last N values.
|
||||||
|
The intended use is to track CPU usage over time window, that is directly related to sample rate and window size.
|
||||||
|
This is the reason why the constructor takes the number of items to store, calculated as window size / sample rate
|
||||||
|
Initial_value is for unit test to emulate OVERLOAD or IDLE condition.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, num_items, initial_value=0):
|
||||||
|
self.buffer = [initial_value] * 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:
|
class BackpressureHandler:
|
||||||
# Track the number of messages currently being processed
|
# Track the number of messages currently being processed
|
||||||
current_availability = 0
|
current_parallel_executions = 0
|
||||||
# helps detect IDLE condition
|
# report when remain availability goes down
|
||||||
# helps prevent flooding the system with backpressure events
|
last_usage_report_time = 0
|
||||||
last_backpressure_event_time = 0
|
# report when remain availability goes up
|
||||||
last_backpressure_event = ScalingRequestAlert.UPDATE
|
last_recover_report_time = 0
|
||||||
# _callback_list is used in unit tests to record the invoked callbacks
|
# _callback_list is used in unit tests to record the invoked callbacks
|
||||||
_callback_list = None
|
_callback_list = None
|
||||||
|
|
||||||
@@ -70,196 +107,80 @@ class BackpressureHandler:
|
|||||||
self.exchange = None
|
self.exchange = None
|
||||||
self.swarm_service_id = self.config.amq_adapter.swarm_service_id
|
self.swarm_service_id = self.config.amq_adapter.swarm_service_id
|
||||||
self.swarm_task_id = self.config.amq_adapter.swarm_task_id
|
self.swarm_task_id = self.config.amq_adapter.swarm_task_id
|
||||||
|
self.parallel_workers = self.config.backpressure.threshold_threads
|
||||||
self.do_loop = -1
|
self.do_loop = -1
|
||||||
self._resource_usage_changed = 0
|
|
||||||
self._resource_average_value = 0
|
|
||||||
self._last_resource_max_value = 100 # Default max value for CPU usage
|
|
||||||
self.max_availability = -1
|
|
||||||
self.current_availability = 1
|
|
||||||
self.max_load = 1
|
|
||||||
self.current_load = 0
|
|
||||||
self.idle_state_detected_time = 0
|
|
||||||
|
|
||||||
def increase_current_load(self):
|
def _do_loop(self) -> bool:
|
||||||
"""Increase the number of parallel executions, or current_availability load"""
|
|
||||||
logging_info(
|
|
||||||
"Backpressure: Increase current load (%s) by 1",
|
|
||||||
self.current_load,
|
|
||||||
)
|
|
||||||
self.current_load += 1
|
|
||||||
|
|
||||||
def decrease_current_load(self):
|
|
||||||
"""Decrease the number of parallel executions, or current load"""
|
|
||||||
logging_info(
|
|
||||||
"Backpressure: Decrease current load(%s) by 1",
|
|
||||||
self.current_load,
|
|
||||||
)
|
|
||||||
if self.current_load > 0:
|
|
||||||
self.current_load -= 1
|
|
||||||
|
|
||||||
def update_current_availability(self, count: int):
|
|
||||||
"""Update the number of parallel executions, or current load"""
|
|
||||||
self.current_availability = count
|
|
||||||
|
|
||||||
def update_last_backpressure_event_time(self):
|
|
||||||
# logging_info("Backpressure: Update last data message time")
|
|
||||||
"""Update the last data message time"""
|
|
||||||
self.last_backpressure_event_time = time.time()
|
|
||||||
|
|
||||||
async def update_backpressure_value(self, current_availability: int, maximum: int):
|
|
||||||
"""
|
"""
|
||||||
Update the current backpressure value and check for overload conditions.
|
Check if the loop should continue running.
|
||||||
This method is called by the AMQService.backpressure() method to update
|
Positive value of do_loop indicates the number of iterations left.
|
||||||
the current parallel executions count and check for overload conditions.
|
Negative value indicates the loop should run indefinitely.
|
||||||
|
|
||||||
Args:
|
|
||||||
current_availability: Current value of the backpressure metric
|
|
||||||
maximum: Maximum value of the backpressure metric
|
|
||||||
"""
|
"""
|
||||||
logging_info(
|
_val = self.do_loop != 0
|
||||||
"Backpressure: Updating backpressure value, current_availability=%s, maximum=%s",
|
if self.do_loop > 0:
|
||||||
current_availability,
|
self.do_loop -= 1
|
||||||
maximum,
|
return _val
|
||||||
)
|
|
||||||
if maximum > 0 and maximum > self.max_availability:
|
|
||||||
self.max_availability = maximum
|
|
||||||
# Update the current_availability / parallel executions count
|
|
||||||
self.update_current_availability(current_availability)
|
|
||||||
# Check for overload conditions
|
|
||||||
await self.check_overload_condition()
|
|
||||||
|
|
||||||
def start_backpressure_monitor(self) -> Optional[Thread]:
|
async def increase_parallel_executions(self):
|
||||||
|
"""Increase the number of parallel executions"""
|
||||||
|
self.current_parallel_executions += 1
|
||||||
|
logging_info(
|
||||||
|
"Backpressure: Increased parallel executions, new value: %s",
|
||||||
|
self.current_parallel_executions,
|
||||||
|
)
|
||||||
|
# increase parallel -> reduce in remaining availability -> usage
|
||||||
|
# report usage at least every 1s
|
||||||
|
# TODO: add config for the interval
|
||||||
|
if self.last_usage_report_time + 1 < time.time():
|
||||||
|
await self.send_backpressure_report()
|
||||||
|
self.last_usage_report_time = time.time()
|
||||||
|
|
||||||
|
async def decrease_parallel_executions(self):
|
||||||
|
"""Decrease the number of parallel executions"""
|
||||||
|
if self.current_parallel_executions > 0:
|
||||||
|
self.current_parallel_executions -= 1
|
||||||
|
logging_info(
|
||||||
|
"Backpressure: Decreased parallel executions, new value=%s",
|
||||||
|
self.current_parallel_executions,
|
||||||
|
)
|
||||||
|
# decrease parallel -> add to remaining availability -> recover
|
||||||
|
# report recover at every 3s
|
||||||
|
# TODO: add config for the interval
|
||||||
|
if self.last_recover_report_time + 3 < time.time():
|
||||||
|
await self.send_backpressure_report()
|
||||||
|
self.last_recover_report_time = time.time()
|
||||||
|
|
||||||
|
def start_backpressure_monitor(self) -> Thread:
|
||||||
# Start the Backpressure monitor loop
|
# Start the Backpressure monitor loop
|
||||||
self.thread = Thread(target=self.backpressure_monitor_loop)
|
self.thread = Thread(target=self.regular_report_loop_wrapper)
|
||||||
self.thread.daemon = True # This makes it a daemon thread
|
self.thread.daemon = True # This makes it a daemon thread
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
return self.thread
|
return self.thread
|
||||||
|
|
||||||
async def check_overload_condition(self):
|
async def _regular_report_loop(self):
|
||||||
"""
|
"""Regularly report the availability in a fixed interval"""
|
||||||
Check if the current_availability availability is too low (OVERLOAD)
|
|
||||||
or if the service has been idle for too long with high availability (IDLE).
|
|
||||||
|
|
||||||
Note: current_availability represents available capacity, not used capacity.
|
|
||||||
- Low availability (close to 0) means OVERLOAD
|
|
||||||
- High availability (close to maximum) with no activity means IDLE
|
|
||||||
"""
|
|
||||||
current_time = time.time()
|
|
||||||
last_event_delta: float = current_time - self.last_backpressure_event_time
|
|
||||||
|
|
||||||
# logging_info(
|
|
||||||
# "Backpressure: Check conditions, current_availability=%s, max=%s, last_activity=%s",
|
|
||||||
# self.current_availability,
|
|
||||||
# self.max_availability,
|
|
||||||
# last_event_delta,
|
|
||||||
# )
|
|
||||||
|
|
||||||
# Check for OVERLOAD condition - low availability (less than 20% of maximum)
|
|
||||||
if self.current_availability <= round(0.1 * self.max_availability):
|
|
||||||
# Check if the last backpressure event was not an overload
|
|
||||||
if (
|
|
||||||
self.last_backpressure_event != ScalingRequestAlert.OVERLOAD
|
|
||||||
or last_event_delta > self.config.backpressure.idle_duration
|
|
||||||
):
|
|
||||||
# Trigger the overload event
|
|
||||||
await self.handle_backpressure_overload_event()
|
|
||||||
# update / reset time-window so that the OVERLOAD is not sent too often
|
|
||||||
self.last_backpressure_event_time = current_time
|
|
||||||
self.last_backpressure_event = ScalingRequestAlert.OVERLOAD
|
|
||||||
|
|
||||||
# Check for IDLE condition - high availability (more than 80% of maximum) with no activity
|
|
||||||
elif self.current_availability >= round(0.8 * self.max_availability):
|
|
||||||
idle_duration = self.config.backpressure.idle_duration
|
|
||||||
# Check if service has been idle for longer than the configured duration
|
|
||||||
if (
|
|
||||||
current_time - self.idle_state_detected_time > idle_duration
|
|
||||||
and self.last_backpressure_event == ScalingRequestAlert.IDLE
|
|
||||||
):
|
|
||||||
logging_info(
|
|
||||||
"Backpressure: Service has been idle for %s seconds (threshold: %s seconds)",
|
|
||||||
last_event_delta,
|
|
||||||
idle_duration,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Trigger the idle event
|
|
||||||
await self._handle_backpressure_idle_event()
|
|
||||||
# update / reset time-window so that the IDLE is not sent too often
|
|
||||||
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:
|
|
||||||
# Don't send IDLE right away when the availability increases, but wait for a while, send UPDATE instead
|
|
||||||
self.last_backpressure_event = ScalingRequestAlert.IDLE
|
|
||||||
if current_time - self.idle_state_detected_time > idle_duration:
|
|
||||||
self.idle_state_detected_time = current_time
|
|
||||||
if last_event_delta > self.config.backpressure.time_window:
|
|
||||||
_scaling_request: ScaleRequestV1 = ScaleRequestV1(
|
|
||||||
self.swarm_service_id,
|
|
||||||
self.swarm_task_id,
|
|
||||||
self.max_availability,
|
|
||||||
self.current_availability, # Current availability is passed directly
|
|
||||||
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 = current_time
|
|
||||||
|
|
||||||
# If neither OVERLOAD nor IDLE, and it's time for an update, send UPDATE event
|
|
||||||
elif last_event_delta > self.config.backpressure.time_window:
|
|
||||||
_scaling_request: ScaleRequestV1 = ScaleRequestV1(
|
|
||||||
self.swarm_service_id,
|
|
||||||
self.swarm_task_id,
|
|
||||||
self.max_availability,
|
|
||||||
self.current_availability, # Current availability is passed directly
|
|
||||||
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 = current_time
|
|
||||||
self.last_backpressure_event = ScalingRequestAlert.UPDATE
|
|
||||||
|
|
||||||
async def _backpressure_monitor(self):
|
|
||||||
"""Periodically monitor the backpressure conditions and trigger events accordingly"""
|
|
||||||
_monitor_interval = 0.5 # Monitor every 500ms
|
|
||||||
while self._do_loop():
|
while self._do_loop():
|
||||||
await self.check_overload_condition()
|
await self.send_backpressure_report()
|
||||||
await asyncio.sleep(_monitor_interval)
|
# report recover at every 5s
|
||||||
|
# TODO: add config for the interval
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
def backpressure_monitor_loop(self):
|
def regular_report_loop_wrapper(self):
|
||||||
_loop = asyncio.new_event_loop()
|
_loop = asyncio.new_event_loop()
|
||||||
_loop.run_until_complete(self._backpressure_monitor())
|
_loop.run_until_complete(self._regular_report_loop())
|
||||||
|
|
||||||
async def handle_backpressure_overload_event(self):
|
async def send_backpressure_report(self):
|
||||||
logging_warning("Backpressure: Capacity close to depleted!")
|
|
||||||
# Send an Overload event to the Management service
|
|
||||||
scaling_request: ScaleRequestV1 = ScaleRequestV1(
|
scaling_request: ScaleRequestV1 = ScaleRequestV1(
|
||||||
self.swarm_service_id,
|
serviceId=self.swarm_service_id,
|
||||||
self.swarm_task_id,
|
taskId=self.swarm_task_id,
|
||||||
self.max_availability,
|
currentAvailability=self.parallel_workers - self.current_parallel_executions,
|
||||||
self.current_availability, # Current availability is passed directly
|
|
||||||
ScalingRequestAlert.OVERLOAD,
|
|
||||||
)
|
)
|
||||||
# Address the message to any management service instance capable of supporting BACKPRESSURE request
|
|
||||||
await self.publish_backpressure_request(scaling_request)
|
|
||||||
|
|
||||||
async def _handle_backpressure_idle_event(self):
|
|
||||||
logging_warning("Backpressure: Service is idle.")
|
|
||||||
# Send an Idle event to the Management service
|
|
||||||
scaling_request: ScaleRequestV1 = ScaleRequestV1(
|
|
||||||
self.swarm_service_id,
|
|
||||||
self.swarm_task_id,
|
|
||||||
self.max_availability,
|
|
||||||
self.current_availability, # Current availability is passed directly
|
|
||||||
ScalingRequestAlert.IDLE,
|
|
||||||
)
|
|
||||||
# Address the message to any adapter capable of supporting BACKPRESSURE request
|
|
||||||
await self.publish_backpressure_request(scaling_request)
|
await self.publish_backpressure_request(scaling_request)
|
||||||
|
|
||||||
async def publish_backpressure_request(self, scaling_request: ScaleRequestV1):
|
async def publish_backpressure_request(self, scaling_request: ScaleRequestV1):
|
||||||
# Publish the backpressure request to the management service
|
# Publish the backpressure request to the management service
|
||||||
logging_info(
|
logging_info(
|
||||||
f"Publishing backpressure for {scaling_request.serviceId} with request type {scaling_request.requestType}"
|
f"Publishing backpressure for {scaling_request.serviceId} with availability {scaling_request.currentAvailability}"
|
||||||
)
|
)
|
||||||
if not self.exchange:
|
if not self.exchange:
|
||||||
|
|
||||||
@@ -301,14 +222,3 @@ class BackpressureHandler:
|
|||||||
if BackpressureHandler._callback_list is not None:
|
if BackpressureHandler._callback_list is not None:
|
||||||
BackpressureHandler._callback_list["_wrap_rabbit_mq_api"] = 1
|
BackpressureHandler._callback_list["_wrap_rabbit_mq_api"] = 1
|
||||||
await await_future(asyncio.run_coroutine_threadsafe(_wrap_rabbit_mq_api(), self.loop))
|
await await_future(asyncio.run_coroutine_threadsafe(_wrap_rabbit_mq_api(), self.loop))
|
||||||
|
|
||||||
def _do_loop(self) -> bool:
|
|
||||||
"""
|
|
||||||
Helper function for unit tests to perform several loops only. Check if the loop should continue running.
|
|
||||||
Positive value of do_loop indicates the number of iterations left.
|
|
||||||
Negative value indicates the loop should run indefinitely.
|
|
||||||
"""
|
|
||||||
_val = self.do_loop != 0
|
|
||||||
if self.do_loop > 0:
|
|
||||||
self.do_loop -= 1
|
|
||||||
return _val
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ async def _convert_file_response(
|
|||||||
temporary dedicated queue.
|
temporary dedicated queue.
|
||||||
"""
|
"""
|
||||||
# All calls to RabbitMQ API have to be called threadsafe, because those appear to be attached
|
# All calls to RabbitMQ API have to be called threadsafe, because those appear to be attached
|
||||||
# to parent event loop, and need to execute at that event loop, not the current_availability one
|
# to parent event loop, and need to execute at that event loop, not the current one
|
||||||
# allow coroutine to execute. calling _future.result() will block the entire thread
|
# allow coroutine to execute. calling _future.result() will block the entire thread
|
||||||
# and the coroutine will never execute
|
# and the coroutine will never execute
|
||||||
_channel, _exchange, _routing_key = await await_result(
|
_channel, _exchange, _routing_key = await await_result(
|
||||||
@@ -241,7 +241,7 @@ class CleverThisServiceAdapter:
|
|||||||
|
|
||||||
async def get_current_user(self, token: str) -> object:
|
async def get_current_user(self, token: str) -> object:
|
||||||
"""
|
"""
|
||||||
To Be Overriden in subclass for given CleverThis Service, to return the current_availability user based
|
To Be Overriden in subclass for given CleverThis Service, to return the current user based
|
||||||
on the token, in format appropriate for the service.
|
on the token, in format appropriate for the service.
|
||||||
|
|
||||||
:param token: The token to be used for authentication.
|
:param token: The token to be used for authentication.
|
||||||
|
|||||||
@@ -1,46 +1,51 @@
|
|||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
import random
|
import random
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import consul_kv
|
import consul
|
||||||
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
from amqp.config.amq_configuration import AMQAdapter
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Default configuration values for Adapter
|
||||||
|
DEFAULT_CONSUL_HOST = 'localhost'
|
||||||
|
DEFAULT_CONSUL_PORT = 8500
|
||||||
|
DEFAULT_CONSUL_COUNTER_KEY = 'adapter/ids/counter'
|
||||||
|
DEFAULT_MAX_RETRIES = 5
|
||||||
|
DEFAULT_RETRY_DELAY_SECONDS = 0.1
|
||||||
|
|
||||||
def get_config_values(config: AMQAdapter) -> tuple:
|
def get_config_values():
|
||||||
"""
|
"""
|
||||||
Get configuration values from AMQConfiguration or environment variables.
|
Get configuration values from AMQConfiguration or environment variables.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple: (consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds)
|
tuple: (consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
consul_host = config.consul_host
|
config = AMQConfiguration("application.properties")
|
||||||
consul_port = config.consul_port
|
consul_host = os.environ.get('CONSUL_HOST', DEFAULT_CONSUL_HOST)
|
||||||
consul_counter_key = config.consul_counter_key
|
consul_port = int(os.environ.get('CONSUL_PORT', DEFAULT_CONSUL_PORT))
|
||||||
max_retries = config.consul_max_retries
|
consul_counter_key = os.environ.get('CONSUL_COUNTER_KEY', DEFAULT_CONSUL_COUNTER_KEY)
|
||||||
retry_delay_seconds = config.consul_initial_retry_delay
|
max_retries = int(os.environ.get('CONSUL_MAX_RETRIES', DEFAULT_MAX_RETRIES))
|
||||||
|
retry_delay_seconds = float(os.environ.get('CONSUL_RETRY_DELAY_SECONDS', DEFAULT_RETRY_DELAY_SECONDS))
|
||||||
return consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds
|
return consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to load configuration: {str(e)}. Using default values.")
|
logger.warning(f"Failed to load configuration: {str(e)}. Using default values.")
|
||||||
return (
|
return (
|
||||||
os.environ.get("CONSUL_HOST", consul_kv.settings.DEFAULT_ENDPOINT.split(":")[0]),
|
os.environ.get('CONSUL_HOST', DEFAULT_CONSUL_HOST),
|
||||||
int(os.environ.get("CONSUL_PORT", consul_kv.settings.DEFAULT_ENDPOINT.split(":")[1])),
|
int(os.environ.get('CONSUL_PORT', DEFAULT_CONSUL_PORT)),
|
||||||
os.environ.get("CONSUL_COUNTER_KEY", "service/ids/counter"),
|
os.environ.get('CONSUL_COUNTER_KEY', DEFAULT_CONSUL_COUNTER_KEY),
|
||||||
int(os.environ.get("CONSUL_MAX_RETRIES", 5)),
|
int(os.environ.get('CONSUL_MAX_RETRIES', DEFAULT_MAX_RETRIES)),
|
||||||
float(os.environ.get("CONSUL_RETRY_DELAY_SECONDS", 0.2)),
|
float(os.environ.get('CONSUL_RETRY_DELAY_SECONDS', DEFAULT_RETRY_DELAY_SECONDS))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def generate_fallback_id() -> int:
|
def generate_fallback_id() -> int:
|
||||||
"""
|
"""
|
||||||
Generate a fallback ID when Consul is not available.
|
Generate a fallback ID when Consul is not available.
|
||||||
Uses a combination of timestamp, hostname hash, and random number.
|
Uses a combination of timestamp, hostname hash, and random number.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
int: A reasonably unique integer ID
|
int: A reasonably unique integer ID
|
||||||
"""
|
"""
|
||||||
@@ -52,45 +57,45 @@ def generate_fallback_id() -> int:
|
|||||||
logger.warning(f"Using fallback ID generation method: {unique_id}")
|
logger.warning(f"Using fallback ID generation method: {unique_id}")
|
||||||
return unique_id
|
return unique_id
|
||||||
|
|
||||||
|
def get_unique_instance_id() -> int:
|
||||||
def get_unique_instance_id(config: AMQAdapter) -> int:
|
|
||||||
"""
|
"""
|
||||||
Get a globally unique ID from Consul.
|
Get a globally unique ID from Consul.
|
||||||
Uses Consul's atomic Compare-And-Set operations to safely increment a counter.
|
Uses Consul's atomic Compare-And-Set operations to safely increment a counter.
|
||||||
Falls back to a local generation method if Consul is unavailable.
|
Falls back to a local generation method if Consul is unavailable.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
int: A globally unique integer ID
|
int: A globally unique integer ID
|
||||||
"""
|
"""
|
||||||
consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds = (
|
consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds = get_config_values()
|
||||||
get_config_values(config)
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
c = consul_kv.Connection(endpoint=f"{consul_host}:{consul_port}", timeout=5)
|
c = consul.Consul(host=consul_host, port=consul_port)
|
||||||
index, data = c.get(consul_counter_key)
|
index, data = c.kv.get(consul_counter_key)
|
||||||
if data is None:
|
if data is None:
|
||||||
logger.info(f"Initializing Consul counter at {consul_counter_key}")
|
logger.info(f"Initializing Consul counter at {consul_counter_key}")
|
||||||
if c.put(consul_counter_key, "1", cas=1):
|
if c.kv.put(consul_counter_key, "1"):
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
logger.error("Failed to initialize Consul counter")
|
logger.error("Failed to initialize Consul counter")
|
||||||
return generate_fallback_id()
|
return generate_fallback_id()
|
||||||
current_value = int(data["Value"].decode("utf-8"))
|
current_value = int(data['Value'].decode('utf-8'))
|
||||||
new_value = current_value + 1
|
new_value = current_value + 1
|
||||||
for attempt in range(max_retries):
|
for attempt in range(max_retries):
|
||||||
success = c.put(consul_counter_key, str(new_value), cas=data["ModifyIndex"])
|
success = c.kv.put(
|
||||||
|
consul_counter_key,
|
||||||
|
str(new_value),
|
||||||
|
cas=data['ModifyIndex']
|
||||||
|
)
|
||||||
if success:
|
if success:
|
||||||
logger.debug(f"Successfully obtained unique ID: {new_value}")
|
logger.debug(f"Successfully obtained unique ID: {new_value}")
|
||||||
return new_value
|
return new_value
|
||||||
logger.debug(f"CAS update failed on attempt {attempt + 1}, retrying...")
|
logger.debug(f"CAS update failed on attempt {attempt+1}, retrying...")
|
||||||
if attempt < max_retries - 1:
|
time.sleep(retry_delay_seconds)
|
||||||
time.sleep(retry_delay_seconds * (1 << attempt)) # Exponential backoff
|
index, data = c.kv.get(consul_counter_key)
|
||||||
index, data = c.get(consul_counter_key)
|
if data is None:
|
||||||
if data is None:
|
logger.error("Counter disappeared during update")
|
||||||
logger.error("Counter disappeared during update")
|
return generate_fallback_id()
|
||||||
return generate_fallback_id()
|
current_value = int(data['Value'].decode('utf-8'))
|
||||||
current_value = int(data["Value"].decode("utf-8"))
|
new_value = current_value + 1
|
||||||
new_value = current_value + 1
|
|
||||||
logger.error(f"Failed to update counter after {max_retries} attempts")
|
logger.error(f"Failed to update counter after {max_retries} attempts")
|
||||||
return generate_fallback_id()
|
return generate_fallback_id()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -113,25 +113,6 @@ class DataMessageFactory:
|
|||||||
payload,
|
payload,
|
||||||
)
|
)
|
||||||
|
|
||||||
def safe_rsplit(self, fq_method_name, default="_"):
|
|
||||||
"""
|
|
||||||
Safely split a fully qualified method name into package, class, and method
|
|
||||||
|
|
||||||
Args:
|
|
||||||
fq_method_name (str): Fully qualified method name
|
|
||||||
default (str, optional): Default value for missing tokens. Defaults to ''.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
tuple: (package, class_name, method)
|
|
||||||
"""
|
|
||||||
if not fq_method_name:
|
|
||||||
return default, default, default
|
|
||||||
parts = fq_method_name.rsplit(".", 2)
|
|
||||||
# Pad with default values if insufficient tokens
|
|
||||||
while len(parts) < 3:
|
|
||||||
parts.insert(0, default)
|
|
||||||
return tuple(parts)
|
|
||||||
|
|
||||||
def create_rpc_message(
|
def create_rpc_message(
|
||||||
self,
|
self,
|
||||||
fq_method_name: str,
|
fq_method_name: str,
|
||||||
@@ -144,14 +125,9 @@ class DataMessageFactory:
|
|||||||
:param args: arguments whose serialized form will form the request body
|
:param args: arguments whose serialized form will form the request body
|
||||||
:return: DataMessage object
|
:return: DataMessage object
|
||||||
"""
|
"""
|
||||||
package, class_name, method = self.safe_rsplit(fq_method_name)
|
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(),
|
||||||
@@ -160,7 +136,7 @@ class DataMessageFactory:
|
|||||||
method,
|
method,
|
||||||
headers,
|
headers,
|
||||||
trace_info,
|
trace_info,
|
||||||
body,
|
python_type_to_json(args) if args else b"",
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -219,13 +195,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())
|
||||||
baos = BytesIO(bytearray(msg_length))
|
with BytesIO(bytearray(msg_length)) as baos:
|
||||||
baos.write(prolog_len)
|
baos.write(prolog_len)
|
||||||
baos.write(message.magic().encode("utf-8"))
|
baos.write(DataMessageMagicByte.HTTP_REQUEST.value.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:
|
||||||
@@ -238,7 +214,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(
|
||||||
r"([AR])"
|
f"{DataMessageMagicByte.HTTP_REQUEST.value}"
|
||||||
r"([0-9A-Fa-f]+)\|"
|
r"([0-9A-Fa-f]+)\|"
|
||||||
r"m=([^|]*)\|"
|
r"m=([^|]*)\|"
|
||||||
r"d=([^|]*)\|"
|
r"d=([^|]*)\|"
|
||||||
@@ -254,19 +230,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(2)
|
id_str = match.group(1)
|
||||||
method = match.group(3)
|
method = match.group(2)
|
||||||
domain = match.group(4)
|
domain = match.group(3)
|
||||||
path = match.group(5)
|
path = match.group(4)
|
||||||
headers_str = match.group(6)
|
headers_str = match.group(5)
|
||||||
trace_info_str = match.group(7)
|
trace_info_str = match.group(6)
|
||||||
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(
|
||||||
match.group(1), # Magic byte (R for RPC, A for HTTP)
|
DataMessageMagicByte.HTTP_REQUEST.value,
|
||||||
SnowflakeId.from_hex(id_str),
|
SnowflakeId.from_hex(id_str),
|
||||||
method,
|
method,
|
||||||
domain,
|
domain,
|
||||||
|
|||||||
@@ -13,17 +13,17 @@ __verbose__ = os.environ.get("AMQ_VERBOSE_LOGGING", "0") == "1"
|
|||||||
|
|
||||||
def get_context_info():
|
def get_context_info():
|
||||||
"""
|
"""
|
||||||
Retrieves the current_availability module, line number, and function name.
|
Retrieves the current module, line number, and function name.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A tuple containing (module name, line number, function name).
|
A tuple containing (module name, line number, function name).
|
||||||
Returns (None, None, None) if it fails to retrieve the information.
|
Returns (None, None, None) if it fails to retrieve the information.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Get the frame object for the current_availability function call
|
# Get the frame object for the current function call
|
||||||
frame = inspect.currentframe()
|
frame = inspect.currentframe()
|
||||||
if frame is not None:
|
if frame is not None:
|
||||||
# Get the frame object for the caller of the current_availability function
|
# Get the frame object for the caller of the current function
|
||||||
frame = frame.f_back.f_back
|
frame = frame.f_back.f_back
|
||||||
if frame:
|
if frame:
|
||||||
code_context = frame.f_code
|
code_context = frame.f_code
|
||||||
@@ -44,7 +44,7 @@ def get_context_info():
|
|||||||
|
|
||||||
def logging_error(msg: str, *args) -> None:
|
def logging_error(msg: str, *args) -> None:
|
||||||
"""
|
"""
|
||||||
Log an error message according to current_availability logging for 'ERROR' level.
|
Log an error message according to current logging for 'ERROR' level.
|
||||||
Add module name, line and function name where the error occurred, on single line.
|
Add module name, line and function name where the error occurred, on single line.
|
||||||
Args:
|
Args:
|
||||||
msg (str): The error message to log.
|
msg (str): The error message to log.
|
||||||
@@ -70,7 +70,7 @@ def logging_error(msg: str, *args) -> None:
|
|||||||
|
|
||||||
def logging_warning(msg: str, *args) -> None:
|
def logging_warning(msg: str, *args) -> None:
|
||||||
"""
|
"""
|
||||||
Log a warning message according to current_availability logging for 'WARN' level.
|
Log a warning message according to current logging for 'WARN' level.
|
||||||
Add module name, line and function name where the print occurred, on single line.
|
Add module name, line and function name where the print occurred, on single line.
|
||||||
Args:
|
Args:
|
||||||
msg (str): The warning message to log.
|
msg (str): The warning message to log.
|
||||||
@@ -91,7 +91,7 @@ def logging_warning(msg: str, *args) -> None:
|
|||||||
|
|
||||||
def logging_info(msg: str, *args) -> None:
|
def logging_info(msg: str, *args) -> None:
|
||||||
"""
|
"""
|
||||||
Log an info message according to current_availability logging for 'INFO' level.
|
Log an info message according to current logging for 'INFO' level.
|
||||||
Add module name, line and function name where the print occurred, on single line.
|
Add module name, line and function name where the print occurred, on single line.
|
||||||
Args:
|
Args:
|
||||||
msg (str): The info message to log.
|
msg (str): The info message to log.
|
||||||
@@ -112,7 +112,7 @@ def logging_info(msg: str, *args) -> None:
|
|||||||
|
|
||||||
def logging_debug(msg: str, *args) -> None:
|
def logging_debug(msg: str, *args) -> None:
|
||||||
"""
|
"""
|
||||||
Log a debug message according to current_availability logging for 'DEBUG' level.
|
Log a debug message according to current logging for 'DEBUG' level.
|
||||||
Add module name, line and function name where the print occurred, on single line.
|
Add module name, line and function name where the print occurred, on single line.
|
||||||
Args:
|
Args:
|
||||||
msg (str): The debug message to log.
|
msg (str): The debug message to log.
|
||||||
|
|||||||
@@ -39,14 +39,10 @@ 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 str(input_str[1:-1]).replace("\n", "").split(","):
|
for entry in input_str[1:-1].split(","):
|
||||||
if len(entry) > 1:
|
if len(entry) > 1:
|
||||||
if entry.startswith('"'):
|
key, value = entry.split("=")
|
||||||
key, value = entry.split(":")
|
map_[key] = value
|
||||||
map_[key.strip('"')] = value.strip('"')
|
|
||||||
else:
|
|
||||||
key, value = entry.split("=")
|
|
||||||
map_[key] = value
|
|
||||||
return map_
|
return map_
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ class AMQAdapter:
|
|||||||
|
|
||||||
:param config: config parser to read configuration values
|
:param config: config parser to read configuration values
|
||||||
"""
|
"""
|
||||||
self.generator_id = 0
|
self.generator_id = config.getint(
|
||||||
|
"CleverMicro-AMQ", self.PREFIX + "generator-id", fallback=164
|
||||||
|
)
|
||||||
self.service_name = config.get(
|
self.service_name = config.get(
|
||||||
"CleverMicro-AMQ",
|
"CleverMicro-AMQ",
|
||||||
self.PREFIX + "service-name",
|
self.PREFIX + "service-name",
|
||||||
@@ -43,72 +45,9 @@ class AMQAdapter:
|
|||||||
self.PREFIX + "default-backpressure-enabled",
|
self.PREFIX + "default-backpressure-enabled",
|
||||||
fallback=False,
|
fallback=False,
|
||||||
)
|
)
|
||||||
self.callbacks_enabled = config.getboolean(
|
|
||||||
"CleverMicro-AMQ",
|
|
||||||
self.PREFIX + "callbacks-enabled",
|
|
||||||
fallback=False,
|
|
||||||
)
|
|
||||||
# these 2 shall be provided by Docker swarm, format is a random alphanumeric string
|
# these 2 shall be provided by Docker swarm, format is a random alphanumeric string
|
||||||
self.swarm_service_id = os.getenv("SWARM_SERVICE_ID", default="clever-amqp-service")
|
self.swarm_service_id = os.getenv("SWARM_SERVICE_ID", default="clever-amqp-service")
|
||||||
self.swarm_task_id = os.getenv("SWARM_TASK_ID", default="python-adapter")
|
self.swarm_task_id = os.getenv("SWARM_TASK_ID", default="python-adapter")
|
||||||
#
|
|
||||||
# Consul related configuration
|
|
||||||
self.consul_port = (
|
|
||||||
int(
|
|
||||||
os.getenv(
|
|
||||||
"CONSUL_PORT",
|
|
||||||
config.get("CleverMicro-AMQ", self.PREFIX + "consul-port", fallback="8500"),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if os.getenv("CONSUL_PORT") is not None
|
|
||||||
else config.getint("CleverMicro-AMQ", self.PREFIX + "consul-port", fallback=8500)
|
|
||||||
)
|
|
||||||
self.consul_host = os.getenv(
|
|
||||||
"CONSUL_HOST",
|
|
||||||
config.get("CleverMicro-AMQ", self.PREFIX + "consul-host", fallback="consul"),
|
|
||||||
)
|
|
||||||
self.consul_max_retries = (
|
|
||||||
int(
|
|
||||||
os.getenv(
|
|
||||||
"CONSUL_MAX_RETRIES",
|
|
||||||
config.get("CleverMicro-AMQ", self.PREFIX + "consul-max-retries", fallback="5"),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if os.getenv("CONSUL_MAX_RETRIES") is not None
|
|
||||||
else config.getint("CleverMicro-AMQ", self.PREFIX + "consul-max-retries", fallback=5)
|
|
||||||
)
|
|
||||||
self.consul_initial_retry_delay = (
|
|
||||||
float(
|
|
||||||
os.getenv(
|
|
||||||
"CONSUL_INITIAL_RETRY_DELAY",
|
|
||||||
config.get(
|
|
||||||
"CleverMicro-AMQ",
|
|
||||||
self.PREFIX + "consul-initial-retry-delay",
|
|
||||||
fallback="0.2",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if os.getenv("CONSUL_INITIAL_RETRY_DELAY") is not None
|
|
||||||
else config.getfloat(
|
|
||||||
"CleverMicro-AMQ", self.PREFIX + "consul-initial-retry-delay", fallback=0.2
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self.consul_counter_key = (
|
|
||||||
os.getenv(
|
|
||||||
"CONSUL_COUNTER_KEY",
|
|
||||||
config.get(
|
|
||||||
"CleverMicro-AMQ",
|
|
||||||
self.PREFIX + "consul-counter-key",
|
|
||||||
fallback="service/ids/counter",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
if os.getenv("CONSUL_COUNTER_KEY") is not None
|
|
||||||
else config.get(
|
|
||||||
"CleverMicro-AMQ",
|
|
||||||
self.PREFIX + "consul-counter-key",
|
|
||||||
fallback="service/ids/counter",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def adapter_prefix(self) -> str:
|
def adapter_prefix(self) -> str:
|
||||||
return f"{self.service_name}-{self.generator_id}-"
|
return f"{self.service_name}-{self.generator_id}-"
|
||||||
@@ -168,7 +107,7 @@ class Backpressure:
|
|||||||
|
|
||||||
:param config: config parser to read configuration values
|
:param config: config parser to read configuration values
|
||||||
"""
|
"""
|
||||||
self.threshold_load = config.getint(
|
self.threshold_threads = config.getint(
|
||||||
"CleverMicro-AMQ",
|
"CleverMicro-AMQ",
|
||||||
self.PREFIX + "threshold",
|
self.PREFIX + "threshold",
|
||||||
fallback=int(os.getenv("PARALLEL_WORKERS", 1)),
|
fallback=int(os.getenv("PARALLEL_WORKERS", 1)),
|
||||||
@@ -190,10 +129,6 @@ class Backpressure:
|
|||||||
self.idle_duration = config.getint(
|
self.idle_duration = config.getint(
|
||||||
"CleverMicro-AMQ", self.PREFIX + "idle-duration", fallback=30
|
"CleverMicro-AMQ", self.PREFIX + "idle-duration", fallback=30
|
||||||
)
|
)
|
||||||
# Flag to enable/disable CPU monitoring
|
|
||||||
self.cpu_monitoring_enabled = config.getboolean(
|
|
||||||
"CleverMicro-AMQ", self.PREFIX + "cpu-monitoring-enabled", fallback=False
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class AMQConfiguration:
|
class AMQConfiguration:
|
||||||
@@ -208,7 +143,7 @@ class AMQConfiguration:
|
|||||||
config.read(config_file_name)
|
config.read(config_file_name)
|
||||||
else:
|
else:
|
||||||
# Get the directory of the caller (the module where this function is called)
|
# Get the directory of the caller (the module where this function is called)
|
||||||
caller_frame = inspect.stack()[1] # 0 is current_availability frame, 1 is caller
|
caller_frame = inspect.stack()[1] # 0 is current frame, 1 is caller
|
||||||
caller_module = inspect.getmodule(caller_frame[0])
|
caller_module = inspect.getmodule(caller_frame[0])
|
||||||
if caller_module:
|
if caller_module:
|
||||||
caller_dir = os.path.dirname(os.path.abspath(caller_module.__file__))
|
caller_dir = os.path.dirname(os.path.abspath(caller_module.__file__))
|
||||||
@@ -233,14 +168,3 @@ class AMQConfiguration:
|
|||||||
self.amq_adapter: AMQAdapter = AMQAdapter(config)
|
self.amq_adapter: AMQAdapter = AMQAdapter(config)
|
||||||
self.dispatch: Dispatch = Dispatch(config)
|
self.dispatch: Dispatch = Dispatch(config)
|
||||||
self.backpressure: Backpressure = Backpressure(config)
|
self.backpressure: Backpressure = Backpressure(config)
|
||||||
|
|
||||||
def get_unique_instance_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Get a unique instance ID for this AMQ service.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: A unique instance ID
|
|
||||||
"""
|
|
||||||
if self.instance_id == -1:
|
|
||||||
logging_warning(f"Generated unique instance ID: {self.instance_id}")
|
|
||||||
return self.instance_id
|
|
||||||
|
|||||||
@@ -14,13 +14,8 @@ cm.amq-adapter.generator-id=1
|
|||||||
cm.amq-adapter.route-mapping=
|
cm.amq-adapter.route-mapping=
|
||||||
# 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
|
||||||
cm.amq-adapter.require-authenticated-user=false
|
cm.amq-adapter.require-authenticated-user=false
|
||||||
# Consul configuration for CleverMicro AMQ Adapter
|
# Indicate if AMQ Adapter should use inbuilt backpressure monitor (True) or rely only on the service backpressure updates (False)
|
||||||
cm.amq-adapter.consul-host=consul
|
cm.amq-adapter.default-backpressure-enabled=false
|
||||||
cm.amq-adapter.consul-port=8500
|
|
||||||
cm.amq-adapter.consul-max-retries=5
|
|
||||||
cm.amq-adapter.consul-initial-retry-delay=0.2
|
|
||||||
cm.amq-adapter.consul-id-generator-key=services/ids/counter
|
|
||||||
cm.amq-adapter.callbacks-enabled=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
|
||||||
@@ -47,5 +42,3 @@ cm.backpressure.time-window=10
|
|||||||
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
|
||||||
cm.backpressure.cpu-idle-duration=30
|
cm.backpressure.cpu-idle-duration=30
|
||||||
# AMQ Adapter can monitor CPU usage and trigger backpressure alerts based on CPU usage. Enable this feature explicitly
|
|
||||||
cm.backpressure.cpu-monitoring-enabled=false
|
|
||||||
|
|||||||
@@ -291,21 +291,11 @@ class ServiceMessage:
|
|||||||
return self.message_type is not None
|
return self.message_type is not None
|
||||||
|
|
||||||
|
|
||||||
class ScalingRequestAlert(Enum):
|
|
||||||
OVERLOAD = "OVERLOAD"
|
|
||||||
IDLE = "IDLE"
|
|
||||||
UPDATE = "UPDATE"
|
|
||||||
SCALE_UP = "SCALE_UP"
|
|
||||||
SCALE_DOWN = "SCALE_DOWN"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ScalingRequest:
|
class ScalingRequest:
|
||||||
service_id: str
|
service_id: str
|
||||||
task_id: str
|
task_id: str
|
||||||
max_availability: int
|
|
||||||
current_availability: int
|
current_availability: int
|
||||||
request_type: ScalingRequestAlert
|
|
||||||
version: int = 1
|
version: int = 1
|
||||||
|
|
||||||
def to_json(self) -> str:
|
def to_json(self) -> str:
|
||||||
@@ -315,9 +305,7 @@ class ScalingRequest:
|
|||||||
"version": self.version,
|
"version": self.version,
|
||||||
"serviceId": self.service_id,
|
"serviceId": self.service_id,
|
||||||
"taskId": self.task_id,
|
"taskId": self.task_id,
|
||||||
"maxAvailability": self.max_availability,
|
|
||||||
"currentAvailability": self.current_availability,
|
"currentAvailability": self.current_availability,
|
||||||
"requestType": self.request_type.value, # Using .value for Enum serialization
|
|
||||||
},
|
},
|
||||||
indent=2,
|
indent=2,
|
||||||
)
|
)
|
||||||
@@ -329,9 +317,7 @@ class ScalingRequest:
|
|||||||
return cls(
|
return cls(
|
||||||
service_id=data["serviceId"],
|
service_id=data["serviceId"],
|
||||||
task_id=data["taskId"],
|
task_id=data["taskId"],
|
||||||
max_availability=data["maxAvailability"],
|
|
||||||
current_availability=data["currentAvailability"],
|
current_availability=data["currentAvailability"],
|
||||||
request_type=ScalingRequestAlert(data["requestType"]), # Convert string to Enum
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import aio_pika
|
import aio_pika
|
||||||
from aio_pika.abc import AbstractRobustExchange, AbstractRobustQueue
|
from aio_pika.abc import AbstractRobustExchange, AbstractRobustQueue
|
||||||
@@ -96,25 +95,19 @@ class RabbitMQClient:
|
|||||||
route.queue if route.queue else self.router.get_consume_queue_name()
|
route.queue if route.queue else self.router.get_consume_queue_name()
|
||||||
)
|
)
|
||||||
logging_info("RabbitMQClient register route: [%s]", route)
|
logging_info("RabbitMQClient register route: [%s]", route)
|
||||||
if self.amq_configuration.amq_adapter.callbacks_enabled:
|
queue: AbstractRobustQueue = await self.channel.declare_queue(
|
||||||
queue: Optional[AbstractRobustQueue] = await self.channel.declare_queue(
|
name=queue_name,
|
||||||
name=queue_name,
|
durable=True,
|
||||||
durable=True,
|
exclusive=False,
|
||||||
exclusive=False,
|
auto_delete=False,
|
||||||
auto_delete=False,
|
arguments=queue_args,
|
||||||
arguments=queue_args,
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
queue = None
|
|
||||||
_exchange_name: str = route.exchange if route.exchange else AMQ_RPC_EXCHANGE
|
_exchange_name: str = route.exchange if route.exchange else AMQ_RPC_EXCHANGE
|
||||||
exchange: AbstractRobustExchange = await self.channel.declare_exchange(
|
exchange: AbstractRobustExchange = await self.channel.declare_exchange(
|
||||||
name=_exchange_name, type=ExchangeType.topic
|
name=_exchange_name, type=ExchangeType.topic
|
||||||
)
|
)
|
||||||
if self.amq_configuration.amq_adapter.callbacks_enabled:
|
await queue.bind(exchange, route.key)
|
||||||
await queue.bind(exchange, route.key)
|
_c_tag = await queue.consume(callback, no_ack=False)
|
||||||
_c_tag = await queue.consume(callback, no_ack=False)
|
|
||||||
else:
|
|
||||||
_c_tag = None
|
|
||||||
# Register the route with the router and publish its detail to other adapters
|
# Register the route with the router and publish its detail to other adapters
|
||||||
await self.router.register_route(
|
await self.router.register_route(
|
||||||
AMQRoute(
|
AMQRoute(
|
||||||
@@ -151,9 +144,6 @@ class RabbitMQClient:
|
|||||||
Set up the RabbitMQ connection
|
Set up the RabbitMQ connection
|
||||||
:return: nothing, it applies changes to instance variables
|
:return: nothing, it applies changes to instance variables
|
||||||
"""
|
"""
|
||||||
logging.info(
|
|
||||||
f"RabbitMQClient.setupAMQ, connecting to RabbitMQ at {self.amq_configuration.dispatch.amq_host}:{self.amq_configuration.dispatch.amq_port}, loop={loop}"
|
|
||||||
)
|
|
||||||
self.connection = await aio_pika.connect_robust(
|
self.connection = await aio_pika.connect_robust(
|
||||||
host=self.amq_configuration.dispatch.amq_host,
|
host=self.amq_configuration.dispatch.amq_host,
|
||||||
port=self.amq_configuration.dispatch.amq_port,
|
port=self.amq_configuration.dispatch.amq_port,
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
import logging
|
|
||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
from amqp.rabbitmq.user_management_service_client import (
|
|
||||||
UserManagementServiceClient,
|
|
||||||
UserManagementServiceException,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def get_user_info_from_token(
|
|
||||||
client: UserManagementServiceClient, token: str
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Get user information from a JWT token.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
client: User Management Service client
|
|
||||||
token: JWT token
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
User information
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
UserManagementServiceException: If an error occurs
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
# Validate the token
|
|
||||||
token_info = await client.validate_token(token)
|
|
||||||
|
|
||||||
# Extract user ID from token info
|
|
||||||
user_id = None
|
|
||||||
for item in token_info:
|
|
||||||
if isinstance(item, dict) and "sub" in item:
|
|
||||||
user_id = item["sub"]
|
|
||||||
break
|
|
||||||
|
|
||||||
if not user_id:
|
|
||||||
raise UserManagementServiceException(
|
|
||||||
"cleverthis.clevermicro.auth.invalid_token",
|
|
||||||
"Token does not contain user ID (sub claim)",
|
|
||||||
{},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Query user information
|
|
||||||
user_info = await client.query_user_by_id(user_id)
|
|
||||||
return user_info
|
|
||||||
|
|
||||||
except UserManagementServiceException as e:
|
|
||||||
logging.error(f"User Management Service error: {e.exception_type}: {e.message}")
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Unexpected error: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
# Initialize configuration
|
|
||||||
config = AMQConfiguration("")
|
|
||||||
|
|
||||||
# Create client
|
|
||||||
client = UserManagementServiceClient(config)
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Sample JWT token (replace with actual token)
|
|
||||||
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
|
|
||||||
|
|
||||||
# Get user information
|
|
||||||
user_info = await get_user_info_from_token(client, token)
|
|
||||||
print(f"User information: {user_info}")
|
|
||||||
|
|
||||||
except UserManagementServiceException as e:
|
|
||||||
print(f"Error: {e.exception_type}: {e.message}")
|
|
||||||
if e.additional_info:
|
|
||||||
print(f"Additional info: {e.additional_info}")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Unexpected error: {e}")
|
|
||||||
finally:
|
|
||||||
# Close client
|
|
||||||
await client.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
@@ -1,260 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
import uuid
|
|
||||||
from typing import Any, Dict, List, Optional
|
|
||||||
|
|
||||||
import aio_pika
|
|
||||||
from aio_pika import Message
|
|
||||||
from aio_pika.abc import AbstractIncomingMessage, AbstractRobustChannel, AbstractRobustConnection
|
|
||||||
|
|
||||||
from amqp.adapter.logging_utils import logging_debug, logging_error, logging_info
|
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
|
|
||||||
|
|
||||||
class UserManagementServiceException(Exception):
|
|
||||||
"""Exception raised for errors in the User Management Service."""
|
|
||||||
|
|
||||||
def __init__(self, exception_type: str, message: str, additional_info: Dict[str, Any] = None):
|
|
||||||
self.exception_type = exception_type
|
|
||||||
self.message = message
|
|
||||||
self.additional_info = additional_info or {}
|
|
||||||
super().__init__(f"{exception_type}: {message}")
|
|
||||||
|
|
||||||
|
|
||||||
class UserManagementServiceClient:
|
|
||||||
"""
|
|
||||||
Client for the User Management Service that communicates over RabbitMQ.
|
|
||||||
Supports operations for token, user, group, tenant, and permission services.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Exchange name for User Management Service
|
|
||||||
EXCHANGE_NAME = "cleverthis.clevermicro.auth.endpoints"
|
|
||||||
|
|
||||||
# Service routing keys
|
|
||||||
TOKEN_SERVICE = "token-service-v1"
|
|
||||||
USER_SERVICE = "user-service-v1"
|
|
||||||
GROUP_SERVICE = "group-service-v1"
|
|
||||||
TENANT_SERVICE = "tenant-service-v1"
|
|
||||||
PERMISSION_SERVICE = "permission-service-v1"
|
|
||||||
|
|
||||||
# Response queue name
|
|
||||||
RESPONSE_QUEUE_NAME = "user-management-service-cleverswarm"
|
|
||||||
|
|
||||||
def __init__(self, configuration: AMQConfiguration, loop: asyncio.AbstractEventLoop = None):
|
|
||||||
"""
|
|
||||||
Initialize the User Management Service client.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
configuration: AMQ configuration
|
|
||||||
loop: Event loop to use (optional)
|
|
||||||
"""
|
|
||||||
self.amq_configuration = configuration
|
|
||||||
self.loop = loop or asyncio.get_event_loop()
|
|
||||||
self.connection: Optional[AbstractRobustConnection] = None
|
|
||||||
self.channel: Optional[AbstractRobustChannel] = None
|
|
||||||
self.exchange: Optional[aio_pika.RobustExchange] = None
|
|
||||||
self.response_queue: Optional[aio_pika.RobustQueue] = None
|
|
||||||
self.response_consumer_tag: Optional[str] = None
|
|
||||||
self.futures: Dict[str, asyncio.Future] = {}
|
|
||||||
self.initialized = False
|
|
||||||
|
|
||||||
async def initialize(self) -> None:
|
|
||||||
"""Initialize the RabbitMQ connection and set up the exchange and response queue."""
|
|
||||||
if self.initialized:
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Connect to RabbitMQ
|
|
||||||
self.connection = await aio_pika.connect_robust(
|
|
||||||
host=self.amq_configuration.dispatch.amq_host,
|
|
||||||
port=self.amq_configuration.dispatch.amq_port,
|
|
||||||
login=self.amq_configuration.dispatch.rabbit_mq_user,
|
|
||||||
password=self.amq_configuration.dispatch.rabbit_mq_password,
|
|
||||||
loop=self.loop,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create channel
|
|
||||||
self.channel = await self.connection.channel()
|
|
||||||
await self.channel.set_qos(prefetch_count=1)
|
|
||||||
|
|
||||||
# Declare exchange
|
|
||||||
self.exchange = await self.channel.declare_exchange(
|
|
||||||
name=self.EXCHANGE_NAME,
|
|
||||||
type=aio_pika.ExchangeType.TOPIC,
|
|
||||||
durable=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Declare response queue
|
|
||||||
self.response_queue = await self.channel.declare_queue(
|
|
||||||
name=self.RESPONSE_QUEUE_NAME,
|
|
||||||
durable=True,
|
|
||||||
exclusive=False,
|
|
||||||
auto_delete=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Start consuming responses
|
|
||||||
self.response_consumer_tag = await self.response_queue.consume(
|
|
||||||
self._on_response_callback,
|
|
||||||
no_ack=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.initialized = True
|
|
||||||
logging_info("User Management Service client initialized")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"Failed to initialize User Management Service client: {e}")
|
|
||||||
if self.connection and not self.connection.is_closed:
|
|
||||||
await self.connection.close()
|
|
||||||
raise
|
|
||||||
|
|
||||||
async def _on_response_callback(self, message: AbstractIncomingMessage) -> None:
|
|
||||||
"""
|
|
||||||
Handle responses from the User Management Service.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
message: The incoming message
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
correlation_id = message.correlation_id
|
|
||||||
if not correlation_id:
|
|
||||||
logging_error("Received response without correlation ID")
|
|
||||||
await message.ack()
|
|
||||||
return
|
|
||||||
|
|
||||||
future = self.futures.get(correlation_id)
|
|
||||||
if not future:
|
|
||||||
logging_error(f"No pending request found for correlation ID: {correlation_id}")
|
|
||||||
await message.ack()
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
response_data = json.loads(message.body.decode('utf-8'))
|
|
||||||
logging_debug(f"Received response: {response_data}")
|
|
||||||
future.set_result(response_data)
|
|
||||||
except Exception as e:
|
|
||||||
future.set_exception(e)
|
|
||||||
|
|
||||||
await message.ack()
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"Error processing response: {e}")
|
|
||||||
await message.ack()
|
|
||||||
|
|
||||||
async def close(self) -> None:
|
|
||||||
"""Close the RabbitMQ connection."""
|
|
||||||
if self.connection and not self.connection.is_closed:
|
|
||||||
await self.connection.close()
|
|
||||||
self.initialized = False
|
|
||||||
logging_info("User Management Service client closed")
|
|
||||||
|
|
||||||
async def call_service(
|
|
||||||
self, service: str, method: str, args: Dict[str, Any] = None
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Call a method on a User Management Service.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
service: Service routing key (e.g., 'token-service-v1')
|
|
||||||
method: Method name to call
|
|
||||||
args: Method arguments
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Response data
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
UserManagementServiceException: If the service returns an error
|
|
||||||
"""
|
|
||||||
if not self.initialized:
|
|
||||||
await self.initialize()
|
|
||||||
|
|
||||||
correlation_id = str(uuid.uuid4())
|
|
||||||
future = self.loop.create_future()
|
|
||||||
self.futures[correlation_id] = future
|
|
||||||
|
|
||||||
request_data = {
|
|
||||||
"method": method,
|
|
||||||
"args": args or {},
|
|
||||||
}
|
|
||||||
|
|
||||||
message = Message(
|
|
||||||
body=json.dumps(request_data).encode('utf-8'),
|
|
||||||
content_type='application/json',
|
|
||||||
correlation_id=correlation_id,
|
|
||||||
reply_to=self.RESPONSE_QUEUE_NAME,
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
await self.exchange.publish(message, routing_key=service)
|
|
||||||
logging_info(f"Sent request to {service}.{method}: {args}")
|
|
||||||
|
|
||||||
# Wait for response
|
|
||||||
response = await future
|
|
||||||
|
|
||||||
# Process response
|
|
||||||
if response.get("type") == "OK":
|
|
||||||
return response.get("result", [])
|
|
||||||
else:
|
|
||||||
raise UserManagementServiceException(
|
|
||||||
response.get("exception", "unknown_error"),
|
|
||||||
response.get("message", "Unknown error"),
|
|
||||||
response.get("additional_info", {})
|
|
||||||
)
|
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
raise
|
|
||||||
except UserManagementServiceException:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"Error calling service {service}.{method}: {e}")
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
self.futures.pop(correlation_id, None)
|
|
||||||
|
|
||||||
async def validate_token(self, token: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Validate a JWT token.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
token: JWT token to validate
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Token information
|
|
||||||
"""
|
|
||||||
return await self.call_service(
|
|
||||||
service=self.TOKEN_SERVICE,
|
|
||||||
method="validateToken",
|
|
||||||
args={"token": token}
|
|
||||||
)
|
|
||||||
|
|
||||||
async def query_user_by_id(self, user_id: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Query user information by user ID.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
user_id: User ID
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
User information
|
|
||||||
"""
|
|
||||||
return await self.call_service(
|
|
||||||
service=self.USER_SERVICE,
|
|
||||||
method="queryUserById",
|
|
||||||
args={"userId": user_id}
|
|
||||||
)
|
|
||||||
|
|
||||||
async def list_users_in_group(self, group_id: str) -> List[Dict[str, Any]]:
|
|
||||||
"""
|
|
||||||
List users in a group.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
group_id: Group ID
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of users
|
|
||||||
"""
|
|
||||||
return await self.call_service(
|
|
||||||
service=self.GROUP_SERVICE,
|
|
||||||
method="listUsersInGroup",
|
|
||||||
args={"groupId": group_id}
|
|
||||||
)
|
|
||||||
@@ -50,12 +50,7 @@ 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]:
|
||||||
_route: Optional[AMQRoute] = self.route_database.find_route_by_service(service_name)
|
return 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:
|
||||||
|
|||||||
@@ -174,12 +174,12 @@ class RouterConsumer(RouterProducer):
|
|||||||
)
|
)
|
||||||
if not await self.publish_service_message(service_message, self.cm_response_exchange):
|
if not await self.publish_service_message(service_message, self.cm_response_exchange):
|
||||||
logging_warning(
|
logging_warning(
|
||||||
"RouterConsumer couldn't remove current_availability route: %s, channel not open.",
|
"RouterConsumer couldn't remove current route: %s, channel not open.",
|
||||||
route,
|
route,
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as ioe:
|
except Exception as ioe:
|
||||||
logging_error("RouterConsumer failed remove current_availability route: %s", ioe)
|
logging_error("RouterConsumer failed remove current route: %s", ioe)
|
||||||
|
|
||||||
result: bool = self.remove_route(route)
|
result: bool = self.remove_route(route)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class RouterProducer(RouterBase):
|
|||||||
name=CM_REQUEST_EXCHANGE, type=ExchangeType.fanout
|
name=CM_REQUEST_EXCHANGE, type=ExchangeType.fanout
|
||||||
)
|
)
|
||||||
#
|
#
|
||||||
# 2. declare ResponseExchange where to publish current_availability RoutingData (response to
|
# 2. declare ResponseExchange where to publish current RoutingData (response to
|
||||||
# a RoutingData request)
|
# a RoutingData request)
|
||||||
self.cm_response_exchange = await self.channel.declare_exchange(
|
self.cm_response_exchange = await self.channel.declare_exchange(
|
||||||
name=CM_RESPONSE_EXCHANGE, type=ExchangeType.fanout
|
name=CM_RESPONSE_EXCHANGE, type=ExchangeType.fanout
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import importlib
|
|
||||||
import inspect
|
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from asyncio import AbstractEventLoop, Future
|
from asyncio import AbstractEventLoop, Future
|
||||||
from typing import Any, Dict
|
from typing import Dict
|
||||||
|
|
||||||
from aio_pika import Message
|
from aio_pika import Message
|
||||||
from aio_pika.abc import AbstractIncomingMessage, AbstractRobustExchange
|
from aio_pika.abc import AbstractIncomingMessage, AbstractRobustExchange
|
||||||
from opentelemetry import trace
|
from opentelemetry import trace
|
||||||
from opentelemetry.trace import Tracer, TraceState
|
from opentelemetry.trace import Tracer, TraceState
|
||||||
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
|
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
|
||||||
from pika.exchange_type import ExchangeType
|
|
||||||
|
|
||||||
from amqp.adapter.backpressure_handler import BackpressureHandler
|
from amqp.adapter.backpressure_handler import BackpressureHandler
|
||||||
from amqp.adapter.cleverthis_service_adapter import AMQMessage, CleverThisServiceAdapter
|
from amqp.adapter.cleverthis_service_adapter import AMQMessage, CleverThisServiceAdapter
|
||||||
@@ -20,7 +17,7 @@ from amqp.adapter.data_message_factory import DataMessageFactory
|
|||||||
from amqp.adapter.data_response_factory import DataResponseFactory
|
from amqp.adapter.data_response_factory import DataResponseFactory
|
||||||
from amqp.adapter.file_handler import FileHandler, StreamingFileHandler
|
from amqp.adapter.file_handler import FileHandler, StreamingFileHandler
|
||||||
from amqp.adapter.logging_utils import logging_debug, logging_error, logging_info
|
from amqp.adapter.logging_utils import logging_debug, logging_error, logging_info
|
||||||
from amqp.adapter.serializer import map_as_string, parse_map_string
|
from amqp.adapter.serializer import map_as_string
|
||||||
from amqp.adapter.service_message_factory import ServiceMessageFactory
|
from amqp.adapter.service_message_factory import ServiceMessageFactory
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
from amqp.model.model import (
|
from amqp.model.model import (
|
||||||
@@ -30,7 +27,6 @@ from amqp.model.model import (
|
|||||||
MultipartDataMessage,
|
MultipartDataMessage,
|
||||||
)
|
)
|
||||||
from amqp.rabbitmq.rabbit_mq_client import RabbitMQClient
|
from amqp.rabbitmq.rabbit_mq_client import RabbitMQClient
|
||||||
from amqp.router.utils import sanitize_as_rabbitmq_name
|
|
||||||
|
|
||||||
|
|
||||||
def collect_trace_info():
|
def collect_trace_info():
|
||||||
@@ -49,75 +45,6 @@ def set_text(carrier, key, value):
|
|||||||
carrier[key] = value
|
carrier[key] = value
|
||||||
|
|
||||||
|
|
||||||
async def invoke_command(
|
|
||||||
package_name: str, class_name: str, function_name: str, msg_id: str = "", kwargs=None
|
|
||||||
) -> Any:
|
|
||||||
if kwargs is None:
|
|
||||||
kwargs = {}
|
|
||||||
try:
|
|
||||||
# 1. Dynamically import the package
|
|
||||||
logging_info(f"Attempting to import package: {package_name}")
|
|
||||||
package = importlib.import_module(package_name)
|
|
||||||
logging_info(f"Package '{package_name}' imported successfully.")
|
|
||||||
|
|
||||||
target_callable = None
|
|
||||||
|
|
||||||
if class_name == "__global__":
|
|
||||||
# 2. Handle global function invocation
|
|
||||||
logging_info(f"Attempting to find global function: {function_name} in {package_name}")
|
|
||||||
target_callable = getattr(package, function_name)
|
|
||||||
logging_info(f"Global function '{function_name}' found.")
|
|
||||||
else:
|
|
||||||
# 3. Handle class method invocation
|
|
||||||
logging_info(f"Attempting to find class: {class_name} in {package_name}")
|
|
||||||
target_class = getattr(package, class_name)
|
|
||||||
logging_info(f"Class '{class_name}' found. Instantiating...")
|
|
||||||
|
|
||||||
# Instantiate the class. Assuming a default constructor or one that doesn't
|
|
||||||
# require arguments from kwargs for initialization.
|
|
||||||
# If the class __init__ needs specific arguments, this part might need adjustment
|
|
||||||
# based on how those arguments are provided.
|
|
||||||
instance = target_class()
|
|
||||||
logging_info(f"Instance of '{class_name}' created.")
|
|
||||||
|
|
||||||
logging_info(f"Attempting to find method: {function_name} in class {class_name}")
|
|
||||||
target_callable = getattr(instance, function_name)
|
|
||||||
logging_info(f"Method '{function_name}' found.")
|
|
||||||
|
|
||||||
# 4. Invoke the function/method
|
|
||||||
logging_info(f"Invoking '{function_name}' with arguments: {kwargs}")
|
|
||||||
if inspect.iscoroutinefunction(target_callable):
|
|
||||||
result = await target_callable(**kwargs)
|
|
||||||
logging_info(f"Asynchronous function '{function_name}' invoked. Result: {result}")
|
|
||||||
else:
|
|
||||||
result = target_callable(**kwargs)
|
|
||||||
logging_info(f"Synchronous function '{function_name}' invoked. Result: {result}")
|
|
||||||
|
|
||||||
response: DataResponse = DataResponseFactory.of(
|
|
||||||
id=msg_id,
|
|
||||||
response_code=200,
|
|
||||||
content_type="text/plain",
|
|
||||||
body=result.encode("utf-8"),
|
|
||||||
trace_info=collect_trace_info(),
|
|
||||||
error=None,
|
|
||||||
error_cause=None,
|
|
||||||
)
|
|
||||||
response._magic = DataMessageMagicByte.RPC_REQUEST.value
|
|
||||||
return response
|
|
||||||
|
|
||||||
except ModuleNotFoundError as e:
|
|
||||||
logging_error(f"Error: Package '{package_name}' not found. {e}")
|
|
||||||
raise
|
|
||||||
except AttributeError as e:
|
|
||||||
logging_error(
|
|
||||||
f"Error: Class '{class_name}' or function/method '{function_name}' not found in '{package_name}'. {e}"
|
|
||||||
)
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"An unexpected error occurred during invocation: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
class DataMessageHandler:
|
class DataMessageHandler:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -170,8 +97,6 @@ class DataMessageHandler:
|
|||||||
self.ensure_trace_info(amq_message)
|
self.ensure_trace_info(amq_message)
|
||||||
if amq_message.magic() == DataMessageMagicByte.HTTP_REQUEST.value:
|
if amq_message.magic() == DataMessageMagicByte.HTTP_REQUEST.value:
|
||||||
await self.run_http_request_magic(message, amq_message, self.loop)
|
await self.run_http_request_magic(message, amq_message, self.loop)
|
||||||
elif amq_message.magic() == DataMessageMagicByte.RPC_REQUEST.value:
|
|
||||||
await self.run_rpc_request(message, amq_message, self.loop)
|
|
||||||
else:
|
else:
|
||||||
logging_error(
|
logging_error(
|
||||||
"Unknown MAGIC value: [%s], don't know how to interpret the message",
|
"Unknown MAGIC value: [%s], don't know how to interpret the message",
|
||||||
@@ -198,11 +123,10 @@ class DataMessageHandler:
|
|||||||
:param loop: Event loop
|
:param loop: Event loop
|
||||||
:return: DataResponse
|
:return: DataResponse
|
||||||
"""
|
"""
|
||||||
# Increment the counter for parallel executions and check resource availability
|
# Increment the counter for parallel executions
|
||||||
self.backpressure_handler.increase_current_load()
|
await self.backpressure_handler.increase_parallel_executions()
|
||||||
await self.backpressure_handler.check_overload_condition()
|
|
||||||
logging_info(
|
logging_info(
|
||||||
f"PARALLEL: Current Capacity [{self.backpressure_handler.current_availability}] of [{self.backpressure_handler.max_availability}]"
|
f"PARALLEL: Current Capacity [{self.backpressure_handler.current_parallel_executions}] of [{self.backpressure_handler.parallel_workers}]"
|
||||||
)
|
)
|
||||||
if message.reply_to:
|
if message.reply_to:
|
||||||
self.reply_to = message.reply_to
|
self.reply_to = message.reply_to
|
||||||
@@ -229,97 +153,12 @@ class DataMessageHandler:
|
|||||||
logging_error(f"Deleting file {filename}: {e}")
|
logging_error(f"Deleting file {filename}: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging_error(f"Request handler: {e}")
|
logging_error(f"Request handler: {e}")
|
||||||
|
# recover availability
|
||||||
self.backpressure_handler.decrease_current_load()
|
await self.backpressure_handler.decrease_parallel_executions()
|
||||||
|
|
||||||
async def run_rpc_request(
|
|
||||||
self,
|
|
||||||
message: AbstractIncomingMessage,
|
|
||||||
amq_message: DataMessage,
|
|
||||||
loop: AbstractEventLoop,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Synchronous version of on_message method. This is a wrapper around the async on_message method.
|
|
||||||
It also counts parallel executions and handles backpressure.
|
|
||||||
:param message: RabbitMQ raw message
|
|
||||||
:param amq_message: DataMessage
|
|
||||||
:param loop: Event loop
|
|
||||||
:return: DataResponse
|
|
||||||
"""
|
|
||||||
# Increment the counter for parallel executions and check resource availability
|
|
||||||
self.backpressure_handler.increase_current_load()
|
|
||||||
await self.backpressure_handler.check_overload_condition()
|
|
||||||
logging_info(
|
|
||||||
f"PARALLEL: Current Capacity [{self.backpressure_handler.current_availability}] of [{self.backpressure_handler.max_availability}]"
|
|
||||||
)
|
|
||||||
if message.reply_to:
|
|
||||||
self.reply_to = message.reply_to
|
|
||||||
|
|
||||||
try:
|
|
||||||
_a_message = AMQMessage(amq_message, self.rabbit_mq_client.connection)
|
|
||||||
_response: DataResponse = await self.on_rpc(_a_message)
|
|
||||||
logging_debug(f"Result in thread: {_response}")
|
|
||||||
try:
|
|
||||||
await self.send_reply(message.reply_to, _response)
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"Processing message: {e}")
|
|
||||||
asyncio.run_coroutine_threadsafe(message.nack(requeue=False), loop=loop)
|
|
||||||
|
|
||||||
if isinstance(amq_message, MultipartDataMessage):
|
|
||||||
# remove the downloaded files from the output_dir
|
|
||||||
for file_id, filename in amq_message.extra_data.items():
|
|
||||||
try:
|
|
||||||
logging_info(
|
|
||||||
f"[{message.delivery_tag}] Deleting file: {file_id} in {filename}"
|
|
||||||
)
|
|
||||||
os.remove(filename)
|
|
||||||
except OSError as e:
|
|
||||||
logging_error(f"Deleting file {filename}: {e}")
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"Request handler: {e}")
|
|
||||||
|
|
||||||
self.backpressure_handler.decrease_current_load()
|
|
||||||
|
|
||||||
async def on_rpc(self, a_message: AMQMessage) -> DataResponse:
|
|
||||||
"""
|
|
||||||
Dynamically invokes a function or a class method from a specified package.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
a_message (AMQMessage): The message containing the package, class, and function details.
|
|
||||||
Args encoded in the AMQMessage:
|
|
||||||
package_name (str): The name of the Python package to import.
|
|
||||||
class_name (str): The name of the class within the package.
|
|
||||||
Use '__global__' if the function_name refers to a global function
|
|
||||||
in the package.
|
|
||||||
function_name (str): The name of the function or method to invoke.
|
|
||||||
kwargs (Dict[str, Any]): A dictionary of keyword arguments to pass to the function/method (in the body).
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Any: The return value of the invoked function or method.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
ModuleNotFoundError: If the specified package cannot be imported.
|
|
||||||
AttributeError: If the specified class or function/method does not exist.
|
|
||||||
Exception: For any other errors during invocation.
|
|
||||||
"""
|
|
||||||
package_name: str = a_message.method()
|
|
||||||
class_name: str = a_message.domain()
|
|
||||||
function_name: str = a_message.path()
|
|
||||||
kwargs: Dict[str, Any] = parse_map_string(a_message._base64body.decode("utf-8"))
|
|
||||||
|
|
||||||
return await invoke_command(
|
|
||||||
package_name, class_name, function_name, a_message.id().as_string(), kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
async def reconstitute_data_message(
|
async def reconstitute_data_message(
|
||||||
self, message: AbstractIncomingMessage
|
self, message: AbstractIncomingMessage
|
||||||
) -> DataMessage | None:
|
) -> DataMessage | None:
|
||||||
"""
|
|
||||||
DataMessage may be serialized as string in separate queue (addressing == 1) or be a MultiPart message with
|
|
||||||
files streamed over individual queues, one per file.
|
|
||||||
This function uses 'addressing' mode from message headers to determine how to deserialize (reconstitute)
|
|
||||||
the original message.
|
|
||||||
"""
|
|
||||||
amq_message: DataMessage | None = None
|
amq_message: DataMessage | None = None
|
||||||
delivery_tag = message.delivery_tag
|
delivery_tag = message.delivery_tag
|
||||||
addressing: int = message.headers.get("clevermicro.addressing", 0)
|
addressing: int = message.headers.get("clevermicro.addressing", 0)
|
||||||
@@ -367,12 +206,12 @@ class DataMessageHandler:
|
|||||||
# map_as_string(amq_message.trace_info()), context_with_span, context)
|
# map_as_string(amq_message.trace_info()), context_with_span, context)
|
||||||
|
|
||||||
def record_duration(self, start, delivery_tag):
|
def record_duration(self, start, delivery_tag):
|
||||||
global last_backpressure_event_time
|
global last_data_message_time
|
||||||
last_backpressure_event_time = time.time()
|
last_data_message_time = time.time()
|
||||||
logging_info(
|
logging_info(
|
||||||
"######[%s] Done, single ACK: %sms.",
|
"######[%s] Done, single ACK: %sms.",
|
||||||
delivery_tag,
|
delivery_tag,
|
||||||
last_backpressure_event_time - start,
|
last_data_message_time - start,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def send_reply(self, reply_to: str, response: DataResponse):
|
async def send_reply(self, reply_to: str, response: DataResponse):
|
||||||
@@ -406,8 +245,8 @@ class DataMessageHandler:
|
|||||||
|
|
||||||
async def reply_received_callback(self, message: AbstractIncomingMessage):
|
async def reply_received_callback(self, message: AbstractIncomingMessage):
|
||||||
"""
|
"""
|
||||||
The handler for reply for DataMessage that we sent out earlier -
|
The reply for DataMessage that we sent out earlier - must match the received response with
|
||||||
the code matches the received response with the original request and passes the response to the caller.
|
the original request and respond to the caller.
|
||||||
:param message:
|
:param message:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
@@ -436,96 +275,3 @@ class DataMessageHandler:
|
|||||||
future.set_result(response.body())
|
future.set_result(response.body())
|
||||||
|
|
||||||
await message.ack(multiple=False)
|
await message.ack(multiple=False)
|
||||||
|
|
||||||
async def send_rpc(self, route, message: DataMessage) -> Future:
|
|
||||||
"""
|
|
||||||
Sends an RPC message to the specified route and returns a Future that will be completed
|
|
||||||
when the response is received.
|
|
||||||
|
|
||||||
:param route: AMQRoute object containing exchange and routing information
|
|
||||||
:param message: DataMessage to be sent
|
|
||||||
:return: Future that will be completed with the response payload
|
|
||||||
"""
|
|
||||||
# Create a Future to be completed when the response is received
|
|
||||||
future = asyncio.Future()
|
|
||||||
|
|
||||||
# Store the future in the outstanding dictionary using the message ID as the key
|
|
||||||
message_id = message.id().as_string()
|
|
||||||
self.outstanding[message_id] = future
|
|
||||||
|
|
||||||
# Create a Pika message with the correlation ID set to the message ID
|
|
||||||
pika_message = Message(
|
|
||||||
body=DataMessageFactory.serialize(message),
|
|
||||||
correlation_id=message_id,
|
|
||||||
reply_to=self.rabbit_mq_client.router.get_reply_to_queue_name(),
|
|
||||||
content_type=(
|
|
||||||
message.content_type()[0]
|
|
||||||
if isinstance(message.content_type(), list)
|
|
||||||
else message.content_type()
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get the exchange from the route
|
|
||||||
rpc_exchange_name = route.exchange
|
|
||||||
exchange: AbstractRobustExchange = await self.rabbit_mq_client.channel.declare_exchange(
|
|
||||||
name=rpc_exchange_name, type=ExchangeType.topic
|
|
||||||
)
|
|
||||||
# Publish the message to the exchange with the component name as the routing key
|
|
||||||
await exchange.publish(
|
|
||||||
message=pika_message, routing_key=sanitize_as_rabbitmq_name(route.component_name)
|
|
||||||
)
|
|
||||||
|
|
||||||
logging_debug(
|
|
||||||
"###### RPC Request Published to exchg:(%s) routing_key=%s, msgId=%s",
|
|
||||||
rpc_exchange_name,
|
|
||||||
route.component_name,
|
|
||||||
message_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
return future
|
|
||||||
|
|
||||||
async def send_command(self, route, message: DataMessage) -> bool:
|
|
||||||
"""
|
|
||||||
Sends an RPC message to the specified route and returns a Future that will be completed
|
|
||||||
when the response is received.
|
|
||||||
|
|
||||||
:param route: AMQRoute object containing exchange and routing information
|
|
||||||
:param message: DataMessage to be sent
|
|
||||||
: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
|
|
||||||
pika_message = Message(
|
|
||||||
body=rmq_body,
|
|
||||||
correlation_id=cid,
|
|
||||||
content_type=ctype,
|
|
||||||
)
|
|
||||||
print(f"SENDING RPC COMMAND: {pika_message.body} with ID: {message.id().as_string()}")
|
|
||||||
|
|
||||||
# Get the exchange from the route
|
|
||||||
rpc_exchange_name = route.exchange
|
|
||||||
exchange: AbstractRobustExchange = await self.rabbit_mq_client.channel.declare_exchange(
|
|
||||||
name=rpc_exchange_name, type=ExchangeType.topic
|
|
||||||
)
|
|
||||||
# Publish the message to the exchange with the component name as the routing key
|
|
||||||
await exchange.publish(
|
|
||||||
message=pika_message, routing_key=sanitize_as_rabbitmq_name(route.component_name)
|
|
||||||
)
|
|
||||||
|
|
||||||
logging_info(
|
|
||||||
"###### RPC Request Published to exchg:(%s) routing_key=%s, msgId=%s",
|
|
||||||
rpc_exchange_name,
|
|
||||||
route.component_name,
|
|
||||||
message.id().as_string(),
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|||||||
+6
-118
@@ -4,15 +4,14 @@ import logging.config
|
|||||||
import os
|
import os
|
||||||
from asyncio import Future
|
from asyncio import Future
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from typing import AsyncIterator, Optional
|
from typing import Optional
|
||||||
|
|
||||||
from aio_pika.abc import AbstractRobustExchange
|
from aio_pika.abc import AbstractRobustExchange
|
||||||
|
|
||||||
from amqp.adapter.amq_route_factory import AMQRouteFactory
|
from amqp.adapter.amq_route_factory import AMQRouteFactory
|
||||||
from amqp.adapter.backpressure_handler import BackpressureHandler
|
from amqp.adapter.backpressure_handler import BackpressureHandler
|
||||||
from amqp.adapter.consul_global_id_generator import get_unique_instance_id
|
|
||||||
from amqp.adapter.data_message_factory import DataMessageFactory
|
from amqp.adapter.data_message_factory import DataMessageFactory
|
||||||
from amqp.adapter.logging_utils import logging_error, logging_info, logging_warning
|
from amqp.adapter.logging_utils import logging_info, logging_warning
|
||||||
from amqp.adapter.service_message_factory import ServiceMessageFactory
|
from amqp.adapter.service_message_factory import ServiceMessageFactory
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
from amqp.model.model import AMQRoute, DataMessage
|
from amqp.model.model import AMQRoute, DataMessage
|
||||||
@@ -38,17 +37,14 @@ class AMQService:
|
|||||||
|
|
||||||
def __init__(self, amq_configuration: AMQConfiguration, service_adapter=None):
|
def __init__(self, amq_configuration: AMQConfiguration, service_adapter=None):
|
||||||
logging.info("***********************************************")
|
logging.info("***********************************************")
|
||||||
logging.info("************** AMQ Service ***************")
|
logging.info("********** AMQ Service *******************")
|
||||||
logging.info("***********************************************")
|
logging.info("***********************************************")
|
||||||
self.loop = asyncio.new_event_loop()
|
self.loop = asyncio.new_event_loop()
|
||||||
|
|
||||||
self.amq_configuration = amq_configuration
|
self.amq_configuration = amq_configuration
|
||||||
self.service_adapter = service_adapter
|
self.service_adapter = service_adapter
|
||||||
self.unique_id = get_unique_instance_id(amq_configuration.amq_adapter)
|
|
||||||
amq_configuration.amq_adapter.generator_id = self.unique_id
|
|
||||||
|
|
||||||
self.data_message_factory = DataMessageFactory.get_instance(
|
self.data_message_factory = DataMessageFactory.get_instance(
|
||||||
self.unique_id & DataMessageFactory.MACHINE_ID_MASK
|
self.amq_configuration.amq_adapter.generator_id
|
||||||
)
|
)
|
||||||
self.router = RouterConsumer(self.amq_configuration)
|
self.router = RouterConsumer(self.amq_configuration)
|
||||||
self.rabbit_mq_client = RabbitMQClient(self.amq_configuration, self.router)
|
self.rabbit_mq_client = RabbitMQClient(self.amq_configuration, self.router)
|
||||||
@@ -64,12 +60,10 @@ class AMQService:
|
|||||||
)
|
)
|
||||||
self.backpressure_thread: Optional[Thread] = None
|
self.backpressure_thread: Optional[Thread] = None
|
||||||
self.backpressure_handler: Optional[BackpressureHandler] = None
|
self.backpressure_handler: Optional[BackpressureHandler] = None
|
||||||
self.maximum_availability = -1
|
|
||||||
self.future: Future = asyncio.Future()
|
|
||||||
|
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
# ============== P u b l i c A P I M e t h o d s ===================
|
# ====================== A P I M e t h o d s =========================
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
def run(self):
|
def run(self):
|
||||||
@@ -83,38 +77,8 @@ class AMQService:
|
|||||||
logging.info("***********************************************")
|
logging.info("***********************************************")
|
||||||
logging.info("****** AMQ Service Init Complete *********")
|
logging.info("****** AMQ Service Init Complete *********")
|
||||||
logging.info("***********************************************")
|
logging.info("***********************************************")
|
||||||
self.future.set_result(True)
|
|
||||||
self.loop.run_forever()
|
self.loop.run_forever()
|
||||||
|
|
||||||
def backpressure(self, current_availability: int, maximum: int = -1):
|
|
||||||
"""
|
|
||||||
Send Backpressure event to the Management Service. The Backpressure logic determines the type of the event
|
|
||||||
based on the current_availability and maximum values.
|
|
||||||
- OVERLOAD event is triggered immediately when the current_availability value is below threshold
|
|
||||||
typically set to 10% of the maximum value, or 0 if no maximum is provided.
|
|
||||||
- IDLE event is triggered when the current_availability value exeeds 80% of the maximum value or is greater
|
|
||||||
than 0 if no maximum is provided.
|
|
||||||
- UPDATE event is sent if neither OVERLOAD nor IDLE conditions are met, indicating that the backpressure is
|
|
||||||
within acceptable limits.
|
|
||||||
:param current_availability: Currently available capacity.
|
|
||||||
:param maximum: Maximum value of the backpressure metric. If undefined or set to lt 0 (e.g. -1),
|
|
||||||
the maximum is than determined from max value of current_availability seen over the time.
|
|
||||||
"""
|
|
||||||
if self.backpressure_handler is not None:
|
|
||||||
# watch maximum & current value to always have some valid maximum reference
|
|
||||||
if maximum < 0:
|
|
||||||
if current_availability > self.maximum_availability:
|
|
||||||
self.maximum_availability = current_availability
|
|
||||||
else:
|
|
||||||
if maximum > self.maximum_availability:
|
|
||||||
self.maximum_availability = maximum
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.backpressure_handler.update_backpressure_value(
|
|
||||||
current_availability, maximum if maximum > 0 else self.maximum_availability
|
|
||||||
),
|
|
||||||
loop=self.backpressure_handler.loop,
|
|
||||||
)
|
|
||||||
|
|
||||||
def rpc(self, service_name: str, fq_method_name: str, **kwargs) -> Future:
|
def rpc(self, service_name: str, fq_method_name: str, **kwargs) -> Future:
|
||||||
"""
|
"""
|
||||||
Send a Remote Procedure Call (RPC) message to the AMQ service.
|
Send a Remote Procedure Call (RPC) message to the AMQ service.
|
||||||
@@ -130,10 +94,7 @@ class AMQService:
|
|||||||
swarm_id=self.amq_configuration.amq_adapter.swarm_task_id,
|
swarm_id=self.amq_configuration.amq_adapter.swarm_task_id,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
result: Future = asyncio.run_coroutine_threadsafe(
|
result: Future = self.amq_data_message_handler.send_rpc(route, message)
|
||||||
self.amq_data_message_handler.send_rpc(route, message),
|
|
||||||
loop=self.backpressure_handler.loop,
|
|
||||||
)
|
|
||||||
self.set_outstanding_future(message, result)
|
self.set_outstanding_future(message, result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -143,79 +104,6 @@ class AMQService:
|
|||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def command(self, service_name: str, fq_method_name: str, **kwargs) -> bool:
|
|
||||||
"""
|
|
||||||
Send a unidirectional Command message to the AMQ service.
|
|
||||||
:param service_name: The service name to receive the RPC message.
|
|
||||||
:param fq_method_name: The fully qualified method name to execute the RPC message.
|
|
||||||
:param kwargs: Additional keyword arguments to be passed to the RPC method.
|
|
||||||
:return: A Future that resolves when the RPC response is received.
|
|
||||||
"""
|
|
||||||
route: Optional[AMQRoute] = self.router.find_route_by_service(service_name)
|
|
||||||
if route is not None:
|
|
||||||
m: DataMessage = self.data_message_factory.create_rpc_message(
|
|
||||||
fq_method_name=fq_method_name,
|
|
||||||
swarm_id=self.amq_configuration.amq_adapter.swarm_task_id,
|
|
||||||
args=kwargs,
|
|
||||||
)
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.amq_data_message_handler.send_command(route, m),
|
|
||||||
loop=self.backpressure_handler.loop,
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def data_message_generator(self, route: AMQRoute) -> AsyncIterator[DataMessage]:
|
|
||||||
"""
|
|
||||||
An async iterator that blocks until messages arrive, processes them,
|
|
||||||
and continues in a loop.
|
|
||||||
|
|
||||||
:param queue_name: The name of the queue to consume messages from
|
|
||||||
:yield: The processed DataResponse for each message
|
|
||||||
"""
|
|
||||||
# Get a channel from the connection
|
|
||||||
channel = self.rabbit_mq_client.channel
|
|
||||||
queue_args = (
|
|
||||||
self.rabbit_mq_client.DLQ_ARGS if self.amq_configuration.dispatch.use_dlq else None
|
|
||||||
)
|
|
||||||
queue = await channel.declare_queue(
|
|
||||||
route.queue,
|
|
||||||
durable=True,
|
|
||||||
exclusive=False,
|
|
||||||
auto_delete=False,
|
|
||||||
arguments=queue_args,
|
|
||||||
)
|
|
||||||
await queue.bind(route.exchange, route.key)
|
|
||||||
# Start consuming without a callback
|
|
||||||
async with queue.iterator() as queue_iter:
|
|
||||||
async for message in queue_iter:
|
|
||||||
try:
|
|
||||||
# Process the message using the same logic as in inbound_data_message_callback_no_thread
|
|
||||||
data_message: DataMessage = (
|
|
||||||
await self.amq_data_message_handler.reconstitute_data_message(message)
|
|
||||||
)
|
|
||||||
|
|
||||||
success = yield data_message
|
|
||||||
if not success:
|
|
||||||
logging_warning(f"Message processing failed for {data_message.id()}.")
|
|
||||||
# Nack the message without requeuing in case of failure
|
|
||||||
await message.nack(requeue=False)
|
|
||||||
else:
|
|
||||||
await message.ack()
|
|
||||||
except Exception as e:
|
|
||||||
logging_error(f"Error processing message: {e}")
|
|
||||||
# Nack the message without requeuing in case of error
|
|
||||||
await message.nack(requeue=False)
|
|
||||||
|
|
||||||
def get_unique_instance_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Provides cluster-wide unique instance ID for this AMQ service instance.
|
|
||||||
This ID is used to identify the service instance in the cluster and is unique across all instances.
|
|
||||||
IDs are managed by shared Consul KV store, ensuring that even if multiple instances of the service are running,
|
|
||||||
they will each have unique ID.
|
|
||||||
"""
|
|
||||||
return self.unique_id
|
|
||||||
|
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
# ====================== Internal Methods ============================
|
# ====================== Internal Methods ============================
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
|
|||||||
@@ -0,0 +1,233 @@
|
|||||||
|
import consul_kv
|
||||||
|
import socket
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
|
# --- Configuration ---
|
||||||
|
CONSUL_HOST = os.environ.get('CONSUL_HOST', 'localhost')
|
||||||
|
CONSUL_PORT = int(os.environ.get('CONSUL_PORT', 8500))
|
||||||
|
CONSUL_COUNTER_KEY = 'service/ids/counter'
|
||||||
|
MAX_RETRIES = 5 # Max attempts for atomic update
|
||||||
|
RETRY_DELAY_SECONDS = 0.1 # Delay between retries for atomic update
|
||||||
|
|
||||||
|
# --- Logging Setup ---
|
||||||
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s')
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Fallback ID Generation ---
|
||||||
|
def generate_fallback_id() -> int:
|
||||||
|
"""
|
||||||
|
Generates a fallback ID based on hostname and PID if Consul is unavailable.
|
||||||
|
This provides a unique-ish ID for local testing/development but is not guaranteed
|
||||||
|
to be globally unique across different machines or after restarts.
|
||||||
|
"""
|
||||||
|
hostname = socket.gethostname()
|
||||||
|
pid = os.getpid()
|
||||||
|
# A simple way to combine them into a number.
|
||||||
|
# Hash hostname to an integer and combine with PID.
|
||||||
|
# Note: This is a simple fallback. For production, consider a more robust
|
||||||
|
# local ID generation or a different strategy if Consul is critical.
|
||||||
|
fallback_id = (hash(hostname) % 1000000) * 100000 + pid
|
||||||
|
logger.warning(
|
||||||
|
f"Consul is unavailable. Generating fallback ID: {fallback_id} (based on hostname: {hostname}, PID: {pid})")
|
||||||
|
return fallback_id
|
||||||
|
|
||||||
|
|
||||||
|
# --- Unique Instance ID Generation Function ---
|
||||||
|
def get_unique_instance_id() -> int:
|
||||||
|
"""
|
||||||
|
Reads and atomically increments a counter in Consul KV to get a unique instance ID.
|
||||||
|
If Consul is unavailable or the atomic update fails after retries,
|
||||||
|
it falls back to generating an ID based on hostname and PID.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Initialize Consul client
|
||||||
|
client = consul_kv.Connection(endpoint=f"{CONSUL_HOST}:{CONSUL_PORT}")
|
||||||
|
logger.info(f"Attempting to connect to Consul at {CONSUL_HOST}:{CONSUL_PORT}")
|
||||||
|
|
||||||
|
for attempt in range(MAX_RETRIES):
|
||||||
|
try:
|
||||||
|
# 1. Read the current value and its modifyIndex (for CAS)
|
||||||
|
# client.get returns (value, modifyIndex)
|
||||||
|
current_value_str, modify_index = client.get(CONSUL_COUNTER_KEY)
|
||||||
|
|
||||||
|
# If key doesn't exist, initialize it to 0
|
||||||
|
if current_value_str is None:
|
||||||
|
current_value = 0
|
||||||
|
modify_index = 0 # For a non-existent key, modify_index is 0 for initial CAS
|
||||||
|
logger.info(f"Consul key '{CONSUL_COUNTER_KEY}' not found. Initializing to 0.")
|
||||||
|
else:
|
||||||
|
current_value = int(current_value_str)
|
||||||
|
|
||||||
|
new_value = current_value + 1
|
||||||
|
|
||||||
|
# 2. Attempt atomic update using CAS (Check-And-Set)
|
||||||
|
# client.set returns True on success, False on failure (if modify_index changed)
|
||||||
|
success = client.put(CONSUL_COUNTER_KEY, str(new_value), cas=modify_index)
|
||||||
|
|
||||||
|
if success:
|
||||||
|
logger.info(f"Successfully obtained unique ID: {new_value} (Attempt {attempt + 1}/{MAX_RETRIES})")
|
||||||
|
return new_value
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
f"CAS failed for '{CONSUL_COUNTER_KEY}' (modify_index changed). Retrying... (Attempt {attempt + 1}/{MAX_RETRIES})")
|
||||||
|
time.sleep(RETRY_DELAY_SECONDS * (attempt + 1)) # Exponential backoff for retries
|
||||||
|
except ValueError:
|
||||||
|
# Handle case where the stored value is not an integer
|
||||||
|
logger.error(
|
||||||
|
f"Value stored at '{CONSUL_COUNTER_KEY}' is not a valid integer. Please check Consul KV. Attempting to overwrite.")
|
||||||
|
# To recover, you might force a set without CAS, but that risks data loss.
|
||||||
|
# For this demo, we'll just let it retry or fall back.
|
||||||
|
time.sleep(RETRY_DELAY_SECONDS * (attempt + 1))
|
||||||
|
except Exception as e:
|
||||||
|
# Catch specific ConsulKV errors (e.g., connection issues, invalid response)
|
||||||
|
logger.error(f"ConsulKV error during atomic update (Attempt {attempt + 1}/{MAX_RETRIES}): {e}")
|
||||||
|
time.sleep(RETRY_DELAY_SECONDS * (attempt + 1)) # Exponential backoff
|
||||||
|
|
||||||
|
logger.error(f"Failed to obtain unique ID from Consul after {MAX_RETRIES} attempts.")
|
||||||
|
return generate_fallback_id() # Fallback if all retries fail
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# Catch broader connection errors or unexpected issues with Consul client initialization
|
||||||
|
logger.error(f"Could not connect to Consul or an unexpected error occurred: {e}")
|
||||||
|
return generate_fallback_id() # Fallback if Consul is completely unreachable
|
||||||
|
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import consul
|
||||||
|
|
||||||
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
|
|
||||||
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
|
|
||||||
|
# Configuration from environment variables with defaults
|
||||||
|
CONSUL_HOST = os.environ.get('CONSUL_HOST', 'localhost')
|
||||||
|
CONSUL_PORT = int(os.environ.get('CONSUL_PORT', 8500))
|
||||||
|
CONSUL_COUNTER_KEY = 'service/ids/counter'
|
||||||
|
MAX_RETRIES = 5 # Max attempts for atomic update
|
||||||
|
RETRY_DELAY_SECONDS = 0.1 # Delay between retries for atomic update
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_fallback_id() -> int:
|
||||||
|
"""
|
||||||
|
Generate a fallback ID when Consul is not available.
|
||||||
|
Uses a combination of timestamp, hostname hash, and random number.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: A reasonably unique integer ID
|
||||||
|
"""
|
||||||
|
# Get current timestamp in milliseconds
|
||||||
|
timestamp = int(time.time() * 1000)
|
||||||
|
|
||||||
|
# Get hostname and hash it to an integer
|
||||||
|
hostname = socket.gethostname()
|
||||||
|
hostname_hash = hash(hostname) % 10000 # Limit to 4 digits
|
||||||
|
|
||||||
|
# Generate a random number
|
||||||
|
random_part = random.randint(0, 9999) # 4 digits
|
||||||
|
|
||||||
|
# Combine all parts into a single integer
|
||||||
|
# Format: TTTTTTTTTTT-HHHH-RRRR (T=timestamp, H=hostname hash, R=random)
|
||||||
|
unique_id = (timestamp * 100000000) + (hostname_hash * 10000) + random_part
|
||||||
|
|
||||||
|
logger.warning(f"Using fallback ID generation method: {unique_id}")
|
||||||
|
return unique_id
|
||||||
|
|
||||||
|
|
||||||
|
def get_unique_instance_id() -> int:
|
||||||
|
"""
|
||||||
|
Get a globally unique ID from Consul.
|
||||||
|
Uses Consul's atomic Compare-And-Set operations to safely increment a counter.
|
||||||
|
Falls back to a local generation method if Consul is unavailable.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: A globally unique integer ID
|
||||||
|
"""
|
||||||
|
consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds = get_config_values()
|
||||||
|
try:
|
||||||
|
# Connect to Consul
|
||||||
|
c = consul.Consul(host=consul_host, port=consul_port)
|
||||||
|
|
||||||
|
# Try to get the current counter value
|
||||||
|
index, data = c.kv.get(CONSUL_COUNTER_KEY)
|
||||||
|
|
||||||
|
# If the key doesn't exist yet, create it with initial value
|
||||||
|
if data is None:
|
||||||
|
logger.info(f"Initializing Consul counter at {CONSUL_COUNTER_KEY}")
|
||||||
|
if c.kv.put(CONSUL_COUNTER_KEY, "1"):
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
logger.error("Failed to initialize Consul counter")
|
||||||
|
return generate_fallback_id()
|
||||||
|
|
||||||
|
# Get the current value
|
||||||
|
current_value = int(data['Value'].decode('utf-8'))
|
||||||
|
new_value = current_value + 1
|
||||||
|
|
||||||
|
# Try to atomically update the counter
|
||||||
|
for attempt in range(MAX_RETRIES):
|
||||||
|
# Use Compare-And-Set to ensure atomicity
|
||||||
|
success = c.kv.put(
|
||||||
|
CONSUL_COUNTER_KEY,
|
||||||
|
str(new_value),
|
||||||
|
cas=data['ModifyIndex']
|
||||||
|
)
|
||||||
|
|
||||||
|
if success:
|
||||||
|
logger.debug(f"Successfully obtained unique ID: {new_value}")
|
||||||
|
return new_value
|
||||||
|
|
||||||
|
# If CAS failed, someone else updated the value, retry
|
||||||
|
logger.debug(f"CAS update failed on attempt {attempt+1}, retrying...")
|
||||||
|
time.sleep(RETRY_DELAY_SECONDS)
|
||||||
|
|
||||||
|
# Get the latest value for the next attempt
|
||||||
|
index, data = c.kv.get(CONSUL_COUNTER_KEY)
|
||||||
|
if data is None:
|
||||||
|
logger.error("Counter disappeared during update")
|
||||||
|
return generate_fallback_id()
|
||||||
|
|
||||||
|
current_value = int(data['Value'].decode('utf-8'))
|
||||||
|
new_value = current_value + 1
|
||||||
|
|
||||||
|
# If we've exhausted all retries, use the fallback
|
||||||
|
logger.error(f"Failed to update counter after {MAX_RETRIES} attempts")
|
||||||
|
return generate_fallback_id()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error accessing Consul: {str(e)}")
|
||||||
|
return generate_fallback_id()
|
||||||
|
def get_config_values():
|
||||||
|
"""
|
||||||
|
Get configuration values from AMQConfiguration or environment variables.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple: (consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
config = AMQConfiguration("application.properties")
|
||||||
|
consul_host = os.environ.get('CONSUL_HOST', 'localhost')
|
||||||
|
consul_port = int(os.environ.get('CONSUL_PORT', 8500))
|
||||||
|
consul_counter_key = os.environ.get('CONSUL_COUNTER_KEY', 'service/ids/counter')
|
||||||
|
max_retries = int(os.environ.get('CONSUL_MAX_RETRIES', 5))
|
||||||
|
retry_delay_seconds = float(os.environ.get('CONSUL_RETRY_DELAY_SECONDS', 0.1))
|
||||||
|
return consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to load configuration: {str(e)}. Using default values.")
|
||||||
|
return (
|
||||||
|
os.environ.get('CONSUL_HOST', 'localhost'),
|
||||||
|
int(os.environ.get('CONSUL_PORT', 8500)),
|
||||||
|
os.environ.get('CONSUL_COUNTER_KEY', 'service/ids/counter'),
|
||||||
|
int(os.environ.get('CONSUL_MAX_RETRIES', 5)),
|
||||||
|
float(os.environ.get('CONSUL_RETRY_DELAY_SECONDS', 0.1))
|
||||||
|
)
|
||||||
@@ -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
|
||||||
self.amq_service: AMQService = AMQService(amq_configuration, self)
|
_amq_service: AMQService = AMQService(amq_configuration, self)
|
||||||
self.thread = Thread(target=self.amq_service.run)
|
self.thread = Thread(target=_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:
|
||||||
|
|||||||
@@ -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)
|
||||||
self.amq_service: AMQService = AMQService(amq_configuration, self)
|
_amq_service: AMQService = AMQService(amq_configuration, self)
|
||||||
self.thread = Thread(target=self.amq_service.run)
|
self.thread = Thread(target=_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:
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import shutil
|
import shutil
|
||||||
@@ -135,7 +134,7 @@ async def upload_document(
|
|||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
# httpx instrumentation ensures the current_availability trace context is propagated
|
# httpx instrumentation ensures the current trace context is propagated
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
# IMPORTANT: Use the actual host and port where your FastAPI app is running
|
# IMPORTANT: Use the actual host and port where your FastAPI app is running
|
||||||
# If running directly on host, use http://localhost:8000
|
# If running directly on host, use http://localhost:8000
|
||||||
@@ -195,47 +194,32 @@ async def process_document_metadata(metadata: dict):
|
|||||||
return {"status": "metadata processed", "received_data": metadata}
|
return {"status": "metadata processed", "received_data": metadata}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v4/documents")
|
@app.get("/api/v3/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
|
||||||
documents = []
|
with tracer.start_as_current_span("get_documents_endpoint_logic"):
|
||||||
try:
|
documents = []
|
||||||
with tracer.start_as_current_span("get_documents_endpoint_logic"):
|
# Generate several random documents to emulate a list
|
||||||
documents = []
|
for i in range(random.randint(2, 5)):
|
||||||
# Generate several random documents to emulate a list
|
documents.append(
|
||||||
for i in range(random.randint(2, 5)):
|
{
|
||||||
did = f"doc-{random.randint(1000, 9999)}"
|
"id": f"doc-{random.randint(1000, 9999)}",
|
||||||
documents.append(
|
"name": f"Document {random.randint(1, 100)}",
|
||||||
{
|
"description": f"Description for document {random.randint(1, 1000)}",
|
||||||
"id": did,
|
"uploaded_at": time.strftime(
|
||||||
"name": f"Document {random.randint(1, 100)}",
|
"%Y-%m-%dT%H:%M:%SZ",
|
||||||
"description": f"Description for document {random.randint(1, 1000)}",
|
time.gmtime(time.time() - random.randint(0, 86400 * 30)),
|
||||||
"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",
|
|
||||||
"print_document_id_static",
|
|
||||||
document_id=did + "-static",
|
|
||||||
)
|
|
||||||
otadapter.amq_service.command(
|
|
||||||
"otdemo-2",
|
|
||||||
"print_document_id",
|
|
||||||
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
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
# ====================================================================
|
|
||||||
# CleverMicro AMQ Adapter settings
|
|
||||||
# ====================================================================
|
|
||||||
[CleverMicro-AMQ]
|
|
||||||
cm.amq-adapter.service-name=otdemo-2
|
|
||||||
cm.amq-adapter.generator-id=1
|
|
||||||
cm.amq-adapter.route-mapping=otdemo-2:otdemo-exchange:otdemo-queue-2:otdemo-2.#:5:0
|
|
||||||
cm.amq-adapter.require-authenticated-user=false
|
|
||||||
cm.amq-adapter.callbacks-enabled=False
|
|
||||||
|
|
||||||
# CleverMicro AMQ Adapter dispatcher, settings related to RabbitMQ connection and optional REST forwarding
|
|
||||||
cm.dispatch.use-dlq=true
|
|
||||||
cm.dispatch.amq-host=localhost
|
|
||||||
cm.dispatch.amq-port=5672
|
|
||||||
cm.dispatch.amq-port-tls=5671
|
|
||||||
cm.dispatch.download-dir=/tmp/downloaded_files
|
|
||||||
|
|
||||||
cm.backpressure.threshold=1
|
|
||||||
cm.backpressure.time-window=150
|
|
||||||
cm.backpressure.cpu-overload-duration=300
|
|
||||||
cm.backpressure.cpu-idle-duration=300
|
|
||||||
@@ -2,20 +2,43 @@
|
|||||||
# CleverMicro AMQ Adapter settings
|
# CleverMicro AMQ Adapter settings
|
||||||
# ====================================================================
|
# ====================================================================
|
||||||
[CleverMicro-AMQ]
|
[CleverMicro-AMQ]
|
||||||
cm.amq-adapter.service-name=otdemo
|
# 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=cleverswarm
|
||||||
|
# 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
|
||||||
cm.amq-adapter.route-mapping=otdemo:otdemo-exchange:otdemo-queue-1:api.cm.dev.cleverthis.localhost.test.python.frontend.#:5:0
|
# 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-dev route
|
||||||
|
cm.amq-adapter.route-mapping=otdemo:otdemo-exchange:otdemo-queue:api.cm.dev.cleverthis.com.test.python.#:5:0
|
||||||
|
# local route
|
||||||
|
#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
|
||||||
cm.amq-adapter.require-authenticated-user=false
|
cm.amq-adapter.require-authenticated-user=false
|
||||||
cm.amq-adapter.callbacks-enabled=True
|
|
||||||
|
|
||||||
# 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=localhost
|
cm.dispatch.amq-host=rabbitmq
|
||||||
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
|
||||||
|
# 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=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=1
|
cm.backpressure.threshold=1
|
||||||
cm.backpressure.time-window=150
|
# Define maximum CPU usage (%) before triggering backpressure OVERLOAD alert
|
||||||
cm.backpressure.cpu-overload-duration=300
|
cm.backpressure.threshold-cpu-overload=90
|
||||||
cm.backpressure.cpu-idle-duration=300
|
# 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.cpu-idle-duration=30
|
||||||
|
|||||||
@@ -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)
|
||||||
self.amq_service: AMQService = AMQService(amq_configuration, self)
|
_amq_service: AMQService = AMQService(amq_configuration, self)
|
||||||
self.thread = Thread(target=self.amq_service.run)
|
self.thread = Thread(target=_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:
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
|
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
from amqp.model.model import AMQRoute, DataMessage
|
|
||||||
from otdemo.otdemo_adapter import OTDemoAmqpAdapter
|
|
||||||
|
|
||||||
|
|
||||||
def print_document_id_static(document_id: str) -> str:
|
|
||||||
print(f"STATIC Document ID: {document_id}")
|
|
||||||
return "STATIC PRINTED!"
|
|
||||||
|
|
||||||
|
|
||||||
class Backend:
|
|
||||||
|
|
||||||
def print_document_id(self, document_id: str) -> str:
|
|
||||||
print(f"CLASS Document ID: {document_id}")
|
|
||||||
return "CLASS PRINTED!"
|
|
||||||
|
|
||||||
|
|
||||||
async def processing_loop():
|
|
||||||
while (
|
|
||||||
not otadapter.amq_service.future.done()
|
|
||||||
): # Wait until AMQ adapter initializes RabbitMQ connection
|
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
# IMPORTANT - in the next line the string arg must match the first token in route-mapping property
|
|
||||||
route: AMQRoute = otadapter.amq_service.router.find_route_by_service("otdemo-2")
|
|
||||||
generator = otadapter.amq_service.data_message_generator(route) # this is a generator function
|
|
||||||
print(f"Data message generator created: {generator}")
|
|
||||||
message = await generator.asend(None) # First value must be None to start the generator
|
|
||||||
while True:
|
|
||||||
print(f"Received message: {message}")
|
|
||||||
if message is None:
|
|
||||||
break
|
|
||||||
data_message: DataMessage = message
|
|
||||||
# This is a message processing part. You may use data_message.method() or .domain() or .path()
|
|
||||||
# to access values provided by caller in the send command() call.
|
|
||||||
document_id = data_message._base64body.decode("utf-8")
|
|
||||||
print(
|
|
||||||
f"DATA_MESSAGE Attributes: {data_message.method()} {data_message.domain()} {data_message.path()}"
|
|
||||||
)
|
|
||||||
if data_message.path() == "print_document_id_static":
|
|
||||||
print_document_id_static(document_id)
|
|
||||||
elif data_message.path() == "print_document_id":
|
|
||||||
backend = Backend()
|
|
||||||
backend.print_document_id(document_id)
|
|
||||||
message = await generator.asend(True)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
otadapter = OTDemoAmqpAdapter(AMQConfiguration("otdemo.backend.properties"), [])
|
|
||||||
# Run the processing loop in the AMQ service event loop
|
|
||||||
asyncio.run_coroutine_threadsafe(processing_loop(), otadapter.amq_service.loop)
|
|
||||||
otadapter.thread.join() # if the code above is not running a loop, need to join the thread to keep program running
|
|
||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "amq_adapter"
|
name = "amq_adapter"
|
||||||
version = "0.2.37"
|
version = "0.2.34"
|
||||||
description = "The Python implementation of the AMQ Adapter for CleverMicro"
|
description = "The Python implementation of the AMQ Adapter for CleverMicro"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
authors = [
|
authors = [
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from amqp.adapter.consul_global_id_generator import get_unique_instance_id
|
from amqp.service.consul_global_id_generator import get_unique_instance_id
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
|
|
||||||
CONSUL_HOST = os.environ.get("CONSUL_HOST", "localhost")
|
CONSUL_HOST = os.environ.get("CONSUL_HOST", "localhost")
|
||||||
CONSUL_PORT = int(os.environ.get("CONSUL_PORT", 8500))
|
CONSUL_PORT = int(os.environ.get("CONSUL_PORT", 8500))
|
||||||
@@ -21,7 +20,7 @@ logger = logging.getLogger(__name__)
|
|||||||
# --- Demo: Simulate Concurrent Access ---
|
# --- Demo: Simulate Concurrent Access ---
|
||||||
def worker_thread(results_list):
|
def worker_thread(results_list):
|
||||||
"""A function to be run by multiple threads to get unique IDs."""
|
"""A function to be run by multiple threads to get unique IDs."""
|
||||||
instance_id = get_unique_instance_id(AMQConfiguration("").amq_adapter)
|
instance_id = get_unique_instance_id()
|
||||||
results_list.append(instance_id)
|
results_list.append(instance_id)
|
||||||
logger.info(f"Thread {threading.current_thread().name} got ID: {instance_id}")
|
logger.info(f"Thread {threading.current_thread().name} got ID: {instance_id}")
|
||||||
|
|
||||||
|
|||||||
@@ -1,205 +1,388 @@
|
|||||||
|
#
|
||||||
|
# NOTE: need to install: pytest-asyncio
|
||||||
|
#
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import time
|
import json
|
||||||
import unittest
|
from asyncio import AbstractEventLoop
|
||||||
|
from threading import Thread
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from aio_pika import Exchange
|
||||||
|
from aio_pika.abc import AbstractRobustChannel
|
||||||
|
|
||||||
from amqp.adapter.backpressure_handler import (
|
from amqp.adapter.backpressure_handler import (
|
||||||
BackpressureHandler,
|
BackpressureHandler,
|
||||||
|
RunningAverage,
|
||||||
ScaleRequestV1,
|
ScaleRequestV1,
|
||||||
ScalingRequestAlert,
|
|
||||||
)
|
)
|
||||||
|
from amqp.config.amq_configuration import AMQConfiguration
|
||||||
|
|
||||||
|
|
||||||
class TestBackpressureHandler(unittest.IsolatedAsyncioTestCase):
|
class TestScaleRequestV1:
|
||||||
def setUp(self):
|
"""
|
||||||
self.channel = AsyncMock()
|
A class containing pytest unit tests for the ScaleRequestV1 class.
|
||||||
self.loop = asyncio.get_event_loop()
|
"""
|
||||||
self.config = MagicMock()
|
|
||||||
self.config.amq_adapter.swarm_service_id = "test-service"
|
|
||||||
self.config.amq_adapter.swarm_task_id = "test-task"
|
|
||||||
self.config.backpressure.threshold_load = 100
|
|
||||||
self.config.backpressure.time_window = 1
|
|
||||||
self.config.backpressure.idle_duration = 2
|
|
||||||
self.config.backpressure.cpu_monitoring_enabled = False
|
|
||||||
self.config.backpressure.cpu_overload_duration = 1
|
|
||||||
|
|
||||||
# Record callbacks for testing
|
def test_scale_request_v1_initialization(self):
|
||||||
|
"""
|
||||||
|
Test that the ScaleRequestV1 object is initialized correctly.
|
||||||
|
"""
|
||||||
|
service_id = "test-service"
|
||||||
|
task_id = "test-task"
|
||||||
|
current_availability = 5
|
||||||
|
|
||||||
|
scale_request = ScaleRequestV1(
|
||||||
|
service_id,
|
||||||
|
task_id,
|
||||||
|
current_availability,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert scale_request.version == 1
|
||||||
|
assert scale_request.serviceId == service_id
|
||||||
|
assert scale_request.taskId == task_id
|
||||||
|
assert scale_request.currentAvailability == current_availability
|
||||||
|
assert scale_request.thread is None
|
||||||
|
|
||||||
|
def test_scale_request_v1_to_json(self):
|
||||||
|
"""
|
||||||
|
Test the to_json method of ScaleRequestV1.
|
||||||
|
"""
|
||||||
|
service_id = "test-service"
|
||||||
|
task_id = "test-task"
|
||||||
|
current_availability = 5
|
||||||
|
|
||||||
|
scale_request = ScaleRequestV1(
|
||||||
|
service_id,
|
||||||
|
task_id,
|
||||||
|
current_availability,
|
||||||
|
)
|
||||||
|
|
||||||
|
json_output = scale_request.to_json()
|
||||||
|
expected_json = json.dumps(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"serviceId": service_id,
|
||||||
|
"taskId": task_id,
|
||||||
|
"currentAvailability": current_availability,
|
||||||
|
},
|
||||||
|
indent=2,
|
||||||
|
)
|
||||||
|
assert json_output == expected_json
|
||||||
|
|
||||||
|
def test_scale_request_v1_to_json_different_alert_type(self):
|
||||||
|
"""
|
||||||
|
Test the to_json method with a different ScalingRequestAlert type.
|
||||||
|
"""
|
||||||
|
service_id = "test-service"
|
||||||
|
task_id = "test-task"
|
||||||
|
current_availability = 5
|
||||||
|
|
||||||
|
scale_request = ScaleRequestV1(
|
||||||
|
service_id,
|
||||||
|
task_id,
|
||||||
|
current_availability,
|
||||||
|
)
|
||||||
|
|
||||||
|
json_output = scale_request.to_json()
|
||||||
|
expected_json = json.dumps(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"serviceId": service_id,
|
||||||
|
"taskId": task_id,
|
||||||
|
"currentAvailability": current_availability,
|
||||||
|
},
|
||||||
|
indent=2,
|
||||||
|
)
|
||||||
|
assert json_output == expected_json
|
||||||
|
|
||||||
|
|
||||||
|
class TestRunningAverage:
|
||||||
|
"""
|
||||||
|
A class containing pytest unit tests for the RunningAverage class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_running_average_initialization(self):
|
||||||
|
"""
|
||||||
|
Test that the RunningAverage object is initialized correctly.
|
||||||
|
"""
|
||||||
|
num_items = 5
|
||||||
|
ra = RunningAverage(num_items)
|
||||||
|
assert ra.buffer == [0] * num_items
|
||||||
|
assert ra.pointer == 0
|
||||||
|
assert ra.num_items == num_items
|
||||||
|
assert ra.is_filled is False
|
||||||
|
|
||||||
|
def test_running_average_add_one_value(self):
|
||||||
|
"""
|
||||||
|
Test adding a single value to the RunningAverage.
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(10)
|
||||||
|
assert ra.buffer == [10, 0, 0]
|
||||||
|
assert ra.pointer == 1
|
||||||
|
assert ra.is_filled is False
|
||||||
|
|
||||||
|
def test_running_average_add_multiple_values_within_capacity(self):
|
||||||
|
"""
|
||||||
|
Test adding multiple values within the capacity of the RunningAverage buffer.
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(5)
|
||||||
|
ra.add(10)
|
||||||
|
ra.add(15)
|
||||||
|
assert ra.buffer == [5, 10, 15]
|
||||||
|
assert ra.pointer == 0
|
||||||
|
assert ra.is_filled is True
|
||||||
|
|
||||||
|
def test_running_average_add_more_values_than_capacity(self):
|
||||||
|
"""
|
||||||
|
Test adding more values than the capacity of the RunningAverage buffer, checking wrap-around.
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(1)
|
||||||
|
ra.add(2)
|
||||||
|
ra.add(3)
|
||||||
|
ra.add(4) # Overwrite the first value
|
||||||
|
ra.add(5) # Overwrite the second value
|
||||||
|
assert ra.buffer == [4, 5, 3]
|
||||||
|
assert ra.pointer == 2
|
||||||
|
assert ra.is_filled is True
|
||||||
|
|
||||||
|
def test_running_average_get_average_empty(self):
|
||||||
|
"""
|
||||||
|
Test get_average when no values have been added.
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
assert ra.get_average() == 0.0
|
||||||
|
|
||||||
|
def test_running_average_get_average_one_value(self):
|
||||||
|
"""
|
||||||
|
Test get_average after adding one value.
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(10)
|
||||||
|
assert ra.get_average() == 10.0
|
||||||
|
|
||||||
|
def test_running_average_get_average_multiple_values_within_capacity(self):
|
||||||
|
"""
|
||||||
|
Test get_average with multiple values added within the buffer's capacity.
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(1)
|
||||||
|
ra.add(2)
|
||||||
|
ra.add(3)
|
||||||
|
assert ra.get_average() == 2.0
|
||||||
|
|
||||||
|
def test_running_average_get_average_more_values_than_capacity(self):
|
||||||
|
"""
|
||||||
|
Test get_average after adding more values than the buffer's capacity (check wrap-around).
|
||||||
|
"""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(1)
|
||||||
|
ra.add(2)
|
||||||
|
ra.add(3)
|
||||||
|
ra.add(4)
|
||||||
|
ra.add(5)
|
||||||
|
assert ra.get_average() == 4.0
|
||||||
|
|
||||||
|
def test_get_current_values_empty(self):
|
||||||
|
"""Test get_current_values when no values have been added."""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
assert ra.get_current_values() == []
|
||||||
|
|
||||||
|
def test_get_current_values_one_value(self):
|
||||||
|
"""Test get_current_values after adding one value."""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(10)
|
||||||
|
assert ra.get_current_values() == [10]
|
||||||
|
|
||||||
|
def test_get_current_values_multiple_values_within_capacity(self):
|
||||||
|
"""Test get_current_values with multiple values within capacity."""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(1)
|
||||||
|
ra.add(2)
|
||||||
|
ra.add(3)
|
||||||
|
assert ra.get_current_values() == [1, 2, 3]
|
||||||
|
|
||||||
|
def test_get_current_values_more_values_than_capacity(self):
|
||||||
|
"""Test get_current_values after adding more values than capacity (wrap-around)."""
|
||||||
|
ra = RunningAverage(3)
|
||||||
|
ra.add(1)
|
||||||
|
ra.add(2)
|
||||||
|
ra.add(3)
|
||||||
|
ra.add(4)
|
||||||
|
ra.add(5)
|
||||||
|
assert ra.get_current_values() == [3, 4, 5]
|
||||||
|
|
||||||
|
def test_get_current_values_wrap_around(self):
|
||||||
|
"""Test get_current_values when the buffer has wrapped around."""
|
||||||
|
ra = RunningAverage(4)
|
||||||
|
ra.add(1)
|
||||||
|
ra.add(2)
|
||||||
|
ra.add(3)
|
||||||
|
ra.add(4)
|
||||||
|
ra.add(5) # Overwrite 1
|
||||||
|
ra.add(6) # Overwrite 2
|
||||||
|
assert ra.get_current_values() == [3, 4, 5, 6]
|
||||||
|
|
||||||
|
|
||||||
|
# Test for BackpressureHandler
|
||||||
|
class TestBackpressureHandler:
|
||||||
|
"""
|
||||||
|
A class containing pytest unit tests for the BackpressureHandler class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_channel(self):
|
||||||
|
"""
|
||||||
|
Pytest fixture to create a mock AbstractRobustChannel.
|
||||||
|
"""
|
||||||
|
return AsyncMock(spec=AbstractRobustChannel)
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_loop(self):
|
||||||
|
"""
|
||||||
|
Pytest fixture to create a mock AbstractEventLoop.
|
||||||
|
"""
|
||||||
|
return MagicMock(spec=AbstractEventLoop)
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_config(self):
|
||||||
|
"""
|
||||||
|
Pytest fixture to create a mock AMQConfiguration.
|
||||||
|
"""
|
||||||
|
return AMQConfiguration("")
|
||||||
|
|
||||||
|
def test_backpressure_handler_initialization(self, mock_channel, mock_loop, mock_config):
|
||||||
|
"""
|
||||||
|
Test that the BackpressureHandler object is initialized correctly.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
assert handler.channel == mock_channel
|
||||||
|
assert handler.loop == mock_loop
|
||||||
|
assert handler.config == mock_config
|
||||||
|
assert handler.swarm_service_id == "clever-amqp-service"
|
||||||
|
assert handler.swarm_task_id == "python-adapter"
|
||||||
|
assert handler.parallel_workers == mock_config.backpressure.threshold_threads
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_increase_parallel_executions(self, mock_channel, mock_loop, mock_config):
|
||||||
|
"""
|
||||||
|
Test the increase_parallel_executions method.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.send_backpressure_report = AsyncMock()
|
||||||
|
initial_executions = handler.current_parallel_executions
|
||||||
|
|
||||||
|
with patch("time.time") as mock_time:
|
||||||
|
mock_time.return_value = 12345
|
||||||
|
await handler.increase_parallel_executions()
|
||||||
|
assert handler.current_parallel_executions == initial_executions + 1
|
||||||
|
assert handler.last_usage_report_time == 12345
|
||||||
|
assert handler.send_backpressure_report.call_count == 1
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_decrease_parallel_executions(self, mock_channel, mock_loop, mock_config):
|
||||||
|
"""
|
||||||
|
Test the decrease_parallel_executions method.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.send_backpressure_report = AsyncMock()
|
||||||
|
handler.current_parallel_executions = 5
|
||||||
|
|
||||||
|
with patch("time.time") as mock_time:
|
||||||
|
mock_time.return_value = 12345
|
||||||
|
await handler.decrease_parallel_executions()
|
||||||
|
assert handler.current_parallel_executions == 4
|
||||||
|
assert handler.last_recover_report_time == 12345
|
||||||
|
assert handler.send_backpressure_report.call_count == 1
|
||||||
|
|
||||||
|
handler.last_recover_report_time = 0
|
||||||
|
handler.current_parallel_executions = 0
|
||||||
|
await handler.decrease_parallel_executions()
|
||||||
|
assert handler.current_parallel_executions == 0 # Should not go below 0
|
||||||
|
|
||||||
|
def test_start_backpressure_monitor(self, mock_channel, mock_loop, mock_config):
|
||||||
|
"""
|
||||||
|
Test the start_backpressure_monitor method.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.do_loop = 1 # allow only 1 execution of the loop and then exit the thread, else tests will never end
|
||||||
|
thread = handler.start_backpressure_monitor()
|
||||||
|
assert isinstance(thread, Thread)
|
||||||
|
assert handler.thread == thread
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_regular_report_loop(self, mock_channel, mock_loop, mock_config):
|
||||||
|
"""
|
||||||
|
Test the _regular_report_loop method.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.send_backpressure_report = AsyncMock()
|
||||||
|
|
||||||
|
# test 1 loop
|
||||||
|
handler.do_loop = 1
|
||||||
|
await handler._regular_report_loop()
|
||||||
|
handler.send_backpressure_report.assert_called_once()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_backpressure_report(self, mock_channel, mock_loop, mock_config):
|
||||||
|
"""
|
||||||
|
Test the send_backpressure_report method.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.publish_backpressure_request = AsyncMock()
|
||||||
|
with patch("time.time") as mock_time:
|
||||||
|
mock_time.return_value = 12345
|
||||||
|
await handler.send_backpressure_report()
|
||||||
|
assert handler.publish_backpressure_request.call_count == 1
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_publish_backpressure_request_exchange_exists(
|
||||||
|
self, mock_channel, mock_loop, mock_config
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Test the publish_backpressure_request method when the exchange already exists.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.exchange = AsyncMock(spec=Exchange)
|
||||||
|
with patch("asyncio.run_coroutine_threadsafe") as mock_async_run:
|
||||||
|
mock_async_run.return_value = asyncio.Future()
|
||||||
|
mock_async_run.return_value.set_result(None)
|
||||||
|
await handler.publish_backpressure_request(
|
||||||
|
ScaleRequestV1("test_service", "test_task", 50)
|
||||||
|
)
|
||||||
|
assert mock_async_run.call_count == 1
|
||||||
|
# handler.exchange.publish.assert_called_once()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_publish_backpressure_request_exchange_does_not_exist(
|
||||||
|
self, mock_channel, mock_loop, mock_config
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Test the publish_backpressure_request method when the exchange does not exist and needs to be created.
|
||||||
|
"""
|
||||||
|
handler = BackpressureHandler(mock_channel, mock_loop, mock_config)
|
||||||
|
handler.exchange = None # Simulate exchange not existing
|
||||||
|
mock_exchange = AsyncMock(spec=Exchange)
|
||||||
|
mock_channel.get_exchange.return_value = mock_exchange
|
||||||
BackpressureHandler._callback_list = {}
|
BackpressureHandler._callback_list = {}
|
||||||
|
|
||||||
self.handler = BackpressureHandler(self.channel, self.loop, self.config)
|
with patch("asyncio.run_coroutine_threadsafe") as mock_async_run:
|
||||||
self.handler.exchange = AsyncMock()
|
mock_async_run.return_value = asyncio.Future()
|
||||||
|
mock_async_run.return_value.set_result(
|
||||||
|
mock_exchange
|
||||||
|
) # Make it return the mock exchange
|
||||||
|
await handler.publish_backpressure_request(
|
||||||
|
ScaleRequestV1("test_service", "test_task", 50)
|
||||||
|
)
|
||||||
|
assert mock_async_run.call_count == 2
|
||||||
|
|
||||||
# Set do_loop to 1 to run the loop only once
|
assert "_wrap_rabbit_mq_api_init" in BackpressureHandler._callback_list
|
||||||
self.handler.do_loop = 1
|
assert "_wrap_rabbit_mq_api" in BackpressureHandler._callback_list
|
||||||
|
# Since I need also to mockup the asyncio.run_coroutine_threadsafe, the following mockups are
|
||||||
async def test_increase_decrease_current_load(self):
|
# not called because mockup overrides that call with preset return value.
|
||||||
self.handler.current_load = 0
|
# mock_channel.get_exchange.assert_called_once_with(name="cleverthis.clevermicro.management")
|
||||||
self.handler.increase_current_load()
|
# handler.exchange.publish.assert_called_once()
|
||||||
self.assertEqual(self.handler.current_load, 1)
|
|
||||||
self.handler.decrease_current_load()
|
|
||||||
self.assertEqual(self.handler.current_load, 0)
|
|
||||||
|
|
||||||
async def test_update_current_load(self):
|
|
||||||
self.handler.current_availability = 0
|
|
||||||
self.handler.update_current_availability(5)
|
|
||||||
self.assertEqual(self.handler.current_availability, 5)
|
|
||||||
|
|
||||||
async def test_update_last_data_message_time(self):
|
|
||||||
old_time = self.handler.last_backpressure_event_time
|
|
||||||
self.handler.update_last_backpressure_event_time()
|
|
||||||
self.assertGreater(self.handler.last_backpressure_event_time, old_time)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_update_backpressure_value(self, mock_publish):
|
|
||||||
# Test updating with a higher maximum
|
|
||||||
await self.handler.update_backpressure_value(50, 200)
|
|
||||||
self.assertEqual(self.handler.current_availability, 50)
|
|
||||||
self.assertEqual(self.handler.max_availability, 200)
|
|
||||||
|
|
||||||
# Test updating with a lower maximum (should not change)
|
|
||||||
await self.handler.update_backpressure_value(60, 50)
|
|
||||||
self.assertEqual(self.handler.current_availability, 60)
|
|
||||||
self.assertEqual(self.handler.max_availability, 200)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_check_overload_condition_normal(self, mock_publish):
|
|
||||||
# Set up a normal state (60% available capacity)
|
|
||||||
self.handler.max_availability = 100
|
|
||||||
self.handler.current_availability = 60
|
|
||||||
self.handler.last_backpressure_event = ScalingRequestAlert.UPDATE
|
|
||||||
self.handler.last_backpressure_event_time = time.time() - 2 # 2 seconds ago
|
|
||||||
|
|
||||||
await self.handler.check_overload_condition()
|
|
||||||
|
|
||||||
# Should send an UPDATE event
|
|
||||||
mock_publish.assert_called_once()
|
|
||||||
args = mock_publish.call_args[0][0]
|
|
||||||
self.assertEqual(args.requestType, ScalingRequestAlert.UPDATE)
|
|
||||||
self.assertEqual(args.current_availability, 60)
|
|
||||||
self.assertEqual(args.max_availability, 100)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_check_overload_condition_overload(self, mock_publish):
|
|
||||||
# Set up an overload state (10% available capacity)
|
|
||||||
self.handler.max_availability = 100
|
|
||||||
self.handler.current_availability = 10
|
|
||||||
self.handler.last_backpressure_event = ScalingRequestAlert.UPDATE
|
|
||||||
self.handler.last_backpressure_event_time = time.time() - 2 # 2 seconds ago
|
|
||||||
|
|
||||||
await self.handler.check_overload_condition()
|
|
||||||
|
|
||||||
# Should send an OVERLOAD event
|
|
||||||
mock_publish.assert_called_once()
|
|
||||||
args = mock_publish.call_args[0][0]
|
|
||||||
self.assertEqual(args.requestType, ScalingRequestAlert.OVERLOAD)
|
|
||||||
self.assertEqual(args.current_availability, 10)
|
|
||||||
self.assertEqual(args.max_availability, 100)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_check_overload_condition_idle(self, mock_publish):
|
|
||||||
# Set up an idle state (90% available capacity)
|
|
||||||
self.handler.max_availability = 100
|
|
||||||
self.handler.current_availability = 90
|
|
||||||
self.handler.last_backpressure_event = ScalingRequestAlert.IDLE
|
|
||||||
# emulate fact that IDLE state is for 3 seconds longer than required minimum for sending IDLE event
|
|
||||||
self.handler.last_backpressure_event_time = (
|
|
||||||
time.time() - self.config.backpressure.idle_duration - 3
|
|
||||||
)
|
|
||||||
|
|
||||||
await self.handler.check_overload_condition()
|
|
||||||
|
|
||||||
# Should send an IDLE event
|
|
||||||
mock_publish.assert_called_once()
|
|
||||||
args = mock_publish.call_args[0][0]
|
|
||||||
self.assertEqual(args.requestType, ScalingRequestAlert.IDLE)
|
|
||||||
self.assertEqual(args.current_availability, 90)
|
|
||||||
self.assertEqual(args.max_availability, 100)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_check_overload_condition_auto_max(self, mock_publish):
|
|
||||||
# Test with unset maximum that should be auto-detected
|
|
||||||
self.handler.max_availability = -1
|
|
||||||
self.handler.current_availability = 50
|
|
||||||
self.handler.last_backpressure_event = ScalingRequestAlert.UPDATE
|
|
||||||
self.handler.last_backpressure_event_time = (
|
|
||||||
time.time() - self.config.backpressure.time_window - 2
|
|
||||||
)
|
|
||||||
|
|
||||||
await self.handler.check_overload_condition()
|
|
||||||
|
|
||||||
# Should set maximum to current_availability and send UPDATE
|
|
||||||
# self.assertEqual(self.handler.max_availability, 50)
|
|
||||||
mock_publish.assert_called_once()
|
|
||||||
args = mock_publish.call_args[0][0]
|
|
||||||
self.assertEqual(args.requestType, ScalingRequestAlert.UPDATE)
|
|
||||||
self.assertEqual(args.current_availability, 50)
|
|
||||||
self.assertEqual(args.max_availability, -1)
|
|
||||||
|
|
||||||
# Test with a higher value that should update the maximum
|
|
||||||
mock_publish.reset_mock()
|
|
||||||
await self.handler.update_backpressure_value(80, 80)
|
|
||||||
|
|
||||||
# Should update maximum to 80
|
|
||||||
self.assertEqual(self.handler.max_availability, 80)
|
|
||||||
# mock_publish.assert_called_once()
|
|
||||||
# args = mock_publish.call_args[0][0]
|
|
||||||
# self.assertEqual(args.current_availability, 80)
|
|
||||||
# self.assertEqual(args.max_availability, 80)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_handle_backpressure_overload_event(self, mock_publish):
|
|
||||||
self.handler.max_availability = 100
|
|
||||||
self.handler.current_availability = 10
|
|
||||||
|
|
||||||
await self.handler.handle_backpressure_overload_event()
|
|
||||||
|
|
||||||
mock_publish.assert_called_once()
|
|
||||||
args = mock_publish.call_args[0][0]
|
|
||||||
self.assertEqual(args.requestType, ScalingRequestAlert.OVERLOAD)
|
|
||||||
self.assertEqual(args.current_availability, 10)
|
|
||||||
self.assertEqual(args.max_availability, 100)
|
|
||||||
|
|
||||||
@patch("amqp.adapter.backpressure_handler.BackpressureHandler.publish_backpressure_request")
|
|
||||||
async def test_handle_backpressure_idle_event(self, mock_publish):
|
|
||||||
self.handler.max_availability = 100
|
|
||||||
self.handler.current_availability = 90
|
|
||||||
|
|
||||||
await self.handler._handle_backpressure_idle_event()
|
|
||||||
|
|
||||||
mock_publish.assert_called_once()
|
|
||||||
args = mock_publish.call_args[0][0]
|
|
||||||
self.assertEqual(args.requestType, ScalingRequestAlert.IDLE)
|
|
||||||
self.assertEqual(args.current_availability, 90)
|
|
||||||
self.assertEqual(args.max_availability, 100)
|
|
||||||
|
|
||||||
@patch("asyncio.run_coroutine_threadsafe")
|
|
||||||
async def test_publish_backpressure_request(self, mock_run):
|
|
||||||
scaling_request = ScaleRequestV1(
|
|
||||||
"test-service", "test-task", 100, 50, ScalingRequestAlert.UPDATE
|
|
||||||
)
|
|
||||||
|
|
||||||
await self.handler.publish_backpressure_request(scaling_request)
|
|
||||||
|
|
||||||
# Check that run_coroutine_threadsafe was called
|
|
||||||
self.assertEqual(mock_run.call_count, 1)
|
|
||||||
|
|
||||||
# Check that the callback was recorded
|
|
||||||
self.assertIn("_wrap_rabbit_mq_api", BackpressureHandler._callback_list)
|
|
||||||
|
|
||||||
|
|
||||||
class TestScaleRequestV1(unittest.TestCase):
|
|
||||||
def test_to_json(self):
|
|
||||||
request = ScaleRequestV1("test-service", "test-task", 100, 50, ScalingRequestAlert.UPDATE)
|
|
||||||
json_str = request.to_json()
|
|
||||||
|
|
||||||
# Check that the JSON contains the expected fields
|
|
||||||
self.assertIn('"version": 1', json_str)
|
|
||||||
self.assertIn('"serviceId": "test-service"', json_str)
|
|
||||||
self.assertIn('"taskId": "test-task"', json_str)
|
|
||||||
self.assertIn('"maxAvailability": 100', json_str)
|
|
||||||
self.assertIn('"currentAvailability": 50', json_str)
|
|
||||||
self.assertIn(f'"requestType": "{ScalingRequestAlert.UPDATE.value}"', json_str)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
|
|||||||
@@ -1,203 +0,0 @@
|
|||||||
import unittest
|
|
||||||
from unittest.mock import MagicMock, call, patch
|
|
||||||
|
|
||||||
from amqp.adapter.consul_global_id_generator import (
|
|
||||||
generate_fallback_id,
|
|
||||||
get_config_values,
|
|
||||||
get_unique_instance_id,
|
|
||||||
)
|
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
|
|
||||||
|
|
||||||
class TestConsulGlobalIdGenerator(unittest.TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.mock_config = MagicMock()
|
|
||||||
self.mock_config.consul_host = "test-consul"
|
|
||||||
self.mock_config.consul_port = 8500
|
|
||||||
self.mock_config.consul_counter_key = "test/counter/key"
|
|
||||||
self.mock_config.consul_max_retries = 3
|
|
||||||
self.mock_config.consul_initial_retry_delay = 0.1
|
|
||||||
|
|
||||||
def test_get_config_values_from_config(self):
|
|
||||||
"""Test retrieving configuration values from AMQAdapter config."""
|
|
||||||
host, port, key, retries, delay = get_config_values(self.mock_config)
|
|
||||||
|
|
||||||
self.assertEqual(host, "test-consul")
|
|
||||||
self.assertEqual(port, 8500)
|
|
||||||
self.assertEqual(key, "test/counter/key")
|
|
||||||
self.assertEqual(retries, 3)
|
|
||||||
self.assertEqual(delay, 0.1)
|
|
||||||
|
|
||||||
@patch.dict(
|
|
||||||
"os.environ",
|
|
||||||
{
|
|
||||||
"CONSUL_HOST": "env-consul",
|
|
||||||
"CONSUL_PORT": "8501",
|
|
||||||
"CONSUL_COUNTER_KEY": "env/counter/key",
|
|
||||||
"CONSUL_MAX_RETRIES": "4",
|
|
||||||
"CONSUL_RETRY_DELAY_SECONDS": "0.2",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
def test_get_config_values_from_env(self):
|
|
||||||
"""Test retrieving configuration values from environment variables when config fails."""
|
|
||||||
self.mock_config = MagicMock(side_effect=Exception("Config error"))
|
|
||||||
|
|
||||||
host, port, key, retries, delay = get_config_values(AMQConfiguration("").amq_adapter)
|
|
||||||
|
|
||||||
self.assertEqual(host, "env-consul")
|
|
||||||
self.assertEqual(port, 8501)
|
|
||||||
self.assertEqual(key, "env/counter/key")
|
|
||||||
self.assertEqual(retries, 4)
|
|
||||||
self.assertEqual(delay, 0.2)
|
|
||||||
|
|
||||||
@patch("time.time", return_value=1000.0)
|
|
||||||
@patch("socket.gethostname", return_value="test-host")
|
|
||||||
@patch("random.randint", return_value=1234)
|
|
||||||
def test_generate_fallback_id(self, mock_randint, mock_hostname, mock_time):
|
|
||||||
"""Test fallback ID generation with controlled inputs."""
|
|
||||||
# Calculate expected value based on the mocked values
|
|
||||||
timestamp = int(1000.0 * 1000)
|
|
||||||
hostname_hash = hash("test-host") % 10000
|
|
||||||
random_part = 1234
|
|
||||||
expected_id = (timestamp * 100000000) + (hostname_hash * 10000) + random_part
|
|
||||||
|
|
||||||
result = generate_fallback_id()
|
|
||||||
|
|
||||||
self.assertEqual(result, expected_id)
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
def test_get_unique_instance_id_success(self, mock_connection):
|
|
||||||
"""Test successful ID retrieval from Consul."""
|
|
||||||
mock_consul = MagicMock()
|
|
||||||
mock_connection.return_value = mock_consul
|
|
||||||
|
|
||||||
# Mock the get and put methods
|
|
||||||
mock_data = {"Value": b"42", "ModifyIndex": 123}
|
|
||||||
mock_consul.get.return_value = (0, mock_data)
|
|
||||||
mock_consul.put.return_value = True
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return the incremented value
|
|
||||||
self.assertEqual(result, 43)
|
|
||||||
mock_connection.assert_called_once_with(endpoint="test-consul:8500", timeout=5)
|
|
||||||
mock_consul.get.assert_called_once_with("test/counter/key")
|
|
||||||
mock_consul.put.assert_called_once_with("test/counter/key", "43", cas=123)
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
def test_get_unique_instance_id_initialize(self, mock_connection):
|
|
||||||
"""Test initializing the counter when it doesn't exist."""
|
|
||||||
mock_consul = MagicMock()
|
|
||||||
mock_connection.return_value = mock_consul
|
|
||||||
|
|
||||||
# Return None to simulate counter not existing
|
|
||||||
mock_consul.get.return_value = (0, None)
|
|
||||||
mock_consul.put.return_value = True
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return 1 for the first ID
|
|
||||||
self.assertEqual(result, 1)
|
|
||||||
mock_consul.put.assert_called_once_with("test/counter/key", "1", cas=1)
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
@patch("amqp.adapter.consul_global_id_generator.generate_fallback_id", return_value=9999)
|
|
||||||
def test_get_unique_instance_id_initialize_failure(self, mock_fallback, mock_connection):
|
|
||||||
"""Test fallback when initializing the counter fails."""
|
|
||||||
mock_consul = MagicMock()
|
|
||||||
mock_connection.return_value = mock_consul
|
|
||||||
|
|
||||||
# Return None to simulate counter not existing
|
|
||||||
mock_consul.get.return_value = (0, None)
|
|
||||||
mock_consul.put.return_value = False # Initialization fails
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return fallback ID
|
|
||||||
self.assertEqual(result, 9999)
|
|
||||||
mock_fallback.assert_called_once()
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
@patch("time.sleep")
|
|
||||||
def test_get_unique_instance_id_retry_success(self, mock_sleep, mock_connection):
|
|
||||||
"""Test successful retry after CAS failure."""
|
|
||||||
mock_consul = MagicMock()
|
|
||||||
mock_connection.return_value = mock_consul
|
|
||||||
|
|
||||||
# First attempt fails, second succeeds
|
|
||||||
mock_data1 = {"Value": b"42", "ModifyIndex": 123}
|
|
||||||
mock_data2 = {"Value": b"43", "ModifyIndex": 124}
|
|
||||||
|
|
||||||
# First put fails, second succeeds
|
|
||||||
mock_consul.get.side_effect = [(0, mock_data1), (0, mock_data2)]
|
|
||||||
mock_consul.put.side_effect = [False, True]
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return the incremented value from the second attempt
|
|
||||||
self.assertEqual(result, 44)
|
|
||||||
self.assertEqual(mock_consul.get.call_count, 2)
|
|
||||||
self.assertEqual(mock_consul.put.call_count, 2)
|
|
||||||
mock_sleep.assert_called_once_with(0.1) # First retry delay
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
@patch("time.sleep")
|
|
||||||
@patch("amqp.adapter.consul_global_id_generator.generate_fallback_id", return_value=9999)
|
|
||||||
def test_get_unique_instance_id_max_retries(self, mock_fallback, mock_sleep, mock_connection):
|
|
||||||
"""Test fallback after max retries."""
|
|
||||||
mock_consul = MagicMock()
|
|
||||||
mock_connection.return_value = mock_consul
|
|
||||||
|
|
||||||
# All attempts fail
|
|
||||||
mock_data = {"Value": b"42", "ModifyIndex": 123}
|
|
||||||
mock_consul.get.return_value = (0, mock_data)
|
|
||||||
mock_consul.put.return_value = False
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return fallback ID
|
|
||||||
self.assertEqual(result, 9999)
|
|
||||||
self.assertEqual(mock_consul.put.call_count, 3) # 3 retries as configured
|
|
||||||
mock_fallback.assert_called_once()
|
|
||||||
|
|
||||||
# Check exponential backoff
|
|
||||||
expected_calls = [
|
|
||||||
call(0.1), # First retry
|
|
||||||
call(0.2), # Second retry (doubled)
|
|
||||||
]
|
|
||||||
mock_sleep.assert_has_calls(expected_calls)
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
@patch("amqp.adapter.consul_global_id_generator.generate_fallback_id", return_value=9999)
|
|
||||||
def test_get_unique_instance_id_consul_exception(self, mock_fallback, mock_connection):
|
|
||||||
"""Test fallback when Consul raises an exception."""
|
|
||||||
mock_connection.side_effect = Exception("Consul connection error")
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return fallback ID
|
|
||||||
self.assertEqual(result, 9999)
|
|
||||||
mock_fallback.assert_called_once()
|
|
||||||
|
|
||||||
@patch("consul_kv.Connection")
|
|
||||||
@patch("amqp.adapter.consul_global_id_generator.generate_fallback_id", return_value=9999)
|
|
||||||
def test_get_unique_instance_id_counter_disappeared(self, mock_fallback, mock_connection):
|
|
||||||
"""Test fallback when counter disappears during retry."""
|
|
||||||
mock_consul = MagicMock()
|
|
||||||
mock_connection.return_value = mock_consul
|
|
||||||
|
|
||||||
# First get succeeds, second returns None (counter disappeared)
|
|
||||||
mock_data = {"Value": b"42", "ModifyIndex": 123}
|
|
||||||
mock_consul.get.side_effect = [(0, mock_data), (0, None)]
|
|
||||||
mock_consul.put.return_value = False
|
|
||||||
|
|
||||||
result = get_unique_instance_id(self.mock_config)
|
|
||||||
|
|
||||||
# Should return fallback ID
|
|
||||||
self.assertEqual(result, 9999)
|
|
||||||
mock_fallback.assert_called_once()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -107,7 +107,7 @@ class DataMessageFactoryTest(TestCase):
|
|||||||
self.assertLessEqual(
|
self.assertLessEqual(
|
||||||
_x_timestamp - _timestamp,
|
_x_timestamp - _timestamp,
|
||||||
1,
|
1,
|
||||||
"Timestamp in SnowflakeId differs too much from current_availability time",
|
"Timestamp in SnowflakeId differs too much from current time",
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_to_string(self):
|
def test_to_string(self):
|
||||||
|
|||||||
@@ -1,145 +0,0 @@
|
|||||||
import importlib
|
|
||||||
import sys
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from amqp.service.amq_message_handler import invoke_command
|
|
||||||
|
|
||||||
|
|
||||||
class TestRPCInvocation(unittest.IsolatedAsyncioTestCase):
|
|
||||||
"""Test cases for the RPC invocation functionality in DataMessageHandler."""
|
|
||||||
|
|
||||||
async def asyncSetUp(self):
|
|
||||||
"""Set up the test environment with a dummy module."""
|
|
||||||
# Define a simple module string
|
|
||||||
dummy_module_content = """
|
|
||||||
import asyncio
|
|
||||||
|
|
||||||
# Global function
|
|
||||||
def greet_sync(name: str) -> str:
|
|
||||||
return f"Hello, {name} (sync global)!"
|
|
||||||
|
|
||||||
async def greet_async(name: str) -> str:
|
|
||||||
await asyncio.sleep(0.01) # Simulate async work
|
|
||||||
return f"Hello, {name} (async global)!"
|
|
||||||
|
|
||||||
# A simple class
|
|
||||||
class MyService:
|
|
||||||
def __init__(self):
|
|
||||||
self.service_name = "MyAwesomeService"
|
|
||||||
|
|
||||||
def process_data_sync(self, data: str, count: int) -> str:
|
|
||||||
return f"Service '{self.service_name}' processed '{data}' {count} times (sync method)."
|
|
||||||
|
|
||||||
async def process_data_async(self, data: str, delay: float) -> str:
|
|
||||||
await asyncio.sleep(delay) # Simulate async work
|
|
||||||
return f"Service '{self.service_name}' processed '{data}' after {delay}s (async method)."
|
|
||||||
|
|
||||||
def _private_method(self):
|
|
||||||
return "This is a private method."
|
|
||||||
"""
|
|
||||||
# Dynamically create a module object
|
|
||||||
spec = importlib.util.spec_from_loader("my_package.my_module", loader=None)
|
|
||||||
if spec is None:
|
|
||||||
raise ImportError("Could not create module spec for my_package.my_module")
|
|
||||||
|
|
||||||
self.my_module = importlib.util.module_from_spec(spec)
|
|
||||||
exec(dummy_module_content, self.my_module.__dict__)
|
|
||||||
sys.modules["my_package.my_module"] = (
|
|
||||||
self.my_module
|
|
||||||
) # Add to sys.modules for importlib to find
|
|
||||||
|
|
||||||
async def asyncTearDown(self):
|
|
||||||
"""Clean up after tests."""
|
|
||||||
# Remove the dummy module from sys.modules
|
|
||||||
if "my_package" in sys.modules:
|
|
||||||
del sys.modules["my_package"]
|
|
||||||
|
|
||||||
async def test_invoke_global_sync_function(self):
|
|
||||||
"""Test invoking a global synchronous function."""
|
|
||||||
result = await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="__global__",
|
|
||||||
function_name="greet_sync",
|
|
||||||
kwargs={"name": "Alice"},
|
|
||||||
)
|
|
||||||
self.assertEqual(result.body().decode(), "Hello, Alice (sync global)!")
|
|
||||||
|
|
||||||
async def test_invoke_global_async_function(self):
|
|
||||||
"""Test invoking a global asynchronous function."""
|
|
||||||
result = await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="__global__",
|
|
||||||
function_name="greet_async",
|
|
||||||
kwargs={"name": "Bob"},
|
|
||||||
)
|
|
||||||
self.assertEqual(result.body().decode(), "Hello, Bob (async global)!")
|
|
||||||
|
|
||||||
async def test_invoke_class_sync_method(self):
|
|
||||||
"""Test invoking a class synchronous method."""
|
|
||||||
result = await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="MyService",
|
|
||||||
function_name="process_data_sync",
|
|
||||||
kwargs={"data": "report", "count": 3},
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
result.body().decode(),
|
|
||||||
"Service 'MyAwesomeService' processed 'report' 3 times (sync method).",
|
|
||||||
)
|
|
||||||
|
|
||||||
async def test_invoke_class_async_method(self):
|
|
||||||
"""Test invoking a class asynchronous method."""
|
|
||||||
result = await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="MyService",
|
|
||||||
function_name="process_data_async",
|
|
||||||
kwargs={"data": "metrics", "delay": 0.01},
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
result.body().decode(),
|
|
||||||
"Service 'MyAwesomeService' processed 'metrics' after 0.01s (async method).",
|
|
||||||
)
|
|
||||||
|
|
||||||
async def test_package_not_found(self):
|
|
||||||
"""Test error handling when package is not found."""
|
|
||||||
with self.assertRaises(ModuleNotFoundError):
|
|
||||||
await invoke_command(
|
|
||||||
package_name="non_existent_package",
|
|
||||||
class_name="__global__",
|
|
||||||
function_name="some_func",
|
|
||||||
kwargs={},
|
|
||||||
)
|
|
||||||
|
|
||||||
async def test_class_not_found(self):
|
|
||||||
"""Test error handling when class is not found."""
|
|
||||||
with self.assertRaises(AttributeError):
|
|
||||||
await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="NonExistentClass",
|
|
||||||
function_name="some_method",
|
|
||||||
kwargs={},
|
|
||||||
)
|
|
||||||
|
|
||||||
async def test_method_not_found(self):
|
|
||||||
"""Test error handling when method is not found."""
|
|
||||||
with self.assertRaises(AttributeError):
|
|
||||||
await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="MyService",
|
|
||||||
function_name="non_existent_method",
|
|
||||||
kwargs={},
|
|
||||||
)
|
|
||||||
|
|
||||||
async def test_private_method_access(self):
|
|
||||||
"""Test that private methods can be accessed (Python doesn't enforce privacy)."""
|
|
||||||
result = await invoke_command(
|
|
||||||
package_name="my_package.my_module",
|
|
||||||
class_name="MyService",
|
|
||||||
function_name="_private_method",
|
|
||||||
kwargs={},
|
|
||||||
)
|
|
||||||
self.assertEqual(result.body().decode(), "This is a private method.")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -9,7 +9,6 @@ from amqp.model.model import (
|
|||||||
DataMessage,
|
DataMessage,
|
||||||
DataResponse,
|
DataResponse,
|
||||||
ScalingRequest,
|
ScalingRequest,
|
||||||
ScalingRequestAlert,
|
|
||||||
)
|
)
|
||||||
from amqp.model.snowflake_id import SnowflakeId
|
from amqp.model.snowflake_id import SnowflakeId
|
||||||
|
|
||||||
@@ -159,9 +158,7 @@ class TestScalingRequest(unittest.TestCase):
|
|||||||
self.request = ScalingRequest(
|
self.request = ScalingRequest(
|
||||||
service_id="test_service",
|
service_id="test_service",
|
||||||
task_id="test_task",
|
task_id="test_task",
|
||||||
max_availability=5,
|
|
||||||
current_availability=3,
|
current_availability=3,
|
||||||
request_type=ScalingRequestAlert.OVERLOAD,
|
|
||||||
version=1,
|
version=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -171,9 +168,7 @@ class TestScalingRequest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(data["serviceId"], "test_service")
|
self.assertEqual(data["serviceId"], "test_service")
|
||||||
self.assertEqual(data["taskId"], "test_task")
|
self.assertEqual(data["taskId"], "test_task")
|
||||||
self.assertEqual(data["maxAvailability"], 5)
|
|
||||||
self.assertEqual(data["currentAvailability"], 3)
|
self.assertEqual(data["currentAvailability"], 3)
|
||||||
self.assertEqual(data["requestType"], "OVERLOAD")
|
|
||||||
self.assertEqual(data["version"], 1)
|
self.assertEqual(data["version"], 1)
|
||||||
|
|
||||||
def test_from_json(self):
|
def test_from_json(self):
|
||||||
@@ -181,9 +176,7 @@ class TestScalingRequest(unittest.TestCase):
|
|||||||
{
|
{
|
||||||
"serviceId": "test_service",
|
"serviceId": "test_service",
|
||||||
"taskId": "test_task",
|
"taskId": "test_task",
|
||||||
"maxAvailability": 5,
|
|
||||||
"currentAvailability": 3,
|
"currentAvailability": 3,
|
||||||
"requestType": "OVERLOAD",
|
|
||||||
"version": 1,
|
"version": 1,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -192,9 +185,7 @@ class TestScalingRequest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(request.service_id, "test_service")
|
self.assertEqual(request.service_id, "test_service")
|
||||||
self.assertEqual(request.task_id, "test_task")
|
self.assertEqual(request.task_id, "test_task")
|
||||||
self.assertEqual(request.max_availability, 5)
|
|
||||||
self.assertEqual(request.current_availability, 3)
|
self.assertEqual(request.current_availability, 3)
|
||||||
self.assertEqual(request.request_type, ScalingRequestAlert.OVERLOAD)
|
|
||||||
self.assertEqual(request.version, 1)
|
self.assertEqual(request.version, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,285 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import unittest
|
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
|
||||||
|
|
||||||
from aio_pika import Message
|
|
||||||
|
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
from amqp.rabbitmq.user_management_service_client import (
|
|
||||||
UserManagementServiceClient,
|
|
||||||
UserManagementServiceException
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestUserManagementServiceClient(unittest.IsolatedAsyncioTestCase):
|
|
||||||
def setUp(self):
|
|
||||||
# Mock configuration
|
|
||||||
self.config = MagicMock(spec=AMQConfiguration)
|
|
||||||
self.config.dispatch = MagicMock()
|
|
||||||
self.config.dispatch.amq_host = "localhost"
|
|
||||||
self.config.dispatch.amq_port = 5672
|
|
||||||
self.config.dispatch.rabbit_mq_user = "guest"
|
|
||||||
self.config.dispatch.rabbit_mq_password = "guest"
|
|
||||||
|
|
||||||
# Create client
|
|
||||||
self.client = UserManagementServiceClient(self.config)
|
|
||||||
|
|
||||||
# Mock connection and channel
|
|
||||||
self.client.connection = MagicMock()
|
|
||||||
self.client.channel = MagicMock()
|
|
||||||
self.client.exchange = MagicMock()
|
|
||||||
self.client.exchange.publish = AsyncMock()
|
|
||||||
self.client.response_queue = MagicMock()
|
|
||||||
self.client.initialized = True
|
|
||||||
|
|
||||||
async def test_initialize(self):
|
|
||||||
# Mock aio_pika.connect_robust
|
|
||||||
with patch('aio_pika.connect_robust', new_callable=AsyncMock) as mock_connect:
|
|
||||||
# Mock connection
|
|
||||||
mock_connection = MagicMock()
|
|
||||||
mock_connect.return_value = mock_connection
|
|
||||||
|
|
||||||
# Mock channel - create an AsyncMock that returns the channel when awaited
|
|
||||||
mock_channel = AsyncMock()
|
|
||||||
mock_connection.channel = AsyncMock()
|
|
||||||
mock_connection.channel.return_value = mock_channel
|
|
||||||
mock_channel.set_qos = AsyncMock()
|
|
||||||
|
|
||||||
# Mock exchange
|
|
||||||
mock_exchange = MagicMock()
|
|
||||||
mock_channel.declare_exchange = AsyncMock(return_value=mock_exchange)
|
|
||||||
|
|
||||||
# Mock queue
|
|
||||||
mock_queue = MagicMock()
|
|
||||||
mock_channel.declare_queue = AsyncMock(return_value=mock_queue)
|
|
||||||
mock_queue.consume = AsyncMock(return_value="consumer-tag")
|
|
||||||
|
|
||||||
# Reset client
|
|
||||||
self.client.initialized = False
|
|
||||||
self.client.connection = None
|
|
||||||
self.client.channel = None
|
|
||||||
self.client.exchange = None
|
|
||||||
self.client.response_queue = None
|
|
||||||
|
|
||||||
# Call initialize
|
|
||||||
await self.client.initialize()
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
mock_connect.assert_called_once_with(
|
|
||||||
host=self.config.dispatch.amq_host,
|
|
||||||
port=self.config.dispatch.amq_port,
|
|
||||||
login=self.config.dispatch.rabbit_mq_user,
|
|
||||||
password=self.config.dispatch.rabbit_mq_password,
|
|
||||||
loop=self.client.loop
|
|
||||||
)
|
|
||||||
mock_connection.channel.assert_called_once()
|
|
||||||
mock_channel.set_qos.assert_called_once_with(prefetch_count=1)
|
|
||||||
mock_channel.declare_exchange.assert_called_once()
|
|
||||||
mock_channel.declare_queue.assert_called_once()
|
|
||||||
mock_queue.consume.assert_called_once()
|
|
||||||
self.assertTrue(self.client.initialized)
|
|
||||||
|
|
||||||
async def test_call_service_success(self):
|
|
||||||
# Create a future for the response
|
|
||||||
future = asyncio.Future()
|
|
||||||
future.set_result({"type": "OK", "result": [{"name": "John Doe"}]})
|
|
||||||
|
|
||||||
# Mock the futures dictionary
|
|
||||||
with patch.dict(self.client.futures, {}, clear=True):
|
|
||||||
# Mock uuid.uuid4
|
|
||||||
with patch('uuid.uuid4', return_value="test-correlation-id"):
|
|
||||||
# Mock loop.create_future
|
|
||||||
with patch.object(self.client.loop, 'create_future', return_value=future):
|
|
||||||
# Call service
|
|
||||||
result = await self.client.call_service(
|
|
||||||
service=UserManagementServiceClient.TOKEN_SERVICE,
|
|
||||||
method="validateToken",
|
|
||||||
args={"token": "test-token"}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.client.exchange.publish.assert_called_once()
|
|
||||||
self.assertEqual(result, [{"name": "John Doe"}])
|
|
||||||
|
|
||||||
async def test_call_service_error(self):
|
|
||||||
# Create a future for the response
|
|
||||||
future = asyncio.Future()
|
|
||||||
future.set_result({
|
|
||||||
"type": "ERROR",
|
|
||||||
"exception": "cleverthis.clevermicro.auth.invalid_token",
|
|
||||||
"message": "Invalid token",
|
|
||||||
"additional_info": {"token": "test-token"}
|
|
||||||
})
|
|
||||||
|
|
||||||
# Mock the futures dictionary
|
|
||||||
with patch.dict(self.client.futures, {}, clear=True):
|
|
||||||
# Mock uuid.uuid4
|
|
||||||
with patch('uuid.uuid4', return_value="test-correlation-id"):
|
|
||||||
# Mock loop.create_future
|
|
||||||
with patch.object(self.client.loop, 'create_future', return_value=future):
|
|
||||||
# Call service and expect exception
|
|
||||||
with self.assertRaises(UserManagementServiceException) as context:
|
|
||||||
await self.client.call_service(
|
|
||||||
service=UserManagementServiceClient.TOKEN_SERVICE,
|
|
||||||
method="validateToken",
|
|
||||||
args={"token": "test-token"}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify exception
|
|
||||||
exception = context.exception
|
|
||||||
self.assertEqual(exception.exception_type, "cleverthis.clevermicro.auth.invalid_token")
|
|
||||||
self.assertEqual(exception.message, "Invalid token")
|
|
||||||
self.assertEqual(exception.additional_info, {"token": "test-token"})
|
|
||||||
|
|
||||||
async def test_on_response_callback(self):
|
|
||||||
# Create a future
|
|
||||||
future = asyncio.Future()
|
|
||||||
|
|
||||||
# Add future to futures dictionary
|
|
||||||
self.client.futures["test-correlation-id"] = future
|
|
||||||
|
|
||||||
# Create a mock message
|
|
||||||
message = MagicMock()
|
|
||||||
message.correlation_id = "test-correlation-id"
|
|
||||||
message.body = json.dumps({"type": "OK", "result": [{"name": "John Doe"}]}).encode('utf-8')
|
|
||||||
message.ack = AsyncMock()
|
|
||||||
|
|
||||||
# Call callback
|
|
||||||
await self.client._on_response_callback(message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
message.ack.assert_called_once()
|
|
||||||
self.assertTrue(future.done())
|
|
||||||
self.assertEqual(future.result(), {"type": "OK", "result": [{"name": "John Doe"}]})
|
|
||||||
|
|
||||||
async def test_on_response_callback_no_correlation_id(self):
|
|
||||||
# Create a mock message with no correlation ID
|
|
||||||
message = MagicMock()
|
|
||||||
message.correlation_id = None
|
|
||||||
message.ack = AsyncMock()
|
|
||||||
|
|
||||||
# Call callback
|
|
||||||
await self.client._on_response_callback(message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
message.ack.assert_called_once()
|
|
||||||
|
|
||||||
async def test_on_response_callback_no_future(self):
|
|
||||||
# Create a mock message with unknown correlation ID
|
|
||||||
message = MagicMock()
|
|
||||||
message.correlation_id = "unknown-correlation-id"
|
|
||||||
message.ack = AsyncMock()
|
|
||||||
|
|
||||||
# Call callback
|
|
||||||
await self.client._on_response_callback(message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
message.ack.assert_called_once()
|
|
||||||
|
|
||||||
async def test_validate_token(self):
|
|
||||||
# Mock call_service
|
|
||||||
with patch.object(self.client, 'call_service', new_callable=AsyncMock) as mock_call:
|
|
||||||
mock_call.return_value = [{"sub": "user-123", "name": "John Doe"}]
|
|
||||||
|
|
||||||
# Call validate_token
|
|
||||||
result = await self.client.validate_token("test-token")
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
mock_call.assert_called_once_with(
|
|
||||||
service=UserManagementServiceClient.TOKEN_SERVICE,
|
|
||||||
method="validateToken",
|
|
||||||
args={"token": "test-token"}
|
|
||||||
)
|
|
||||||
self.assertEqual(result, [{"sub": "user-123", "name": "John Doe"}])
|
|
||||||
|
|
||||||
async def test_query_user_by_id(self):
|
|
||||||
# Mock call_service
|
|
||||||
with patch.object(self.client, 'call_service', new_callable=AsyncMock) as mock_call:
|
|
||||||
mock_call.return_value = [{"id": "user-123", "name": "John Doe", "email": "john@example.com"}]
|
|
||||||
|
|
||||||
# Call query_user_by_id
|
|
||||||
result = await self.client.query_user_by_id("user-123")
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
mock_call.assert_called_once_with(
|
|
||||||
service=UserManagementServiceClient.USER_SERVICE,
|
|
||||||
method="queryUserById",
|
|
||||||
args={"userId": "user-123"}
|
|
||||||
)
|
|
||||||
self.assertEqual(result, [{"id": "user-123", "name": "John Doe", "email": "john@example.com"}])
|
|
||||||
|
|
||||||
async def test_close(self):
|
|
||||||
# Mock connection
|
|
||||||
self.client.connection.is_closed = False
|
|
||||||
self.client.connection.close = AsyncMock()
|
|
||||||
|
|
||||||
# Call close
|
|
||||||
await self.client.close()
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.client.connection.close.assert_called_once()
|
|
||||||
self.assertFalse(self.client.initialized)
|
|
||||||
|
|
||||||
|
|
||||||
class TestGetUserInfoFromToken(unittest.IsolatedAsyncioTestCase):
|
|
||||||
async def test_get_user_info_from_token_success(self):
|
|
||||||
# Mock client
|
|
||||||
client = MagicMock(spec=UserManagementServiceClient)
|
|
||||||
client.validate_token = AsyncMock(return_value=[{"sub": "user-123"}])
|
|
||||||
client.query_user_by_id = AsyncMock(return_value=[{"id": "user-123", "name": "John Doe"}])
|
|
||||||
|
|
||||||
# Import the function
|
|
||||||
from amqp.rabbitmq.user_management_sample import get_user_info_from_token
|
|
||||||
|
|
||||||
# Call function
|
|
||||||
result = await get_user_info_from_token(client, "test-token")
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
client.validate_token.assert_called_once_with("test-token")
|
|
||||||
client.query_user_by_id.assert_called_once_with("user-123")
|
|
||||||
self.assertEqual(result, [{"id": "user-123", "name": "John Doe"}])
|
|
||||||
|
|
||||||
async def test_get_user_info_from_token_no_user_id(self):
|
|
||||||
# Mock client
|
|
||||||
client = MagicMock(spec=UserManagementServiceClient)
|
|
||||||
client.validate_token = AsyncMock(return_value=[{"name": "John Doe"}]) # No sub claim
|
|
||||||
|
|
||||||
# Import the function
|
|
||||||
from amqp.rabbitmq.user_management_sample import get_user_info_from_token
|
|
||||||
|
|
||||||
# Call function and expect exception
|
|
||||||
with self.assertRaises(UserManagementServiceException) as context:
|
|
||||||
await get_user_info_from_token(client, "test-token")
|
|
||||||
|
|
||||||
# Verify exception
|
|
||||||
exception = context.exception
|
|
||||||
self.assertEqual(exception.exception_type, "cleverthis.clevermicro.auth.invalid_token")
|
|
||||||
self.assertEqual(exception.message, "Token does not contain user ID (sub claim)")
|
|
||||||
|
|
||||||
async def test_get_user_info_from_token_service_exception(self):
|
|
||||||
# Mock client
|
|
||||||
client = MagicMock(spec=UserManagementServiceClient)
|
|
||||||
client.validate_token = AsyncMock(
|
|
||||||
side_effect=UserManagementServiceException(
|
|
||||||
"cleverthis.clevermicro.auth.invalid_token",
|
|
||||||
"Invalid token",
|
|
||||||
{}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Import the function
|
|
||||||
from amqp.rabbitmq.user_management_sample import get_user_info_from_token
|
|
||||||
|
|
||||||
# Call function and expect exception
|
|
||||||
with self.assertRaises(UserManagementServiceException) as context:
|
|
||||||
await get_user_info_from_token(client, "test-token")
|
|
||||||
|
|
||||||
# Verify exception
|
|
||||||
exception = context.exception
|
|
||||||
self.assertEqual(exception.exception_type, "cleverthis.clevermicro.auth.invalid_token")
|
|
||||||
self.assertEqual(exception.message, "Invalid token")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,419 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
import unittest
|
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
|
||||||
|
|
||||||
from amqp.adapter.backpressure_handler import BackpressureHandler
|
|
||||||
from amqp.adapter.cleverthis_service_adapter import CleverThisServiceAdapter
|
|
||||||
from amqp.adapter.data_message_factory import DataMessageFactory
|
|
||||||
from amqp.adapter.file_handler import FileHandler
|
|
||||||
from amqp.adapter.service_message_factory import ServiceMessageFactory
|
|
||||||
from amqp.config.amq_configuration import AMQConfiguration
|
|
||||||
from amqp.model.model import (
|
|
||||||
DataMessage,
|
|
||||||
DataMessageMagicByte,
|
|
||||||
DataResponse,
|
|
||||||
MultipartDataMessage,
|
|
||||||
)
|
|
||||||
from amqp.model.snowflake_id import SnowflakeId
|
|
||||||
from amqp.rabbitmq.rabbit_mq_client import RabbitMQClient
|
|
||||||
from amqp.service.amq_message_handler import (
|
|
||||||
DataMessageHandler,
|
|
||||||
collect_trace_info,
|
|
||||||
get_default_trace_state,
|
|
||||||
set_text,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDataMessageHandler(unittest.IsolatedAsyncioTestCase):
|
|
||||||
def setUp(self):
|
|
||||||
# Mock dependencies
|
|
||||||
self.service_adapter = MagicMock(spec=CleverThisServiceAdapter)
|
|
||||||
self.service_adapter.on_message = AsyncMock()
|
|
||||||
|
|
||||||
self.tracer = MagicMock()
|
|
||||||
self.tracer.start_span = MagicMock()
|
|
||||||
|
|
||||||
self.message_factory = MagicMock(spec=DataMessageFactory)
|
|
||||||
self.service_message_factory = MagicMock(spec=ServiceMessageFactory)
|
|
||||||
self.reply_to_exchange = MagicMock()
|
|
||||||
self.reply_to_exchange.publish = AsyncMock()
|
|
||||||
self.reply_to_exchange.name = "test-exchange"
|
|
||||||
|
|
||||||
self.rabbit_mq_client = MagicMock(spec=RabbitMQClient)
|
|
||||||
self.rabbit_mq_client.connection = MagicMock()
|
|
||||||
|
|
||||||
self.amq_configuration = MagicMock(spec=AMQConfiguration)
|
|
||||||
self.amq_configuration.dispatch = MagicMock()
|
|
||||||
self.amq_configuration.dispatch.rabbit_mq_url = "amqp://guest:guest@localhost:5672/"
|
|
||||||
self.amq_configuration.dispatch.download_dir = "/tmp/test_downloads"
|
|
||||||
|
|
||||||
self.loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
self.backpressure_handler = MagicMock(spec=BackpressureHandler)
|
|
||||||
self.backpressure_handler.increase_current_load = MagicMock()
|
|
||||||
self.backpressure_handler.decrease_current_load = MagicMock()
|
|
||||||
self.backpressure_handler.check_overload_condition = AsyncMock()
|
|
||||||
self.backpressure_handler.current_availability = 5
|
|
||||||
self.backpressure_handler.max_availability = 10
|
|
||||||
|
|
||||||
self.file_handler = MagicMock(spec=FileHandler)
|
|
||||||
self.file_handler.on_message = AsyncMock(return_value={})
|
|
||||||
|
|
||||||
# Create the handler
|
|
||||||
self.handler = DataMessageHandler(
|
|
||||||
service_adapter=self.service_adapter,
|
|
||||||
tracer=self.tracer,
|
|
||||||
message_factory=self.message_factory,
|
|
||||||
service_message_factory=self.service_message_factory,
|
|
||||||
reply_to_exchange=self.reply_to_exchange,
|
|
||||||
rabbit_mq_client=self.rabbit_mq_client,
|
|
||||||
amq_configuration=self.amq_configuration,
|
|
||||||
loop=self.loop,
|
|
||||||
backpressure_handler=self.backpressure_handler,
|
|
||||||
file_handler=self.file_handler,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create a mock message
|
|
||||||
self.mock_message = MagicMock()
|
|
||||||
self.mock_message.delivery_tag = 123
|
|
||||||
self.mock_message.consumer_tag = "consumer-tag"
|
|
||||||
self.mock_message.reply_to = "reply-queue"
|
|
||||||
self.mock_message.correlation_id = "correlation-id"
|
|
||||||
self.mock_message.headers = {"clevermicro.addressing": 0}
|
|
||||||
self.mock_message.properties = {}
|
|
||||||
self.mock_message.body = b"test-body"
|
|
||||||
self.mock_message.ack = AsyncMock()
|
|
||||||
self.mock_message.nack = AsyncMock()
|
|
||||||
|
|
||||||
# Create a mock data message
|
|
||||||
self.mock_data_message = MagicMock(spec=DataMessage)
|
|
||||||
self.mock_data_message.magic.return_value = DataMessageMagicByte.HTTP_REQUEST.value
|
|
||||||
self.mock_data_message.id.return_value = SnowflakeId.from_hex("1234567890abcdef1234")
|
|
||||||
self.mock_data_message.method.return_value = "GET"
|
|
||||||
self.mock_data_message.path.return_value = "/test/path"
|
|
||||||
self.mock_data_message.trace_info.return_value = {
|
|
||||||
"traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
|
|
||||||
}
|
|
||||||
self.mock_data_message.body.return_value = b'{"test": "data"}'
|
|
||||||
|
|
||||||
# Create a mock data response
|
|
||||||
self.mock_data_response = MagicMock(spec=DataResponse)
|
|
||||||
self.mock_data_response.id.return_value = "response-id"
|
|
||||||
self.mock_data_response.content_type.return_value = "application/json"
|
|
||||||
|
|
||||||
# Patch DataMessageFactory.from_bytes
|
|
||||||
self.from_bytes_patch = patch(
|
|
||||||
"amqp.adapter.data_message_factory.DataMessageFactory.from_bytes",
|
|
||||||
return_value=self.mock_data_message,
|
|
||||||
)
|
|
||||||
self.mock_from_bytes = self.from_bytes_patch.start()
|
|
||||||
|
|
||||||
# Patch DataMessageFactory.from_stream
|
|
||||||
self.from_stream_patch = patch(
|
|
||||||
"amqp.adapter.data_message_factory.DataMessageFactory.from_stream",
|
|
||||||
return_value=self.mock_data_message,
|
|
||||||
)
|
|
||||||
self.mock_from_stream = self.from_stream_patch.start()
|
|
||||||
|
|
||||||
# Patch DataResponseFactory.serialize
|
|
||||||
self.serialize_patch = patch(
|
|
||||||
"amqp.adapter.data_response_factory.DataResponseFactory.serialize",
|
|
||||||
return_value=b"serialized-response",
|
|
||||||
)
|
|
||||||
self.mock_serialize = self.serialize_patch.start()
|
|
||||||
|
|
||||||
# Patch DataResponseFactory.from_bytes
|
|
||||||
self.response_from_bytes_patch = patch(
|
|
||||||
"amqp.adapter.data_response_factory.DataResponseFactory.from_bytes",
|
|
||||||
return_value=self.mock_data_response,
|
|
||||||
)
|
|
||||||
self.mock_response_from_bytes = self.response_from_bytes_patch.start()
|
|
||||||
|
|
||||||
# Patch asyncio.run_coroutine_threadsafe
|
|
||||||
self.run_coroutine_patch = patch("asyncio.run_coroutine_threadsafe")
|
|
||||||
self.mock_run_coroutine = self.run_coroutine_patch.start()
|
|
||||||
|
|
||||||
# Patch time.time
|
|
||||||
self.time_patch = patch("time.time", return_value=1234567890.0)
|
|
||||||
self.mock_time = self.time_patch.start()
|
|
||||||
|
|
||||||
# Patch os.remove
|
|
||||||
self.os_remove_patch = patch("os.remove")
|
|
||||||
self.mock_os_remove = self.os_remove_patch.start()
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.from_bytes_patch.stop()
|
|
||||||
self.from_stream_patch.stop()
|
|
||||||
self.serialize_patch.stop()
|
|
||||||
self.response_from_bytes_patch.stop()
|
|
||||||
self.run_coroutine_patch.stop()
|
|
||||||
self.time_patch.stop()
|
|
||||||
self.os_remove_patch.stop()
|
|
||||||
|
|
||||||
async def test_inbound_data_message_callback_no_thread_success(self):
|
|
||||||
"""Test successful processing of an inbound message."""
|
|
||||||
# Setup
|
|
||||||
self.service_adapter.on_message.return_value = self.mock_data_response
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.inbound_data_message_callback_no_thread(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_from_bytes.assert_called_once_with(self.mock_message.body)
|
|
||||||
self.tracer.start_span.assert_called_once()
|
|
||||||
self.backpressure_handler.increase_current_load.assert_called_once()
|
|
||||||
self.service_adapter.on_message.assert_called_once()
|
|
||||||
self.reply_to_exchange.publish.assert_called_once()
|
|
||||||
self.backpressure_handler.decrease_current_load.assert_called_once()
|
|
||||||
self.mock_message.ack.assert_called_once_with(multiple=False)
|
|
||||||
|
|
||||||
async def test_inbound_data_message_callback_no_thread_unknown_magic(self):
|
|
||||||
"""Test handling of a message with unknown magic value."""
|
|
||||||
# Setup
|
|
||||||
self.mock_data_message.magic.return_value = "UNKNOWN"
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.inbound_data_message_callback_no_thread(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_message.nack.assert_called_once_with(requeue=False)
|
|
||||||
# self.mock_message.ack.assert_called_once_with(multiple=False)
|
|
||||||
|
|
||||||
async def test_inbound_data_message_callback_no_thread_no_message(self):
|
|
||||||
"""Test handling when no valid message is present."""
|
|
||||||
# Setup
|
|
||||||
self.mock_from_bytes.return_value = None
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.inbound_data_message_callback_no_thread(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_message.ack.assert_called_once_with(multiple=False)
|
|
||||||
|
|
||||||
async def test_inbound_data_message_callback_as_thread(self):
|
|
||||||
"""Test the thread-based callback method."""
|
|
||||||
# Setup
|
|
||||||
with patch("threading.Thread") as mock_thread:
|
|
||||||
mock_thread_instance = MagicMock()
|
|
||||||
mock_thread.return_value = mock_thread_instance
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.inbound_data_message_callback_as_thread(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
mock_thread.assert_called_once()
|
|
||||||
mock_thread_instance.start.assert_called_once()
|
|
||||||
|
|
||||||
async def test_reconstitute_data_message_standard(self):
|
|
||||||
"""Test reconstituting a standard data message."""
|
|
||||||
# Execute
|
|
||||||
result = await self.handler.reconstitute_data_message(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.assertEqual(result, self.mock_data_message)
|
|
||||||
self.mock_from_bytes.assert_called_once_with(self.mock_message.body)
|
|
||||||
|
|
||||||
async def test_reconstitute_data_message_stream(self):
|
|
||||||
"""Test reconstituting a message from a stream."""
|
|
||||||
# Setup
|
|
||||||
self.mock_message.headers = {"clevermicro.addressing": 1}
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
result = await self.handler.reconstitute_data_message(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.assertEqual(MultipartDataMessage, result.__class__)
|
|
||||||
self.mock_from_stream.assert_called_once()
|
|
||||||
self.file_handler.on_message.assert_called_once()
|
|
||||||
|
|
||||||
async def test_run_http_request_magic_success(self):
|
|
||||||
"""Test successful processing of an HTTP request."""
|
|
||||||
# Setup
|
|
||||||
self.service_adapter.on_message.return_value = self.mock_data_response
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.run_http_request_magic(
|
|
||||||
self.mock_message, self.mock_data_message, self.loop
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.backpressure_handler.increase_current_load.assert_called_once()
|
|
||||||
self.backpressure_handler.check_overload_condition.assert_called()
|
|
||||||
self.service_adapter.on_message.assert_called_once()
|
|
||||||
self.reply_to_exchange.publish.assert_called_once()
|
|
||||||
self.backpressure_handler.decrease_current_load.assert_called_once()
|
|
||||||
|
|
||||||
async def test_run_http_request_magic_multipart(self):
|
|
||||||
"""Test processing a multipart message with file cleanup."""
|
|
||||||
# Setup
|
|
||||||
self.service_adapter.on_message.return_value = self.mock_data_response
|
|
||||||
multipart_message = MultipartDataMessage(
|
|
||||||
self.mock_data_message, {"file1": "/tmp/file1.txt"}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.run_http_request_magic(self.mock_message, multipart_message, self.loop)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_os_remove.assert_called_once_with("/tmp/file1.txt")
|
|
||||||
|
|
||||||
async def test_run_http_request_magic_service_exception(self):
|
|
||||||
"""Test handling of an exception from the service adapter."""
|
|
||||||
# Setup
|
|
||||||
self.service_adapter.on_message.side_effect = Exception("Service error")
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.run_http_request_magic(
|
|
||||||
self.mock_message, self.mock_data_message, self.loop
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.backpressure_handler.decrease_current_load.assert_called_once()
|
|
||||||
self.backpressure_handler.check_overload_condition.assert_called()
|
|
||||||
|
|
||||||
async def test_run_http_request_magic_reply_exception(self):
|
|
||||||
"""Test handling of an exception when sending the reply."""
|
|
||||||
# Setup
|
|
||||||
self.service_adapter.on_message.return_value = self.mock_data_response
|
|
||||||
self.reply_to_exchange.publish.side_effect = Exception("Reply error")
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.run_http_request_magic(
|
|
||||||
self.mock_message, self.mock_data_message, self.loop
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
# [no ACK at this level, ACK is in parent call] self.mock_message.ack.assert_called_once_with(requeue=False)
|
|
||||||
self.backpressure_handler.decrease_current_load.assert_called_once()
|
|
||||||
|
|
||||||
async def test_send_reply_success(self):
|
|
||||||
"""Test successful sending of a reply."""
|
|
||||||
# Execute
|
|
||||||
await self.handler.send_reply("reply-queue", self.mock_data_response)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_run_coroutine.assert_called_once()
|
|
||||||
|
|
||||||
async def test_send_reply_no_reply_to(self):
|
|
||||||
"""Test handling when no reply_to is provided."""
|
|
||||||
# Execute
|
|
||||||
await self.handler.send_reply(None, self.mock_data_response)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_run_coroutine.assert_not_called()
|
|
||||||
|
|
||||||
async def test_reply_received_callback_with_future(self):
|
|
||||||
"""Test handling a reply when there's a matching future."""
|
|
||||||
# Setup
|
|
||||||
future = asyncio.Future()
|
|
||||||
self.handler.outstanding["correlation-id"] = future
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.reply_received_callback(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.assertTrue(future.done())
|
|
||||||
self.assertEqual(future.result(), self.mock_data_response.body())
|
|
||||||
self.mock_message.ack.assert_called_once_with(multiple=False)
|
|
||||||
|
|
||||||
async def test_reply_received_callback_no_future(self):
|
|
||||||
"""Test handling a reply when there's no matching future."""
|
|
||||||
# Execute
|
|
||||||
await self.handler.reply_received_callback(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_message.ack.assert_called_once_with(multiple=False)
|
|
||||||
|
|
||||||
def test_ensure_trace_info(self):
|
|
||||||
"""Test ensuring trace info is properly set."""
|
|
||||||
# Execute
|
|
||||||
self.handler.ensure_trace_info(self.mock_data_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.tracer.start_span.assert_called_once()
|
|
||||||
|
|
||||||
def test_record_duration(self):
|
|
||||||
"""Test recording the duration of message processing."""
|
|
||||||
# Setup
|
|
||||||
start_time = 1234567880.0 # 10 seconds before mock_time
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
self.handler.record_duration(start_time, 123)
|
|
||||||
|
|
||||||
# No specific assertions needed as this is mostly logging
|
|
||||||
|
|
||||||
def test_collect_trace_info(self):
|
|
||||||
"""Test collecting trace info."""
|
|
||||||
# Execute
|
|
||||||
result = collect_trace_info()
|
|
||||||
|
|
||||||
# Verify it returns a dictionary
|
|
||||||
self.assertIsInstance(result, dict)
|
|
||||||
|
|
||||||
def test_get_default_trace_state(self):
|
|
||||||
"""Test getting default trace state."""
|
|
||||||
# Execute
|
|
||||||
result = get_default_trace_state()
|
|
||||||
|
|
||||||
# Verify it returns a TraceState
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_set_text(self):
|
|
||||||
"""Test setting text in a carrier."""
|
|
||||||
# Setup
|
|
||||||
carrier = {}
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
set_text(carrier, "key", "value")
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.assertEqual(carrier["key"], "value")
|
|
||||||
|
|
||||||
async def test_reconstitute_data_message_stream_none_result(self):
|
|
||||||
"""Test reconstituting a message from a stream when the result is None."""
|
|
||||||
# Setup
|
|
||||||
self.mock_message.headers = {"clevermicro.addressing": 1}
|
|
||||||
self.mock_from_stream.return_value = None
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
result = await self.handler.reconstitute_data_message(self.mock_message)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.assertIsNone(result)
|
|
||||||
self.mock_from_stream.assert_called_once()
|
|
||||||
self.file_handler.on_message.assert_not_called()
|
|
||||||
|
|
||||||
async def test_run_http_request_magic_file_removal_exception(self):
|
|
||||||
"""Test handling of an exception when removing files."""
|
|
||||||
# Setup
|
|
||||||
self.service_adapter.on_message.return_value = self.mock_data_response
|
|
||||||
multipart_message = MultipartDataMessage(
|
|
||||||
self.mock_data_message, {"file1": "/tmp/file1.txt"}
|
|
||||||
)
|
|
||||||
self.mock_os_remove.side_effect = OSError("File removal error")
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.run_http_request_magic(self.mock_message, multipart_message, self.loop)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_os_remove.assert_called_once_with("/tmp/file1.txt")
|
|
||||||
# Should continue without raising an exception
|
|
||||||
|
|
||||||
async def test_send_reply_content_type_list(self):
|
|
||||||
"""Test sending a reply with content type as a list."""
|
|
||||||
# Setup
|
|
||||||
self.mock_data_response.content_type.return_value = ["application/json", "text/plain"]
|
|
||||||
|
|
||||||
# Execute
|
|
||||||
await self.handler.send_reply("reply-queue", self.mock_data_response)
|
|
||||||
|
|
||||||
# Verify
|
|
||||||
self.mock_run_coroutine.assert_called_once()
|
|
||||||
# We verify that Message is created with correct content type by inspecting the RabbitMQ call args if available.
|
|
||||||
self.assertEqual(
|
|
||||||
"application/json", self.reply_to_exchange.publish.call_args[1]["message"].content_type
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,87 +1,184 @@
|
|||||||
import asyncio
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
|
from amqp.config.amq_configuration import AMQAdapter, AMQConfiguration
|
||||||
from amqp.service.amq_service import AMQService
|
from amqp.service.amq_service import AMQService
|
||||||
|
|
||||||
|
|
||||||
class TestAMQService(unittest.IsolatedAsyncioTestCase):
|
class TestAMQService(unittest.IsolatedAsyncioTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Mock the configuration
|
# Create a mock configuration
|
||||||
self.config = MagicMock()
|
self.mock_adapter = Mock(spec=AMQAdapter)
|
||||||
self.config.amq_adapter.service_name = "test-service"
|
self.mock_adapter.generator_id = 12345
|
||||||
self.config.amq_adapter.swarm_task_id = "test-task"
|
self.mock_adapter.service_name = "test-service"
|
||||||
self.config.amq_adapter.swarm_service_id = "test-service-id"
|
self.mock_adapter.route_mapping = {}
|
||||||
self.config.backpressure.threshold_load = 100
|
self.mock_adapter.swarm_service_id = "test-swarm-id"
|
||||||
|
self.mock_adapter.swarm_task_id = "test-task-id"
|
||||||
|
|
||||||
# Mock the service adapter
|
self.mock_config = Mock(spec=AMQConfiguration)
|
||||||
self.service_adapter = MagicMock()
|
self.mock_config.amq_adapter = self.mock_adapter
|
||||||
|
|
||||||
# Patch the get_unique_instance_id function
|
# Add dispatch configuration
|
||||||
with patch("amqp.service.amq_service.get_unique_instance_id", return_value=12345):
|
self.mock_dispatch = Mock()
|
||||||
# Create the AMQService instance
|
self.mock_dispatch.amq_host = "localhost"
|
||||||
self.service = AMQService(self.config, self.service_adapter)
|
self.mock_dispatch.amq_port = 5672
|
||||||
|
self.mock_dispatch.rabbit_mq_user = "guest"
|
||||||
|
self.mock_dispatch.rabbit_mq_password = "guest"
|
||||||
|
self.mock_dispatch.use_dlq = False
|
||||||
|
self.mock_config.dispatch = self.mock_dispatch
|
||||||
|
|
||||||
# Mock the backpressure handler with AsyncMock for the coroutine method
|
# Add backpressure configuration
|
||||||
self.service.backpressure_handler = MagicMock()
|
self.mock_backpressure = Mock()
|
||||||
self.service.backpressure_handler.update_backpressure_value = AsyncMock()
|
self.mock_backpressure.threshold_threads = 10
|
||||||
self.service.backpressure_handler.loop = asyncio.get_event_loop()
|
self.mock_config.backpressure = self.mock_backpressure
|
||||||
|
|
||||||
async def test_backpressure_with_maximum(self):
|
# Create service instance with mocked configuration
|
||||||
"""Test backpressure method with a specified maximum value."""
|
self.service = AMQService(self.mock_config)
|
||||||
# Call the backpressure method with a maximum value
|
|
||||||
self.service.backpressure(current_availability=50, maximum=200)
|
|
||||||
|
|
||||||
# Check that run_coroutine_threadsafe was called with the correct arguments
|
# Mock the event loop
|
||||||
# We need to patch asyncio.run_coroutine_threadsafe to capture its arguments
|
self.mock_loop = Mock()
|
||||||
with patch("asyncio.run_coroutine_threadsafe") as mock_run:
|
self.service.loop = self.mock_loop
|
||||||
self.service.backpressure(current_availability=50, maximum=200)
|
|
||||||
mock_run.assert_called_once()
|
|
||||||
|
|
||||||
# The second argument should be the loop
|
def tearDown(self):
|
||||||
loop_arg = mock_run.call_args[1]["loop"]
|
# Clean up any resources
|
||||||
|
if hasattr(self, "service"):
|
||||||
|
self.service.cleanup()
|
||||||
|
|
||||||
self.assertEqual(loop_arg, self.service.backpressure_handler.loop)
|
@patch("amqp.service.amq_service.RabbitMQClient")
|
||||||
|
@patch("amqp.service.amq_service.DataMessageFactory")
|
||||||
|
@patch("amqp.service.amq_service.ServiceMessageFactory")
|
||||||
|
@patch("amqp.service.amq_service.initialize_telemetry")
|
||||||
|
def test_init(
|
||||||
|
self, mock_telemetry, mock_service_factory, mock_data_factory, mock_rabbit_client
|
||||||
|
):
|
||||||
|
# Test initialization of AMQService
|
||||||
|
service = AMQService(self.mock_config)
|
||||||
|
|
||||||
# We can also verify the arguments passed to update_backpressure_value
|
# Verify that all required components are initialized
|
||||||
self.service.backpressure_handler.update_backpressure_value.assert_called_with(50, 200)
|
self.assertIsNotNone(service.amq_configuration)
|
||||||
|
self.assertIsNotNone(service.data_message_factory)
|
||||||
|
self.assertIsNotNone(service.rabbit_mq_client)
|
||||||
|
self.assertIsNotNone(service.tracer)
|
||||||
|
self.assertIsNotNone(service.service_message_factory)
|
||||||
|
|
||||||
async def test_backpressure_without_maximum(self):
|
@patch("amqp.service.amq_service.RabbitMQClient")
|
||||||
"""Test backpressure method without a maximum value."""
|
async def test_register_routes_valid(self, mock_rabbit_client):
|
||||||
# Set a maximum_availability value
|
# Setup mock route mapping
|
||||||
self.service.maximum_availability = 100
|
self.mock_adapter.route_mapping = "test-service:test-exchange:test-queue:test.#:5:0"
|
||||||
|
|
||||||
with patch("asyncio.run_coroutine_threadsafe") as mock_run:
|
# Setup mock rabbit client
|
||||||
# Call the backpressure method without a maximum value
|
mock_client = Mock()
|
||||||
self.service.backpressure(current_availability=50)
|
mock_client.register_inbound_routes = AsyncMock()
|
||||||
|
mock_client.register_data_reply_callback = AsyncMock()
|
||||||
|
mock_client.init = AsyncMock()
|
||||||
|
mock_client.request_routes = AsyncMock()
|
||||||
|
mock_client.channel = Mock()
|
||||||
|
mock_client.channel.is_closed = False
|
||||||
|
self.service.rabbit_mq_client = mock_client
|
||||||
|
|
||||||
# Verify the handler was called with the right values
|
# Setup mock service adapter
|
||||||
self.service.backpressure_handler.update_backpressure_value.assert_called_with(50, 100)
|
mock_service_adapter = Mock()
|
||||||
mock_run.assert_called_once()
|
mock_service_adapter.loop = self.mock_loop
|
||||||
|
|
||||||
async def test_backpressure_with_negative_maximum(self):
|
# Setup mock message handler
|
||||||
"""Test backpressure method with a negative maximum value."""
|
mock_handler = Mock()
|
||||||
# Set a maximum_availability value
|
mock_handler.inbound_data_message_callback_no_thread = AsyncMock()
|
||||||
self.service.maximum_availability = 100
|
mock_handler.reply_received_callback = AsyncMock()
|
||||||
|
self.service.amq_data_message_handler = mock_handler
|
||||||
|
|
||||||
with patch("asyncio.run_coroutine_threadsafe") as mock_run:
|
# Initialize the service
|
||||||
# Call the backpressure method with a negative maximum value
|
await self.service.init(self.mock_loop, mock_service_adapter)
|
||||||
self.service.backpressure(current_availability=50, maximum=-1)
|
|
||||||
|
|
||||||
# Verify the handler was called with the right values
|
# Call register_routes
|
||||||
self.service.backpressure_handler.update_backpressure_value.assert_called_with(50, 100)
|
await self.service.register_routes()
|
||||||
mock_run.assert_called_once()
|
|
||||||
|
|
||||||
async def test_backpressure_no_handler(self):
|
# Verify that routes were registered twice (once in init, once in register_routes)
|
||||||
"""Test backpressure method when no handler is available."""
|
self.assertEqual(mock_client.register_inbound_routes.call_count, 2)
|
||||||
# Set the backpressure handler to None
|
self.assertEqual(mock_client.register_data_reply_callback.call_count, 2)
|
||||||
self.service.backpressure_handler = None
|
|
||||||
|
|
||||||
# Call the backpressure method
|
@patch("amqp.service.amq_service.RabbitMQClient")
|
||||||
self.service.backpressure(current_availability=50)
|
async def test_register_routes_invalid(self, mock_rabbit_client):
|
||||||
|
# Setup invalid route mapping
|
||||||
|
self.mock_adapter.route_mapping = None
|
||||||
|
|
||||||
# Nothing should happen, no exception should be raised
|
# Setup mock rabbit client
|
||||||
|
mock_client = Mock()
|
||||||
|
mock_client.register_inbound_routes = AsyncMock()
|
||||||
|
mock_client.register_data_reply_callback = AsyncMock()
|
||||||
|
self.service.rabbit_mq_client = mock_client
|
||||||
|
|
||||||
|
# Call register_routes
|
||||||
|
result = await self.service.register_routes()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# Verify that no routes were registered
|
||||||
unittest.main()
|
mock_client.register_inbound_routes.assert_not_called()
|
||||||
|
mock_client.register_data_reply_callback.assert_not_called()
|
||||||
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
@patch("amqp.service.amq_service.RabbitMQClient")
|
||||||
|
async def test_reinitialize_if_disconnected(self, mock_rabbit_client):
|
||||||
|
# Setup mock route mapping
|
||||||
|
self.mock_adapter.route_mapping = "test-service:test-exchange:test-queue:test.#:5:0"
|
||||||
|
|
||||||
|
# Setup mock rabbit client with closed channel
|
||||||
|
mock_client = AsyncMock()
|
||||||
|
mock_client.channel = Mock()
|
||||||
|
mock_client.channel.is_closed = True
|
||||||
|
mock_client.init = AsyncMock()
|
||||||
|
mock_client.register_inbound_routes = AsyncMock()
|
||||||
|
mock_client.register_data_reply_callback = AsyncMock()
|
||||||
|
mock_client.request_routes = AsyncMock()
|
||||||
|
|
||||||
|
# Make the constructor return our mock client
|
||||||
|
mock_rabbit_client.return_value = mock_client
|
||||||
|
self.service.rabbit_mq_client = mock_client
|
||||||
|
|
||||||
|
# Setup mock message handler
|
||||||
|
mock_handler = Mock()
|
||||||
|
mock_handler.inbound_data_message_callback_no_thread = AsyncMock()
|
||||||
|
mock_handler.reply_received_callback = AsyncMock()
|
||||||
|
self.service.amq_data_message_handler = mock_handler
|
||||||
|
|
||||||
|
# Call reinitialize_if_disconnected
|
||||||
|
await self.service.reinitialize_if_disconnected()
|
||||||
|
|
||||||
|
# Verify that client was reinitialized
|
||||||
|
mock_client.init.assert_called_once()
|
||||||
|
|
||||||
|
# def test_send_message(self):
|
||||||
|
# # Create mock message and future
|
||||||
|
# mock_message = Mock(spec=DataMessage)
|
||||||
|
# mock_message.id.return_value = "test-id"
|
||||||
|
# mock_future = MagicMock()
|
||||||
|
#
|
||||||
|
# # Setup mock message handler
|
||||||
|
# self.service.amq_data_message_handler = Mock()
|
||||||
|
# self.service.amq_data_message_handler.outstanding = {}
|
||||||
|
#
|
||||||
|
# # Send message
|
||||||
|
# self.service.send_message(mock_message, mock_future)
|
||||||
|
#
|
||||||
|
# # Verify message was added to outstanding
|
||||||
|
# self.assertEqual(self.service.amq_data_message_handler.outstanding["test-id"], mock_future)
|
||||||
|
|
||||||
|
def test_remove_outstanding_future(self):
|
||||||
|
# Setup mock message handler with outstanding message
|
||||||
|
self.service.amq_data_message_handler = Mock()
|
||||||
|
self.service.amq_data_message_handler.outstanding = {"test-id": Mock()}
|
||||||
|
|
||||||
|
# Remove future
|
||||||
|
self.service.remove_outstanding_future("test-id")
|
||||||
|
|
||||||
|
# Verify message was removed from outstanding
|
||||||
|
self.assertNotIn("test-id", self.service.amq_data_message_handler.outstanding)
|
||||||
|
|
||||||
|
def test_cleanup(self):
|
||||||
|
# Setup mock rabbit client
|
||||||
|
mock_client = Mock()
|
||||||
|
self.service.rabbit_mq_client = mock_client
|
||||||
|
|
||||||
|
# Call cleanup
|
||||||
|
self.service.cleanup()
|
||||||
|
|
||||||
|
# Verify rabbit client cleanup was called
|
||||||
|
mock_client.cleanup.assert_called_once()
|
||||||
|
|||||||
Reference in New Issue
Block a user