Check there's room for the database's copy before an update

Before dumping the app's database for a rollback, coreapps reads its size
and refuses the update, changing nothing, if the apps' disk can't hold
the copy and keep 1 GB free; big databases say the copy takes a while.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 19:40:03 -07:00
co-authored by Claude Opus 5.5
parent 2d29ca62b3
commit f56641bbe1
4 changed files with 66 additions and 1 deletions
+10 -1
View File
@@ -502,7 +502,16 @@ Later:
updating each of its apps (`lib/functions.sh`), and `bin/btrfs-snapshot`, updating each of its apps (`lib/functions.sh`), and `bin/btrfs-snapshot`,
`restore-btrfs-snapshot` and `free-up-space -s` manage snapshots. So `restore-btrfs-snapshot` and `free-up-space -s` manage snapshots. So
`/data`'s reflink copy works on every Core `/data`'s reflink copy works on every Core
- [ ] Large databases: the dump takes time and disk on every update - Large databases (2026-12-03): before dumping, coreapps reads the
database's size (`pg_database_size`; MariaDB's `information_schema`
table sizes) and refuses the update, changing nothing, if the apps'
disk can't hold the copy and still keep 1 GB free; above 200 MB it
says the copy takes a while, and above 30 seconds how long it took.
Tested on dev.fedcom.net (sizes, the refusal with a raised reserve, the
message with a lowered limit)
- [ ] Very large databases: a dump on every update may still be too
slow; a btrfs snapshot of the database server's data, or skipping
the copy with the user's agreement, could replace it
- [ ] Uninstalling a need without removing the app (now refused) - [ ] Uninstalling a need without removing the app (now refused)
- [ ] `bin/start` / `bin/stop` don't know coreapps' apps (see "Tell - [ ] `bin/start` / `bin/stop` don't know coreapps' apps (see "Tell
Federated"); `restart: always` covers reboots Federated"); `restart: always` covers reboots
+9
View File
@@ -259,6 +259,15 @@ The Core's own scripts trace secrets, so their output goes to
the table's owner is the app's user, the app's user can still read it, the table's owner is the app's user, the app's user can still read it,
and no `.coreapps-rollback-*` folder is left. A successful update leaves and no `.coreapps-rollback-*` folder is left. A successful update leaves
none either. none either.
10a. Room for the database's copy (with `core/coreapps` loaded as a
module, only in the test): `database_size("postgres",
"coreapp_<name>")` and `database_size("mysql", "powerdns")` give sizes,
a missing database `None`. With `ROLLBACK_RESERVE` larger than the disk,
an update is refused: "keeps a copy of its database (... MB) until the
update has worked ... has ... GB free ... Nothing was changed", the
container isn't restarted and no `.coreapps-rollback-*` folder is left.
With `BIG_DATABASE = 0`: "Keeping a copy of its database (...) in case
the update fails; this takes a while...", then "Updated".
11. Superpanel: after deploy, `/federated/apps/superpanel/data/applist.json` 11. Superpanel: after deploy, `/federated/apps/superpanel/data/applist.json`
(the dashboard) and `catalog.json` have the app; `service` has (the dashboard) and `catalog.json` have the app; `service` has
`URL="https://hello.$DOMAIN/"` and applist the same `url`. Change `URL="https://hello.$DOMAIN/"` and applist the same `url`. Change
+42
View File
@@ -967,6 +967,31 @@ RESTORE = {
} }
# The database's size, to check there's room for its copy first
DB_SIZE = {
"postgres": ["docker", "exec", "postgresql", "psql", "-U", "postgres", "-Atc",
"SELECT pg_database_size('{db}')"],
"mysql": ["docker", "exec", "pdnsmysql", "sh", "-c",
'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb -uroot -N -e "SELECT '
"COALESCE(SUM(data_length + index_length), 0) FROM information_schema.tables "
"WHERE table_schema = '{db}'\""],
}
# Left free on the apps' disk after the database's copy, for the Core
ROLLBACK_RESERVE = 1024 ** 3
# Above this, say that keeping the copy takes a while
BIG_DATABASE = 200 * 1024 ** 2
def size_text(n):
return f"{n / 1024 ** 3:.1f} GB" if n >= 1024 ** 3 else f"{n / 1024 ** 2:.0f} MB"
def database_size(need, db):
out = run([c.replace("{db}", db) for c in DB_SIZE[need]])
text = out.stdout.strip()
return int(text) if out.returncode == 0 and text.isdigit() else None
def keep_for_rollback(m, p, backup, log): def keep_for_rollback(m, p, backup, log):
"""Copies of the app's service folder, settings, database and (where the """Copies of the app's service folder, settings, database and (where the
filesystem makes it cheap) /data, in backup. Refuses the update if the filesystem makes it cheap) /data, in backup. Refuses the update if the
@@ -980,12 +1005,29 @@ def keep_for_rollback(m, p, backup, log):
for need in marker_needs(name): for need in marker_needs(name):
if need in DUMP: if need in DUMP:
file, cmd = DUMP[need] file, cmd = DUMP[need]
# The copy is about the database's size at most (pg_dump -Fc
# compresses; mariadb-dump's SQL is close to it)
size = database_size(need, m["dbname"])
free = shutil.disk_usage(backup).free
if size is not None and size + ROLLBACK_RESERVE > free:
raise Refused(
f"Updating {name} keeps a copy of its database ({size_text(size)}) until the "
f"update has worked, to roll back to, and the apps' disk has "
f"{size_text(free)} free (it keeps {size_text(ROLLBACK_RESERVE)} for the "
"Core). Nothing was changed. Free some space first.")
if size is not None and size > BIG_DATABASE:
say(f"Keeping a copy of its database ({size_text(size)}) in case the update "
"fails; this takes a while...")
started = time.monotonic()
with open(os.path.join(backup, file), "wb") as out, open(log, "a") as err: with open(os.path.join(backup, file), "wb") as out, open(log, "a") as err:
result = subprocess.run([c.replace("{db}", m["dbname"]) for c in cmd], result = subprocess.run([c.replace("{db}", m["dbname"]) for c in cmd],
stdout=out, stderr=err) stdout=out, stderr=err)
if result.returncode != 0: if result.returncode != 0:
raise Refused(f"Couldn't back up {name}'s database before updating, " raise Refused(f"Couldn't back up {name}'s database before updating, "
f"so nothing was changed (log on the Core: {log}).") f"so nothing was changed (log on the Core: {log}).")
if time.monotonic() - started > 30:
say(f"Kept it ({size_text(os.path.getsize(os.path.join(backup, file)))}, "
f"{time.monotonic() - started:.0f} seconds).")
# A reflink copy shares the files' blocks until one side changes (btrfs, # A reflink copy shares the files' blocks until one side changes (btrfs,
# XFS); a full copy of /data could be too big, so there's none # XFS); a full copy of /data could be too big, so there's none
data = os.path.join(APPS, name, "data") data = os.path.join(APPS, name, "data")
+5
View File
@@ -488,6 +488,11 @@ Common causes:
the failed version wrote is gone. Its log is printed above that line the failed version wrote is gone. Its log is printed above that line
(`coreapp logs` now shows the old version's). When the version wasn't (`coreapp logs` now shows the old version's). When the version wasn't
raised, the image that failed is the one running again. raised, the image that failed is the one running again.
- "keeps a copy of its database ... Nothing was changed. Free some space
first.": before an update the Core copies the app's database, to roll
back to, and the apps' disk hasn't room for it (it keeps 1 GB free for
the Core). That's the user's to sort out; tell them. With a big database,
updates say they take a while for the same reason.
- `coreapp check` fails but deploy worked: a new certificate can take a - `coreapp check` fails but deploy worked: a new certificate can take a
minute; it retries for two. It also tries the VM directly, which tells the minute; it retries for two. It also tries the VM directly, which tells the
app apart from the way in. app apart from the way in.