-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathattachmentPruneWorkload.ts
More file actions
178 lines (161 loc) · 5.45 KB
/
Copy pathattachmentPruneWorkload.ts
File metadata and controls
178 lines (161 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { AttachmentStatus, type PrismaClient } from "@sourcebot/db";
import {
ATTACHMENT_PRUNE_QUEUE,
createLogger,
getStorageBackend,
JOB_PRIORITIES,
type StorageBackend,
} from "@sourcebot/shared";
import type { Workload } from "./types.js";
const BATCH_SIZE = 1_000;
const ONE_HOUR_MS = 60 * 60 * 1000;
const logger = createLogger("attachment-prune-workload");
interface Props {
db: PrismaClient;
ttlHours: number;
storage?: StorageBackend;
}
/**
* Reclaims orphaned attachment blobs 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 run 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 this is the backstop for an
* interrupted sweep.
*
* @note Byte deletion goes through the shared `StorageBackend`, so the web app
* and this worker share one on-disk layout.
*/
export const createAttachmentPruneWorkload = ({
db,
ttlHours,
storage = getStorageBackend(),
}: Props): Workload<"attachment-prune"> => ({
queueSpec: ATTACHMENT_PRUNE_QUEUE,
concurrency: 1,
...(ttlHours > 0
? {
schedule: {
interval: "1h",
data: {},
options: { priority: JOB_PRIORITIES.SCHEDULED },
},
}
: {}),
process: async () => {
if (ttlHours <= 0) {
logger.debug("Attachment orphan pruning is disabled.");
return {
pendingClaimed: 0,
committedClaimed: 0,
reclaimed: 0,
};
}
const cutoff = new Date(Date.now() - ttlHours * ONE_HOUR_MS);
// Each claim is atomic, so a PENDING blob committed by a concurrent
// send or a zero-link blob re-linked by a concurrent duplicate-chat
// loses the claim and is left intact.
const pendingClaimed = await db.attachment.updateMany({
where: {
status: AttachmentStatus.PENDING,
createdAt: { lt: cutoff },
},
data: { status: AttachmentStatus.DELETING },
});
const committedClaimed = await db.attachment.updateMany({
where: {
status: AttachmentStatus.COMMITTED,
createdAt: { lt: cutoff },
chats: { none: {} },
},
data: { status: AttachmentStatus.DELETING },
});
const reclaimed = await reclaimTombstonedAttachments({
db,
storage,
warn: (message) => logger.warn(message),
});
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).`,
);
}
return {
pendingClaimed: pendingClaimed.count,
committedClaimed: committedClaimed.count,
reclaimed,
};
},
});
/**
* Deletes the bytes for every `DELETING` tombstone, then removes the row. A
* failed byte delete leaves the tombstone in place for the next scheduled run.
* Failed IDs are excluded from later batches in this run so a persistent
* storage failure cannot spin the loop.
*/
const reclaimTombstonedAttachments = async ({
db,
storage,
warn,
}: {
db: PrismaClient;
storage: StorageBackend;
warn: (message: string) => void;
}): Promise<number> => {
let totalReclaimed = 0;
const failedIds: string[] = [];
while (true) {
const batch = await 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) => storage.delete(attachment.storageKey)),
);
const reclaimedIds: string[] = [];
batch.forEach((attachment, index) => {
const outcome = settled[index];
if (outcome.status === "fulfilled") {
reclaimedIds.push(attachment.id);
} else {
warn(
`Failed to delete bytes for tombstoned attachment ${attachment.id}, will retry next run: ${outcome.reason}`,
);
failedIds.push(attachment.id);
}
});
if (reclaimedIds.length > 0) {
const result = await db.attachment.deleteMany({
where: {
id: { in: reclaimedIds },
status: AttachmentStatus.DELETING,
},
});
totalReclaimed += result.count;
}
if (batch.length < BATCH_SIZE) {
break;
}
}
return totalReclaimed;
};