Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

12.7 KB
Raw Blame
import { Server, createServer } from 'node:http'
import { AddressInfo } from 'node:net'

import { ImageScrubConsumerMetrics } from './metrics'
import {
    POISON_MAX_REJECTED_MS,
    ScrubAborted,
    ScrubClient,
    ScrubContractError,
    ScrubPoisoned,
    socketWaitReason,
} from './scrub-client'

type Reply = { status: number; body?: string; durationMs?: number; destroySocket?: boolean; neverReply?: boolean }

describe('ScrubClient', () => {
    let server: Server
    let replies: Reply[]
    let requests: number
    let replyFor: ((body: string) => Reply | undefined) | undefined
    let nowMs: number
    let port: number

    // A real loopback server rather than a mocked `request`: the retry loop only matters in terms of
    // what it does with actual responses, and the 503 shed path in particular is a status the sidecar
    // sends without reading the body, which a mock would not reproduce.
    beforeEach(async () => {
        replies = []
        requests = 0
        replyFor = undefined
        nowMs = 0
        server = createServer((req, res) => {
            requests += 1
            const chunks: Buffer[] = []
            req.on('data', (c: Buffer) => chunks.push(c))
            req.on('end', () => {
                // Keyed on the request body when a rule is set, so a poison image and its healthy
                // neighbours can be in flight at once, which is the only arrangement in which the
                // "failing while others succeed" rule means anything.
                const reply = replyFor?.(Buffer.concat(chunks).toString()) ??
                    replies.shift() ?? { status: 200, body: 'scrubbed' }
                nowMs += reply.durationMs ?? 0
                if (reply.neverReply) {
                    return
                }
                if (reply.destroySocket) {
                    req.socket.destroy()
                    return
                }
                res.writeHead(reply.status).end(reply.body ?? '')
            })
        })
        await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
        port = (server.address() as AddressInfo).port
    })

    afterEach(async () => {
        await new Promise((resolve) => server.close(resolve))
    })

    const client = (deadLetters = false, timeoutMs = 1000, onSleep?: () => Promise<void>): ScrubClient =>
        new ScrubClient(
            `http://127.0.0.1:${port}`,
            timeoutMs,
            deadLetters,
            // Backoff is asserted separately; sleeping for real would only make this slow and flaky.
            async (ms) => {
                nowMs += ms
                await onSleep?.()
            },
            () => 1,
            () => nowMs
        )

    const closeServer = (): Promise<void> =>
        new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())))
    const listenAgain = (): Promise<void> =>
        server.listening ? Promise.resolve() : new Promise((resolve) => server.listen(port, '127.0.0.1', resolve))

    it.each([
        ['shed with 503', 503],
        ['a 500 from a struggling sidecar', 500],
        ['a 502 from a half-started sidecar', 502],
        ['a 429', 429],
        // Reachable from image content, not from a bad deployment: the sidecar's success path
        // returns whatever the scrub produced, so a zero-length result is a plain 200 with no body.
        ['success with no bytes', 200],
    ])('keeps waiting through %s until the sidecar returns bytes', async (_label, status) => {
        // The regression this exists for: a bounded retry that gives up turns a saturated sidecar
        // into permanent data loss, when Kafka is already holding the image durably and the only
        // cost of waiting is lag. Nothing here may resolve to null or throw.
        replies = Array.from({ length: 25 }, () => ({ status, body: '' }))

        await expect(client().scrub(Buffer.from('image'))).resolves.toEqual(Buffer.from('scrubbed'))
        expect(requests).toBe(26)
    })

    it.each([
        ['a misdirected URL or renamed route', 404, ''],
        ['a drifted request contract', 400, ''],
        ['a method the sidecar does not serve', 405, ''],
    ])('fails the batch on %s rather than waiting on it', async (_label, status, body) => {
        // Waiting only makes sense for answers a later attempt could change. These say the deployment
        // is wrong, and waiting on them forever would turn a config mistake into a pod that consumes
        // nothing, passes every probe, and shows up only as lag hours later.
        replies = [{ status, body }]

        await expect(client().scrub(Buffer.from('image'))).rejects.toThrow(ScrubContractError)
        expect(requests).toBe(1)
    })

    it.each([
        ['undecodable', 422],
        ['too large', 413],
    ])('gives up immediately on %s, which no retry could change', async (_label, status) => {
        replies = [{ status }]

        await expect(client().scrub(Buffer.from('image'))).resolves.toBeNull()
        expect(requests).toBe(1)
    })

    it('retries until the sidecar listener accepts connections', async () => {
        await closeServer()
        let retries = 0
        const failedProbesBeforeListen = 3
        const scrubClient = new ScrubClient(`http://127.0.0.1:${port}`, 1000)

        await scrubClient.waitUntilReachable(async () => {
            retries += 1
            if (retries === failedProbesBeforeListen) {
                await listenAgain()
            }
        })

        expect(retries).toBe(failedProbesBeforeListen)
        expect(requests).toBe(1)
    })

    it.each([
        ['nothing listening on the sidecar port', 'refused', { listening: false }],
        ['a connection the sidecar dropped before replying', 'reset', { reply: { status: 0, destroySocket: true } }],
        ['a request the sidecar never answered', 'timeout', { neverReplyToFirst: true, timeoutMs: 100 }],
    ] as const)('labels %s as "%s" and keeps waiting', async (_label, reason, setup) => {
        // A dropped socket is what every pod sees when the sidecar closes its idle connections on
        // shutdown, and a timeout is a sidecar that is slow rather than absent, so either of those
        // landing on "refused" would page for a rollout.
        const incScrubWait = jest.spyOn(ImageScrubConsumerMetrics, 'incScrubWait')
        if ('reply' in setup) {
            replies = [setup.reply]
        }
        // Keyed on the request count, not queued: a reply left unconsumed by a socket the client already destroyed would be served to the retry.
        if ('neverReplyToFirst' in setup) {
            replyFor = () => (requests === 1 ? { status: 0, neverReply: true } : undefined)
        }
        const scrubClient = client(false, 'timeoutMs' in setup ? setup.timeoutMs : 1000, listenAgain)
        if ('listening' in setup) {
            await closeServer()
        }

        await expect(scrubClient.scrub(Buffer.from('image'))).resolves.toEqual(Buffer.from('scrubbed'))

        expect(incScrubWait.mock.calls).toEqual([[reason]])
        incScrubWait.mockRestore()
    })

    it.each([
        ['ECONNREFUSED', 'refused'],
        ['ECONNRESET', 'reset'],
        ['EPIPE', 'reset'],
        ['ECONNABORTED', 'reset'],
        ['ENOTFOUND', 'transport'],
        ['ETIMEDOUT', 'transport'],
        ['EMFILE', 'transport'],
        [undefined, 'transport'],
    ])('maps socket error code %s to "%s"', (code, reason) => {
        // Only a peer closing an accepted connection may read as "reset"; anything else, or no code, must stay on a label the alert selects.
        expect(socketWaitReason(Object.assign(new Error('boom'), code ? { code } : {}))).toBe(reason)
    })

    it('never dead-letters on saturation alone, however long the sidecar sheds', async () => {
        // The safety property of the whole dead-letter path. Under a backlog every image waits a
        // long time, so anything keyed on waiting or failure count alone would park the entire
        // stream, which is the mass loss the wait exists to prevent arriving through another door.
        // Nothing succeeds here, so nothing may be blamed on any one image.
        replies = Array.from({ length: 400 }, () => ({ status: 503, body: '' }))

        await expect(client(true).scrub(Buffer.from('image'))).resolves.toEqual(Buffer.from('scrubbed'))
    })

    it('holds an image through a sidecar failing everything, rather than parking it for the outage', async () => {
        // A sidecar 500ing on every image is the sidecar, not the content, and parking images for it
        // would quarantine the whole stream for a bug that has nothing to do with them. How long it
        // rides out is bounded by Kafka's poll lease rather than chosen: the batch cannot return
        // while this image is in flight, so a pod that waits past the lease is fenced and the work
        // simply moves to another pod to be repeated.
        replies = Array.from({ length: 8 }, () => ({ status: 500, body: '' }))

        await expect(client(true).scrub(Buffer.from('image'))).resolves.toEqual(Buffer.from('scrubbed'))
    })

    it('counts rejected request time when no peers can prove the sidecar works', async () => {
        // The success test cannot pass when nothing else is succeeding, and the images in a batch are
        // chosen by whoever produced them: fill one with content the sidecar rejects and no peer is
        // left to vouch for it. Without a way out, that stalls a partition shared by every team whose
        // records hash to it, which is worse than parking for an outage — parking keeps the bytes and
        // is loud, a stall keeps nothing moving and is silent.
        replies = Array.from({ length: 5000 }, () => ({ status: 500, body: '', durationMs: 15_000 }))

        let poisoned: unknown
        try {
            await client(true).scrub(Buffer.from('image'), undefined, 'ref-1')
        } catch (error) {
            poisoned = error
        }

        expect(poisoned).toBeInstanceOf(ScrubPoisoned)
        const detail = (poisoned as ScrubPoisoned).detail
        expect(detail.rejectedMs).toBeGreaterThanOrEqual(POISON_MAX_REJECTED_MS)
        expect(detail.rejectedMs).toBeLessThan(POISON_MAX_REJECTED_MS + 15_000)
        expect(detail.elapsedMs).toBe(detail.rejectedMs)
    })

    /**
     * One image the sidecar always 500s on, alongside a stream it scrubs fine, in flight together.
     *
     * Returned wrapped, because awaiting an async function that returns a promise adopts that
     * promise instead of handing it back, which would await the poison scrub here rather than in the
     * assertion.
     */
    const poisonAmongHealthy = async (c: ScrubClient): Promise<{ inFlight: Promise<Buffer | null> }> => {
        replyFor = (body) => (body.startsWith('poison') ? { status: 500, body: '' } : undefined)
        const inFlight = c.scrub(Buffer.from('poison'), undefined, 'ref-1')
        inFlight.catch(() => {}) // settled by the caller; this only stops an unhandled rejection
        // Only a handful, deliberately: a poison image late in a batch has just its few concurrent
        // neighbours to prove the sidecar works, because the batch cannot finish while it is in
        // flight and the pod cannot poll for more. A gate needing more than that never opens.
        for (let i = 0; i < 4; i++) {
            await c.scrub(Buffer.from(`healthy-${i}`))
        }
        return { inFlight }
    }

    it('dead-letters an image that keeps failing while the sidecar succeeds on others', async () => {
        // The case the dead-letter topic exists for: the sidecar demonstrably works, and this one
        // image still cannot get through, so it is the content and it must stop holding the head of
        // its partition.
        const { inFlight } = await poisonAmongHealthy(client(true))

        await expect(inFlight).rejects.toThrow(ScrubPoisoned)
    })

    it('keeps waiting on a poison image when there is nowhere to park it', async () => {
        // Without a dead-letter destination the only alternative to waiting is discarding, so a
        // misconfigured producer must cost throughput on one partition rather than the image.
        const { inFlight } = await poisonAmongHealthy(client(false))
        replyFor = undefined

        await expect(inFlight).resolves.toEqual(Buffer.from('scrubbed'))
    })

    it('stops waiting when the caller hangs up', async () => {
        // The one escape from the loop. Without it a shutdown would block on a sidecar that is never
        // going to answer, and the pod would have to be killed rather than draining.
        replies = Array.from({ length: 50 }, () => ({ status: 503, body: '' }))
        const controller = new AbortController()
        const scrubbing = client().scrub(Buffer.from('image'), controller.signal)
        controller.abort()

        await expect(scrubbing).rejects.toThrow(ScrubAborted)
    })
})