Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions charts/sturnus/templates/bot-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@ spec:
# Fixed at 1, not a values knob: two instances would hold two Discord
# gateway connections and record every session twice (Spec 4.1).
#
# `STURNUS_SHARD_COUNT` (bot.env in values.yaml) does not relax this and
# is not a step towards relaxing it by itself. Shards are gateway
# connections *within* one process: N shards here is still one pod, one
# voice connection per guild, one RWO recording PVC. Two pods would each
# open the same shards and record every session twice exactly as before.
#
# Running one shard range per pod is a different deployment shape, not a
# larger `replicas`: it needs a StatefulSet (stable ordinals, so a pod
# knows which shard range it owns), a volumeClaimTemplate instead of the
# single shared PVC below, and an announcement sweep that speaks only for
# the guilds its own shards carry. See docs/operations.md section 3.6.
# Running more than one pod is a different deployment shape, not a larger
# `replicas`: each pod would need its own recording volume and its own share of
# the guilds. See docs/operations.md section 3.6.
replicas: 1
# Recreate, not RollingUpdate: a rolling update briefly runs the old and
# the new pod together, which is exactly the double-recording situation
Expand Down
29 changes: 4 additions & 25 deletions charts/sturnus/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,10 @@ bot:
# Deployment template hardcodes both `replicas: 1` and `strategy.type:
# Recreate` for this reason, so nothing can turn it into a rolling update.
#
# `STURNUS_SHARD_COUNT` below does NOT change this, and it is worth being
# blunt about that because the word "sharding" invites the opposite
# conclusion. Shards are gateway connections *inside* one process: four
# shards is still one pod, one voice connection per guild, one recording
# PVC. Two pods would still record every session twice, shard count or
# not, because both would identify as the same bot on overlapping shard
# ranges. Splitting shard ranges across pods is a different change --
# a StatefulSet, a PVC per pod, a shard range per ordinal -- and
# docs/operations.md section 3.6 lists what it needs.
# Two pods would still record every session twice: both would identify as
# the same bot and receive the same gateway events. Splitting the work across
# pods is a different change, and docs/operations.md section 3.6 says what it
# would take.
command: ["sturnus-bot"]
env:
STURNUS_RECORDING_DIR: /data/recordings
Expand All @@ -259,22 +254,6 @@ bot:
# talks to Discord. Set "Public Bot" off and GUILD_INSTALL only in the
# Developer Portal as well (docs/first-deployment.md).
STURNUS_GUILD_ID: ""
# How many gateway connections this one process opens. **Empty means
# "let Discord decide"**, which is the right answer for almost every
# deployment: `AutoShardedClient` asks `/gateway/bot` and takes the
# recommendation, and that recommendation grows with the guild count
# on its own where a number pinned here would not.
#
# Discord *requires* sharding above 2500 guilds; below that it is
# headroom and resilience -- one shard's reconnect no longer stalls
# every other guild's events behind it. Set it only if you know why
# (matching a `max_concurrency` bucket, reproducing a routing
# problem), and note that `/readyz` then requires *every* shard to be
# up: four shards with one reconnecting is a NotReady pod, on purpose.
#
# Values below 1 are refused at startup rather than at the gateway,
# so a typo names itself in the pod's first log line.
STURNUS_SHARD_COUNT: ""
# Recordings themselves go straight to the PVC above; this only needs to
# cover Python/library scratch writes to /tmp under readOnlyRootFilesystem.
tmpSizeLimit: 256Mi
Expand Down
133 changes: 26 additions & 107 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ the Kubernetes `Secret` rather than from plain manifest text (see section
| `STURNUS_OUTLINE_BASE_URL` | **yes** | no | Base URL of the Outline instance. The bot needs it only to build the authorization URL `/link` sends a user's browser to; it never calls Outline's API itself. |
| `STURNUS_OUTLINE_CLIENT_ID` | **yes** | no | OAuth client id of the Sturnus application registered in Outline. Public by design — it travels in the query string of the authorization URL every user's browser opens. |
| `STURNUS_OUTLINE_REDIRECT_URI` | **yes** | no | The callback URL that authorization returns to. Must be the same value `sturnus-link` is given, and must actually route to `link` — see section 1.5. |
| `STURNUS_SHARD_COUNT` | unset | no | How many Discord gateway connections this one process opens. Unset or empty — the chart's default — means **let Discord decide**: `AutoShardedClient` asks `/gateway/bot` and uses the recommendation, which grows with the guild count on its own. An explicit value is for an operator who knows why; anything below `1` is refused at startup. It does **not** make the bot horizontally scalable — every shard lives in this one process and the Deployment stays at one replica. See section 3.6. |
| `STURNUS_HEALTH_PORT` | `8080` | no | Port the `/healthz`, `/readyz`, `/metrics`, `/version` HTTP endpoints listen on. `/metrics` answers **`501 Not Implemented`**: metrics are *pushed* over OTLP, not scraped — see section 7. |
| `STURNUS_SENTRY_DSN` | unset | no | Sentry DSN for error reporting. Empty disables it entirely: `sentry_sdk.init()` is never called, so no instrumentation is installed and the process runs exactly as it does without Sentry. Supplied through the `Secret`, and required to be present even when blank; see section 1.4 for why a value that is not a credential is stored like one. |
| `STURNUS_SENTRY_ENVIRONMENT` | `production` | no | Names the deployment in Sentry's environment filter **and** supplies OpenTelemetry's `deployment.environment.name`. One variable for both on purpose — `OtelSettings.environment` reads this exact name rather than adding a second one, so the environment filter in Sentry and the one in Grafana can never disagree. |
Expand Down Expand Up @@ -537,118 +536,38 @@ is one constant (`MAX_CONCURRENT_SESSIONS_PER_GUILD`) that the code asks
rather than assumes — but raising that constant without supplying the
second identity breaks the bot instead of improving it.

### 3.6 Shards, and the four things they do not change
### 3.6 One gateway connection

A Discord bot's guilds are split across **shards**. A shard is one gateway
connection, and Discord routes a guild to shard `(guild_id >> 22) %
shard_count`. Sturnus's bot process holds all of them
(`discord.AutoShardedClient`), and `STURNUS_SHARD_COUNT` is the only knob.
The bot process holds a single Discord gateway connection. Discord asks for
sharding (several connections, each carrying a share of the guilds) only
above 2,500 guilds
(<https://docs.discord.com/developers/topics/gateway>). An installation
serves one guild, so Sturnus does not shard, and there is no variable to set.
If `STURNUS_SHARD_COUNT` is still present in a deployment from before this
change, it is ignored and the bot logs `config.shard_count_ignored` once at
startup; unset it.

**Leave it unset.** Unset means the bot asks Discord `/gateway/bot` and
opens the number Discord recommends — which tracks the guild count by
itself, where a number pinned in a values file does not. Discord *requires*
sharding above 2500 guilds; below that, shards buy headroom and one
concrete piece of resilience: a shard that has to reconnect stalls only its
own guilds' events, instead of every guild's queueing behind one socket.

Set it explicitly only with a reason — matching a `max_concurrency` bucket
during a large-bot rollout, or reproducing a routing problem on a known
shard layout. Values below `1` are refused when the process starts, naming
the variable, rather than producing a bot that opens no connection and sits
there looking alive.

**What sharding changes about `/readyz`.** Readiness is now "every shard
this process holds is up", rather than one boolean set at startup and never
cleared. With four shards and one reconnecting, `/readyz` answers `503`
with a body naming the missing shard:
**What `/readyz` reports.** `discord_connected` is `true` while the gateway
connection is up and `database_reachable` while the database answers. A
dropped connection clears the first flag at once, and the connection
resuming sets it again:

```json
{"status": "not ready", "discord_connected": false, "database_reachable": true,
"shards_connected": 3, "shards_expected": 4, "shards_missing": [2]}
```

That is deliberate. Three shards up means a quarter of the servers Sturnus
is in cannot be recorded in at all, and a probe reporting perfect health
through that is worse than one that occasionally flaps. It does **not**
flap on a routine reconnect: the probe polls every 10 s with
`failureThreshold: 3`, so a shard has to be absent for 30 continuous
seconds before Kubernetes marks the pod NotReady, and an ordinary RESUME
takes a second or two. The bot's Service carries no traffic, so a NotReady
pod here is a signal to go and look, not a load-shedding action.

**What sharding changes about the logs.** `bot.shard_ready`,
`bot.shard_resumed` and `bot.shard_disconnected` carry `shard_id` and
`shard_count`. Per-guild lines — the guild tick, the voice lifecycle, the
capture failures — carry `shard_id` **only when this process holds more
than one shard**: with one shard it would be `0` in every line for ever,
which is a key in every Loki stream that answers nothing. So, once a shard
count above one is in use:

```logql
# Which guilds went with the shard that just dropped
{app="sturnus-bot"} | json | shard_id="2"
{"status": "not ready", "discord_connected": false, "database_reachable": true}
```

**What sharding does not change.** All four are worth stating plainly,
because the word invites the opposite conclusion:

1. **The Deployment stays at `replicas: 1`, with `strategy: Recreate`.**
Four shards is still one process. Two pods would each open the same
shards, hold two gateway connections to the same guilds, and record
every session twice — exactly the situation `replicas: 1` exists to
prevent, shard count or not.
2. **One voice connection per guild.** That is a platform limit and section
3.5 is unaffected: a guild with three allowed channels still records one
of them at a time.
3. **One recording PVC.** In-progress recordings live on the pod's own
`ReadWriteOnce` volume, and the SIGTERM handler that flushes them is
still one handler in one process.
4. **One announcement sweep.** The poll that posts a finished session's
document link reads `sessions` rows for every guild there is. With one
process that is exactly right; it is the one sweep in the bot that would
stop being right otherwise.

#### What running one shard range per pod would actually take

Written down so that it stays a decision rather than becoming an
archaeology exercise. Sturnus has **not** built this. In rough dependency
order:

1. **A StatefulSet, not a Deployment.** Each pod needs a stable ordinal to
derive its shard range from; a Deployment's pods are interchangeable and
have no such identity. `sturnus.application.sharding.shards_this_process_owns`
is the one function that would read it — it returns every shard today,
and would return `range(ordinal * per_pod, (ordinal + 1) * per_pod)`.
2. **`shard_ids` passed to `AutoShardedClient`** alongside `shard_count`.
discord.py requires both when shard ids are given by hand, and refuses
ids without a count outright.
3. **A `volumeClaimTemplate` instead of the single shared PVC.** The
recording volume is `ReadWriteOnce` and holds in-progress audio; two
pods cannot share it, and a pod that restarts must come back to *its
own* unflushed recordings for `recover_orphans` to find them.
4. **The announcement sweep scoped to the pod's shards.** Already asked:
`announce_ready_sessions` calls `process_serves_guild` for every
candidate session, so this half is a change to
`shards_this_process_owns` and nothing else. Without it, four pods post
the same document link four times.
5. **A PodDisruptionBudget that means something at N replicas.**
`minAvailable: 1` at one replica blocks every voluntary eviction, which
is the point today; at four it would permit three quarters of the bot to
be drained at once, mid-recording.
6. **Session-start rate limiting across pods.** Discord's `max_concurrency`
bounds how many shards may IDENTIFY per five seconds. One process
serialises its own launches; N pods starting together do not, so a
rollout would need `AutoShardedClient.fetch_session_start_limits` and a
`before_identity_hook` coordinating between them.
7. **The console's mirrors read as though they were complete.** The
administrator and directory mirrors are written per guild off each
process's own gateway cache, so they stay correct — but a pod that is
down leaves its guilds' rows stale with nothing saying so, and the API
currently has no way to express that.

Items 1, 3, 5 and 6 are deployment work with no code in this repository to
change. Item 4 is one function body. Items 2 and 7 are small. The ordering
is the useful part: none of it is hard, and none of it happens by raising
It does **not** make the pod flap on a routine reconnect. The probe polls
every 10 s with `failureThreshold: 3`, so the connection has to be down for
30 continuous seconds before Kubernetes marks the pod NotReady, and an
ordinary RESUME takes a second or two. The bot's Service carries no traffic,
so a NotReady pod is a signal to go and look, not a load-shedding action.

**Why the Deployment stays at one pod.** `replicas: 1` with
`strategy: Recreate` is unchanged. Two pods would each hold a gateway
connection to the same guilds and record every session twice. Each pod also
has its own `ReadWriteOnce` recording volume, and the SIGTERM handler that
flushes an in-progress recording runs in one process. Running more than one
pod would be a different deployment shape, with its own design, not a larger
`replicas`.

## 4. First run
Expand Down
22 changes: 0 additions & 22 deletions src/sturnus/application/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

from jinja2.sandbox import SandboxedEnvironment

from sturnus.application.sharding import process_serves_guild
from sturnus.observability.events import Event, log_event, log_exception

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -178,13 +177,6 @@ async def candidates_for_announcement(self) -> list[dict[str, object]]:
mention them. An implementation that omits the key still works:
`announce_ready_sessions` falls back to no mentions rather than
failing to post the link.

Each row also carries `guild_id`, which is not used to render
anything: it is what lets `announce_ready_sessions` ask whether
this process serves that guild at all. This is the one sweep in
the bot whose input is the database rather than a gateway cache,
so it is the one that has to ask -- see
`sturnus.application.sharding`.
"""
...

Expand All @@ -202,8 +194,6 @@ async def announce_ready_sessions(
announcer: Announcer,
now: datetime,
template_source: str = DEFAULT_ANNOUNCEMENT_TEMPLATE,
*,
shard_count: int | None = None,
) -> None:
"""Posts each documented session's link once and stamps `announced_at`.

Expand All @@ -222,22 +212,10 @@ async def announce_ready_sessions(
at all whenever the post itself is what fails, which is the more
common failure mode (a flaky Discord API call) and the one this
function exists to survive.

**The one sweep in the bot that asks whether it serves a guild.**
Everything else the bot sweeps periodically is driven off its own
gateway cache and is therefore scoped to this process's shards for
free; this reads `sessions` rows for every guild there is. Today
`process_serves_guild` answers `True` for all of them -- one process
holds every shard -- and the call is there so that the day it does
not, four pods do not each post the same document link four times.
`shard_count` is `None` for a caller with no gateway (every test of
this function), which changes nothing today and is the honest value.
"""
for session in sessions_to_announce(await sessions.candidates_for_announcement()):
session_id = cast(int, session["id"])
channel_id = cast(int, session["channel_id"])
if not process_serves_guild(cast(int, session["guild_id"]), shard_count):
continue
document_url = cast(str, session["document_url"])
# `.get`, not `[...]`: a reader that does not supply participants
# loses the mentions, not the announcement itself.
Expand Down
Loading
Loading