BAT-042 (dcy-028 / issue #26 C-1/C-2/C-2b/C-3, REQ-079~082 + REQ-073/074 CHG): - slot_strategy_state 별도 테이블 + StrategyStateStore(in-memory+PG) + occupancy_seq·version CAS + opaque UTF-8 data + 2단 size cap (REQ-079) - trex.slots.strategy_state.set write RPC — reject reason 집합, 멱등 (REQ-080) - Slot.occupancy_seq 필드 + inactive→pending_buy bump + →INACTIVE close 삭제 훅 → 점유 전환 ABA 구조적 차단 (REQ-081) - slots.req 응답 행에 strategy_state 맵(batched 조립)·occupancy_seq additive (REQ-073 AC-7/REQ-074 AC-6) - 마이그레이션 014(occupancy_seq)/015(slot_strategy_state), baseline 문서 정합 (REQ-082) BAT-043 (issue #27 / REQ-007 CHG): - rejected SellIntent 응답에 additive retryable: bool (core 소유 reason→retryable 매핑, 미인식 기본 true, bad_request* false) — 손절매 재시도 안전 경로 decoupling 검증: 487 passed, 47 skipped (신규 41: store CAS 6+ABA, occupancy bump/close 4경로, set RPC 9, slots read 5, retryable 8, PG 게이트), ruff clean. REQ-079·080·081·082·073·074·007 → Implemented. issue #27 close. PG 경로 실 DB 검증은 release 위임. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
93 lines
4.0 KiB
Python
93 lines
4.0 KiB
Python
"""Messaging topology constants + builders (REQ-021, dcy-006 OQ-2).
|
|
|
|
Three patterns:
|
|
1. slots RPC — direct queue `trex.slots.req`, reply via amq.rabbitmq.reply-to
|
|
2. intent RPC — direct queue `trex.intent.req`, reply via amq.rabbitmq.reply-to
|
|
3. events — topic exchange `trex.events`, routing key {event_type}.{strategy_id}.{symbol}
|
|
|
|
Mapping principle: requests carry filter/spy in *payload*; events carry
|
|
strategy_id/symbol in the *routing key* so brokers can filter per-strategy
|
|
queues with patterns like `*.{strategy_id}.#`.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
# --- RPC queues (durable for intent; transient-ish for slots but kept
|
|
# durable for simplicity since the queue itself is shared) ---
|
|
SLOTS_REQ_QUEUE = "trex.slots.req" # REQ-073 / dcy-025: replaces snapshot.req
|
|
STRATEGY_STATE_SET_QUEUE = "trex.slots.strategy_state.set" # dcy-028 / REQ-080
|
|
INTENT_REQ_QUEUE = "trex.intent.req"
|
|
PAUSE_REQ_QUEUE = "trex.pause.req" # BAT-008 / REQ-032 (legacy alias of control)
|
|
CONTROL_REQ_QUEUE = "trex.control.req" # BAT-012 / REQ-043 operator control channel
|
|
# (action: pause|resume|list|kill_switch|resume_kill_switch)
|
|
|
|
# --- multi-instance topology (BAT-013 / dcy-009) ---
|
|
DEFAULT_INSTANCE_KEY = "default"
|
|
DIRECT_EXCHANGE = "trex.direct" # topic, durable — directed per-instance routing (REQ-050)
|
|
BROADCAST_EXCHANGE = "trex.broadcast" # fanout, durable — global control to all instances (REQ-050)
|
|
|
|
|
|
def _instance_queue(base: str, instance_key: str | None) -> str:
|
|
"""`{base}.{instance_key}` for named instances; literal `base` for default.
|
|
|
|
The default special-case keeps existing queue names (and the entire test
|
|
suite + deployed clients) byte-for-byte (REQ-049 FR-5 backward-compat).
|
|
"""
|
|
if instance_key in (None, DEFAULT_INSTANCE_KEY):
|
|
return base
|
|
return f"{base}.{instance_key}"
|
|
|
|
|
|
def intent_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
|
|
return _instance_queue(INTENT_REQ_QUEUE, instance_key)
|
|
|
|
|
|
def slots_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
|
|
return _instance_queue(SLOTS_REQ_QUEUE, instance_key)
|
|
|
|
|
|
def strategy_state_set_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
|
|
return _instance_queue(STRATEGY_STATE_SET_QUEUE, instance_key)
|
|
|
|
|
|
def control_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
|
|
return _instance_queue(CONTROL_REQ_QUEUE, instance_key)
|
|
|
|
|
|
def direct_routing_key(instance_key: str, channel_type: str) -> str:
|
|
"""Directed routing key for `trex.direct` (REQ-050): `{instance_key}.{channel_type}`."""
|
|
return f"{instance_key}.{channel_type}"
|
|
|
|
# --- Events ---
|
|
EVENTS_EXCHANGE = "trex.events" # topic, durable
|
|
EVENTS_DLQ_EXCHANGE = "trex.events.dlq" # fanout, durable; receives unroutable/NACKed
|
|
EVENTS_DLQ_QUEUE = "trex.events.dlq" # one collector queue bound to DLQ exchange
|
|
|
|
|
|
def event_routing_key(event_type: str, strategy_id: str, symbol: str) -> str:
|
|
"""Build the routing key for an event (dcy-006 OQ-2 mapping)."""
|
|
# Sanitize tokens — AMQP routing keys cannot contain '.', '*', '#'.
|
|
safe = lambda s: str(s or "_").replace(".", "_").replace("*", "_").replace("#", "_") # noqa: E731
|
|
return f"{safe(event_type)}.{safe(strategy_id)}.{safe(symbol)}"
|
|
|
|
|
|
def strategy_event_queue_name(strategy_id: str) -> str:
|
|
"""Per-strategy durable queue name (catch-up + last-wins)."""
|
|
return f"trex.events.strategy.{strategy_id}"
|
|
|
|
|
|
def strategy_event_binding_pattern(strategy_id: str) -> str:
|
|
"""Bind pattern for a strategy queue — all event types, all symbols.
|
|
|
|
Wildcards: `*` matches one word, `#` matches zero-or-more words.
|
|
"""
|
|
return f"*.{strategy_id}.#"
|
|
|
|
|
|
# --- per-queue arguments (REQ-023 TTL / max-length / drop-oldest) ---
|
|
STRATEGY_QUEUE_ARGS: dict = {
|
|
"x-message-ttl": 3_600_000, # 1h
|
|
"x-max-length": 10_000,
|
|
"x-overflow": "drop-head", # drop-oldest, then accept new (REQ-023 AC-7)
|
|
"x-dead-letter-exchange": EVENTS_DLQ_EXCHANGE,
|
|
}
|