mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-17 09:11:03 +02:00
- Refactored retry for rate limits into the base class
- ConsumerProducer is derived from Consumer to simplify code - Added retry_count metrics for rate limit events
This commit is contained in:
parent
477fd1420e
commit
f2a8ebb21f
3 changed files with 106 additions and 127 deletions
|
|
@ -7,6 +7,9 @@ import time
|
||||||
from . base_processor import BaseProcessor
|
from . base_processor import BaseProcessor
|
||||||
from .. exceptions import TooManyRequests
|
from .. exceptions import TooManyRequests
|
||||||
|
|
||||||
|
default_rate_limit_retry = 10
|
||||||
|
default_rate_limit_timeout = 7200
|
||||||
|
|
||||||
class Consumer(BaseProcessor):
|
class Consumer(BaseProcessor):
|
||||||
|
|
||||||
def __init__(self, **params):
|
def __init__(self, **params):
|
||||||
|
|
@ -22,11 +25,18 @@ class Consumer(BaseProcessor):
|
||||||
|
|
||||||
super(Consumer, self).__init__(**params)
|
super(Consumer, self).__init__(**params)
|
||||||
|
|
||||||
input_queue = params.get("input_queue")
|
self.input_queue = params.get("input_queue")
|
||||||
subscriber = params.get("subscriber")
|
self.subscriber = params.get("subscriber")
|
||||||
input_schema = params.get("input_schema")
|
self.input_schema = params.get("input_schema")
|
||||||
|
|
||||||
if input_schema == None:
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.input_schema == None:
|
||||||
raise RuntimeError("input_schema must be specified")
|
raise RuntimeError("input_schema must be specified")
|
||||||
|
|
||||||
if not hasattr(__class__, "request_metric"):
|
if not hasattr(__class__, "request_metric"):
|
||||||
|
|
@ -44,18 +54,27 @@ class Consumer(BaseProcessor):
|
||||||
'processing_count', 'Processing count', ["status"]
|
'processing_count', 'Processing count', ["status"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not hasattr(__class__, "retry_metric"):
|
||||||
|
__class__.retry_metric = Counter(
|
||||||
|
'retry_count', 'Retry count',
|
||||||
|
)
|
||||||
|
|
||||||
__class__.pubsub_metric.info({
|
__class__.pubsub_metric.info({
|
||||||
"input_queue": input_queue,
|
"input_queue": self.input_queue,
|
||||||
"subscriber": subscriber,
|
"subscriber": self.subscriber,
|
||||||
"input_schema": input_schema.__name__,
|
"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.consumer = self.client.subscribe(
|
||||||
input_queue, subscriber,
|
self.input_queue, self.subscriber,
|
||||||
consumer_type=pulsar.ConsumerType.Shared,
|
consumer_type=pulsar.ConsumerType.Shared,
|
||||||
schema=JsonSchema(input_schema),
|
schema=JsonSchema(self.input_schema),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
print("Initialised consumer.", flush=True)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
||||||
__class__.state_metric.state('running')
|
__class__.state_metric.state('running')
|
||||||
|
|
@ -64,31 +83,61 @@ class Consumer(BaseProcessor):
|
||||||
|
|
||||||
msg = self.consumer.receive()
|
msg = self.consumer.receive()
|
||||||
|
|
||||||
try:
|
expiry = time.time() + self.rate_limit_timeout
|
||||||
|
|
||||||
with __class__.request_metric.time():
|
# This loop is for retry on rate-limit / resource limits
|
||||||
self.handle(msg)
|
while True:
|
||||||
|
|
||||||
# Acknowledge successful processing of the message
|
if time.time() > expiry:
|
||||||
self.consumer.acknowledge(msg)
|
|
||||||
|
|
||||||
__class__.processing_metric.labels(status="success").inc()
|
print("Gave up waiting for rate-limit retry", flush=True)
|
||||||
|
|
||||||
except TooManyRequests:
|
# Message failed to be processed, this causes it to
|
||||||
self.consumer.negative_acknowledge(msg)
|
# be retried
|
||||||
print("TooManyRequests: will retry")
|
self.consumer.negative_acknowledge(msg)
|
||||||
__class__.processing_metric.labels(status="rate-limit").inc()
|
|
||||||
time.sleep(5)
|
__class__.processing_metric.labels(status="error").inc()
|
||||||
continue
|
|
||||||
|
# Break out of retry loop, processes next message
|
||||||
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
with __class__.request_metric.time():
|
||||||
|
self.handle(msg)
|
||||||
|
|
||||||
|
# Acknowledge successful processing of the message
|
||||||
|
self.consumer.acknowledge(msg)
|
||||||
|
|
||||||
|
__class__.processing_metric.labels(status="success").inc()
|
||||||
|
|
||||||
|
# Break out of retry loop
|
||||||
|
break
|
||||||
|
|
||||||
|
except TooManyRequests:
|
||||||
|
|
||||||
|
print("TooManyRequests: will retry...", flush=True)
|
||||||
|
|
||||||
|
__class__.retry_metric.inc()
|
||||||
|
|
||||||
|
# Sleep
|
||||||
|
time.sleep(self.rate_limit_retry)
|
||||||
|
|
||||||
|
# Contine from retry loop, just causes a reprocessing
|
||||||
|
continue
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
||||||
print("Exception:", e, flush=True)
|
print("Exception:", e, flush=True)
|
||||||
|
|
||||||
# Message failed to be processed
|
# Message failed to be processed, this causes it to
|
||||||
self.consumer.negative_acknowledge(msg)
|
# be retried
|
||||||
|
self.consumer.negative_acknowledge(msg)
|
||||||
|
|
||||||
__class__.processing_metric.labels(status="error").inc()
|
__class__.processing_metric.labels(status="error").inc()
|
||||||
|
|
||||||
|
# Break out of retry loop, processes next message
|
||||||
|
break
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_args(parser, default_input_queue, default_subscriber):
|
def add_args(parser, default_input_queue, default_subscriber):
|
||||||
|
|
@ -107,3 +156,17 @@ class Consumer(BaseProcessor):
|
||||||
help=f'Queue subscriber name (default: {default_subscriber})'
|
help=f'Queue subscriber name (default: {default_subscriber})'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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})'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,111 +4,43 @@ import pulsar
|
||||||
from prometheus_client import Histogram, Info, Counter, Enum
|
from prometheus_client import Histogram, Info, Counter, Enum
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from . base_processor import BaseProcessor
|
from . consumer import Consumer
|
||||||
from .. exceptions import TooManyRequests
|
from .. exceptions import TooManyRequests
|
||||||
|
|
||||||
# FIXME: Derive from consumer? And producer?
|
class ConsumerProducer(Consumer):
|
||||||
|
|
||||||
class ConsumerProducer(BaseProcessor):
|
|
||||||
|
|
||||||
def __init__(self, **params):
|
def __init__(self, **params):
|
||||||
|
|
||||||
if not hasattr(__class__, "state_metric"):
|
super(ConsumerProducer, self).__init__(**params)
|
||||||
__class__.state_metric = Enum(
|
|
||||||
'processor_state', 'Processor state',
|
|
||||||
states=['starting', 'running', 'stopped']
|
|
||||||
)
|
|
||||||
__class__.state_metric.state('starting')
|
|
||||||
|
|
||||||
__class__.state_metric.state('starting')
|
self.output_queue = params.get("output_queue")
|
||||||
|
self.output_schema = params.get("output_schema")
|
||||||
input_queue = params.get("input_queue")
|
|
||||||
output_queue = params.get("output_queue")
|
|
||||||
subscriber = params.get("subscriber")
|
|
||||||
input_schema = params.get("input_schema")
|
|
||||||
output_schema = params.get("output_schema")
|
|
||||||
|
|
||||||
if not hasattr(__class__, "request_metric"):
|
|
||||||
__class__.request_metric = Histogram(
|
|
||||||
'request_latency', 'Request latency (seconds)'
|
|
||||||
)
|
|
||||||
|
|
||||||
if not hasattr(__class__, "output_metric"):
|
if not hasattr(__class__, "output_metric"):
|
||||||
__class__.output_metric = Counter(
|
__class__.output_metric = Counter(
|
||||||
'output_count', 'Output items created'
|
'output_count', 'Output items created'
|
||||||
)
|
)
|
||||||
|
|
||||||
if not hasattr(__class__, "pubsub_metric"):
|
|
||||||
__class__.pubsub_metric = Info(
|
|
||||||
'pubsub', 'Pub/sub configuration'
|
|
||||||
)
|
|
||||||
|
|
||||||
if not hasattr(__class__, "processing_metric"):
|
|
||||||
__class__.processing_metric = Counter(
|
|
||||||
'processing_count', 'Processing count', ["status"]
|
|
||||||
)
|
|
||||||
|
|
||||||
__class__.pubsub_metric.info({
|
__class__.pubsub_metric.info({
|
||||||
"input_queue": input_queue,
|
"input_queue": self.input_queue,
|
||||||
"output_queue": output_queue,
|
"output_queue": self.output_queue,
|
||||||
"subscriber": subscriber,
|
"subscriber": self.subscriber,
|
||||||
"input_schema": input_schema.__name__,
|
"input_schema": self.input_schema.__name__,
|
||||||
"output_schema": output_schema.__name__,
|
"output_schema": self.output_schema.__name__,
|
||||||
|
"rate_limit_retry": str(self.rate_limit_retry),
|
||||||
|
"rate_limit_timeout": str(self.rate_limit_timeout),
|
||||||
})
|
})
|
||||||
|
|
||||||
super(ConsumerProducer, self).__init__(**params)
|
if self.output_schema == None:
|
||||||
|
|
||||||
if input_schema == None:
|
|
||||||
raise RuntimeError("input_schema must be specified")
|
|
||||||
|
|
||||||
if output_schema == None:
|
|
||||||
raise RuntimeError("output_schema must be specified")
|
raise RuntimeError("output_schema must be specified")
|
||||||
|
|
||||||
self.producer = self.client.create_producer(
|
self.producer = self.client.create_producer(
|
||||||
topic=output_queue,
|
topic=self.output_queue,
|
||||||
schema=JsonSchema(output_schema),
|
schema=JsonSchema(self.output_schema),
|
||||||
chunking_enabled=True,
|
chunking_enabled=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.consumer = self.client.subscribe(
|
print("Initialised consumer/producer.")
|
||||||
input_queue, subscriber,
|
|
||||||
consumer_type=pulsar.ConsumerType.Shared,
|
|
||||||
schema=JsonSchema(input_schema),
|
|
||||||
)
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
|
|
||||||
__class__.state_metric.state('running')
|
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
msg = self.consumer.receive()
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
with __class__.request_metric.time():
|
|
||||||
resp = self.handle(msg)
|
|
||||||
|
|
||||||
# Acknowledge successful processing of the message
|
|
||||||
self.consumer.acknowledge(msg)
|
|
||||||
|
|
||||||
__class__.processing_metric.labels(status="success").inc()
|
|
||||||
|
|
||||||
except TooManyRequests:
|
|
||||||
self.consumer.negative_acknowledge(msg)
|
|
||||||
print("TooManyRequests: will retry")
|
|
||||||
__class__.processing_metric.labels(status="rate-limit").inc()
|
|
||||||
time.sleep(5)
|
|
||||||
continue
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
|
|
||||||
print("Exception:", e, flush=True)
|
|
||||||
|
|
||||||
# Message failed to be processed
|
|
||||||
self.consumer.negative_acknowledge(msg)
|
|
||||||
|
|
||||||
__class__.processing_metric.labels(status="error").inc()
|
|
||||||
|
|
||||||
def send(self, msg, properties={}):
|
def send(self, msg, properties={}):
|
||||||
self.producer.send(msg, properties)
|
self.producer.send(msg, properties)
|
||||||
|
|
@ -120,19 +52,7 @@ class ConsumerProducer(BaseProcessor):
|
||||||
default_output_queue,
|
default_output_queue,
|
||||||
):
|
):
|
||||||
|
|
||||||
BaseProcessor.add_args(parser)
|
Consumer.add_args(parser, default_input_queue, default_subscriber)
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
'-i', '--input-queue',
|
|
||||||
default=default_input_queue,
|
|
||||||
help=f'Input queue (default: {default_input_queue})'
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
'-s', '--subscriber',
|
|
||||||
default=default_subscriber,
|
|
||||||
help=f'Queue subscriber name (default: {default_subscriber})'
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-o', '--output-queue',
|
'-o', '--output-queue',
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,3 @@ class LlmError(Exception):
|
||||||
class ParseError(Exception):
|
class ParseError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue