Orchestrator
Apps/Orchestrator. Knows every Gateway and GameServer, decides which GameServer simulates which cell, and starts/stops instances through a provider.
Instance lifecycle
Starting --register--> Ready --Remove/drain--> Draining --empty--> Dead --60 s--> forgotten
| | ^
+--no register 120 s--+--no heartbeat 6 s-------------------------+
- A provider-started instance is recorded as
Startingwith its id (INSTANCE_ID), and becomesReadywhen it registers. - An instance started by hand registers without id and gets one (
gw-N,gs-N, providermanual). - Heartbeat every second with metrics and players. The answer gives a GameServer its cells and a Gateway the whole directory (cells + GameServer endpoints) and the players to kick, the announcements and the admin commands for its players (a test session:
POST /api/players/{id}/command,PlayerCommand: a DEV command, an outfit forged and worn, a teleport that heals, an arena fight; the Gateway does it as if the player had asked).Known=falsemakes the instance register again (an Orchestrator restart without Redis; with Redis the instances are still known). - Remove (
DELETE /api/instances/{id}) drains: a GameServer's cells move to the others (its players follow by handoff), then the reconciler stops it once it has no cell and no player (or after 60 s). A Gateway is stopped once empty or after 60 s; its players reconnect throughGET /api/gateway. - Reconciler (every second): marks silent instances dead and moves their cells, stops drained instances, releases idle cells, starts instances up to
MinGateways/MinGameServers, records one history point for the Monitor. - Why it stops one (2026-12-01,
InstanceRecord.StopReason,StopReasons): its log saysStopping GameServer gs-N (no heartbeat | start timeout | drained | empty dungeon)(before:(Dead)for all, a dungeon's instance stopped 30 s after its last player looked like a crash). One that went silent (no heartbeat, start timeout), and every dungeon's instance (a visit read afterwards), has its last 200 log lines written to the Orchestrator's log before its container is removed (IInstanceProvider.LastLogsAsync: the Docker provider's container logs;Reconciler.LastLogLines).
Camps: the 9 cells of a camp's block (ServerWorldGen, same seed as the GameServers) always belong to one GameServer: a player near a camp activates the whole block, a camp cell goes where its block already is, a Monitor assignment or a drain moves the whole block.
All decisions are in ClusterStore (one lock, TimeProvider injected) and covered by Tests/Orchestrator.Tests. It is one class in several files: ClusterStore.cs (state, Monitor view), .Instances.cs (register, heartbeat, drain, death, best Gateway, kick), .Cells.cs (claim, assign, moves, active areas, idle cells, directory), .Maintenance.cs (the reconciler's pass), .Persistence.cs (snapshot / restore). The pure rules are apart and tested alone: CellPlacementPolicy (which GameServer gets a cell: least loaded, a neighbour's owner preferred within a load of 3; a player weighs half a cell) and ClusterView / ClusterHistory (what the Monitor shows). Records: ClusterRecords.cs.
State in Redis, leader and standby (Coordination/)
With Redis:Connection set (docker compose, deploy), the cluster state survives an Orchestrator restart and several replicas can run; without it (Start-Dev.ps1, tests) everything stays in memory with one replica, as before.
- One leader. The replicas compete for a lease in Redis (
{prefix}:orchestrator:leader, value = replica id + address, expiryRedis:LeaseSeconds= 5 s), renewed every 250 ms, and every third of the lease while it starts instances (a start can take longer: a Docker image pulled; before, a standby could take over and start the same ones; a leader that lost the lease starts no more). Only the leader decides: cells, heartbeats, Monitor actions, the reconciler, starting and stopping containers. - The others forward. A standby answers its own
/api/healthand forwards every other request to the leader (LeaderProxy, headerX-Orchestrator-Forwarded, never forwarded twice; 503 while there is no leader). So the instances, the ApiServer and the Monitor use one name for all the replicas (orchestrator, compose network alias). - Saved state. After every change worth keeping (instances, cells and epochs, id counters, pending kicks:
ClusterStore.Revision), the leader writes aClusterSnapshot(JSON,{prefix}:orchestrator:cluster). Metrics, players and the Monitor history are not saved: the next heartbeats bring them back. Saving is a Lua script that checks the lease first: a replica that lost the lease can never overwrite the new leader's state. - Taking over. When a standby gets the lease (the leader died: within ~5 s; a clean shutdown gives the lease up at once), it loads the snapshot and gives every instance and cell a fresh "last seen", so nothing is declared dead or idle before its next heartbeat. Epochs and the directory version jump by
ClusterStore.RestoreMargin(100 000): the old leader may have handed out a few epochs after its last save, the new ones are higher. The provider then adopts the instances (IInstanceProvider.AdoptAsync): Docker keeps the containers of the snapshot that are running the image configured now and removes the others (leftovers, or old versions after a deploy with a new tag); a known instance that is gone is forgotten and its cells move at once. Players stay connected: Gateways and GameServers do not see the change of leader. - Redis unreachable. The leader stops deciding after 80 % of the lease without renewal (another replica may have taken over) and takes over again when Redis answers; the instances keep running with their last cells meanwhile.
GET /api/healthtellsstore(memory/redis),role(leader/standby),replicaandleader; the Monitor shows the store and the leader in its header.
Compose: service redis (AOF persistence, password REDIS_PASSWORD), orchestrator (leader at first) and orchestrator-standby (docker compose --profile ha up -d). Tests: LeadershipTests (fake Redis), RedisCoordinatorTests against a real Redis when NEXUSOFFATES_TEST_REDIS is set (docker run -d --rm -p 6399:6379 redis:7-alpine, then NEXUSOFFATES_TEST_REDIS=localhost:6399 dotnet test Tests/Orchestrator.Tests).
Providers (Providers/IInstanceProvider.cs)
Orchestrator:Provider |
What it does |
|---|---|
Manual |
Starts nothing. Instances started by hand register themselves. |
Process |
Local processes (Process:Gateway / Process:GameServer commands). Ports 7100+ (gateways) and 7200+ (game servers). Used by Tools/Dev/Start-Dev.ps1. |
Docker |
Containers on the compose network (Docker:Network), images nexusoffates/gateway and nexusoffates/gameserver, label nexusoffates.managed=true. A Gateway gets a host UDP port from 7100. When a replica becomes the leader, managed containers that are not in the saved state, or run another image than the configured one, are removed. Needs the Docker socket. |
Every started instance receives the variables of Orchestrator:InstanceEnv (JWT key, worker key, DbWorker and Orchestrator URLs) plus INSTANCE_ID, PORT, ADVERTISE_HOST, PUBLIC_HOST, PUBLIC_PORT.
Kubernetes (next): implement IInstanceProvider with the official KubernetesClient: a GameServer = a Pod (or a StatefulSet replica) with ADVERTISE_HOST = its Pod DNS name; a Gateway = a Pod behind a NodePort/LoadBalancer UDP Service whose address becomes PUBLIC_HOST/PUBLIC_PORT. k9s can then watch them like any workload. Nothing else in the cluster depends on the provider.
HTTP API
Internal (instances): POST /api/instances/register, POST /api/instances/{id}/heartbeat, GET /api/directory, POST /api/cells/claim, GET /api/gateway/best (the Gateway with the fewest players, counting those it sent there since that Gateway's last heartbeat: a crowd logging in at once, e.g. after a restart, is spread instead of all going to the one that looked empty), GET /api/health.
Raids (called by the ApiServer, see Raid board in Architecture.md): POST /api/camps/{id}/reserve (give a camp's block to one GameServer now, for the countdown), POST /api/raids/start (start the raid on the camp's server and teleport its members through their Gateways), POST /api/notices (lobby messages to players, through their Gateways).
Admin (header X-Admin-Key when Orchestrator:AdminKey is set): GET /api/state, POST /api/instances {kind}, DELETE /api/instances/{id}, POST /api/cells/assign {x, z, gameServerId}, POST /api/players/{id}/kick.
kind: 0 = Gateway, 1 = GameServer.
Settings
| Key | Default | |
|---|---|---|
Orchestrator:WorldSeed |
1234 | seed of the world |
Orchestrator:CampSeed |
6210279 (dev) | secret seed of the camps, sent to the GameServers at registration (RegisterResponse.CampSeed), never to a client; 0 = no camps. Set your own (CAMP_SEED) |
Orchestrator:CampsEnabled |
false | the goblin camps and the raid board on or off (CAMPS_ENABLED); off for the alpha: the GameServers get camp seed 0 (no camp spawns, no plateau, no scouting nor raid). The client hides the Raids tab and the camps' buttons (Client.Core.Features.Camps). |
Orchestrator:Provider |
Manual | Manual, Process, Docker |
Orchestrator:MinGateways / MinGameServers |
1 / 1 | kept running by the reconciler |
Orchestrator:HeartbeatTimeoutSeconds |
6 | silence before an instance is dead |
Orchestrator:IdleCellSeconds |
10 | grace before a cell outside every player's active area (5×5 cells) is released |
Orchestrator:AdminKey |
empty (open) | required by the admin endpoints |
Orchestrator:AdvertiseUrl |
http://<own IPv4>:<port> |
address the other replicas forward to when this one leads |
Redis:Connection |
empty (memory, one replica) | e.g. redis:6379,password=... |
Redis:Prefix |
nexusoffates | key prefix (several clusters on one Redis) |
Redis:LeaseSeconds |
5 | a standby takes over this long after the leader died |
Docker:PublicHost |
localhost | address the clients use to reach the Gateways |