-
Notifications
You must be signed in to change notification settings - Fork 9
/
bdr_dbcache.c
230 lines (182 loc) · 5.33 KB
/
bdr_dbcache.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
/* -------------------------------------------------------------------------
*
* bdr_dbcache.c
* coherent, per database, cache for BDR.
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* bdr_dbcache.c
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "bdr.h"
#include "bdr_label.h"
#include "miscadmin.h"
#include "access/xact.h"
#include "catalog/pg_database.h"
#include "commands/seclabel.h"
#include "utils/catcache.h"
#include "utils/inval.h"
#include "utils/jsonapi.h"
#include "utils/json.h"
#include "utils/jsonb.h"
#include "utils/syscache.h"
#include "utils/builtins.h"
/* Cache entry. */
typedef struct BDRDatabaseCacheEntry
{
Oid oid; /* cache key, needs to be first */
const char *dbname;
bool valid;
bool bdr_activated;
} BDRDatabaseCacheEntry;
static HTAB *BDRDatabaseCacheHash = NULL;
static BDRDatabaseCacheEntry * bdr_dbcache_lookup(Oid dboid, bool missing_ok);
static void
bdr_dbcache_invalidate_entry(Datum arg, int cacheid, uint32 hashvalue)
{
HASH_SEQ_STATUS status;
BDRDatabaseCacheEntry *hentry;
Assert(BDRDatabaseCacheHash != NULL);
/*
* Currently we just reset all entries; it's unlikely anything more
* complicated is worthwile.
*/
hash_seq_init(&status, BDRDatabaseCacheHash);
while ((hentry = (BDRDatabaseCacheEntry *) hash_seq_search(&status)) != NULL)
{
hentry->valid = false;
}
}
static void
bdr_dbcache_initialize()
{
HASHCTL ctl;
/* Make sure we've initialized CacheMemoryContext. */
if (CacheMemoryContext == NULL)
CreateCacheMemoryContext();
/* Initialize the hash table. */
MemSet(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(Oid);
ctl.entrysize = sizeof(BDRDatabaseCacheEntry);
ctl.hash = tag_hash;
ctl.hcxt = CacheMemoryContext;
BDRDatabaseCacheHash = hash_create("BDR database cache", 128, &ctl,
HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
/* Watch for invalidation events. */
CacheRegisterSyscacheCallback(DATABASEOID, bdr_dbcache_invalidate_entry, (Datum) 0);
}
void
bdr_parse_database_options(const char *label, bool *is_active)
{
JsonbIterator *it;
JsonbValue v;
int r;
int level = 0;
Jsonb *data = NULL;
bool parsing_bdr = false;
if (label == NULL)
return;
data = DatumGetJsonbP(
DirectFunctionCall1(jsonb_in, CStringGetDatum(label)));
if (!JB_ROOT_IS_OBJECT(data))
elog(ERROR, "root needs to be an object");
it = JsonbIteratorInit(&data->root);
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
{
if (level == 0 && r != WJB_BEGIN_OBJECT)
elog(ERROR, "root element needs to be an object");
else if (level == 0 && it->nElems > 1)
elog(ERROR, "only 'bdr' allowed on root level");
else if (level == 0 && r == WJB_BEGIN_OBJECT)
{
level++;
}
else if (level == 1 && r == WJB_KEY)
{
if (strncmp(v.val.string.val, "bdr", v.val.string.len) != 0)
elog(ERROR, "unexpected key: %s",
pnstrdup(v.val.string.val, v.val.string.len));
parsing_bdr = true;
}
else if (level == 1 && r == WJB_VALUE)
{
if (!parsing_bdr)
elog(ERROR, "in wrong state when parsing key");
if (v.type != jbvBool)
elog(ERROR, "unexpected type for key 'bdr': %u", v.type);
if (is_active != NULL)
*is_active = v.val.boolean;
}
else if (level == 1 && r != WJB_END_OBJECT)
{
elog(ERROR, "unexpected content: %u at level %d", r, level);
}
else if (r == WJB_END_OBJECT)
{
level--;
parsing_bdr = false;
}
else
elog(ERROR, "unexpected content: %u at level %d", r, level);
}
}
/*
* Lookup a database cache entry, via its oid.
*
* At some future point this probably will need to be externally accessible,
* but right now we don't need it yet.
*/
static BDRDatabaseCacheEntry *
bdr_dbcache_lookup(Oid dboid, bool missing_ok)
{
BDRDatabaseCacheEntry *entry;
bool found;
ObjectAddress object;
HeapTuple dbtuple;
const char *label;
if (BDRDatabaseCacheHash == NULL)
bdr_dbcache_initialize();
/*
* HASH_ENTER returns the existing entry if present or creates a new one.
*/
entry = hash_search(BDRDatabaseCacheHash, (void *) &dboid,
HASH_ENTER, &found);
if (found && entry->valid)
return entry;
/* zero out data part of the entry */
memset(((char *) entry) + offsetof(BDRDatabaseCacheEntry, dbname),
0,
sizeof(BDRDatabaseCacheEntry) - offsetof(BDRDatabaseCacheEntry, dbname));
/* lookup db entry and error out when the db doesn't exist && !missin_ok */
dbtuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dboid));
if (!HeapTupleIsValid(dbtuple) && !missing_ok)
elog(ERROR, "cache lookup failed for database %u", dboid);
else if (!HeapTupleIsValid(dbtuple))
return NULL;
entry->dbname = MemoryContextStrdup(CacheMemoryContext,
NameStr(((Form_pg_database) GETSTRUCT(dbtuple))->datname));
ReleaseSysCache(dbtuple);
object.classId = DatabaseRelationId;
object.objectId = dboid;
object.objectSubId = 0;
label = GetSecurityLabel(&object, "bdr");
bdr_parse_database_options(label, &entry->bdr_activated);
entry->valid = true;
return entry;
}
/*
* Is the database configured for bdr?
*/
bool
bdr_is_bdr_activated_db(Oid dboid)
{
BDRDatabaseCacheEntry *entry;
/* won't know until we've forked/execed */
Assert(IsUnderPostmaster);
/* potentially need to access syscaches */
Assert(IsTransactionState());
entry = bdr_dbcache_lookup(dboid, false);
return entry->bdr_activated;
}