Skip to content

Commit ca88923

Browse files
committed
messaging: Delay applying exchange/queue name prefix config
Exchanges are defined as module level vars initialized on import. But, oslo_config is not setup yet at import time, so delay applying the new prefix setting until just before they get created in RabbitMQ. Luckily, Exchange() objects are lightweight objects that just hold the exchange name and type. They could do more, but we don't bind them to a channel. Because the exchange objects merely hold strings, and because they are basically singletons (module-level vars), we can safely update the exchange name just before declaring it in RabbitMQ. From that point on, everything that uses a singleton exchange object will get the updated name.
1 parent f103ad5 commit ca88923

12 files changed

Lines changed: 35 additions & 53 deletions

File tree

st2common/st2common/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ def register_opts(ignore_errors=False):
408408
"prefix",
409409
default="st2",
410410
help="Prefix for all exchange and queue names.",
411-
advanced=True,
412411
),
413412
]
414413

st2common/st2common/services/sensor_watcher.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import six
2323
from kombu.mixins import ConsumerMixin
24-
from oslo_config import cfg
2524

2625
from st2common import log as logging
2726
from st2common.transport import reactor, publishers
@@ -127,7 +126,7 @@ def stop(self):
127126
@staticmethod
128127
def _get_queue(queue_suffix):
129128
queue_name = queue_utils.get_queue_name(
130-
queue_name_base=f"{cfg.CONF.messaging.prefix}.sensor.watch",
129+
queue_name_base="st2.sensor.watch",
131130
queue_name_suffix=queue_suffix,
132131
add_random_uuid_to_suffix=True,
133132
)

st2common/st2common/services/triggerwatcher.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import six
2020
from kombu.mixins import ConsumerMixin
21-
from oslo_config import cfg
2221

2322
from st2common import log as logging
2423
from st2common.persistence.trigger import Trigger
@@ -164,7 +163,7 @@ def _load_triggers_from_db(self):
164163
@staticmethod
165164
def _get_queue(queue_suffix, exclusive):
166165
queue_name = queue_utils.get_queue_name(
167-
queue_name_base=f"{cfg.CONF.messaging.prefix}.trigger.watch",
166+
queue_name_base="st2.trigger.watch",
168167
queue_name_suffix=queue_suffix,
169168
add_random_uuid_to_suffix=True,
170169
)

st2common/st2common/transport/actionalias.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import absolute_import
1818
from kombu import Exchange, Queue
19-
from oslo_config import cfg
2019

2120
from st2common.transport import publishers
2221

@@ -25,7 +24,7 @@
2524
"get_queue",
2625
]
2726

28-
ACTIONALIAS_XCHG = Exchange(f"{cfg.CONF.messaging.prefix}.actionalias", type="topic")
27+
ACTIONALIAS_XCHG = Exchange("st2.actionalias", type="topic")
2928

3029

3130
class ActionAliasPublisher(publishers.CUDPublisher):

st2common/st2common/transport/actionexecutionstate.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
from __future__ import absolute_import
1919

2020
from kombu import Exchange, Queue
21-
from oslo_config import cfg
2221

2322
from st2common.transport import publishers
2423

2524
__all__ = ["ActionExecutionStatePublisher"]
2625

27-
ACTIONEXECUTIONSTATE_XCHG = Exchange(
28-
f"{cfg.CONF.messaging.prefix}.actionexecutionstate", type="topic"
29-
)
26+
ACTIONEXECUTIONSTATE_XCHG = Exchange("st2.actionexecutionstate", type="topic")
3027

3128

3229
class ActionExecutionStatePublisher(publishers.CUDPublisher):

st2common/st2common/transport/announcement.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from __future__ import absolute_import
1717

1818
from kombu import Exchange, Queue
19-
from oslo_config import cfg
2019

2120
from st2common import log as logging
2221
from st2common.constants.trace import TRACE_CONTEXT
@@ -28,7 +27,7 @@
2827
LOG = logging.getLogger(__name__)
2928

3029
# Exchange for Announcements
31-
ANNOUNCEMENT_XCHG = Exchange(f"{cfg.CONF.messaging.prefix}.announcement", type="topic")
30+
ANNOUNCEMENT_XCHG = Exchange("st2.announcement", type="topic")
3231

3332

3433
class AnnouncementPublisher(object):

