Gateway and read plans

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

CodeWhat it owns
internal/transport/grpc/clusterThe cluster client: routing each call to the stream's leader and following NOT_OWNER
internal/gateway/vend.goPlan-driven serving: a client wrapper whose Read and Subscribe serve the vended part from storage
internal/gateway/fetch.go, footercache.goThe byte budget over every vended fetch, and the decoded-footer cache
internal/gateway/cacheroute.goFetches through the object cache
internal/gateway/hosts.goBucket hosts: the host grammar and the rewrite to path-style
internal/star/reader/plan.goPlan: a read plan, cut from the catalog alone
internal/transport/grpc/dataplane/readplan.goThe ReadPlan verb: authorization, presigning, and the plan's lifetime
cmd/novagatewayThe 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.

BoundValueAt the bound
Remembered leaders4,096The oldest is forgotten
Open connections128The least recently used idle connection closes; a dial is refused only when all 128 carry calls
Attempts per call, each redirect counting as one3The last answer goes back to the caller
Timeouts in a row on one leader3The 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.

internal/star/reader/plan.go
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:

--readplan-vendingNOVAD_READPLAN_VENDINGDefaultpresigned
presigned hands out URLs the node signed, the only form safe for tenants; raw hands out the bucket and key, for consumers inside the trust domain that fetch with their own storage identity.

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:

loop [each entry, within the fetch budget] GET records from seq 1000 ReadPlan(from 1000) entries, residual 50000 footer (cached), then record ranges Read(from 50000) the rest of the page one response Client Gateway Any node Object storage Leader
  1. 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.
  2. 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.
  3. Join at the residual. When the vended records run out before the page is full, the Gateway calls Read on the leader from the residual and joins the two parts into one response. When the vended records fill the page, it still calls CheckTail on the leader, because the response carries the tail and a plan never names it.
  4. 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:

EventWhat the Gateway does
The plan fails or is emptyProxies the whole read
A direct fetch fails, for example because the object was replaced or deletedReplans from the cursor, at most twice, then proxies the rest
A fetch through the cache failsFetches directly; see Object cache
The leader moves during the residual readFollows the NOT_OWNER redirect
The fetch budget is fullThe fetch waits for room

Bounds and signals

BoundDefaultSet by
Bytes in flight across vended fetches64 MiB--fetch-inflight on novagateway
Decoded footers32 MiB, each kept 5 minutes--footer-cache-bytes on novagateway
A vended page without max_bytes8 MiBFixed
Entries per plan256Fixed
Replans per read2Fixed
Plan lifetimeHalf the read grace--auditor-read-grace on novad
SignalWhat it shows
nova.gateway.vended.bytes, nova.gateway.proxied.bytesBytes served from storage, and bytes served through nodes
nova.gateway.seam.handoffsReads that joined vended records with the leader's residual
nova.gateway.replansPlans asked for again after churn
nova.gateway.fetch.wait, nova.gateway.fetch.inflight.bytesTime waiting for the fetch budget, and the budget in use
nova.gateway.footercache.hits, .misses, .evictionsThe footer cache
nova.gateway.fleet.fallbacksFalls 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.

See also

On this page