PartitionedRouter

class PartitionedRouter(groups: Collection<PartitionOwnership>, channel: ClusterChannel, options: ClusterOptions = ClusterOptions(), observer: Observer = Observer.NONE, clock: Clock = Clock.systemUTC()) : CommandRouter

The Partitioned router: a command runs on the owner of its log partition, whose state cache is warm and who appends without contention.

  • A command of a context this node has no lease group for in groups (keyed by context) runs locally.

  • A command for a partition this node holds and accepts runs here, counted for the partition's hand-off.

  • Otherwise it is forwarded over channel to PartitionOwnership.forwardTarget by its deadline, ClusterOptions.forwardTimeout from then, capped by the command's own wait deadline. The timeout is the request's: the channel connects to the peer first, within the wait's deadline, so the first forward to a peer that started a moment ago does not spend its second opening a connection. The forward then reserves one of the peer's streams, waiting its turn while they are all taken, until its deadline, and is sent once it has one.

  • A command whose route names no owner first reads its lease group's routes once more (RouteLookup: one read at a time, at most one each tenth of a second, within a fifth of one, none after one failed until a refresh succeeds) and is sent to the owner they name now: an owner that acquired a moment ago, which the routes read each ClusterOptions.routeRefreshInterval do not show yet. Run here without the lease, its commit would land above the new owner's block, where that owner's tail reaches it only at its next read of the store. A command with a route never reads.

  • A forward the owner surely did not receive degrades to running locally, without retrying it: no known owner, the peer unreachable or not connected by the wait's deadline (ClusterChannelException.unsent), or a refusal before the forward's deadline (Refusal.NOT_OWNER, Refusal.EXPIRED, a type the peer does not know, a forward it could not read or of a format it does not read): the peer did not run it.

  • A forward to a peer that has ClusterOptions.maxInFlightPerNode forwards from this node in flight is answered busy (ResultCodes.BUSY), as the node's own admission bound answers, and runs nowhere. The bound keeps one slow peer from taking the node's whole budget of commands in flight; running the command here instead, without the lease, would make the owner reload the aggregate's state and stall its tail, adding to the load of the peer that is already slow.

  • Past its deadline a forward runs nowhere: one not sent by then (no stream to the peer was free, or its wait ran out before it could be sent), or refused after it, is withdrawn (ResultCodes.WAIT_TIMEOUT) once its wait has run out, as a command still queued in its lane at its wait's deadline is, and answered busy (ResultCodes.BUSY) while it has not, as a full lane is. Either way it was not executed, so it is not reported unknown; and running it here instead, under the load that kept it from the owner, would put a second writer beside the owner.

  • A forward that was sent waits for the reply until its deadline and the channel's ClusterChannel.answerGrace after it: the owner answers a command it withdraws at its wait's deadline just after that deadline, and that answer (ResultCodes.WAIT_TIMEOUT, not executed) is the outcome. One that got no reply by then, or failed after it was sent, does not degrade (revising): the owner may be deciding it still, and deciding it here too would run a decide that does I/O twice. Its outcome is ResultCodes.UNKNOWN; a retry with the same request id is answered from the owner's commit, if any. So route answers a forward at most the grace after its deadline; a waited command's wait still ends at its own deadline.

A failed forward also refreshes the routes. Degrading is safe without fencing: the kernel deduplicates by request id, and version-unique keys turn concurrent writers into conflicts that are decided again.

A command runs under ExecutionLease.HELD when this node holds its partition, else under ExecutionLease.NOT_HELD: only the owner's kernel caches the states it loads and lets a decide that does I/O skip the request-id lookup.

Constructors

Link copied to clipboard
constructor(groups: Collection<PartitionOwnership>, channel: ClusterChannel, options: ClusterOptions = ClusterOptions(), observer: Observer = Observer.NONE, clock: Clock = Clock.systemUTC())

Functions

Link copied to clipboard
open suspend override fun route(model: AggregateModel<*>, command: Command<*>, wait: WaitContext?, local: suspend () -> Settlement): Settlement