st2common/st2common/transport/bootstrap_utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from st2common.transport.actionexecutionstate import ACTIONEXECUTIONSTATE_XCHG
3232
from st2common.transport.announcement import ANNOUNCEMENT_XCHG
3333
from st2common.transport.connection_retry_wrapper import ConnectionRetryWrapper
34-
from st2common.transport.execution import EXECUTION_XCHG
34+
from st2common.transport.execution import EXECUTION_XCHG, EXECUTION_OUTPUT_XCHG
3535
from st2common.transport.liveaction import LIVEACTION_XCHG, LIVEACTION_STATUS_MGMT_XCHG
3636
from st2common.transport.reactor import SENSOR_CUD_XCHG
3737
from st2common.transport.reactor import TRIGGER_CUD_XCHG, TRIGGER_INSTANCE_XCHG
@@ -67,6 +67,7 @@
6767
ACTIONEXECUTIONSTATE_XCHG,
6868
ANNOUNCEMENT_XCHG,
6969
EXECUTION_XCHG,
70+
EXECUTION_OUTPUT_XCHG,
7071
LIVEACTION_XCHG,
7172
LIVEACTION_STATUS_MGMT_XCHG,
7273
TRIGGER_CUD_XCHG,
@@ -94,16 +95,15 @@
9495
WORKFLOW_EXECUTION_RESUME_QUEUE,
9596
# Those queues are dynamically / late created on some class init but we still need to
9697
# pre-declare them for redis Kombu backend to work.
97-
reactor.get_trigger_cud_queue(
98-
name=f"{cfg.CONF.messaging.prefix}.preinit", routing_key="init"
99-
),
100-
reactor.get_sensor_cud_queue(
101-
name=f"{cfg.CONF.messaging.prefix}.preinit", routing_key="init"
102-
),
98+
reactor.get_trigger_cud_queue(name="st2.preinit", routing_key="init"),
99+
reactor.get_sensor_cud_queue(name="st2.preinit", routing_key="init"),
103100
]
104101

105102

106103
def _do_register_exchange(exchange, connection, channel, retry_wrapper):
104+
prefix = cfg.CONF.messaging.prefix
105+
if exchange.name and prefix != "st2":
106+
exchange.name = exchange.name.replace("st2.", f"{prefix}.", 1)
107107
try:
108108
kwargs = {
109109
"exchange": exchange.name,
@@ -127,6 +127,9 @@ def _do_register_exchange(exchange, connection, channel, retry_wrapper):
127127

128128

129129
def _do_predeclare_queue(channel, queue):
130+
prefix = cfg.CONF.messaging.prefix
131+
if queue.name and prefix != "st2":
132+
queue.name = queue.name.replace("st2.", f"{prefix}.", 1)
130133
LOG.debug('Predeclaring queue for exchange "%s"' % (queue.exchange.name))
131134

132135
bound_queue = None

st2common/st2common/transport/execution.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from __future__ import absolute_import
1919
from kombu import Exchange, Queue
20-
from oslo_config import cfg
2120

2221
from st2common.transport import publishers
2322

@@ -28,10 +27,8 @@
2827
"get_output_queue",
2928
]
3029

31-
EXECUTION_XCHG = Exchange(f"{cfg.CONF.messaging.prefix}.execution", type="topic")
32-
EXECUTION_OUTPUT_XCHG = Exchange(
33-
f"{cfg.CONF.messaging.prefix}.execution.output", type="topic"
34-
)
30+
EXECUTION_XCHG = Exchange("st2.execution", type="topic")
31+
EXECUTION_OUTPUT_XCHG = Exchange("st2.execution.output", type="topic")
3532

3633

3734
class ActionExecutionPublisher(publishers.CUDPublisher):

st2common/st2common/transport/liveaction.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
from __future__ import absolute_import
1919

2020
from kombu import Exchange, Queue
21-
from oslo_config import cfg
2221

2322
from st2common.transport import publishers
2423

2524
__all__ = ["LiveActionPublisher", "get_queue", "get_status_management_queue"]
2625

2726

28-
LIVEACTION_XCHG = Exchange(f"{cfg.CONF.messaging.prefix}.liveaction", type="topic")
29-
LIVEACTION_STATUS_MGMT_XCHG = Exchange(
30-
f"{cfg.CONF.messaging.prefix}.liveaction.status", type="topic"
31-
)
27+
LIVEACTION_XCHG = Exchange("st2.liveaction", type="topic")
28+
LIVEACTION_STATUS_MGMT_XCHG = Exchange("st2.liveaction.status", type="topic")
3229

