Files
MikrotikManager/backend/src/services/database-restore-checkpoint-operator.ts
T

145 lines
7.0 KiB
TypeScript

import { constants, type BigIntStats } from "node:fs"
import { lstat, open, realpath, rename, unlink } from "node:fs/promises"
import path from "node:path"
import { isDeepStrictEqual } from "node:util"
import type { RestoreJournalHead } from "../db/database-restore-quarantine.js"
import {
DatabaseRestoreCheckpointRefused, parseDatabaseRestoreCheckpoint, readRestoreCheckpointFile,
sameRestoreCheckpointFile, type RestoreCheckpoint, type RestoreWriterHead,
} from "./database-restore-checkpoint.js"
const createExclusive = constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0)
function sameEntry(left: BigIntStats, right: BigIntStats): boolean {
return left.dev === right.dev && left.ino === right.ino
}
async function requireOwnedFile(file: string, identity: BigIntStats): Promise<void> {
const current = await lstat(file, { bigint: true })
if (!current.isFile() || current.nlink !== 1n || !sameEntry(current, identity)) throw new DatabaseRestoreCheckpointRefused()
}
export async function stagePendingRestoreCheckpointFile(
file: string, expected: Extract<RestoreCheckpoint, { status: "sealed" }>,
permit: { operationId: string; archiveSha256: string; dataSha256: string; writerHead?: RestoreWriterHead }, expectedFileSha256: string,
): Promise<void> {
if (expected.status !== "sealed" || !expectedFileSha256) throw new DatabaseRestoreCheckpointRefused()
const { operationId, archiveSha256, dataSha256 } = permit
const writerHead = permit.writerHead && { epoch: permit.writerHead.epoch, sha256: permit.writerHead.sha256 }
await replaceRestoreCheckpointFile(file, expected, {
...expected, status: "pending", operationId, archiveSha256, dataSha256, writerHead,
}, expectedFileSha256)
}
/** Consumes the operator-owned pending checkpoint before the restore worker can begin. */
export async function consumeRestoreCheckpointFile(
file: string, expected: Extract<RestoreCheckpoint, { status: "pending" }>, claimId: string,
): Promise<void> {
if (expected.status !== "pending") throw new DatabaseRestoreCheckpointRefused()
await replaceRestoreCheckpointFile(file, expected, { ...expected, status: "consumed", claimId })
}
export async function publishSealedRestoreCheckpointFile(
file: string, expected: Extract<RestoreCheckpoint, { status: "consumed" }>, appliedHead: RestoreJournalHead, writerHead?: RestoreWriterHead,
): Promise<void> {
if (expected.status !== "consumed" || !appliedHead || appliedHead.bytes <= (expected.head?.bytes ?? 0)) {
throw new DatabaseRestoreCheckpointRefused()
}
if (expected.writerHead ? !writerHead || BigInt(writerHead.epoch) <= BigInt(expected.writerHead.epoch) : writerHead !== undefined) {
throw new DatabaseRestoreCheckpointRefused()
}
const { format, runtimeTargetSha256, activationSha256 } = expected
await replaceRestoreCheckpointFile(file, expected, {
format, status: "sealed", runtimeTargetSha256, activationSha256, head: appliedHead,
writerHead: writerHead && { epoch: writerHead.epoch, sha256: writerHead.sha256 },
})
}
async function replaceRestoreCheckpointFile(
file: string, expected: RestoreCheckpoint, next: RestoreCheckpoint, expectedFileSha256?: string,
): Promise<void> {
const parent = path.dirname(file)
const lockFile = `${file}.lock`
const pendingFile = `${file}.pending`
try {
if (!path.isAbsolute(file) || path.resolve(file) !== file) throw new DatabaseRestoreCheckpointRefused()
const canonicalParent = await realpath(parent)
const windows = process.platform === "win32"
if ((windows ? canonicalParent.toLowerCase() !== parent.toLowerCase() : canonicalParent !== parent)) {
throw new DatabaseRestoreCheckpointRefused()
}
const parentIdentity = await lstat(parent, { bigint: true })
if (!parentIdentity.isDirectory()) throw new DatabaseRestoreCheckpointRefused()
const bytes = Buffer.from(JSON.stringify(next))
parseDatabaseRestoreCheckpoint(bytes)
// shortcut: Windows test runs lack directory fsync, use POSIX for power-loss durability.
const directory = windows ? undefined : await open(parent, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW)
const requireParent = async () => {
const current = await lstat(parent, { bigint: true })
if (!current.isDirectory() || !sameEntry(current, parentIdentity)) throw new DatabaseRestoreCheckpointRefused()
}
try {
if (directory && !sameEntry(await directory.stat({ bigint: true }), parentIdentity)) throw new DatabaseRestoreCheckpointRefused()
await requireParent()
const lock = await open(lockFile, createExclusive, 0o600)
let preserveSidecars = false
let lockIdentity: BigIntStats | undefined
let pendingIdentity: BigIntStats | undefined
try {
lockIdentity = await lock.stat({ bigint: true })
await lock.sync()
await directory?.sync()
const before = await readRestoreCheckpointFile(file)
if (!isDeepStrictEqual(before.checkpoint, expected) ||
(expectedFileSha256 !== undefined && before.sha256 !== expectedFileSha256)) {
throw new DatabaseRestoreCheckpointRefused()
}
const pending = await open(pendingFile, createExclusive, 0o600)
try {
pendingIdentity = await pending.stat({ bigint: true })
await pending.writeFile(bytes)
if (!windows) {
const { uid, gid, mode } = before.identity
if (pendingIdentity.uid !== uid || pendingIdentity.gid !== gid) await pending.chown(Number(uid), Number(gid))
await pending.chmod(Number(mode & 0o777n))
}
await pending.sync()
pendingIdentity = await pending.stat({ bigint: true })
} finally { await pending.close() }
await requireParent()
await requireOwnedFile(lockFile, lockIdentity)
if (!sameRestoreCheckpointFile(pendingIdentity, await lstat(pendingFile, { bigint: true }))) {
throw new DatabaseRestoreCheckpointRefused()
}
const current = await readRestoreCheckpointFile(file)
if (!sameRestoreCheckpointFile(before.identity, current.identity) || !isDeepStrictEqual(current.checkpoint, expected) ||
(expectedFileSha256 !== undefined && current.sha256 !== expectedFileSha256)) {
throw new DatabaseRestoreCheckpointRefused()
}
// A failed rename or directory sync leaves the operator lease as an unresolved attempt.
preserveSidecars = true
await rename(pendingFile, file)
pendingIdentity = undefined
await directory?.sync()
preserveSidecars = false
} finally {
await lock.close()
if (!preserveSidecars && lockIdentity) {
await requireParent()
await requireOwnedFile(lockFile, lockIdentity)
if (pendingIdentity) {
await requireOwnedFile(pendingFile, pendingIdentity)
await unlink(pendingFile)
}
await unlink(lockFile)
await directory?.sync()
}
}
} finally { await directory?.close() }
} catch { throw new DatabaseRestoreCheckpointRefused() }
}