Merge ha: replicated store, second control plane, join codes, setup and console screens
Release / build (push) Successful in 4m26s
Release / notes (push) Successful in 3s
CI / test (push) Successful in 5m14s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-03 22:59:49 +02:00
commit 36cdbce125
67 files changed
+8574 -490

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
package store
import (
"context"
"database/sql"
"time"
)
// ControlPlane is one control plane agents may dial.
type ControlPlane struct {
// URL is what an agent dials: https://10.0.0.150:7443.
URL string
Name string
AddedAt time.Time
}
// ControlPlanes lists them, oldest first: the first is the one agents try
// first.
func (s *DB) ControlPlanes(ctx context.Context) ([]ControlPlane, error) {
return queryAll(ctx, s.db, `SELECT url, name, added_at FROM control_planes ORDER BY added_at, url`, nil,
func(rows *sql.Rows) (ControlPlane, error) {
var c ControlPlane
var at int64
err := rows.Scan(&c.URL, &c.Name, &at)
c.AddedAt = fromMillis(at)
return c, err
})
}
// PutControlPlane records one, keeping when it was first added.
func (s *DB) PutControlPlane(ctx context.Context, c ControlPlane) error {
if c.AddedAt.IsZero() {
c.AddedAt = time.Now()
}
_, err := s.db.ExecContext(ctx, `INSERT INTO control_planes (url, name, added_at) VALUES (?, ?, ?)
ON CONFLICT (url) DO UPDATE SET name = excluded.name`, c.URL, c.Name, millis(c.AddedAt))
return err
}
// RemoveControlPlane forgets one.
func (s *DB) RemoveControlPlane(ctx context.Context, url string) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM control_planes WHERE url = ?`, url)
return err
}
+1
View File
@@ -51,6 +51,7 @@ var copyOrder = []string{
// of them at the moment of the move.
"cluster_secrets",
"node_engines",
"control_planes",
}
// notCopied are tables deliberately left behind.
@@ -0,0 +1,10 @@
-- Ported from the SQLite migration of the same number.
-- The control planes agents may dial, by the address they dial: what the
-- leader tells every agent, so a control plane added after the nodes were
-- onboarded is one they fail over to without their configuration being
-- edited. Written by `llmgw server add` and by init for the first one.
CREATE TABLE control_planes (
url TEXT PRIMARY KEY,
name TEXT NOT NULL,
added_at BIGINT NOT NULL
);
@@ -0,0 +1,9 @@
-- The control planes agents may dial, by the address they dial: what the
-- leader tells every agent, so a control plane added after the nodes were
-- onboarded is one they fail over to without their configuration being
-- edited. Written by `llmgw server add` and by init for the first one.
CREATE TABLE control_planes (
url TEXT PRIMARY KEY,
name TEXT NOT NULL,
added_at INTEGER NOT NULL
);
+44
View File
@@ -0,0 +1,44 @@
package store
import (
"fmt"
"net/url"
"strings"
)
// SplitPostgres reads a PostgreSQL address that may name several hosts —
// "postgres://u:p@h1:5432,h2:5432/db?…", the members of a replicated cluster,
// as pgx and libpq both accept — into its hosts, their ports, and the rest of
// the address with one placeholder host, which url.Parse can read. url.Parse
// cannot read the list itself: it took everything before the last colon for
// one host name.
func SplitPostgres(dsn string) (hosts, ports []string, rest *url.URL, err error) {
scheme, after, ok := strings.Cut(dsn, "://")
if !ok {
return nil, nil, nil, fmt.Errorf("the store address is not a URL")
}
authority, tail := after, ""
if i := strings.IndexAny(after, "/?"); i >= 0 {
authority, tail = after[:i], after[i:]
}
userinfo, hostlist := "", authority
if i := strings.LastIndexByte(authority, '@'); i >= 0 {
userinfo, hostlist = authority[:i+1], authority[i+1:]
}
for _, hp := range strings.Split(hostlist, ",") {
u, err := url.Parse("x://" + hp)
if err != nil || u.Hostname() == "" {
return nil, nil, nil, fmt.Errorf("the store address has a host that is not one: %q", hp)
}
port := u.Port()
if port == "" {
port = "5432"
}
hosts, ports = append(hosts, u.Hostname()), append(ports, port)
}
rest, err = url.Parse(scheme + "://" + userinfo + "host" + tail)
if err != nil {
return nil, nil, nil, fmt.Errorf("the store address is not a URL")
}
return hosts, ports, rest, nil
}
+25 -1
View File
@@ -43,7 +43,7 @@ func openPG(t *testing.T) *DB {
// every subtest gets a fresh file, and failed only here.
`TRUNCATE thread_messages, threads, memory_notes, usage, usage_5m, usage_1h, usage_1d, api_keys, memberships,
key_claims, limits, models, sessions, users, projects, teams,
organizations, leadership RESTART IDENTITY CASCADE`)
organizations, leadership, control_planes RESTART IDENTITY CASCADE`)
if err != nil {
t.Fatalf("clearing the test database: %v", err)
}
@@ -252,3 +252,27 @@ func TestPostgresBucketsUsageByEveryGrouping(t *testing.T) {
}
}
}
func TestPostgresMakesAnAdministrator(t *testing.T) {
// init --postgres and --patroni create the first administrator straight
// in PostgreSQL, which refused the bool SetAdmin sent.
ctx := context.Background()
s := openPG(t)
if err := s.CreateUser(ctx, User{ID: "u1", Email: "a@x", Name: "a", PasswordHash: "h",
State: UserActive, CreatedAt: time.Now()}); err != nil {
t.Fatal(err)
}
if err := s.SetAdmin(ctx, "u1", true); err != nil {
t.Fatalf("making an administrator: %v", err)
}
u, err := s.UserByEmail(ctx, "a@x")
if err != nil || !u.IsAdmin {
t.Fatalf("%+v, %v", u, err)
}
if err := s.SetAdmin(ctx, "u1", false); err != nil {
t.Fatal(err)
}
if u, _ := s.UserByEmail(ctx, "a@x"); u.IsAdmin {
t.Error("still an administrator")
}
}
+25
View File
@@ -493,3 +493,28 @@ func TestNoDefaultsLeavesLimitsUnset(t *testing.T) {
t.Errorf("limits appeared with no defaults set: rpm=%v tpm=%v", e.RPM, e.TPM)
}
}
func TestControlPlanesAreKeptInTheOrderTheyWereAdded(t *testing.T) {
ctx := context.Background()
s := openDB(t)
at := time.Now()
for i, u := range []string{"https://10.0.0.140:7443", "https://10.0.0.150:7443"} {
if err := s.PutControlPlane(ctx, ControlPlane{URL: u, Name: "c", AddedAt: at.Add(time.Duration(i) * time.Second)}); err != nil {
t.Fatal(err)
}
}
// Recorded again, as init and server add do: not moved, not doubled.
if err := s.PutControlPlane(ctx, ControlPlane{URL: "https://10.0.0.140:7443", Name: "ctl"}); err != nil {
t.Fatal(err)
}
cps, err := s.ControlPlanes(ctx)
if err != nil || len(cps) != 2 || cps[0].URL != "https://10.0.0.140:7443" || cps[0].Name != "ctl" {
t.Fatalf("%+v %v", cps, err)
}
if err := s.RemoveControlPlane(ctx, cps[1].URL); err != nil {
t.Fatal(err)
}
if cps, _ := s.ControlPlanes(ctx); len(cps) != 1 {
t.Errorf("%+v", cps)
}
}
+8 -1
View File
@@ -175,7 +175,14 @@ func (s *DB) SetUserState(ctx context.Context, id string, state UserState) error
// account is what closes sessions, and that is the lever for someone who should
// not be here at all.
func (s *DB) SetAdmin(ctx context.Context, id string, admin bool) error {
res, err := s.db.ExecContext(ctx, `UPDATE users SET is_admin = ? WHERE id = ?`, admin, id)
// An integer, not a bool: the column is an integer on both dialects,
// and PostgreSQL refuses a bool for it where SQLite took one — so making
// an administrator worked in every test and failed on a fresh install.
flag := 0
if admin {
flag = 1
}
res, err := s.db.ExecContext(ctx, `UPDATE users SET is_admin = ? WHERE id = ?`, flag, id)
if err != nil {
return err
}