-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_rabbitmq.py
299 lines (245 loc) · 10 KB
/
init_rabbitmq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import pika
from snapshotter.settings.config import settings
from snapshotter.utils.default_logger import default_logger
# Setup logging for RabbitMQ initialization
init_rmq_logger = default_logger.bind(module='RabbitMQ|Init')
def create_rabbitmq_conn() -> pika.BlockingConnection:
"""
Creates a connection to RabbitMQ using the settings specified in the application configuration.
Returns:
pika.BlockingConnection: A blocking connection object representing the connection to RabbitMQ.
"""
c = pika.BlockingConnection(
pika.ConnectionParameters(
host=settings.rabbitmq.host,
port=settings.rabbitmq.port,
virtual_host='/',
credentials=pika.PlainCredentials(
username=settings.rabbitmq.user,
password=settings.rabbitmq.password,
),
heartbeat=30,
),
)
return c
def get_snapshot_queue_routing_key_pattern() -> tuple[str, str]:
"""
Returns the queue name and routing key pattern for snapshot messages.
Returns:
tuple[str, str]: A tuple containing the queue name and routing key pattern.
"""
queue_name = f'powerloom-backend-cb-snapshot:{settings.namespace}:{settings.instance_id}'
routing_key_pattern = f'powerloom-backend-callback:{settings.namespace}:{settings.instance_id}:EpochReleased.*'
return queue_name, routing_key_pattern
def get_aggregate_queue_routing_key_pattern() -> tuple[str, str]:
"""
Returns the queue name and routing key pattern for the aggregate queue.
Returns:
tuple[str, str]: A tuple containing the queue name and routing key pattern.
"""
queue_name = f'powerloom-backend-cb-aggregate:{settings.namespace}:{settings.instance_id}'
routing_key_pattern = f'powerloom-backend-callback:{settings.namespace}:{settings.instance_id}:CalculateAggregate.*'
return queue_name, routing_key_pattern
def get_delegate_worker_request_queue_routing_key() -> tuple[str, str]:
"""
Returns the name and routing key for the request queue used by the delegated worker.
Returns:
tuple[str, str]: A tuple containing the request queue name and routing key.
"""
request_queue_routing_key = f'powerloom-delegated-worker:{settings.namespace}:{settings.instance_id}:Request'
request_queue_name = f'powerloom-delegated-worker-request:{settings.namespace}:{settings.instance_id}'
return request_queue_name, request_queue_routing_key
def get_delegate_worker_response_queue_routing_key_pattern() -> tuple[str, str]:
"""
Returns the response queue name and routing key pattern for a delegated worker.
Returns:
tuple[str, str]: A tuple containing the response queue name and routing key pattern.
"""
response_queue_routing_key = f'powerloom-delegated-worker:{settings.namespace}:{settings.instance_id}:Response.*'
response_queue_name = f'powerloom-delegated-worker-response:{settings.namespace}:{settings.instance_id}'
return response_queue_name, response_queue_routing_key
def init_queue(
ch: pika.adapters.blocking_connection.BlockingChannel,
queue_name: str,
routing_key: str,
exchange_name: str,
bind: bool = True,
) -> None:
"""
Declare a queue and optionally bind it to an exchange with a routing key.
Args:
ch (pika.adapters.blocking_connection.BlockingChannel): A blocking channel object from a Pika connection.
queue_name (str): The name of the queue to declare.
routing_key (str): The routing key to use for binding the queue to an exchange.
exchange_name (str): The name of the exchange to bind the queue to.
bind (bool, optional): Whether or not to bind the queue to the exchange. Defaults to True.
Returns:
None
"""
ch.queue_declare(queue_name)
if bind:
ch.queue_bind(
exchange=exchange_name, queue=queue_name, routing_key=routing_key,
)
init_rmq_logger.info(
'Initialized RabbitMQ setup | Queue: {} | Exchange: {} | Routing Key: {}',
queue_name,
exchange_name,
routing_key,
)
def init_topic_exchange_and_queue(
ch: pika.adapters.blocking_connection.BlockingChannel,
exchange_name: str,
queue_name: str,
routing_key_pattern: str,
) -> None:
"""
Initialize a topic exchange and queue in RabbitMQ.
Args:
ch (pika.adapters.blocking_connection.BlockingChannel): A blocking channel object for RabbitMQ.
exchange_name (str): The name of the exchange to declare.
queue_name (str): The name of the queue to declare.
routing_key_pattern (str): The routing key pattern to use for the queue.
Returns:
None
"""
# Declare the topic exchange
ch.exchange_declare(
exchange=exchange_name, exchange_type='topic', durable=True,
)
init_rmq_logger.info(
'Initialized RabbitMQ Topic exchange: {}', exchange_name,
)
# Initialize the queue and bind it to the exchange
init_queue(
ch=ch,
exchange_name=exchange_name,
queue_name=queue_name,
routing_key=routing_key_pattern,
)
def init_callback_queue(
ch: pika.adapters.blocking_connection.BlockingChannel,
) -> None:
"""
Initializes the callback queues for snapshot and aggregate.
Args:
ch (pika.adapters.blocking_connection.BlockingChannel): The blocking channel object.
Returns:
None
"""
callback_exchange_name = f'{settings.rabbitmq.setup.callbacks.exchange}:{settings.namespace}'
# Initialize snapshot queue
queue_name, routing_key_pattern = get_snapshot_queue_routing_key_pattern()
init_topic_exchange_and_queue(
ch,
exchange_name=callback_exchange_name,
queue_name=queue_name,
routing_key_pattern=routing_key_pattern,
)
# Initialize aggregate queue
queue_name, routing_key_pattern = get_aggregate_queue_routing_key_pattern()
init_queue(
ch,
exchange_name=callback_exchange_name,
queue_name=queue_name,
routing_key=routing_key_pattern,
)
def init_commit_payload_queue(
ch: pika.adapters.blocking_connection.BlockingChannel,
) -> None:
"""
Initializes a RabbitMQ queue for commit payloads.
Args:
ch (pika.adapters.blocking_connection.BlockingChannel): The RabbitMQ channel to use.
Returns:
None
"""
commit_payload_exchange_name = f'{settings.rabbitmq.setup.commit_payload.exchange}:{settings.namespace}'
routing_key_pattern = f'powerloom-backend-commit-payload:{settings.namespace}:{settings.instance_id}.*'
queue_name = f'powerloom-backend-commit-payload-queue:{settings.namespace}:{settings.instance_id}'
init_topic_exchange_and_queue(
ch,
exchange_name=commit_payload_exchange_name,
queue_name=queue_name,
routing_key_pattern=routing_key_pattern,
)
def init_delegate_worker_queue(
ch: pika.adapters.blocking_connection.BlockingChannel,
) -> None:
"""
Initializes the delegate worker queue by declaring the response and request exchanges
and initializing the request queue.
Args:
ch (pika.adapters.blocking_connection.BlockingChannel): The blocking channel to use for
declaring exchanges and initializing the queue.
Returns:
None
"""
# Declare response exchange
delegated_worker_response_exchange_name = (
f'{settings.rabbitmq.setup.delegated_worker.exchange}:Response:{settings.namespace}'
)
ch.exchange_declare(
exchange=delegated_worker_response_exchange_name, exchange_type='direct', durable=True,
)
# Declare request exchange
delegated_worker_request_exchange_name = (
f'{settings.rabbitmq.setup.delegated_worker.exchange}:Request:{settings.namespace}'
)
ch.exchange_declare(
exchange=delegated_worker_request_exchange_name, exchange_type='direct', durable=True,
)
# Initialize request queue
request_queue_name, request_queue_routing_key = get_delegate_worker_request_queue_routing_key()
init_queue(
ch,
exchange_name=delegated_worker_request_exchange_name,
queue_name=request_queue_name,
routing_key=request_queue_routing_key,
bind=True,
)
def init_event_detector_queue(
ch: pika.adapters.blocking_connection.BlockingChannel,
) -> None:
"""
Initializes the event detector queue by creating a topic exchange and a queue
with the given exchange name, queue name, and routing key pattern.
Args:
ch (pika.adapters.blocking_connection.BlockingChannel): The blocking channel
to use for creating the exchange and queue.
Returns:
None
"""
event_detector_exchange_name = f'{settings.rabbitmq.setup.event_detector.exchange}:{settings.namespace}'
routing_key_pattern = f'powerloom-event-detector:{settings.namespace}:{settings.instance_id}.*'
queue_name = f'powerloom-event-detector:{settings.namespace}:{settings.instance_id}'
init_topic_exchange_and_queue(
ch,
exchange_name=event_detector_exchange_name,
queue_name=queue_name,
routing_key_pattern=routing_key_pattern,
)
def init_exchanges_queues():
"""
Initializes all RabbitMQ exchanges and queues required for the snapshotter.
This includes the core exchange, processhub commands queue, callback queues,
event detector queue, commit payload queue, and delegate worker queue.
"""
# Create a new RabbitMQ connection
c = create_rabbitmq_conn()
ch: pika.adapters.blocking_connection.BlockingChannel = c.channel()
# Initialize core exchange
exchange_name = f'{settings.rabbitmq.setup.core.exchange}:{settings.namespace}'
ch.exchange_declare(
exchange=exchange_name, exchange_type='direct', durable=True,
)
init_rmq_logger.info(
'Initialized RabbitMQ Direct exchange: {}', exchange_name,
)
# Initialize other queues
init_callback_queue(ch)
init_event_detector_queue(ch)
init_commit_payload_queue(ch)
init_delegate_worker_queue(ch)
if __name__ == '__main__':
init_exchanges_queues()