~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

The relays are asked to read the host as it starts and every 20 minutes

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:relay/crawl-at-start

What changes

Asked as it starts. The host asks each relay in PDS_CRAWLERS to read its firehose (com.atproto.sync.requestCrawl) as it starts, before it writes anything, from firehose::crawling, which serve spawns. It logs asked the relay to crawl or requestCrawl failed for each relay each time.

Asked again every 20 minutes. The same loop asks again every CRAWL_EVERY (20 minutes), whether or not anything was written. Writing an event no longer asks: Host::announce only wakes the firehose's streams, and the crawled timestamp on Host is gone.

No re-announcement. A relay that connects with no cursor still gets only the events after it connects. The host sends no #sync, replays nothing and makes no fresh commits on its behalf (Why says why).

Docs. docs/reference/config.md (PDS_CRAWLERS), a new "How relays hear of a group" section in docs/design/groups.md, its line in docs/design/README.md, and the log lines in docs/deploying.md.

Why

groups.grain.social started with PDS_CRAWLERS=https://bsky.network and wrote nothing for hours, so it asked no relay. The first group's #identity, #account and #commit went out at 00:50:13, and that first write is what made the host ask. The relays connected at 00:50:15. A relay that has never taken an event from a host connects with no cursor (indigo's subscribeWithRedialer adds ?cursor= only when cursor > 0; its HACKING.md: "for a new host … the relay will connect at 'current' firehose offset"). So they missed all three, and still report the host with accountCount 0, seq -1. Grain learns groups from the relays, through Jetstream, so it never saw the group's org.atgrp.declaration.

Asking at the start means a relay is reading before the first group exists. Asking every 20 minutes keeps it reading. Per indigo's relay:

  • requestCrawl for a host it is already subscribed to returns at once (SubscribeToHost checks CheckIfSubscribed), so asking again costs one describeServer check.
  • It stops reading a host after 15 failed dials, a few minutes of the host being unreachable (a long upgrade, a network fault), and marks it offline. Its HACKING.md also plans to drop quiet hosts as idle. A host in either state is subscribed again only by requestCrawl. On restart, the relay resubscribes only active hosts.

Asking only on news fixes neither case. The event that prompts the ask is written before the relay connects, and while the relay's cursor for the host is still -1 it reconnects with no cursor. Once a relay holds a cursor, it reconnects with it, and the host replays from its event log, which it never prunes. 20 minutes is the interval CRAWL_EVERY already set.

Re-announcement was considered and left out:

  • Replaying events to a subscriber with no cursor. The event-stream spec says a subscriber with no cursor starts "from the current stream position".
  • #sync, or a fresh commit of the declaration, on each cursorless connect. The firehose is one sequenced log that every subscriber reads, so an event can't go to one consumer alone. Anyone may open subscribeRepos (it spends no limits), so anyone could make the host write. A relay reconnects with no cursor on every redial until it takes its first event, so each redial would write again. A commit made only to be seen would also change a group's rev without anyone writing.
  • #sync alone. It wouldn't reach Grain. The sync spec defines it as the repo's current commit with no records, and Jetstream turns it into no record ops. Grain only updates accounts it already tracks on #identity and #account.

The relay-side cause (no relay reading when the first group starts) is fixed here. The one group already missed won't appear on the relay until its next event. Grain should read such a group's repo itself (com.atproto.sync.getRepo), which belongs in Grain's own fix.

How it was checked

  • serve.rs asks_the_relays_to_crawl_as_it_starts: the built binary, with PDS_CRAWLERS naming a fake relay and nothing written, asks it with {"hostname": "groups.example.com"} as it starts. It is stopped with SIGTERM, so it writes coverage.
  • firehose.rs asks_the_relays_to_crawl_every_so_often: crawling every 50 ms asks the answering relay three times or more, the event log still empty. A second relay that isn't there covers the failure path.
  • Locally: cargo fmt --check, clippy with -D warnings on group-host (all targets), cargo test --test serve (all pass) and --test firehose. The firehose run failed only reads_as_atprotos_own_consumer_reads_it and a_groups_events_read_as_a_jetstream_reads_them, which need harness/firehose.mjs's packages, not installed in this worktree, and don't touch crawling. The coverage gate and the rest are left to CI.

Shortcomings

  • A relay that drops the host is subscribed again within 20 minutes at worst, not at once. If its cursor for the host is still -1 then, it misses the events from that gap.
  • Nothing re-sends the first events of a group a relay already missed (see Why).
Approved

This moves the requestCrawl asks out of Host::announce and into a firehose::crawling loop that serve spawns. The loop asks each relay as the host starts and again every 20 minutes. announce now only wakes the firehose streams.

I read the whole diff and found nothing that must change.

  • The loop does what the body says. A failing relay is logged and does not stop the loop. Asks go out one relay at a time, so a slow relay delays the ones after it. That is fine at a 20-minute interval.
  • Removing the crawled field and CRAWL_EVERY from repo.rs leaves no stray references in the diff.
  • GROUP_HOST_CRAWLERS in deploying.md is the compose-side name for PDS_CRAWLERS, so that line is correct.
  • Tests cover the start-up ask through the real binary, stopped with SIGTERM as AGENTS.md asks. They also cover the repeated ask and the failing-relay path.
  • The change file and the doc updates are present.

The rest is covered by CI's coverage gate.

Approved

The only change since my approval is in the serve.rs test. After the relay is asked, it now waits for /xrpc/_health to answer 200 before sending SIGTERM. That closes a race where the ask can arrive before the host listens for SIGTERM. It doesn't affect the earlier approval.

Merged as 23c8f43