-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstar_connect.c
347 lines (294 loc) · 8.55 KB
/
cstar_connect.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
#include "postgres.h"
#include "cstar_fdw.h"
#include "access/xact.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
#include "commands/defrem.h"
#include "storage/ipc.h"
typedef struct ConnCacheKey
{
Oid serverid; /* OID of foreign server */
Oid userid; /* OID of local user whose mapping we use */
} ConnCacheKey;
typedef struct ConnCacheEntry
{
ConnCacheKey key; /* hash key (must be first) */
CassSession *conn; /* connection to foreign server, or NULL */
int xact_depth; /* 0 = no xact open, 1 = main xact open, 2 =
* one level of subxact open, etc */
bool have_prep_stmt; /* have we prepared any stmts in this xact? */
bool have_error; /* have any subxacts aborted in this xact? */
} ConnCacheEntry;
/*
* Connection cache (initialized on first use)
*/
static HTAB *ConnectionHash = NULL;
/* tracks whether any work is needed in callback functions */
static bool xact_got_connection = false;
/* prototypes of private functions */
static CassSession *connect_cass_server(ForeignServer *server, UserMapping *user);
static int ExtractOptions(List *defelems, const char **keywords,
const char **values);
static CassCluster* cluster;
static void pgcass_close(int code, Datum arg);
CassSession *
pgcass_GetConnection(ForeignServer *server, UserMapping *user,
bool will_prep_stmt)
{
bool found;
ConnCacheEntry *entry;
ConnCacheKey key;
/* First time through, initialize connection cache hashtable */
if (ConnectionHash == NULL)
{
HASHCTL ctl;
MemSet(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(ConnCacheKey);
ctl.entrysize = sizeof(ConnCacheEntry);
ctl.hash = tag_hash;
/* allocate ConnectionHash in the cache context */
ctl.hcxt = CacheMemoryContext;
ConnectionHash = hash_create("cassandra_fdw connections", 8,
&ctl,
HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
}
/* Set flag that we did GetConnection during the current transaction */
xact_got_connection = true;
/* Create hash key for the entry. Assume no pad bytes in key struct */
key.serverid = server->serverid;
key.userid = user->userid;
/*
* Find or create cached entry for requested connection.
*/
entry = hash_search(ConnectionHash, &key, HASH_ENTER, &found);
if (!found)
{
/* initialize new hashtable entry (key is already filled in) */
entry->conn = NULL;
entry->xact_depth = 0;
entry->have_prep_stmt = false;
entry->have_error = false;
}
/*
* We don't check the health of cached connection here, because it would
* require some overhead. Broken connection will be detected when the
* connection is actually used.
*/
/*
* If cache entry doesn't have a connection, we have to establish a new
* connection. (If connect_pg_server throws an error, the cache entry
* will be left in a valid empty state.)
*/
if (entry->conn == NULL)
{
entry->xact_depth = 0; /* just to be sure */
entry->have_prep_stmt = false;
entry->have_error = false;
entry->conn = connect_cass_server(server, user);
elog(DEBUG3, CSTAR_FDW_NAME ": new connection %p for server \"%s\"",
entry->conn, server->servername);
}
/*
* do nothing for connection pre-cmd execute till now.
*/
/* Remember if caller will prepare statements */
entry->have_prep_stmt |= will_prep_stmt;
return entry->conn;
}
/*
* Release connection reference count created by calling GetConnection.
*/
void
pgcass_ReleaseConnection(CassSession *session)
{
/*
* Currently, we don't actually track connection references because all
* cleanup is managed on a transaction or subtransaction basis instead. So
* there's nothing to do here.
*/
CassFuture* close_future = NULL;
return;
/* Close the session */
close_future = cass_session_close(session);
cass_future_wait(close_future);
cass_future_free(close_future);
}
/*
* Connect to remote server using specified server and user mapping properties.
*/
static CassSession *
connect_cass_server(ForeignServer *server, UserMapping *user)
{
CassFuture* conn_future = NULL;
CassSession* session = NULL;
if (!cluster)
{
cluster = cass_cluster_new();
on_proc_exit(pgcass_close, 0);
}
/*
* Use PG_TRY block to ensure closing connection on error.
*/
PG_TRY();
{
const char **keywords;
const char **values;
int n;
int i;
const char *svr_host = NULL;
int svr_port = 0;
int svr_proto = 0;
const char *svr_username = NULL;
const char *svr_password = NULL;
/*
* Construct connection params from generic options of ForeignServer
* and UserMapping.
*/
n = list_length(server->options) + list_length(user->options) + 1;
keywords = (const char **) palloc(n * sizeof(char *));
values = (const char **) palloc(n * sizeof(char *));
n = 0;
n += ExtractOptions(server->options,
keywords + n, values + n);
n += ExtractOptions(user->options,
keywords + n, values + n);
keywords[n] = values[n] = NULL;
/* Add contact points */
for (i = 0; keywords[i] != NULL; i++)
{
if (strcmp(keywords[i], "host") == 0)
{
svr_host = values[i];
}
else if (strcmp(keywords[i], "port") == 0)
{
svr_port = atoi(values[i]);
}
else if (strcmp(keywords[i], "protocol") == 0)
{
svr_proto = atoi(values[i]);
}
else if (strcmp(keywords[i], "username") == 0)
{
svr_username = values[i];
}
else if (strcmp(keywords[i], "password") == 0)
{
svr_password = values[i];
}
}
if (svr_host)
cass_cluster_set_contact_points(cluster, svr_host);
if (svr_port)
cass_cluster_set_port(cluster, svr_port);
if (svr_proto)
cass_cluster_set_protocol_version(cluster, svr_proto);
if (svr_username && svr_password)
cass_cluster_set_credentials(cluster, svr_username, svr_password);
session = cass_session_new();
/* Provide the cluster object as configuration to connect the session */
conn_future = cass_session_connect(session, cluster);
if (cass_future_error_code(conn_future) != CASS_OK)
{
/* Handle error */
const char* message;
size_t message_length;
cass_future_error_message(conn_future, &message, &message_length);
ereport(ERROR,
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
errmsg("could not connect to server \"%s\"",
server->servername),
errdetail_internal("%.*s", (int)message_length, message)));
}
cass_future_free(conn_future);
pfree(keywords);
pfree(values);
}
PG_CATCH();
{
/* Release data structure if we managed to create one */
if (conn_future)
cass_future_free(conn_future);
if (session)
cass_session_free(session);
PG_RE_THROW();
}
PG_END_TRY();
return session;
}
/*
* pgcass_close
* Shuts down the Connection.
*/
static void
pgcass_close(int code, Datum arg)
{
cass_cluster_free(cluster);
}
/*
* Report an error we got from the remote server.
*
* elevel: error level to use (typically ERROR, but might be less)
* clear: if true, clear the result (otherwise caller will handle it)
* sql: NULL, or text of remote command we tried to execute
*
* Note: callers that choose not to throw ERROR for a remote error are
* responsible for making sure that the associated ConnCacheEntry gets
* marked with have_error = true.
*/
void
pgcass_report_error(int elevel, CassFuture* result_future,
bool clear, const char *sql)
{
PG_TRY();
{
const char *message_primary = NULL;
const char *message_detail = NULL;
const char *message_hint = NULL;
const char *message_context = NULL;
int sqlstate;
size_t message_length;
sqlstate = ERRCODE_CONNECTION_FAILURE;
cass_future_error_message(result_future, &message_primary, &message_length);
ereport(elevel,
(errcode(sqlstate),
message_primary ? errmsg_internal("%s", message_primary) :
errmsg("unknown error"),
message_detail ? errdetail_internal("%s", message_detail) : 0,
message_hint ? errhint("%s", message_hint) : 0,
message_context ? errcontext("%s", message_context) : 0,
sql ? errcontext("Remote SQL command: %s", sql) : 0));
}
PG_CATCH();
{
if (clear)
cass_future_free(result_future);
PG_RE_THROW();
}
PG_END_TRY();
if (clear)
cass_future_free(result_future);
}
/*
* Generate key-value arrays which include only libpq options from the
* given list (which can contain any kind of options). Caller must have
* allocated large-enough arrays. Returns number of options found.
*/
static int
ExtractOptions(List *defelems, const char **keywords,
const char **values)
{
ListCell *lc;
int i;
i = 0;
foreach(lc, defelems)
{
DefElem *d = (DefElem *) lfirst(lc);
keywords[i] = d->defname;
values[i] = defGetString(d);
i++;
}
return i;
}