-
Notifications
You must be signed in to change notification settings - Fork 313
feat(web): binary file attachments for Ask #1375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b30cea1
feat(web): binary file attachments for Ask
whoisthey 4a25197
fix(worker): make attachment pruner byte-safe and reclaim committed o…
whoisthey 74791b3
fix(db): resequence chat attachments migration after main's latest
whoisthey 6b6b5ed
fix(worker): tombstone protocol for attachment byte reclamation
whoisthey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { AttachmentStatus, PrismaClient } from "@sourcebot/db"; | ||
| import { createLogger, env, getStorageBackend } from "@sourcebot/shared"; | ||
| import { setIntervalAsync } from "./utils.js"; | ||
|
|
||
| const BATCH_SIZE = 1_000; | ||
| const ONE_HOUR_MS = 60 * 60 * 1000; | ||
|
|
||
| const logger = createLogger('attachment-pruner'); | ||
|
|
||
| /** | ||
| * Periodically reclaims orphaned attachment blobs older than the configured TTL, | ||
| * along with their stored bytes, using the `DELETING` tombstone protocol: an | ||
| * orphan is first atomically flipped to `DELETING`, then its bytes are deleted, | ||
| * and only then is the row removed. Because the row (the only durable handle to | ||
| * the bytes) outlives the byte delete, a failed byte delete is always retryable. | ||
| * | ||
| * Each tick condemns two classes of orphan to `DELETING`, then reclaims all | ||
| * tombstones: | ||
| * | ||
| * 1. PENDING (uploaded-but-never-linked): produced when a user selects a file | ||
| * in the chat box but never sends the message. | ||
| * 2. COMMITTED with zero links: normally a committed blob is reclaimed inline | ||
| * by the chat-delete sweep in the web app, but if that sweep is interrupted | ||
| * (process crash / DB error / failed byte delete) the blob is left tombstoned | ||
| * or unlinked. This is the backstop for that case. | ||
| * | ||
| * @note Byte deletion goes through the shared `StorageBackend`, so the web app | ||
| * and this worker share one on-disk layout. | ||
| */ | ||
| export class AttachmentPruner { | ||
| private interval?: NodeJS.Timeout; | ||
| private readonly storage = getStorageBackend(); | ||
|
|
||
| constructor(private db: PrismaClient) {} | ||
|
|
||
| startScheduler() { | ||
| const ttlHours = env.SOURCEBOT_CHAT_ATTACHMENT_ORPHAN_TTL_HOURS; | ||
| if (ttlHours <= 0) { | ||
| logger.info('SOURCEBOT_CHAT_ATTACHMENT_ORPHAN_TTL_HOURS is 0, attachment orphan pruning is disabled.'); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug(`Attachment pruner started. Reclaiming orphaned attachments older than ${ttlHours} hours.`); | ||
|
|
||
| // Run immediately on startup, then every hour. The startup call isn't | ||
| // awaited, so log any failure here: this worker exits on | ||
| // unhandledRejection, and the recurring schedule will retry. | ||
| this.pruneOrphanedAttachments().catch((error) => { | ||
| logger.warn(`Initial attachment prune failed: ${error}`); | ||
| }); | ||
| this.interval = setIntervalAsync(() => this.pruneOrphanedAttachments(), ONE_HOUR_MS); | ||
| } | ||
|
|
||
| async dispose() { | ||
| if (this.interval) { | ||
| clearInterval(this.interval); | ||
| this.interval = undefined; | ||
| } | ||
| } | ||
|
|
||
| private async pruneOrphanedAttachments() { | ||
| const cutoff = new Date(Date.now() - env.SOURCEBOT_CHAT_ATTACHMENT_ORPHAN_TTL_HOURS * ONE_HOUR_MS); | ||
|
|
||
| // Condemn orphans by flipping them to the DELETING tombstone. Each claim | ||
| // is atomic, so a PENDING blob committed by a concurrent send (its commit | ||
| // matches only PENDING rows) or a zero-link blob re-linked by a concurrent | ||
| // duplicate-chat loses the claim and is left intact. | ||
| // | ||
| // PENDING orphans: uploaded but the message was never sent. | ||
| const pendingClaimed = await this.db.attachment.updateMany({ | ||
| where: { | ||
| status: AttachmentStatus.PENDING, | ||
| createdAt: { lt: cutoff }, | ||
| }, | ||
| data: { status: AttachmentStatus.DELETING }, | ||
| }); | ||
|
|
||
| // COMMITTED orphans: blobs left with zero links by an interrupted | ||
| // chat-delete sweep in the web app. | ||
| const committedClaimed = await this.db.attachment.updateMany({ | ||
| where: { | ||
| status: AttachmentStatus.COMMITTED, | ||
| createdAt: { lt: cutoff }, | ||
| chats: { none: {} }, | ||
| }, | ||
| data: { status: AttachmentStatus.DELETING }, | ||
| }); | ||
|
|
||
| // Reclaim every tombstone: delete bytes, then the row. This also picks up | ||
| // tombstones left behind by the web app's inline reclaim (or a crashed | ||
| // earlier tick) whose byte delete failed. | ||
| const reclaimed = await this.reclaimTombstonedAttachments(); | ||
|
|
||
| if (pendingClaimed.count > 0 || committedClaimed.count > 0 || reclaimed > 0) { | ||
| logger.debug( | ||
| `Attachment prune: condemned ${pendingClaimed.count} PENDING + ` + | ||
| `${committedClaimed.count} COMMITTED orphan(s), reclaimed ${reclaimed} tombstone(s).`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the bytes for every `DELETING` tombstone, then removes the row. | ||
| * The row (the only durable handle to the bytes) is removed only after its | ||
| * bytes are confirmed gone, so a failed byte delete leaves the tombstone in | ||
| * place to be retried on the next tick — bytes can never be orphaned by a | ||
| * transient storage error. Rows whose byte delete fails this run are | ||
| * excluded from subsequent batches so a persistent failure can't spin the | ||
| * loop. | ||
| * | ||
| * @returns the number of tombstones fully reclaimed (bytes + row). | ||
| */ | ||
| private async reclaimTombstonedAttachments(): Promise<number> { | ||
| let totalReclaimed = 0; | ||
| const failedIds: string[] = []; | ||
|
|
||
| while (true) { | ||
| const batch = await this.db.attachment.findMany({ | ||
| where: { status: AttachmentStatus.DELETING, id: { notIn: failedIds } }, | ||
| select: { id: true, storageKey: true }, | ||
| take: BATCH_SIZE, | ||
| }); | ||
|
|
||
| if (batch.length === 0) { | ||
| break; | ||
| } | ||
|
|
||
| const settled = await Promise.allSettled( | ||
| batch.map((attachment) => this.storage.delete(attachment.storageKey))); | ||
|
|
||
| const reclaimedIds: string[] = []; | ||
| batch.forEach((attachment, index) => { | ||
| const outcome = settled[index]; | ||
| if (outcome.status === 'fulfilled') { | ||
| reclaimedIds.push(attachment.id); | ||
| } else { | ||
| logger.warn(`Failed to delete bytes for tombstoned attachment ${attachment.id}, will retry next tick: ${outcome.reason}`); | ||
| failedIds.push(attachment.id); | ||
| } | ||
| }); | ||
|
|
||
| if (reclaimedIds.length > 0) { | ||
| const result = await this.db.attachment.deleteMany({ | ||
| where: { id: { in: reclaimedIds }, status: AttachmentStatus.DELETING }, | ||
| }); | ||
| totalReclaimed += result.count; | ||
| } | ||
|
|
||
| if (batch.length < BATCH_SIZE) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return totalReclaimed; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/db/prisma/migrations/20260629200000_add_chat_attachments/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| -- CreateEnum | ||
| CREATE TYPE "AttachmentStatus" AS ENUM ('PENDING', 'COMMITTED'); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "Attachment" ( | ||
| "id" TEXT NOT NULL, | ||
| "orgId" INTEGER NOT NULL, | ||
| "storageKey" TEXT NOT NULL, | ||
| "filename" TEXT NOT NULL, | ||
| "mediaType" TEXT NOT NULL, | ||
| "sizeBytes" INTEGER NOT NULL, | ||
| "checksum" TEXT NOT NULL, | ||
| "uploadedById" TEXT, | ||
| "status" "AttachmentStatus" NOT NULL DEFAULT 'PENDING', | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "Attachment_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "ChatAttachment" ( | ||
| "id" TEXT NOT NULL, | ||
| "chatId" TEXT NOT NULL, | ||
| "attachmentId" TEXT NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "ChatAttachment_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "Attachment_status_createdAt_idx" ON "Attachment"("status", "createdAt"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "ChatAttachment_attachmentId_idx" ON "ChatAttachment"("attachmentId"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "ChatAttachment_chatId_attachmentId_key" ON "ChatAttachment"("chatId", "attachmentId"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Attachment" ADD CONSTRAINT "Attachment_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "Org"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Attachment" ADD CONSTRAINT "Attachment_uploadedById_fkey" FOREIGN KEY ("uploadedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "ChatAttachment" ADD CONSTRAINT "ChatAttachment_chatId_fkey" FOREIGN KEY ("chatId") REFERENCES "Chat"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "ChatAttachment" ADD CONSTRAINT "ChatAttachment_attachmentId_fkey" FOREIGN KEY ("attachmentId") REFERENCES "Attachment"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
2 changes: 2 additions & 0 deletions
2
packages/db/prisma/migrations/20260629210000_add_attachment_deleting_status/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- AlterEnum | ||
| ALTER TYPE "AttachmentStatus" ADD VALUE 'DELETING'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.