Gateway and read plans
How the Gateway routes calls to stream leaders and serves older records straight from object storage.
The Gateway is an HTTP front door that holds no stream state. It forwards each call to the node that serves the stream, with the caller's own token. For reads of older history, it asks any node for a read plan and fetches the records from object storage itself. A vended read returns exactly the records the node would have returned. A plan never names the tail: every record at or after the plan's residual comes from the node. When a plan cannot be used, the node serves the read, or the part of it not yet fetched.
Where it lives
| Code | What it owns |
|---|---|
internal/transport/grpc/cluster | The cluster client: routing each call to the stream's leader and following NOT_OWNER |
internal/gateway/vend.go | Plan-driven serving: a client wrapper whose Read and Subscribe serve the vended part from storage |
internal/gateway/fetch.go, footercache.go | The byte budget over every vended fetch, and the decoded-footer cache |
internal/gateway/cacheroute.go | Fetches through the object cache |
internal/gateway/hosts.go | Bucket hosts: the host grammar and the rewrite to path-style |
internal/star/reader/plan.go | Plan: a read plan, cut from the catalog alone |
internal/transport/grpc/dataplane/readplan.go | The ReadPlan verb: authorization, presigning, and the plan's lifetime |
cmd/novagateway | The flags and assembly |
The cluster client
The cluster client implements the data-plane interface over the whole fleet. Each call goes to its stream's remembered leader. A NOT_OWNER answer names the leader's address, and the client follows it and remembers it. The first call to a stream may take one redirect; later calls go straight to the leader. Calls that name no stream, such as the auth exchange and the queue verbs, go to the current seed node, and a dead seed rotates to the next address. The Go SDK, and the CLI built on it, use the same client.
| Bound | Value | At the bound |
|---|---|---|
| Remembered leaders | 4,096 | The oldest is forgotten |
| Open connections | 128 | The least recently used idle connection closes; a dial is refused only when all 128 carry calls |
| Attempts per call, each redirect counting as one | 3 | The last answer goes back to the caller |
| Timeouts in a row on one leader | 3 | The leader is forgotten |
A leader that refuses a call is forgotten at once. A leader whose calls run out of the caller's time is kept for up to three calls, because a slow refusal ahead of the redirect can use up the time too. When a call fails because a departed node no longer answers, the client forgets that leader and closes its connection, unless another call is still using it. Connections to the seed addresses are never closed.
Every call forwards the caller's bearer token unchanged, so the node checks the caller's own permissions. Redirect targets are the nodes' advertised addresses, dialed as given. Each node's --advertise must therefore resolve and route from where the Gateway runs, just as for any SDK client.
The fleet listener
A node can also listen on a second gRPC address, --fleet-listen, kept for Nova's own processes. Its connections do not count against the client listeners' caps (--max-client-connections, 4,096), so a flood of client connections cannot keep the Gateway from connecting. It has its own backstop, --max-fleet-connections (2,048). A Gateway started with --fleet-addr dials these listeners first. It presents its own fleet credential beside each caller's token. The credential admits the call to the listener and grants nothing else. Redirects then name each node's fleet address (--fleet-advertise).
When the fleet listener cannot be reached or refuses the credential, the Gateway sends calls to its --addr seeds instead. It logs a warning, counts the fall in nova.gateway.fleet.fallbacks, and tries the fleet listener again after about 30 seconds.
Subscriptions over SSE
Each SSE event is one batch, and its event ID is the bookmark after it: on a plain stream, the next sequence number. When the subscription behind the response fails, the response ends, and the client reconnects with Last-Event-ID to resume exactly there. The Gateway keeps no cursor itself. When the caller's token expires, the response sends event: auth-expired, and the client reconnects with a fresh token and its last bookmark.
Bucket hosts
Every handler speaks the path-style grammar, /v1/streams/{bucket}/{key}. A bucket host only rewrites a request into it. With one or more --domain, a host that reads {bucket}.b.{door} beneath any of the domains belongs to that bucket. The door is at least one label, by convention the location: orders.b.eu-west-1.nova.example. The Gateway inserts the bucket into the path and passes the request on, so /v1/streams/events/records on that host is /v1/streams/orders/events/records. Only the b label makes a host a bucket's, so no bucket name is reserved.
Every other host passes through unchanged and serves path-style: the door itself, the bare domain, a host outside every domain, a load balancer's health check. A bucket host serves only the stream routes. Anything else on it answers 404, because administration goes through the door.
A Gateway can serve several domains at once, as during a domain move, and each names the same buckets. The bucket is always a host's first label, so two domains that overlap can only agree on it. Without --domain, every host serves path-style.
Read plans
ReadPlan tells a consumer which stream objects hold a stream's records from a starting position. Any node answers from one catalog read. It touches no object storage and needs no redirect.
type Plan struct {
Entries []PlanEntry
VendedUntil stream.SeqNum // the residual: ask the node from here
More bool
Resolved bool
}
type PlanEntry struct {
ObjectKey string
FirstID stream.SeqNum
FooterOff, FooterLen int64
PayloadBytes int64
MinTsMs, MaxTsMs int64
}Only stream objects are listed. A transit bundle holds many streams, so it is never handed out. The entries tile the range, each ending where the next begins, and the last ends at VendedUntil. A start that no stream object covers returns no entries, with the residual at the start itself. A plan returns at most 256 entries, about 4 GiB at denorm's target object size, and pages on with More. It requires the read permission. A tail-relative start is refused by shape, since a plan never names the tail.
Vending is handing those locations to a consumer. A flag on novad chooses the form:
A plan's lifetime is half of --auditor-read-grace, so 5 minutes by default. It is not a setting of its own, so a plan can never outlive the grace. Presigned URLs expire then; for raw plans it is advice. Deleting an object waits the full read grace after its last reference, so a consumer has time to finish. If a plan expires, or names a revision that was replaced, the consumer asks for a new plan from its current cursor and carries on.
Plan-driven serving in the Gateway
With --serve-plan on (the default), the Gateway wraps its cluster client. A read, or the catch-up of an SSE subscription, then runs like this:
- Plan. The Gateway asks for a plan at the read's start. Without a caller limit, or with a tail-relative start, the read goes to the node unchanged.
- Fetch. For each entry, it reads the footer through its footer cache, then the record ranges, through the object cache when one is set. Each fetch first takes its bytes from one budget shared by all responses (
--fetch-inflight, 64 MiB), so a slow store or consumer waits instead of growing memory. - Join at the residual. When the vended records run out before the page is full, the Gateway calls
Readon the leader from the residual and joins the two parts into one response. When the vended records fill the page, it still callsCheckTailon the leader, because the response carries the tail and a plan never names it. - Replan. Any failed fetch, usually an object that was replaced or deleted, asks for a new plan from the current cursor, at most twice. The records already read stay in the response. After the second replan, the node serves the rest from the cursor, and the Gateway logs
gateway: vended read fell back to the fleet.
A plan error, an empty plan, or an unresolved start sends the whole read through the node as if vending were off. So does a failed residual read or tail check. The worst vending can do is return the system to proxied reads. --serve-plan=false turns it off entirely.
Raw plans need --storage-bucket, the bucket the Gateway fetches from under its own identity. Without it, the Gateway cannot fetch a raw entry, so each such read replans twice and then reads from the node. Presigned plans are fetched either way.
Failure points
The Gateway keeps nothing durable, so a crash loses only the requests in flight. A client retries from its last cursor. These are the places a vended read can stop:
| Event | What the Gateway does |
|---|---|
| The plan fails or is empty | Proxies the whole read |
| A direct fetch fails, for example because the object was replaced or deleted | Replans from the cursor, at most twice, then proxies the rest |
| A fetch through the cache fails | Fetches directly; see Object cache |
| The leader moves during the residual read | Follows the NOT_OWNER redirect |
| The fetch budget is full | The fetch waits for room |
Bounds and signals
| Bound | Default | Set by |
|---|---|---|
| Bytes in flight across vended fetches | 64 MiB | --fetch-inflight on novagateway |
| Decoded footers | 32 MiB, each kept 5 minutes | --footer-cache-bytes on novagateway |
A vended page without max_bytes | 8 MiB | Fixed |
| Entries per plan | 256 | Fixed |
| Replans per read | 2 | Fixed |
| Plan lifetime | Half the read grace | --auditor-read-grace on novad |
| Signal | What it shows |
|---|---|
nova.gateway.vended.bytes, nova.gateway.proxied.bytes | Bytes served from storage, and bytes served through nodes |
nova.gateway.seam.handoffs | Reads that joined vended records with the leader's residual |
nova.gateway.replans | Plans asked for again after churn |
nova.gateway.fetch.wait, nova.gateway.fetch.inflight.bytes | Time waiting for the fetch budget, and the budget in use |
nova.gateway.footercache.hits, .misses, .evictions | The footer cache |
nova.gateway.fleet.fallbacks | Falls from the fleet listener to the --addr seeds, by reason (down or refused) |
Tests
internal/gateway:TestVendedReadServesStreamObjectBytes,TestVendedReadCrossesTheSeam,TestVendedSubscribeCatchUpSplices,TestVendedReadReplansUnderChurn,TestVendedReadExhaustsReplans,TestDisableVendingKillSwitch,TestFetchPoolBackpressure,TestFooterCacheBudgetAndTTL; bucket hosts:TestVirtualHostedBucket,TestEveryDomainNamesTheSameBuckets,TestHostsOutsideTheBucketTreePassThrough,TestBucketHostServesOnlyStreams.internal/transport/grpc/cluster:TestBounceTeachesTheHolder,TestHolderMemoryEvictsTheOldest,TestConnPopulationStaysBoundedUnderChurn,TestADepartedStarsConnIsClosed,TestAHangingHolderIsDroppedAfterThreeTimeouts,TestARefusingHolderIsForgottenAtOnce.