Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ Registros: `journalctl -u varanda-seeder -f`. `seed list` e `seed drop`: `system

Provisória até o parecer jurídico sobre registros de acesso (issue #38).

- **Formato.** Uma linha por evento na saída padrão. No serviço, `--json`: JSON Lines com os eventos `health`, `ws`, `ready`, `peer`, `admitted`, `rejected`, `have`, `peer-dropped`, `warning` e `fatal` (o mais frequente é `have`, a cada crescimento de um diário). Erros vão para a saída de erro como `erro: <mensagem>`. A linha não traz data e hora: o journal e o Docker carimbam cada uma.
- **Formato.** Uma linha por evento na saída padrão. No serviço, `--json`: JSON Lines com os eventos `health`, `ws`, `ready`, `peer`, `admitted`, `rejected`, `have`, `peer-dropped`, `warning`, `fatal` e `stopped` (o mais frequente é `have`, a cada crescimento de um diário). Erros vão para a saída de erro como `erro: <mensagem>`. A linha não traz data e hora (o journal e o Docker carimbam cada uma), exceto o instante `at` de `ws` e `ready` e o `stopListeningAt` de `stopped`, em ms desde 1970, que medem uma implantação.
- **O que entra.** Chaves públicas de diários (64 hex), com tamanho ou motivo de recusa; a chave Noise dos pares (`peer`, `peer-dropped`); o tópico da associação (`ready`). São pseudônimos, e contam como dado pessoal quando alguém consegue ligá-los a uma pessoa.
- **O que nunca entra.** Endereço IP ou porta de par (a entrada WebSocket nunca registra o endereço do cliente), conteúdo de publicação ou de perfil, certificados, chaves secretas e as requisições ao endpoint de saúde.
- **Estatísticas de uso (serviço de conta).** O serviço de conta acrescenta só `{ event: 'usage', reason }`, com `reason` igual a `ligada`, `desligada`, `invalido` ou `falhou`. A linha não traz e-mail, chave, @nome nem endereço.
Expand Down
21 changes: 19 additions & 2 deletions apps/cli/lib/commands/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ async function runSeeder (flags) {
// Handlers go in BEFORE start(): a signal during startup (DHT join) must not
// take Node's default terminate path; the seeder is closed once start settles.
let onSignal
let stopStartedAt = 0
const stopped = new Promise((resolve) => {
onSignal = () => resolve()
onSignal = () => { if (stopStartedAt === 0) stopStartedAt = Date.now(); resolve() }
process.once('SIGINT', onSignal)
process.once('SIGTERM', onSignal)
})
Expand All @@ -147,7 +148,7 @@ async function runSeeder (flags) {
}
if (flags.wsPort !== null) {
ws = await listenWs(flags, seeder)
log({ event: 'ws', host: ws.host, port: ws.port })
log({ event: 'ws', host: ws.host, port: ws.port, at: Date.now() })
}
await seeder.start()
} catch (err) {
Expand All @@ -162,15 +163,31 @@ async function runSeeder (flags) {
// A fatal purge failure closes the seeder by itself.
await Promise.race([stopped, seeder.closed])
} finally {
// Drain start: the signal. When the seeder closed itself (fatal), its
// store is already closed here, so closeMs would measure nothing: it is
// left out and `fatal: true` is logged instead.
const selfClosed = stopStartedAt === 0
if (selfClosed) stopStartedAt = Date.now()
process.off('SIGINT', onSignal)
process.off('SIGTERM', onSignal)
if (relay) relay.close()
// Before seeder.close(), which destroys the app clients' sockets (a bare
// 1006): they get 1012 'reiniciando' instead. Adds at most about 1 s.
// drain() also stops listening, so a redial during seeder.close() is
// refused (Caddy retries it for up to 5 s) rather than answered 503.
// stopListeningAt: drain() stops listening first, so this is its epoch ms
// to within the call; with the next process's ws `at` it gives the dark
// port time across a deploy.
const stopListeningAt = Date.now()
if (ws) await ws.drain()
const drainedAt = Date.now()
await seeder.close()
const timing = { event: 'stopped', drainMs: drainedAt - stopStartedAt }
if (selfClosed) timing.fatal = true
else timing.closeMs = Date.now() - stopStartedAt
// Only when a WebSocket listener actually stopped.
if (ws) timing.stopListeningAt = stopListeningAt
log(timing)
if (health) await health.close()
if (ws) await ws.close()
}
Expand Down
58 changes: 58 additions & 0 deletions apps/cli/test/seed-shutdown-timing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// `seed --json` logs the timings of one deploy: `ready` and `ws` carry an
// absolute `at`, and a SIGTERM ends with a `stopped` event holding drainMs,
// closeMs and stopListeningAt, so one production deploy gives the close time
// and (with the next process's `ws.at`) the time the port was dark.
const test = require('brittle')
const fs = require('node:fs')
const path = require('node:path')
const { spawn } = require('node:child_process')
const b4a = require('b4a')
const { testnet, tmpDir, until, bootstrapFlags } = require('./helpers/net')
const { Seeder } = require('@varanda/core/lib/seeder')
const { createSeederCert, generateAssociationKeyPair } = require('@varanda/core/lib/cert')

const BIN = path.join(__dirname, '..', 'bin', 'varanda.js')

const env = {}
for (const [k, v] of Object.entries(process.env)) if (!k.startsWith('VARANDA_')) env[k] = v

function prepareSeederDir (t) {
const kp = generateAssociationKeyPair()
const assoc = b4a.toString(kp.publicKey, 'hex')
const dataDir = tmpDir(t, 'varanda-seed-')
Seeder.ensureConfig(dataDir, assoc)
const { publicKey } = Seeder.createKey(dataDir)
const certFile = path.join(tmpDir(t, 'varanda-sc-'), 'seeder-cert.json')
fs.writeFileSync(certFile, JSON.stringify(createSeederCert({ seeder: publicKey }, kp)))
Seeder.addCert(dataDir, certFile, assoc)
return dataDir
}

test('seed: ws and ready carry at; SIGTERM logs stopped with drainMs, closeMs, stopListeningAt', async (t) => {
t.timeout(120000)
const { bootstrap } = await testnet(t)
const child = spawn(process.execPath, [BIN, 'seed', '--json', '--data', prepareSeederDir(t), '--ws-port', '0', ...bootstrapFlags(bootstrap)], { env })
let stdout = ''
child.stdout.on('data', (d) => { stdout += d })
child.stderr.on('data', () => {})
const exited = new Promise((resolve) => child.on('close', resolve))
t.teardown(() => child.kill('SIGKILL'), { force: true })
const events = () => stdout.split('\n').filter(Boolean).map((l) => JSON.parse(l))
const event = (name) => until(() => events().find((e) => e.event === name), { name: 'evento ' + name, timeoutMs: 30000 })

const ws = await event('ws')
const ready = await event('ready')
const before = Date.now()
t.ok(Number.isSafeInteger(ws.at) && ws.at <= before && ws.at > before - 120000, 'ws.at is a plausible epoch ms')
t.ok(Number.isSafeInteger(ready.at) && ready.at >= ws.at && ready.at <= before, 'ready.at is a plausible epoch ms, after ws.at')

child.kill('SIGTERM')
t.is(await exited, 0, 'seed exits 0')
const stopped = events().find((e) => e.event === 'stopped')
t.ok(stopped, 'stopped event logged')
if (!stopped) return
t.ok(Number.isSafeInteger(stopped.drainMs) && stopped.drainMs >= 0, 'drainMs >= 0')
t.ok(Number.isSafeInteger(stopped.closeMs) && stopped.closeMs >= stopped.drainMs, 'closeMs >= drainMs')
t.ok(Number.isSafeInteger(stopped.stopListeningAt) && stopped.stopListeningAt >= before && stopped.stopListeningAt <= Date.now(), 'stopListeningAt is a plausible epoch ms after the signal was sent')
t.alike(Object.keys(stopped).sort(), ['closeMs', 'drainMs', 'event', 'stopListeningAt'], 'no other fields')
})
3 changes: 3 additions & 0 deletions docs/changelog/feat-seeder-shutdown-timing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Alterado

- O semeador passa a registrar, no log JSON, o instante absoluto (`at`, em ms desde 1970) nos eventos `ws` e `ready` e, ao desligar, o evento `stopped` com `drainMs`, `closeMs` e `stopListeningAt`, para medir numa implantação real o tempo de fechamento e o tempo em que a porta ficou sem escuta; nenhum comportamento muda (PR `feat/seeder-shutdown-timing`). Verificação em `docs/verification/feat-seeder-shutdown-timing.md`.
Loading
Loading