-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathdeletion_queue.c
More file actions
398 lines (305 loc) · 10.4 KB
/
deletion_queue.c
File metadata and controls
398 lines (305 loc) · 10.4 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Copyright 2025 Snowflake Inc.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Functions for cleaning up orphaned files.
*/
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pg_lake/cleanup/deletion_queue.h"
#include "pg_lake/extensions/pg_lake_table.h"
#include "pg_lake/pgduck/remote_storage.h"
#include "pg_lake/util/array_utils.h"
#include "pg_extension_base/spi_helpers.h"
#include "pg_lake/util/string_utils.h"
#include "datatype/timestamp.h"
#include "storage/procarray.h"
#define DELETION_QUEUE_TABLE "lake_engine.deletion_queue"
/* managed by GUC */
int OrphanedFileRetentionPeriod = 60 * 60 * 24 * 10; /* 10 days */
/* managed by GUC, not exposed to the users */
int VacuumFileRemoveMaxRetries = 145;
/*
* DeletionQueueEntry represents a deletion entry from the
* deletion queue.
*/
typedef struct DeletionQueueEntry
{
char *path;
TimestampTz orphanedAt;
int retryCount;
bool isPrefix;
} DeletionQueueEntry;
static void RemoveDeletionQueuePathsFromCatalog(List *filePaths);
static void IncrementDeletionQueueRetryCount(List *failedRemovalPaths);
PG_FUNCTION_INFO_V1(flush_deletion_queue);
/*
* flush_deletion_queue removes all eligible files from
* the deletion queue.
*/
Datum
flush_deletion_queue(PG_FUNCTION_ARGS)
{
Oid relationId = PG_GETARG_OID(0);
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
/* remove all */
bool isFull = true;
bool isVerbose = false;
List *deletionQueueRecords = GetDeletionQueueRecords(relationId, isFull);
RemoveDeletionQueueRecords(deletionQueueRecords, isVerbose);
ListCell *fileCell = NULL;
foreach(fileCell, deletionQueueRecords)
{
DeletionQueueEntry *deletedFile = lfirst(fileCell);
Datum values[] = {CStringGetTextDatum(deletedFile->path)};
bool nulls[] = {false};
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
}
PG_RETURN_VOID();
}
/*
* RemoveDeletionQueueRecords removes all files that are no longer referenced .
* Returns true if at least one file was successfully removed.
*/
bool
RemoveDeletionQueueRecords(List *deletionQueueRecords, bool isVerbose)
{
List *deletedFilePathList = NIL;
List *failedFilePathList = NIL;
ListCell *cleanupRecordCell = NULL;
foreach(cleanupRecordCell, deletionQueueRecords)
{
DeletionQueueEntry *entry = lfirst(cleanupRecordCell);
ereport(isVerbose ? INFO : LOG,
(errmsg("deleting expired %s %s",
entry->isPrefix ? "prefix" : "file",
entry->path)));
bool success = false;
if (entry->isPrefix)
{
/* ok, let's try to fetch and delete all of its tree */
success = DeleteRemotePrefix(entry->path);
}
else
{
/* remove the file */
success = DeleteRemoteFile(entry->path);
}
if (success)
{
/* remove the record */
deletedFilePathList = lappend(deletedFilePathList, entry->path);
}
else
{
/* add to failed list */
failedFilePathList = lappend(failedFilePathList, entry->path);
}
}
if (list_length(deletedFilePathList) > 0)
{
RemoveDeletionQueuePathsFromCatalog(deletedFilePathList);
}
if (list_length(failedFilePathList) > 0)
{
IncrementDeletionQueueRetryCount(failedFilePathList);
}
/* if we can remove at least 1 file, continue removing */
return list_length(deletedFilePathList) > 0;
}
/*
* RemoveDeletionQueuePathsFromCatalog removes the given paths from the
* deletion queue catalog.
*/
static void
RemoveDeletionQueuePathsFromCatalog(List *filePaths)
{
/* switch to schema owner, we assume callers checked permissions */
Oid savedUserId = InvalidOid;
int savedSecurityContext = 0;
GetUserIdAndSecContext(&savedUserId, &savedSecurityContext);
SetUserIdAndSecContext(ExtensionOwnerId(PgLakeTable),
SECURITY_LOCAL_USERID_CHANGE);
ArrayType *failedRemovalPathsArray = StringListToArray(filePaths);
char *query =
"DELETE FROM " DELETION_QUEUE_TABLE " "
"WHERE path OPERATOR(pg_catalog.=) ANY($1)";
DECLARE_SPI_ARGS(1);
SPI_ARG_VALUE(1, TEXTARRAYOID, failedRemovalPathsArray, false);
SPI_START();
bool readOnly = false;
SPI_EXECUTE(query, readOnly);
SPI_END();
SetUserIdAndSecContext(savedUserId, savedSecurityContext);
}
/*
* IncrementDeletionQueueRetryCount increments the retry count
* for the given paths in the deletion queue.
*/
static void
IncrementDeletionQueueRetryCount(List *failedRemovalPaths)
{
/* switch to schema owner, we assume callers checked permissions */
Oid savedUserId = InvalidOid;
int savedSecurityContext = 0;
GetUserIdAndSecContext(&savedUserId, &savedSecurityContext);
SetUserIdAndSecContext(ExtensionOwnerId(PgLakeTable),
SECURITY_LOCAL_USERID_CHANGE);
ArrayType *failedRemovalPathsArray = StringListToArray(failedRemovalPaths);
bool readOnly = false;
char *updateQuery =
"UPDATE " DELETION_QUEUE_TABLE " "
"SET retry_count = retry_count + 1 "
"WHERE path OPERATOR(pg_catalog.=) ANY($1) ";
DECLARE_SPI_ARGS(1);
SPI_ARG_VALUE(1, TEXTARRAYOID, failedRemovalPathsArray, false);
SPI_START();
SPI_EXECUTE(updateQuery, readOnly);
SPI_END();
SetUserIdAndSecContext(savedUserId, savedSecurityContext);
}
/*
* GetDeletionQueueRecords gets a list of paths that are eligible for
* deletion, meaning delete_after condition is met on DELETION_QUEUE_TABLE.
*/
List *
GetDeletionQueueRecords(Oid relationId, bool isFull)
{
/* switch to schema owner, we assume callers checked permissions */
Oid savedUserId = InvalidOid;
int savedSecurityContext = 0;
GetUserIdAndSecContext(&savedUserId, &savedSecurityContext);
SetUserIdAndSecContext(ExtensionOwnerId(PgLakeTable),
SECURITY_LOCAL_USERID_CHANGE);
MemoryContext callerContext = CurrentMemoryContext;
List *result = NIL;
StringInfo query = makeStringInfo();
appendStringInfo(query,
"WITH del AS (");
if (OidIsValid(relationId))
{
appendStringInfo(query,
" SELECT ctid, path, orphaned_at, retry_count, is_prefix "
" FROM " DELETION_QUEUE_TABLE " "
" WHERE (orphaned_at IS NULL or pg_catalog.now() OPERATOR(pg_catalog.>=) (orphaned_at OPERATOR(pg_catalog.+) INTERVAL '%d seconds')) AND "
" table_name OPERATOR(pg_catalog.=) %d AND retry_count OPERATOR(pg_catalog.<=) %d FOR UPDATE",
OrphanedFileRetentionPeriod, relationId, VacuumFileRemoveMaxRetries);
}
else
{
/*
* This is for dropped tables, so join with pg_class to find all
* entries in the DELETION_QUEUE_TABLE that are not associated with
* any existing table.
*/
appendStringInfo(query,
" SELECT del.ctid, del.path, del.orphaned_at, del.retry_count, del.is_prefix "
" FROM " DELETION_QUEUE_TABLE " del "
" LEFT JOIN pg_catalog.pg_class c ON c.oid OPERATOR(pg_catalog.=) del.table_name "
" WHERE (del.orphaned_at IS NULL or pg_catalog.now() OPERATOR(pg_catalog.>=) (del.orphaned_at OPERATOR(pg_catalog.+) INTERVAL '%d seconds')) AND "
" c.oid IS NULL AND retry_count OPERATOR(pg_catalog.<=) %d FOR UPDATE OF del",
OrphanedFileRetentionPeriod, VacuumFileRemoveMaxRetries);
}
if (!isFull)
{
appendStringInfo(query,
" LIMIT " PG_LAKE_TOSTRING(PER_LOOP_FILE_CLEANUP_LIMIT));
}
appendStringInfo(query,
") "
"SELECT path, orphaned_at, retry_count, is_prefix FROM del");
SPI_START();
bool readOnly = false;
SPI_execute(query->data, readOnly, 0);
for (int rowIndex = 0; rowIndex < SPI_processed; rowIndex++)
{
bool isNull;
MemoryContext spiContext = MemoryContextSwitchTo(callerContext);
DeletionQueueEntry *entry = palloc0(sizeof(DeletionQueueEntry));
entry->path = GET_SPI_VALUE(TEXTOID, rowIndex, 1, &isNull);
entry->orphanedAt = GET_SPI_VALUE(TIMESTAMPTZOID, rowIndex, 2, &isNull);
entry->retryCount = GET_SPI_VALUE(INT4OID, rowIndex, 3, &isNull);
entry->isPrefix = GET_SPI_VALUE(BOOLOID, rowIndex, 4, &isNull);
result = lappend(result, entry);
MemoryContextSwitchTo(spiContext);
}
SPI_END();
SetUserIdAndSecContext(savedUserId, savedSecurityContext);
return result;
}
/*
* InsertPrefixDeletionRecord adds a prefix into the deletion queue for
* later removal. When the prefix is removed, all files under the prefix
* will be removed.
*/
void
InsertPrefixDeletionRecord(char *path, TimestampTz orphanedAt)
{
InsertDeletionQueueRecordExtended(path, InvalidOid, orphanedAt, true);
}
/*
* InsertDeletionQueueRecord adds a path into the deletion queue for
* later removal.
*/
void
InsertDeletionQueueRecord(char *path, Oid relationId, TimestampTz orphanedAt)
{
InsertDeletionQueueRecordExtended(path, relationId, orphanedAt, false);
}
/*
* InsertDeletionQueueRecordExtended is the internal function to insert
* a record into the deletion queue.
*/
void
InsertDeletionQueueRecordExtended(char *path, Oid relationId, TimestampTz orphanedAt,
bool isPrefix)
{
/* switch to schema owner, we assume callers checked permissions */
Oid savedUserId = InvalidOid;
int savedSecurityContext = 0;
GetUserIdAndSecContext(&savedUserId, &savedSecurityContext);
SetUserIdAndSecContext(ExtensionOwnerId(PgLakeTable),
SECURITY_LOCAL_USERID_CHANGE);
char *query =
"insert into " DELETION_QUEUE_TABLE " "
"(path, table_name, orphaned_at, is_prefix) "
"values ($1,$2,$3,$4)";
DECLARE_SPI_ARGS(4);
SPI_ARG_VALUE(1, TEXTOID, path, false);
SPI_ARG_VALUE(2, OIDOID, relationId, false);
SPI_ARG_VALUE(3, TIMESTAMPTZOID, orphanedAt, orphanedAt == 0);
SPI_ARG_VALUE(4, BOOLOID, isPrefix, false);
SPI_START();
bool readOnly = false;
SPI_EXECUTE(query, readOnly);
SPI_END();
SetUserIdAndSecContext(savedUserId, savedSecurityContext);
}
/*
* DeleteDeletionQueueRecordsByPath removes the given paths from the
* deletion queue table without deleting the actual remote files.
* Used to undo premature deletion queue entries when a REST catalog
* commit fails.
*/
void
DeleteDeletionQueueRecordsByPath(List *paths)
{
if (paths == NIL)
return;
RemoveDeletionQueuePathsFromCatalog(paths);
}