|
nanoMMO 0.97.8
Building an MMO: The Engineering Series
|
Each world serves its metrics for Prometheus at http://localhost:9464/metrics. They cover the players online in each land, how long each tick takes, how long saving the players takes in each tick, the bytes it sends, the messages it receives, the lines of chat, said or refused, and the stories asked for, told, refused or failed, with how long each one took, the players it dropped without saving them, by why (nanommo_players_dropped_total), and the war kills it handed the saver, for their wars' scores (nanommo_war_kills_total), which the scores in MySQL should add up to. The bots serve theirs on port 9465, measuring how old the world on a player's screen would be, by transport, and how long each bot's login took, by what came of it, timeouts too (nanommo_bot_login_duration_seconds). The saver serves its on port 9466. They cover nanommo_duplicate_items, the serials that more than one item in MySQL has, after its last flush, which is zero unless an item's been copied (see Saving players), when its last good flush ended, and the players waiting for the next. The login server serves its on port 9467, measuring how long it took to answer each request, by what came of it, ok, refused or failed (nanommo_login_duration_seconds). Prometheus finds those two by their names, saver and login. Those are what the objectives in Reliability are made from.
compose.yaml starts the servers, MySQL and Redis together with Prometheus, which finds the worlds in Consul and scrapes each of them every two seconds, with its name as the world label, and Grafana, with a dashboard of those metrics, world by world. The dashboard and Grafana's data source are files in monitoring/, so they're set up the same way on any machine.
nanommo_bot connects a swarm of bots that play like players. Each one runs to a random place on the island, within 170 metres of the middle, or on one trip in five into its land's gate, and wanders around there like a player at the keys, running a third of the time, for 5 to 15 directions of half a second to 3 seconds each, before it sets off again. When a bot gets where it was going, it says something, one time in four, from a few lines. Once it has seen its land's storyteller, one trip in eight goes there instead, where it asks for a story about three words from the game. While it wanders around somewhere, one direction in four uses one of the skills it knows, from its world, so that the bots' cooldowns go out with their worlds, as players' do. A bot drinks a potion once it's down to half its health, one every ten seconds, and wears what it finds in a slot it has free, so that the servers save and send bags as they would players'. Quests come first. Between trips, a bot looks at what's around it, put together from the world's parts as the client does, and turns in a quest it's done if its giver is there, or goes after the nearest monster a quest it's on asks for, or takes a quest it hasn't done from an NPC there who gives it. It runs there, and swings, or takes or turns in the quest, so that the servers count kills and save quests as they would players'. Before its quests, a bot swears to a faction, each in turn by its number (FactionFor, in bot/factions.h), unless its world shows it sworn already. It walks to the faction's herald, where it sees them, or where the NPCs file stands them in its land, and swears there, or gives up if nobody's there, so the crowd in the Meadows wears all three colours within a few seconds of arriving (see Factions). A bot that stops getting closer, at the sea or a lake, gives up on its trip. Each one logs in as bot1, bot2 and so on, and creates its account the first time. It takes the login server's host, the game server's host, how many bots, how many seconds to spread their arrival over, the number of its first bot, so that several swarms can play on one server, the transport for the worlds, udp or tcp, UDP by default, and war, which sends the swarm to war. The bots with odd numbers are in Red, and the even ones in Blue, whose first bots found them, take them to war and ask the rest in, by name. Until all its side is in, each bot waits at its side's camp, in lines of five 8 metres west (Red) or east (Blue) of where new players come in, or for a minute and a half after it joined, and then, in their guild, a bot goes for the nearest enemy it can see, before monsters and quests (bot/war.h), so the two sides charge each other in lines.
About sixty times a second, like a client's frames, the bots look at how old their newest world is, from the time the server puts in each world, and Prometheus finds them through Docker's DNS, under the name bots. The bots and the server have to share a clock for that, as they do on one machine.
A bot that loses its world, say as a world dropped it, logs in again, after a wait that doubles with each try that fails, from a second to thirty, and a random part of that, so that bots dropped together don't all come back together, and five a second at most.
Or in Docker, next to the server, with 300 bots, arriving over two minutes, unless BOTS and RAMP say otherwise, from bot1, or BOTS_FIRST.
A few hundred bots need more open files than macOS allows a shell by default, so on the Mac, run ulimit -n 4096 first. Stop everything with docker compose --profile load down.
Prometheus checks the rules in monitoring/alerts.yml every five seconds, the objectives' every 30, and its alerts page, http://localhost:9090/alerts, shows each of the alerts below as inactive, pending or firing.
Prometheus sends the alerts to Alertmanager, at http://localhost:9093, which emails each one when it fires and when it resolves. The mail goes to Mailpit, a mail server for development, which keeps it, and shows it at http://localhost:8025 and through its API.
To get the mail somewhere else, smtp_smarthost in monitoring/alertmanager.yml points at a real mail server, with smtp_auth_username and smtp_auth_password_file. The rules have tests, in monitoring/alerts_test.yml, which CI runs too.
Each server writes what it does to its output. That's who joins and leaves, what they say, what they learn, and what goes wrong. docker compose logs shows that a service at a time, and loses it with the container. Loki, Grafana's log store, keeps the logs of all the game's containers, and Grafana queries them next to the metrics.
The server dashboard has two panels from Loki at the bottom, under the metrics. One is errors a minute, the servers' stderr lines counted a minute at a time by service with LogQL's count_over_time, and the other what the servers say, their lines as they come, newest first. A spike on a chart and the lines from that minute are on one page. Grafana marks each line with its level label, which Alloy gives our servers' lines from their stream, error from stderr, and info from stdout.
Loki checks rules on the logs too, like Prometheus checks its rules on the metrics, every 15 seconds, from monitoring/loki-rules.yml, and sends the alerts to the same Alertmanager, which emails them (see Alerts).
Loki has no tenants of its own here, so its rules are in its tenant's folder, fake, the name Loki gives the tenant when auth_enabled is off.
CI checks both configs, as a typo in either would leave us without logs, and Loki and Alloy refuse a field they don't know.
Neither looks at the rules' LogQL. A rule Loki can't parse shows up in its log, as unable to list rules, and the rest of its group doesn't load, which docker compose logs loki | grep rules shows.
In production, Loki listens on the server's localhost, like Prometheus, and Alloy has no port.
The metrics say the logins' 99th percentile went up, and a server's log says what that one process did. A trace follows one request through each process it goes through, the login server, Redis, MySQL, a world and the saver, as spans, each a piece of the work, with when it started, how long it took, and which span it was part of. So a slow login shows where its time went, and in which process (decision 38).
The tracer is our own, server/tracing.h, which speaks OpenTelemetry's formats, using W3C Trace Context's traceparent, 00-<trace id>-<span id>-01, for a span's place, and OTLP's JSON for the spans. A span that's open is its thread's current span, and a span started on the thread goes under it. A span that ends goes on a queue, and a thread of the tracer's sends what's there once a second, or as soon as there are 512, so the thread that ended it, which may be the tick's, never waits for the collector. With 4096 waiting, a span that ends is dropped, and so is a batch the collector doesn't take, each counted in nanommo_spans_dropped_total, by the worlds and the saver. The login server serves no metrics, so its drops aren't counted. A server that stops sends what's left, for two seconds at most.
Each trace starts with a request, and the work it does in each process is a span, named by what it is. The login server's spans are login's, a world's are its instance's, such as Ember-7777, as two instances of a world can run at once, and the saver's are saver's.
| Trace | Its spans |
|---|---|
| A request to the login server | log in, create account, enter world, create character, delete character, list characters or list worlds, with argon2id hash or argon2id verify under a login, which is slow on purpose |
| Entering a world | The login server's enter world, its issue token, and the world's hello under that, with lease player and load player |
| A trade | The world's trade, save players and publish trade, then the saver's save trades, with its transaction in MySQL |
| A handover | The leaving instance's hand over, pass leases and an issue token for each player, and each player's hello on the next instance |
Each call to Redis, each statement to MySQL and each request to Consul is a span under the span that's current as it's made, such as redis GETDEL, mysql SELECT players or consul GET /v1/health/service/nanommo-world, with db.system and db.operation, as OpenTelemetry names them. A statement's span has its SQL, with a ? for each value, and never a key or a value. A player's mistake, such as a wrong password, is an attribute, nanommo.refused, and the servers' failures, such as MySQL being down, mark their spans as errors. The lines a server prints as it answers a traced request end with its trace's id, trace_id=.
These aren't traced.
A trace goes from one process to another in what already carries the request there, as a traceparent.
A record without a traceparent, from a server that doesn't trace, is read as before, and one that can't be read loses the trace, not the player. A session from the login server carries no trace, as it lasts half an hour, through every request a player makes with it, which would make a trace with no end, so logging in is a trace of its own, and entering a world another.
Alloy keeps some of the traces, not all (monitoring/config.alloy). Sampling at the head, as a trace starts, decides before anything's happened, so it keeps a slow trace, or one that failed, no more often than the rest, and those are the ones worth reading. Sampling at the tail waits until a trace is whole, and decides from all its spans, at the cost of holding each one in memory until then. Alloy waits ten seconds from a trace's first span, as a trade's saver half comes up to five seconds after its world half, and keeps every trace with an error, every trace that took over 100 milliseconds, and a tenth of the rest, picked by their ids, to see what a normal one looks like.
A span that comes after its trace's decision, such as a hello with a token issued 30 seconds before, follows it. Tempo makes its metrics from what's kept, so they count a tenth of the quick traces, and a log line's trace_id opens its trace when Alloy kept it.
In Grafana's Explore, http://localhost:3000/explore, Tempo searches with TraceQL, Tempo's query language, and a trace opens as a waterfall of its spans, with a button to its servers' lines in Loki, a minute either side. In Loki, a line's trace_id opens its trace, and on a Prometheus chart, a dot, an exemplar, opens the trace a sample came from.
Tempo's API answers the same, as JSON.
In episode 87, with a hundred bots, a player's hello took about a millisecond on the world's thread, the one that ticks, and most of it was MySQL, with three statements to load the player and one for their guild, each 0.15 to 0.25 milliseconds against Redis's 0.06. With MySQL's replies delayed by two milliseconds, each statement took 4.7, two round trips, as Database::Run prepared each statement, a round trip, ran it, another, and closed it. A hello took 19 milliseconds, and a handover of a hundred players, whose hellos come all at once, kept the next instance busy with them for 1.95 seconds, in which it didn't tick. Since 0.87.0, a connection keeps the reads it's prepared, and runs one again in a round trip. With the same delay, each statement takes 2.4 milliseconds, a hello 9.9, and the handover's hundred hellos 0.97 seconds.
To see that for yourself, netem, from the Dockerfile's netem stage, delays what MySQL sends.
In production, Tempo listens on the server's localhost, like Loki, and the servers reach Alloy on Compose's network.
The metrics say how the servers are doing, and the game's analytics say what the players do. They say who killed what, what dropped, which quests they take and which they finish, the levels they reach, what they drink and where they die. Each of those is an event, a row in ClickHouse, a database that keeps each column on its own and reads the columns a query asks about and nothing more, so that counting millions of rows by a few of their columns is quick.
In production, ClickHouse listens on the server's localhost, like Loki.