Repository navigation
Wake Switch's own reader only for command rows, and read only those (CHOO-3667) - #1236
abeldantas wants to merge 3 commits into
Conversation
…CHOO-3667) The admin client's CommandConsumer acts on `!command` rows only, but the process holding it was woken for every row of every room and read each one to find them, so one process did a read per message for the whole workspace on top of its share of everything else. The message announcement now carries the row's type, a listener subscriber can ask for some types only (MessageListener.subscribe), and a consumer that names its types (Consumer.ROW_TYPES) is woken for those and reads only those rows, through a partial index on the command rows. A database or process from before this sends or reads announcements without a type, which still wake every reader.
…-reads-commands # Conflicts: # CHANGELOG.md
There was a problem hiding this comment.
🟡 Changes recommended
The main blocker is a moderate migration issue: the IF NOT EXISTS guard would accept an existing INVALID index with the same name, so the upgrade reports success while command reads silently fall back to scans.
3 open issues
What changed in this PR
This PR stops Switch's own admin reader from waking for and reading every row in every room. It only needs the !command rows, which the CommandConsumer acts on. A new row's notification now includes its type, and a listener can subscribe for certain types only. The consumer declares its types, and a partial index covers command rows so reads stay fast however far behind the consumer is. This is the second PR in a stack that follows #1222, which already moved the tagged-agent warnings to the process that writes the message.
Changes:
- A migration replaces the notify function so the announcement includes the row's type, and adds a partial index on command rows.
MessageListener.subscribeaccepts an optional set of types. Announcements without a type still wake every subscriber.CommandConsumersetsROW_TYPESto command rows, so its transport reads and wake-ups are limited to those rows.
| File | Description |
|---|---|
| core/tests/switch_core/transport/test_postgres_transport.py | Adds a test that a typed reader is handed only command rows. |
| core/tests/switch_core/transport/test_membership_signals.py | Updates the fake listener to accept the new types argument. |
| core/tests/switch_core/messages/test_notify.py | Tests the announcement type field and typed wake-ups, including the no-type fallback. |
| core/tests/switch_core/gateway/conftest.py | Updates the fake listener signature for the types argument. |
| core/tests/db_buffer.py | Updates the no-op listener signature for the types argument. |
| core/switch_core/transport/postgres.py | Passes row types through receive, watch, and read paths, and skips the shared room cache for typed readers. |
| core/switch_core/transport/port.py | Adds the optional row_types parameter to the transport interface. |
| core/switch_core/migrations/versions/f2a9c7d41e08_command_rows_for_the_admin_reader.py | Replaces the notify function to include the type and adds the partial command index. |
| core/switch_core/messages/notify.py | Parses the optional type and wakes subscribers only for their types. |
| core/switch_core/events.py | Defines the shared COMMAND_EVENT_TYPE constant. |
| core/switch_core/db/stores/message_store.py | Adds an optional type filter to head and list queries. |
| core/switch_core/db/notify_ddl.py | Documents the type field in the notification trigger. |
| core/switch_core/db/models.py | Declares the partial index on command rows in the model. |
| core/switch_core/clients/consumer.py | Adds the ROW_TYPES class attribute and passes it to the transport. |
| core/switch_core/clients/command_consumer.py | Sets ROW_TYPES so the command consumer reads only command rows. |
| CHANGELOG.md | Notes that the reader is now woken only by commands. |
Review effort: Lite
| # `IF NOT EXISTS` so an index built by hand ahead of the upgrade is kept. | ||
| op.execute( | ||
| f"CREATE INDEX IF NOT EXISTS {_INDEX} ON messages (room_id, seq) " | ||
| "WHERE event_type = 'com.switch.command'" | ||
| ) |
| **The notification is a hint, never the payload.** It carries a room, a | ||
| position and a row id, and a consumer reads the row itself. That is what makes | ||
| position, a row id and the row's type, and a consumer reads the row itself. | ||
| The type lets a reader that acts on some kinds of row only be woken for those | ||
| (`MessageListener.subscribe`). That is what makes |
| if types is not None: | ||
| query = query.where(Message.event_type.in_(list(types))) |
|
Combined into #1307 with the other scaling PRs. |


The process that holds a workspace's admin client read every row of every room to find the
!commandrows its consumer answers. Since #1222 the warnings for tags nobody can answer are posted by the process that writes the message, so commands are the only rows that consumer acts on.In this PR we:
MessageListener.subscribe); an announcement with no type, from a database before the migration, still wakes everyoneCommandConsumerits row types (Consumer.ROW_TYPES), so it is woken for command rows only and reads only those, through a partial index on them, however far behind its cursor isStack (merge order)