Drei Teile, die zusammengehören, ohne dass ein Dienst dazwischen nötig wäre. so_reuseport: mehrere Instanzen binden dieselbe Adresse, der Kernel verteilt. Zusammen mit dem neuen Drain ergibt das ein Rolling Update — die neue Instanz nimmt Verkehr an, während die alte noch bedient. SIGTERM schließt zuerst den Listener, bedient Laufendes fertig und beendet dann; ein Request, der nach drain_timeout (Standard 15 s) noch hängt, wird verworfen und geloggt. Vorher war SIGTERM ein Abbruch mitten in der Antwort. packaging/junction@.service macht daraus zwei Befehle (start junction@blue, stop junction@green); auf n95 durchgespielt: zwei Listener auf einem Port, blue loggt draining/drained, green antwortet weiter mit 200. Gemeinsamer Cache ohne Redis: jeder Knoten gibt seinen Speicher über einen eigenen Port aus, cache_peer fragt ihn bei einem lokalen Fehlschlag, ein Fill wird asynchron weitergereicht (höchstens vier gleichzeitig, ein verworfener Push ist kein Fehler). Der Transport ist eine kleine HTTP-Teilmenge mit einem Rahmen aus zwei 32-Bit-Längen — Head und Body so, wie der Knoten sie gesendet hätte. Ein Treffer beim Nachbarn landet mit der Restlaufzeit lokal, damit der nächste Aufruf nicht wieder fragt. Ohne cache_secret_env öffnet der Port gar nicht erst. Zwei echte Funde dabei: SO_REUSEPORT ist unter -D_POSIX_C_SOURCE=200809L nicht deklariert, ein #ifdef darum kompiliert stillschweigend weg (Symptom: "Address already in use" trotz so_reuseport on) — der Makefile setzt jetzt -D_DEFAULT_SOURCE. Und ein Peer-Aufruf ohne Half-Close lässt den Nachbarn auf seinen Empfangs-Timeout warten, womit jeder Lookup in den 150 ms verhungert und der Mesh wie kaputt aussieht. --check prüft die Konfiguration jetzt wirklich, ohne zu binden (stand schon als Behauptung in den Beispielen). Tests: neuer Abschnitt 33/34 (15 Prüfungen, eigener Portbereich PORT+200…207): Peer-Treffer ohne Anbieter-Aufruf, Geheimnis erzwungen, fehlendes Geheimnis = kein Port, stiller Peer kostet genau den Timeout (203 ms gemessen), zwei Instanzen auf einem Port, Drain, Übergabe ohne Ausfall, exclusiver Port ohne so_reuseport. make test: PASS, 210 Prüfungen, 154 s.
40 lines
1.7 KiB
C
40 lines
1.7 KiB
C
/* junction/mesh.h — shared response cache across instances, without a service in
|
|
* between.
|
|
*
|
|
* Every node keeps its cache in its own memory and hands it out over a port of its
|
|
* own. A node that misses locally asks its peers before it pays the provider; a node
|
|
* that fills a cache entry pushes it to its peers. No Redis, no memcached, no daemon:
|
|
* the transport is a very small HTTP subset with a binary frame inside.
|
|
*
|
|
* Replication is a speedup, never a dependency. A peer that is slow, dead or has a
|
|
* different key costs the timeout and nothing else — the request goes to the provider
|
|
* as if the mesh did not exist. That is why the timeouts are short and the push is
|
|
* fire-and-forget.
|
|
*/
|
|
#ifndef J_MESH_H
|
|
#define J_MESH_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
/* Starts the listener if the configuration names one. Returns 1 when the mesh is
|
|
* running, 0 when it is off or the port could not be bound. */
|
|
int j_mesh_start(void);
|
|
void j_mesh_stop(void);
|
|
|
|
/* 0 = a peer had it; the buffers are malloc'd and belong to the caller.
|
|
* Everything else = nobody had it, or nobody answered. */
|
|
int j_mesh_lookup(uint64_t key, char **head, size_t *head_len,
|
|
char **body, size_t *body_len, double *age);
|
|
|
|
/* Hands one filled entry to the peers. Returns immediately; the transfer runs in its
|
|
* own thread and is dropped when too many are already on their way. */
|
|
void j_mesh_store(uint64_t key, const char *head, size_t head_len,
|
|
const char *body, size_t body_len, double ttl_s);
|
|
|
|
/* Counters for /status and /metrics. Any pointer may be NULL. */
|
|
void j_mesh_stats(unsigned long *lookups, unsigned long *hits,
|
|
unsigned long *pushes, unsigned long *errors, int *peers);
|
|
|
|
#endif /* J_MESH_H */
|