Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

11.3 KB
Raw Blame
import crypto from "node:crypto";

import { Knex } from "knex";

import { TDbClient } from "@app/db";
import { TableName, TEventOutbox } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { chunkArray } from "@app/lib/fn";

import { computeBackoffMs, EventOutboxStatus, TOutboxFlushKey } from "./event-outbox-types";

export type TEventOutboxDALFactory = ReturnType<typeof eventOutboxDALFactory>;

// A claim always carries an eventId, even on a row written without one (see claimBatch).
export type TClaimedOutboxRow = TEventOutbox & { eventId: string };

export type TOutboxClaim = {
  lockToken: string;
  rows: TClaimedOutboxRow[];
};

export type TOutboxInsertRow = {
  consumer: string;
  eventId: string;
  eventType: string;
  payload: unknown;
  occurredAt: Date;
};

// Postgres caps a statement at 65535 bind parameters and a row takes five, so a bulk write of ~13k+
// secrets would otherwise fail.
const INSERT_BATCH_SIZE = 1000;

export const eventOutboxDALFactory = (db: TDbClient) => {
  const insertEvents = async (rows: TOutboxInsertRow[], tx?: Knex): Promise<void> => {
    if (rows.length === 0) return;
    try {
      for (const chunk of chunkArray(rows, INSERT_BATCH_SIZE)) {
        // eslint-disable-next-line no-await-in-loop -- one statement at a time on the caller's connection
        await (tx || db)(TableName.EventOutbox)
          .insert(
            chunk.map((row) => ({
              ...row,
              payload: JSON.stringify(row.payload)
            }))
          )
          .onConflict(["consumer", "eventId"])
          .ignore();
      }
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: insertEvents" });
    }
  };

  const findDueFlushKeys = async (consumers: string[]): Promise<TOutboxFlushKey[]> => {
    if (consumers.length === 0) return [];
    try {
      const rows = await db
        .replicaNode()(TableName.EventOutbox)
        .whereIn("consumer", consumers)
        .whereIn("status", [EventOutboxStatus.Pending, EventOutboxStatus.Retry])
        .andWhere("nextRetryAt", "<=", db.fn.now())
        .groupBy("consumer")
        .select<TOutboxFlushKey[]>("consumer");

      return rows;
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: findDueFlushKeys" });
    }
  };

  // One statement on purpose so the row locks only live as long as the UPDATE. The fresh lockToken is
  // how extendClaims and commitResults tell this claim apart from a later one on the same rows. Old pods
  // write rows without an eventId during a rolling deploy, so the claim fills it in; consumers rely on
  // every event having one.
  const claimBatch = async (key: TOutboxFlushKey, limit: number): Promise<TOutboxClaim> => {
    const lockToken = crypto.randomUUID();
    try {
      const claimed = await db(TableName.EventOutbox)
        .whereIn("id", (qb) => {
          void qb
            .select("id")
            .from(TableName.EventOutbox)
            .where("consumer", key.consumer)
            .whereIn("status", [EventOutboxStatus.Pending, EventOutboxStatus.Retry])
            .andWhere("nextRetryAt", "<=", db.fn.now())
            .orderBy("id", "asc")
            .limit(limit)
            .forUpdate()
            .skipLocked();
        })
        .update({
          status: EventOutboxStatus.Processing,
          lockedAt: db.fn.now(),
          lockToken,
          eventId: db.raw('COALESCE("eventId", gen_random_uuid())')
        })
        .returning("*");

      // RETURNING order is arbitrary, and handle() is promised id order.
      const rows = (claimed as unknown as TClaimedOutboxRow[]).sort((a, b) => (BigInt(a.id) < BigInt(b.id) ? -1 : 1));
      return { lockToken, rows };
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: claimBatch" });
    }
  };

  // Keeps lockedAt fresh so the sweeper doesn't hand a slow batch to another worker mid-delivery.
  // Scoped to this claim's token so a late heartbeat can't refresh a lock someone else now holds.
  const extendClaims = async (ids: string[], lockToken: string): Promise<void> => {
    if (ids.length === 0) return;
    try {
      await db(TableName.EventOutbox)
        .whereIn("id", ids)
        .where("status", EventOutboxStatus.Processing)
        .where("lockToken", lockToken)
        .update({ lockedAt: db.fn.now() });
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: extendClaims" });
    }
  };

  // Returns the settled count. It comes up short when the sweeper recycled the claim mid-delivery and
  // another worker now owns the rows.
  const commitResults = async (input: {
    lockToken: string;
    delivered: { ids: string[] }[];
    retriable: { ids: string[]; nextRetryDelayMs: number; error?: string }[];
    failed: { ids: string[]; error?: string }[];
  }): Promise<number> => {
    const total =
      input.delivered.reduce((n, g) => n + g.ids.length, 0) +
      input.retriable.reduce((n, g) => n + g.ids.length, 0) +
      input.failed.reduce((n, g) => n + g.ids.length, 0);
    if (total === 0) return 0;

    try {
      return await db.transaction(async (tx) => {
        let settled = 0;
        const settle = async (ids: string[], values: Record<string, unknown>) => {
          settled += await tx(TableName.EventOutbox)
            .whereIn("id", ids)
            .where("status", EventOutboxStatus.Processing)
            .where("lockToken", input.lockToken)
            .update({ lockedAt: null, lockToken: null, ...values });
        };

        for (const group of input.delivered) {
          // eslint-disable-next-line no-await-in-loop -- one shared tx connection; writes are serial
          await settle(group.ids, { status: EventOutboxStatus.Delivered, lastError: null });
        }

        for (const group of input.retriable) {
          // eslint-disable-next-line no-await-in-loop -- see above
          await settle(group.ids, {
            status: EventOutboxStatus.Retry,
            attempts: db.raw('"attempts" + 1'),
            nextRetryAt: db.raw(`NOW() + (? || ' milliseconds')::INTERVAL`, [group.nextRetryDelayMs]),
            lastError: group.error ?? null
          });
        }

        for (const group of input.failed) {
          // eslint-disable-next-line no-await-in-loop -- see above
          await settle(group.ids, {
            status: EventOutboxStatus.Failed,
            attempts: db.raw('"attempts" + 1'),
            lastError: group.error ?? null
          });
        }

        return settled;
      });
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: commitResults" });
    }
  };

  const recoverStaleClaims = async (input: {
    thresholdMs: number;
    maxAttempts: number;
    limit: number;
  }): Promise<{ retried: number; failed: { consumer: string; count: number }[] }> => {
    try {
      return await db.transaction(async (tx) => {
        const staleRows = await tx(TableName.EventOutbox)
          .where("status", EventOutboxStatus.Processing)
          .andWhereRaw(`"lockedAt" < NOW() - (? || ' milliseconds')::INTERVAL`, [input.thresholdMs])
          .orderBy("lockedAt", "asc")
          .limit(input.limit)
          .forUpdate()
          .skipLocked()
          .select<{ id: string; consumer: string; attempts: number }[]>("id", "consumer", "attempts");

        if (staleRows.length === 0) return { retried: 0, failed: [] };

        const exhaustedRows = staleRows.filter((row) => row.attempts + 1 >= input.maxAttempts);
        const exhausted = exhaustedRows.map((row) => row.id);
        const retriable = staleRows.filter((row) => row.attempts + 1 < input.maxAttempts);

        const retriableByAttempt = new Map<number, string[]>();
        for (const row of retriable) {
          const ids = retriableByAttempt.get(row.attempts) ?? [];
          ids.push(row.id);
          retriableByAttempt.set(row.attempts, ids);
        }
        for (const [attempts, ids] of retriableByAttempt) {
          // eslint-disable-next-line no-await-in-loop -- one shared tx connection; writes are serial
          await tx(TableName.EventOutbox)
            .whereIn("id", ids)
            .update({
              status: EventOutboxStatus.Retry,
              attempts: db.raw('"attempts" + 1'),
              nextRetryAt: db.raw(`NOW() + (? || ' milliseconds')::INTERVAL`, [computeBackoffMs(attempts + 1)]),
              lockedAt: null,
              lockToken: null,
              lastError: "Worker did not report a result before the claim went stale"
            });
        }

        if (exhausted.length > 0) {
          await tx(TableName.EventOutbox)
            .whereIn("id", exhausted)
            .update({
              status: EventOutboxStatus.Failed,
              attempts: db.raw('"attempts" + 1'),
              lockedAt: null,
              lockToken: null,
              lastError: "Worker did not report a result before the claim went stale"
            });
        }

        const failedByConsumer = new Map<string, number>();
        for (const row of exhaustedRows) {
          failedByConsumer.set(row.consumer, (failedByConsumer.get(row.consumer) ?? 0) + 1);
        }

        return {
          retried: retriable.length,
          failed: [...failedByConsumer].map(([consumer, count]) => ({ consumer, count }))
        };
      });
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: recoverStaleClaims" });
    }
  };

  const findOldestPendingAgeSeconds = async (): Promise<{ consumer: string; ageSeconds: number }[]> => {
    try {
      const rows = await db
        .replicaNode()(TableName.EventOutbox)
        .whereIn("status", [EventOutboxStatus.Pending, EventOutboxStatus.Retry, EventOutboxStatus.Processing])
        .groupBy("consumer")
        .select<{ consumer: string; ageSeconds: string }[]>(
          "consumer",
          db.raw('EXTRACT(EPOCH FROM (NOW() - MIN("occurredAt")))::float8 as "ageSeconds"')
        );

      return rows.map((row) => ({ consumer: row.consumer, ageSeconds: Number(row.ageSeconds) }));
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: findOldestPendingAgeSeconds" });
    }
  };

  const deleteTerminalOlderThan = async (input: {
    deliveredBefore: Date;
    failedBefore: Date;
    batchSize: number;
  }): Promise<number> => {
    try {
      return await db(TableName.EventOutbox)
        .whereIn(
          "id",
          (qb) =>
            void qb
              .select("id")
              .from(TableName.EventOutbox)
              .where(
                (builder) =>
                  void builder
                    .where(
                      (delivered) =>
                        void delivered
                          .where("status", EventOutboxStatus.Delivered)
                          .andWhere("updatedAt", "<", input.deliveredBefore)
                    )
                    .orWhere(
                      (failed) =>
                        void failed
                          .where("status", EventOutboxStatus.Failed)
                          .andWhere("updatedAt", "<", input.failedBefore)
                    )
              )
              .limit(input.batchSize)
        )
        .del();
    } catch (error) {
      throw new DatabaseError({ error, name: "EventOutbox: deleteTerminalOlderThan" });
    }
  };

  return {
    insertEvents,
    findDueFlushKeys,
    claimBatch,
    extendClaims,
    commitResults,
    recoverStaleClaims,
    findOldestPendingAgeSeconds,
    deleteTerminalOlderThan
  };
};