2024-07-23 21:34:03 +01:00
|
|
|
|
2025-02-11 23:24:46 +00:00
|
|
|
import asyncio
|
2024-07-23 21:34:03 +01:00
|
|
|
from pulsar.schema import JsonSchema
|
2025-01-11 18:10:04 +00:00
|
|
|
import pulsar
|
2024-09-29 23:50:57 +01:00
|
|
|
from prometheus_client import Histogram, Info, Counter, Enum
|
2024-08-19 22:15:32 +01:00
|
|
|
import time
|
2024-07-23 21:34:03 +01:00
|
|
|
|
|
|
|
|
from . base_processor import BaseProcessor
|
2024-08-19 22:15:32 +01:00
|
|
|
from .. exceptions import TooManyRequests
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
default_rate_limit_retry = 10
|
|
|
|
|
default_rate_limit_timeout = 7200
|
|
|
|
|
|
2024-07-23 21:34:03 +01:00
|
|
|
class Consumer(BaseProcessor):
|
|
|
|
|
|
|
|
|
|
def __init__(self, **params):
|
|
|
|
|
|
2024-09-29 23:50:57 +01:00
|
|
|
if not hasattr(__class__, "state_metric"):
|
|
|
|
|
__class__.state_metric = Enum(
|
|
|
|
|
'processor_state', 'Processor state',
|
|
|
|
|
states=['starting', 'running', 'stopped']
|
|
|
|
|
)
|
|
|
|
|
__class__.state_metric.state('starting')
|
|
|
|
|
|
|
|
|
|
__class__.state_metric.state('starting')
|
|
|
|
|
|
2024-07-23 21:34:03 +01:00
|
|
|
super(Consumer, self).__init__(**params)
|
|
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
self.subscriber = params.get("subscriber")
|
|
|
|
|
self.input_schema = params.get("input_schema")
|
|
|
|
|
|
|
|
|
|
self.rate_limit_retry = params.get(
|
|
|
|
|
"rate_limit_retry", default_rate_limit_retry
|
|
|
|
|
)
|
|
|
|
|
self.rate_limit_timeout = params.get(
|
|
|
|
|
"rate_limit_timeout", default_rate_limit_timeout
|
|
|
|
|
)
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
if self.input_schema == None:
|
2024-07-23 21:34:03 +01:00
|
|
|
raise RuntimeError("input_schema must be specified")
|
|
|
|
|
|
|
|
|
|
if not hasattr(__class__, "request_metric"):
|
|
|
|
|
__class__.request_metric = Histogram(
|
2025-04-14 09:05:42 +01:00
|
|
|
'request_latency', 'Request latency (seconds)',
|
|
|
|
|
["flow"]
|
2024-07-23 21:34:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not hasattr(__class__, "pubsub_metric"):
|
|
|
|
|
__class__.pubsub_metric = Info(
|
2025-04-14 09:05:42 +01:00
|
|
|
'pubsub', 'Pub/sub configuration',
|
|
|
|
|
["flow"]
|
2024-07-23 21:34:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not hasattr(__class__, "processing_metric"):
|
|
|
|
|
__class__.processing_metric = Counter(
|
2025-04-14 09:05:42 +01:00
|
|
|
'processing_count', 'Processing count',
|
|
|
|
|
["flow", "status"]
|
2024-07-23 21:34:03 +01:00
|
|
|
)
|
|
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
if not hasattr(__class__, "rate_limit_metric"):
|
|
|
|
|
__class__.rate_limit_metric = Counter(
|
|
|
|
|
'rate_limit_count', 'Rate limit event count',
|
2025-04-14 09:05:42 +01:00
|
|
|
["flow"]
|
2025-01-27 17:04:49 +00:00
|
|
|
)
|
|
|
|
|
|
2025-04-14 09:05:42 +01:00
|
|
|
# __class__.pubsub_metric.info({
|
|
|
|
|
# "input_queue": self.input_queue,
|
|
|
|
|
# "subscriber": self.subscriber,
|
|
|
|
|
# "input_schema": self.input_schema.__name__,
|
|
|
|
|
# "rate_limit_retry": str(self.rate_limit_retry),
|
|
|
|
|
# "rate_limit_timeout": str(self.rate_limit_timeout),
|
|
|
|
|
# })
|
|
|
|
|
|
|
|
|
|
# self.consumer = self.client.subscribe(
|
|
|
|
|
# self.input_queue, self.subscriber,
|
|
|
|
|
# consumer_type=pulsar.ConsumerType.Shared,
|
|
|
|
|
# schema=JsonSchema(self.input_schema),
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
self.config_queues = {}
|
|
|
|
|
self.config_handlers = [self.on_config_pubsub]
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
print("Initialised consumer.", flush=True)
|
|
|
|
|
|
2025-04-14 09:05:42 +01:00
|
|
|
async def on_config_pubsub(self, version, config):
|
|
|
|
|
|
|
|
|
|
print("Configuring using config", version)
|
|
|
|
|
|
|
|
|
|
flows = config.get("flows", {}).get(self.ident, {})
|
|
|
|
|
|
|
|
|
|
print(flows)
|
|
|
|
|
|
|
|
|
|
print("Configuration completed OK")
|
|
|
|
|
|
2025-02-11 23:24:46 +00:00
|
|
|
async def run(self):
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2024-09-29 23:50:57 +01:00
|
|
|
__class__.state_metric.state('running')
|
|
|
|
|
|
2024-07-23 21:34:03 +01:00
|
|
|
while True:
|
|
|
|
|
|
2025-04-14 09:05:42 +01:00
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
2025-04-02 00:23:30 +01:00
|
|
|
msg = await asyncio.to_thread(self.consumer.receive)
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
expiry = time.time() + self.rate_limit_timeout
|
|
|
|
|
|
|
|
|
|
# This loop is for retry on rate-limit / resource limits
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
|
|
if time.time() > expiry:
|
|
|
|
|
|
|
|
|
|
print("Gave up waiting for rate-limit retry", flush=True)
|
|
|
|
|
|
|
|
|
|
# Message failed to be processed, this causes it to
|
|
|
|
|
# be retried
|
|
|
|
|
self.consumer.negative_acknowledge(msg)
|
|
|
|
|
|
|
|
|
|
__class__.processing_metric.labels(status="error").inc()
|
|
|
|
|
|
|
|
|
|
# Break out of retry loop, processes next message
|
|
|
|
|
break
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
try:
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
with __class__.request_metric.time():
|
2025-02-11 23:24:46 +00:00
|
|
|
await self.handle(msg)
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
# Acknowledge successful processing of the message
|
|
|
|
|
self.consumer.acknowledge(msg)
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
__class__.processing_metric.labels(status="success").inc()
|
|
|
|
|
|
|
|
|
|
# Break out of retry loop
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
except TooManyRequests:
|
|
|
|
|
|
|
|
|
|
print("TooManyRequests: will retry...", flush=True)
|
|
|
|
|
|
|
|
|
|
__class__.rate_limit_metric.inc()
|
|
|
|
|
|
|
|
|
|
# Sleep
|
|
|
|
|
time.sleep(self.rate_limit_retry)
|
|
|
|
|
|
|
|
|
|
# Contine from retry loop, just causes a reprocessing
|
|
|
|
|
continue
|
2024-08-19 22:15:32 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
|
|
print("Exception:", e, flush=True)
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
# Message failed to be processed, this causes it to
|
|
|
|
|
# be retried
|
|
|
|
|
self.consumer.negative_acknowledge(msg)
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
__class__.processing_metric.labels(status="error").inc()
|
2024-07-23 21:34:03 +01:00
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
# Break out of retry loop, processes next message
|
|
|
|
|
break
|
2024-07-23 21:34:03 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def add_args(parser, default_input_queue, default_subscriber):
|
|
|
|
|
|
|
|
|
|
BaseProcessor.add_args(parser)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-s', '--subscriber',
|
|
|
|
|
default=default_subscriber,
|
|
|
|
|
help=f'Queue subscriber name (default: {default_subscriber})'
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-27 17:04:49 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--rate-limit-retry',
|
|
|
|
|
type=int,
|
|
|
|
|
default=default_rate_limit_retry,
|
|
|
|
|
help=f'Rate limit retry (default: {default_rate_limit_retry})'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--rate-limit-timeout',
|
|
|
|
|
type=int,
|
|
|
|
|
default=default_rate_limit_timeout,
|
|
|
|
|
help=f'Rate limit timeout (default: {default_rate_limit_timeout})'
|
|
|
|
|
)
|
|
|
|
|
|