-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathcache.c
364 lines (312 loc) · 8.24 KB
/
cache.c
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
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <access/xact.h>
#include <storage/ipc.h>
#include "compat/compat.h"
#include "cache.h"
/* List of pinned caches. A cache occurs once in this list for every pin
* taken */
static List *pinned_caches = NIL;
static MemoryContext pinned_caches_mctx = NULL;
typedef struct CachePin
{
Cache *cache;
SubTransactionId subtxnid;
} CachePin;
static void
cache_reset_pinned_caches(void)
{
if (NULL != pinned_caches_mctx)
MemoryContextDelete(pinned_caches_mctx);
pinned_caches_mctx =
AllocSetContextCreate(CacheMemoryContext, "Cache pins", ALLOCSET_DEFAULT_SIZES);
pinned_caches = NIL;
}
void
ts_cache_init(Cache *cache)
{
if (cache->htab != NULL)
{
elog(ERROR, "cache %s is already initialized", cache->name);
return;
}
/*
* The cache object should have been created in its own context so that
* cache_destroy can just delete the context to free everything.
*/
Assert(GetMemoryChunkContext(cache) == ts_cache_memory_ctx(cache));
/*
* We always want to be explicit about the memory context our hash table
* ends up in to ensure it's not accidentally put in TopMemoryContext.
*/
Assert(cache->flags & HASH_CONTEXT);
cache->htab = hash_create(cache->name, cache->numelements, &cache->hctl, cache->flags);
cache->refcount = 1;
cache->handle_txn_callbacks = true;
cache->release_on_commit = true;
}
static void
cache_destroy(Cache *cache)
{
if (cache->refcount > 0)
{
/* will be destroyed later */
return;
}
if (cache->pre_destroy_hook != NULL)
cache->pre_destroy_hook(cache);
hash_destroy(cache->htab);
MemoryContextDelete(cache->hctl.hcxt);
}
void
ts_cache_invalidate(Cache *cache)
{
if (cache == NULL)
return;
cache->refcount--;
cache_destroy(cache);
}
/*
* Pinning is needed if any items returned by the cache may need to survive
* invalidation events (i.e. AcceptInvalidationMessages() may be called).
*
* Invalidation messages may be processed on any internal function that takes a
* lock (e.g. table_open).
*
* Each call to cache_pin MUST BE paired with a call to cache_release.
*
*/
Cache *
ts_cache_pin(Cache *cache)
{
MemoryContext old = MemoryContextSwitchTo(pinned_caches_mctx);
CachePin *cp = palloc(sizeof(CachePin));
cp->cache = cache;
cp->subtxnid = GetCurrentSubTransactionId();
if (cache->handle_txn_callbacks)
pinned_caches = lappend(pinned_caches, cp);
MemoryContextSwitchTo(old);
cache->refcount++;
return cache;
}
static void
remove_pin(Cache *cache, SubTransactionId subtxnid)
{
ListCell *lc;
foreach (lc, pinned_caches)
{
CachePin *cp = lfirst(lc);
if (cp->cache == cache && cp->subtxnid == subtxnid)
{
pinned_caches = list_delete_cell(pinned_caches, lc);
pfree(cp);
return;
}
}
/* should never reach here: there should always be a pin to remove */
Assert(false);
}
static int
cache_release_subtxn(Cache *cache, SubTransactionId subtxnid)
{
int refcount = cache->refcount - 1;
Assert(cache->refcount > 0);
cache->refcount--;
if (cache->handle_txn_callbacks)
remove_pin(cache, subtxnid);
cache_destroy(cache);
return refcount;
}
int
ts_cache_release(Cache *cache)
{
return cache_release_subtxn(cache, GetCurrentSubTransactionId());
}
MemoryContext
ts_cache_memory_ctx(Cache *cache)
{
return cache->hctl.hcxt;
}
void *
ts_cache_fetch(Cache *cache, CacheQuery *query)
{
HASHACTION action;
bool found;
if (cache->htab == NULL || cache->valid_result == NULL)
elog(ERROR, "cache \"%s\" is not initialized", cache->name);
if (query->flags & CACHE_FLAG_NOCREATE)
action = HASH_FIND;
else if (cache->create_entry == NULL)
elog(ERROR, "cache \"%s\" does not support creating new entries", cache->name);
else
action = HASH_ENTER;
query->result = hash_search(cache->htab, cache->get_key(query), action, &found);
if (found)
{
cache->stats.hits++;
if (cache->update_entry != NULL)
query->result = cache->update_entry(cache, query);
}
else
{
cache->stats.misses++;
if (action == HASH_ENTER)
{
cache->stats.numelements++;
query->result = cache->create_entry(cache, query);
}
}
if (!(query->flags & CACHE_FLAG_MISSING_OK) && !cache->valid_result(query->result))
{
if (cache->missing_error != NULL)
cache->missing_error(cache, query);
else
elog(ERROR, "failed to find entry in cache \"%s\"", cache->name);
}
return query->result;
}
static void
release_all_pinned_caches()
{
ListCell *lc;
/*
* release once for every occurrence of a cache in the pinned caches list.
* On abort, release irrespective of cache->release_on_commit.
*/
foreach (lc, pinned_caches)
{
CachePin *cp = lfirst(lc);
cp->cache->refcount--;
cache_destroy(cp->cache);
}
cache_reset_pinned_caches();
}
static void
release_subtxn_pinned_caches(SubTransactionId subtxnid, bool abort)
{
ListCell *lc;
/*
* Need a copy because cache_release will modify pinned_caches.
*
* This needs to be allocated in pinned cache memory context.
* Otherwise leaks ensue if CurTransactionContext (which is the
* CurrentMemoryContext) gets used!
*/
MemoryContext old = MemoryContextSwitchTo(pinned_caches_mctx);
List *pinned_caches_copy = list_copy(pinned_caches);
MemoryContextSwitchTo(old);
/* Only release caches created in subtxn */
foreach (lc, pinned_caches_copy)
{
CachePin *cp = lfirst(lc);
if (cp->subtxnid == subtxnid)
{
/*
* This assert makes sure that that we don't have a cache leak
* when running with debugging
*/
Assert(abort);
cache_release_subtxn(cp->cache, subtxnid);
}
}
list_free(pinned_caches_copy);
}
/*
* Transaction end callback that cleans up any pinned caches. This is a
* safeguard that protects against indefinitely pinned caches (memory leaks)
* that may occur if a transaction ends (normally or abnormally) while a pin is
* held. Without this, a ts_cache_pin() call always needs to be paired with a
* ts_cache_release() call and wrapped in a PG_TRY() block to capture and handle
* any exceptions that occur.
*
* Note that this checks that ts_cache_release() is always called by the end
* of a non-aborted transaction unless cache->release_on_commit is set to true.
* */
static void
cache_xact_end(XactEvent event, void *arg)
{
switch (event)
{
case XACT_EVENT_ABORT:
case XACT_EVENT_PARALLEL_ABORT:
release_all_pinned_caches();
break;
default:
{
/*
* Make a copy of the list of pinned caches since
* ts_cache_release() can manipulate the original list.
*/
List *pinned_caches_copy = list_copy(pinned_caches);
ListCell *lc;
/*
* Only caches left should be marked as non-released
*/
foreach (lc, pinned_caches_copy)
{
CachePin *cp = lfirst(lc);
/*
* This assert makes sure that that we don't have a cache
* leak when running with debugging
*/
Assert(!cp->cache->release_on_commit);
/*
* This may still happen in optimized environments where
* Assert is turned off. In that case, release.
*/
if (cp->cache->release_on_commit)
ts_cache_release(cp->cache);
}
list_free(pinned_caches_copy);
}
break;
}
}
static void
cache_subxact_abort(SubXactEvent event, SubTransactionId subtxn_id, SubTransactionId parentSubid,
void *arg)
{
/*
* Note that cache->release_on_commit is irrelevant here since can't have
* cross-commit operations in subtxns
*/
/*
* In subtxns, caches should have already been released, unless an abort
* happened. Be careful to only release caches that were created in the
* same subtxn.
*/
switch (event)
{
case SUBXACT_EVENT_START_SUB:
case SUBXACT_EVENT_PRE_COMMIT_SUB:
/* do nothing */
break;
case SUBXACT_EVENT_COMMIT_SUB:
release_subtxn_pinned_caches(subtxn_id, false);
break;
case SUBXACT_EVENT_ABORT_SUB:
release_subtxn_pinned_caches(subtxn_id, true);
break;
}
}
void
_cache_init(void)
{
cache_reset_pinned_caches();
RegisterXactCallback(cache_xact_end, NULL);
RegisterSubXactCallback(cache_subxact_abort, NULL);
}
void
_cache_fini(void)
{
release_all_pinned_caches();
MemoryContextDelete(pinned_caches_mctx);
pinned_caches_mctx = NULL;
pinned_caches = NIL;
UnregisterXactCallback(cache_xact_end, NULL);
UnregisterSubXactCallback(cache_subxact_abort, NULL);
}