~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
158 lines · 8.5 KB Raw History
// The processes: the reference PDS and its PLC directory, in this one, and
// the server, which the harness starts, kills and starts again, through
// its implementation's adapter (lib/adapters/); and what receives the
// server's traces.

import { createServer } from 'node:net'
import { chmodSync, mkdtempSync, readFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { TestNetworkNoAppView } from '@atproto/dev-env'
import { BACKFILL_MS, CACHE_BYTES, IMPL, INVITES, LIFECYCLE, LIMITS, MIGRATE, S3, command } from './args.mjs'
import { ADAPTERS } from './adapters/index.mjs'
import { LIMITS_UPLOAD_LIMIT, MAX_REPO_IMPORT } from './env.mjs'
import { startCrawlers } from './crawlers.mjs'
import { startClientWeb } from './clients.mjs'
import { startPlcProxy } from './plc-proxy.mjs'
import { catchReferenceMail } from './mail.mjs'
import { CDN_PATTERN, startAppView, startServices } from './services.mjs'
import { watch } from './observe.mjs'
import { setFront } from './front.mjs'
import { hostSets } from './sets.mjs'
import { resolveHere, startHandles } from './handles.mjs'
import { startReceiver } from './otlp.mjs'
import { BUCKETS, S3_KEYS, S3_TIMEOUT_MS, startS3 } from './s3.mjs'

/// The reference's test network, the PLC directory both sides register
/// DIDs with, the server's process, and the two sides requests go to, each
/// `{name, url, publicUrl, recoveryKey, rotationKey, plcSigner}`: the URL
/// its DID documents name, the `did:key`s of the rotation keys it lists in
/// each DID it creates, and its rotation key, to sign operations as it
/// does. The server's side also has its `adapter`, its `admin`
/// authorization, `takeMail()`, without the page API `signIn`, and on a
/// server that hosts one account, its `password` and `claim`
/// (lib/adapters/). Set by `start`, with the server's data directory,
/// and with `--limits` or `--lifecycle` the proxy its requests to the
/// directory go through; the OTLP receiver the server exports its spans to,
/// and the URL of its metrics (spec/OPERATIONS.md); the lexicon
/// authority both sides resolve permission sets from, `{pds, did}`
/// (lib/sets.mjs); and the handle service both sides resolve handles
/// through, `{dns, fetch}` (lib/handles.mjs).
export let net, plcUrl, server, ref, cand, dataDir, plcProxy, otlp, metricsUrl, authority, s3, handles

export const freePort = () =>
  new Promise((ok) => {
    const s = createServer()
    s.listen(0, '127.0.0.1', () => {
      const { port } = s.address()
      s.close(() => ok(port))
    })
  })

/// The server's implementation's adapter.
export const adapter = ADAPTERS[IMPL]
if (!adapter) {
  console.error(`no adapter ${IMPL}; there are ${Object.keys(ADAPTERS).join(', ')}`)
  process.exit(2)
}

/// Start the reference and the server, neither with an account, the server
/// on an empty data directory, with its metrics on a port of their own, its
/// traces going to a receiver here and its log kept in an empty directory
/// apart from its data (spec/OPERATIONS.md, "Kept"), both resolving every
/// NSID's lexicon from the harness's authority, a third PDS on the same
/// directory; wait until the server answers.
export async function start() {
  const crawlers = await startCrawlers()
  const appView = await startAppView()
  const bsky = { bskyAppViewUrl: appView.url, bskyAppViewDid: appView.did, bskyAppViewCdnUrlPattern: CDN_PATTERN }
  // With --s3, both sides keep their blobs in buckets of the store here, a
  // bucket each (spec/BLOBS.md, "Where blobs are kept"); with --migrate, the
  // server's are moved into its bucket and out again, the reference's on disk.
  if (S3 || MIGRATE) {
    s3 = await startS3(S3_KEYS)
    for (const b of Object.values(BUCKETS)) s3.makeBucket(b)
  }
  const blobstore = S3 ? { blobstoreDiskLocation: undefined, blobstoreS3Bucket: BUCKETS.reference, blobstoreS3Region: 'us-east-1', blobstoreS3Endpoint: s3.url, blobstoreS3ForcePathStyle: true, blobstoreS3AccessKeyId: S3_KEYS.accessKeyId, blobstoreS3SecretAccessKey: S3_KEYS.secretAccessKey, blobstoreS3UploadTimeoutMs: S3_TIMEOUT_MS, blobstoreS3RequestTimeoutMs: S3_TIMEOUT_MS } : {}
  net = await TestNetworkNoAppView.create({ pds: { hostname: 'localhost', rateLimitsEnabled: LIMITS, inviteRequired: INVITES, crawlers: crawlers.reference, ...bsky, ...(BACKFILL_MS ? { repoBackfillLimitMs: BACKFILL_MS } : {}), ...(LIMITS ? { blobUploadLimit: LIMITS_UPLOAD_LIMIT } : {}), maxImportSize: MAX_REPO_IMPORT, ...blobstore } })
  plcUrl = net.plc.url
  catchReferenceMail(net.pds.ctx.mailer)
  handles = await startHandles()
  resolveHere(net.pds.ctx.idResolver.handle, handles, net.pds.ctx.cfg.identity.serviceHandleDomains)
  await startServices(plcUrl)
  authority = await hostSets(plcUrl)
  // The reference reads its setting on each resolution: set now, as the
  // authority's DID is not known before the network is up.
  net.pds.ctx.cfg.lexicon.didAuthority = authority.did
  const recoveryKey = net.pds.ctx.cfg.identity.recoveryDidKey
  const port = await (adapter.port ?? freePort)()
  dataDir = mkdtempSync(join(tmpdir(), 'pds-server-'))
  const logDir = mkdtempSync(join(tmpdir(), 'pds-server-log-'))
  // Outside the data directory, whose size is held to its bounds without it
  // (spec/OPERATIONS.md, "Kept"; spec/STORE.md, "Bounds").
  const keptLogDir = mkdtempSync(join(tmpdir(), 'pds-server-kept-'))
  // Open to all, so that the server must close it (spec/LIMITS.md, "Files").
  chmodSync(dataDir, 0o755)
  if (LIMITS || LIFECYCLE) plcProxy = await startPlcProxy(plcUrl)
  const clientFetch = await startClientWeb()
  otlp = await startReceiver()
  const metricsPort = await freePort()
  metricsUrl = `http://127.0.0.1:${metricsPort}/metrics`
  const made = await adapter.start({ command, port, dataDir, logDir, keptLogDir, plc: plcProxy?.url ?? plcUrl, recoveryKey, crawlers: crawlers.server, appView, clientFetch, handles, limits: LIMITS, invites: INVITES, backfillMs: BACKFILL_MS, cacheBytes: CACHE_BYTES, metricsPort, otlp, lexiconAuthority: authority.did, blobstore: S3 ? { url: s3.url, bucket: BUCKETS.server, ...S3_KEYS, timeoutMs: S3_TIMEOUT_MS } : null })
  server = made.server
  ref = { name: 'reference', url: net.pds.url, publicUrl: net.pds.ctx.cfg.service.publicUrl, recoveryKey, rotationKey: net.pds.ctx.plcRotationKey.did(), plcSigner: net.pds.ctx.plcRotationKey }
  cand = { name: 'server', url: made.url, publicUrl: made.publicUrl, issuer: made.issuer, recoveryKey: made.recoveryKey, rotationKey: made.rotationKey, plcSigner: made.plcSigner, adapter, admin: made.admin, takeMail: made.takeMail, signIn: adapter.signIn?.bind(adapter), password: made.password, claim: made.claim }
  // A server named by an origin it is not reached at (spec/COMPAT.md, "Its
  // own address"): requests for that origin go to its URL.
  if (made.fronted) setFront(made.publicUrl, made.url)
  watch(cand.url, () => server.lives.length - 1)
  server.start()
  await waitReady(server, cand.url)
}

/// The last `n` lines of the file at `path`, or nothing if it cannot be read.
function tail(path, n) {
  try {
    return readFileSync(path, 'utf8').trimEnd().split('\n').slice(-n).join('\n')
  } catch {
    return ''
  }
}

/// The logs to read when something differs: where they are, and the end of
/// the server's standard error and of its log, for a runner whose files are
/// gone once the run ends.
export const logs = () => `${server.name} log: ${server.logPath} (standard error: ${server.errPath})\n--- its standard error, last 20 lines:\n${tail(server.errPath, 20)}\n--- its log, last 15 lines:\n${tail(server.logPath, 15)}`

export async function shutdown(code) {
  server?.close()
  s3?.close()
  otlp?.close()
  plcProxy?.close()
  await authority?.pds.close().catch(() => {})
  await net?.close()
  process.exit(code)
}

/// Wait until `p` answers describeServer at `url`, and say how long it took.
export async function waitReady(p, url) {
  const started = Date.now()
  for (;;) {
    if (p.exited) {
      console.error(`the ${p.name} exited before it was ready (${JSON.stringify(p.exited)}); its log is ${p.logPath}\n--- its standard error, last 20 lines:\n${tail(p.errPath, 20)}`)
      await shutdown(1)
    }
    try {
      const r = await fetch(`${url}/xrpc/com.atproto.server.describeServer`)
      if (r.ok) return Date.now() - started
    } catch {}
    const limit = adapter.readyMs ?? 30_000
    if (Date.now() - started > limit) {
      console.error(`the ${p.name} did not answer describeServer within ${limit / 1000}s; its log is ${p.logPath}`)
      await shutdown(1)
    }
    await new Promise((ok) => setTimeout(ok, 20))
  }
}

export const wait = (ms) => new Promise((ok) => setTimeout(ok, ms))