3330

3431
class LiveActionPublisher(publishers.CUDPublisher, publishers.StatePublisherMixin):

st2common/st2common/transport/queues.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from __future__ import absolute_import
2424

2525
from kombu import Queue
26-
from oslo_config import cfg
2726

2827
from st2common.constants import action as action_constants
2928
from st2common.transport import actionalias
@@ -56,49 +55,49 @@
5655

5756
# Used by the action scheduler service
5857
ACTIONSCHEDULER_REQUEST_QUEUE = liveaction.get_status_management_queue(
59-
f"{cfg.CONF.messaging.prefix}.actionrunner.req",
58+
"st2.actionrunner.req",
6059
routing_key=action_constants.LIVEACTION_STATUS_REQUESTED,
6160
)
6261

6362

6463
# Used by the action runner service
6564
ACTIONRUNNER_WORK_QUEUE = liveaction.get_status_management_queue(
66-
f"{cfg.CONF.messaging.prefix}.actionrunner.work",
65+
"st2.actionrunner.work",
6766
routing_key=action_constants.LIVEACTION_STATUS_SCHEDULED,
6867
)
6968

7069
ACTIONRUNNER_CANCEL_QUEUE = liveaction.get_status_management_queue(
71-
f"{cfg.CONF.messaging.prefix}.actionrunner.cancel",
70+
"st2.actionrunner.cancel",
7271
routing_key=action_constants.LIVEACTION_STATUS_CANCELING,
7372
)
7473

7574
ACTIONRUNNER_PAUSE_QUEUE = liveaction.get_status_management_queue(
76-
f"{cfg.CONF.messaging.prefix}.actionrunner.pause",
75+
"st2.actionrunner.pause",
7776
routing_key=action_constants.LIVEACTION_STATUS_PAUSING,
7877
)
7978

8079
ACTIONRUNNER_RESUME_QUEUE = liveaction.get_status_management_queue(
81-
f"{cfg.CONF.messaging.prefix}.actionrunner.resume",
80+
"st2.actionrunner.resume",
8281
routing_key=action_constants.LIVEACTION_STATUS_RESUMING,
8382
)
8483

8584

8685
# Used by the notifier service
8786
NOTIFIER_ACTIONUPDATE_WORK_QUEUE = execution.get_queue(
88-
f"{cfg.CONF.messaging.prefix}.notifiers.execution.work",
87+
"st2.notifiers.execution.work",
8988
routing_key=publishers.UPDATE_RK,
9089
)
9190

9291

9392
# Used by the results tracker service
9493
RESULTSTRACKER_ACTIONSTATE_WORK_QUEUE = actionexecutionstate.get_queue(
95-
f"{cfg.CONF.messaging.prefix}.resultstracker.work", routing_key=publishers.CREATE_RK
94+
"st2.resultstracker.work", routing_key=publishers.CREATE_RK
9695
)
9796

9897

9998
# Used by the rules engine service
10099
RULESENGINE_WORK_QUEUE = reactor.get_trigger_instances_queue(
101-
name=f"{cfg.CONF.messaging.prefix}.trigger_instances_dispatch.rules_engine",
100+
name="st2.trigger_instances_dispatch.rules_engine",
102101
routing_key="#",
103102
)
104103

@@ -137,16 +136,16 @@
137136

138137
# Used by the workflow engine service
139138
WORKFLOW_EXECUTION_WORK_QUEUE = workflow.get_status_management_queue(
140-
name=f"{cfg.CONF.messaging.prefix}.workflow.work",
139+
name="st2.workflow.work",
141140
routing_key=action_constants.LIVEACTION_STATUS_REQUESTED,
142141
)
143142

144143
WORKFLOW_EXECUTION_RESUME_QUEUE = workflow.get_status_management_queue(
145-
name=f"{cfg.CONF.messaging.prefix}.workflow.resume",
144+
name="st2.workflow.resume",
146145
routing_key=action_constants.LIVEACTION_STATUS_RESUMING,
147146
)
148147

149148
WORKFLOW_ACTION_EXECUTION_UPDATE_QUEUE = execution.get_queue(
150-
f"{cfg.CONF.messaging.prefix}.workflow.action.update",
149+
"st2.workflow.action.update",
151150
routing_key=publishers.UPDATE_RK,
152151
)

0 commit comments

Comments
 (0)