1
1
/*-------------------------------------------------------------------------
2
2
*
3
- * libpqpagestore .c
3
+ * libpagestore .c
4
4
* Handles network communications with the remote pagestore.
5
5
*
6
6
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * contrib/zenith/libpqpagestore .c
11
+ * contrib/zenith/libpagestore .c
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -34,23 +34,23 @@ void _PG_init(void);
34
34
35
35
#define PqPageStoreTrace DEBUG5
36
36
37
- #define ZENITH_TAG "[ZENITH_SMGR ] "
38
- #define zenith_log (tag , fmt , ...) ereport(tag, \
39
- (errmsg(ZENITH_TAG fmt, ## __VA_ARGS__), \
37
+ #define NEON_TAG "[NEON_SMGR ] "
38
+ #define neon_log (tag , fmt , ...) ereport(tag, \
39
+ (errmsg(NEON_TAG fmt, ## __VA_ARGS__), \
40
40
errhidestmt(true), errhidecontext(true)))
41
41
42
42
bool connected = false;
43
43
PGconn * pageserver_conn = NULL ;
44
44
45
45
char * page_server_connstring_raw ;
46
46
47
- static ZenithResponse * zenith_call (ZenithRequest * request );
47
+ static ZenithResponse * pageserver_call (ZenithRequest * request );
48
48
page_server_api api = {
49
- .request = zenith_call
49
+ .request = pageserver_call
50
50
};
51
51
52
52
static void
53
- zenith_connect ()
53
+ pageserver_connect ()
54
54
{
55
55
char * query ;
56
56
int ret ;
@@ -67,7 +67,7 @@ zenith_connect()
67
67
pageserver_conn = NULL ;
68
68
ereport (ERROR ,
69
69
(errcode (ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION ),
70
- errmsg ("[ZENITH_SMGR ] could not establish connection" ),
70
+ errmsg ("[NEON_SMGR ] could not establish connection to pageserver " ),
71
71
errdetail_internal ("%s" , msg )));
72
72
}
73
73
@@ -84,8 +84,7 @@ zenith_connect()
84
84
{
85
85
PQfinish (pageserver_conn );
86
86
pageserver_conn = NULL ;
87
- zenith_log (ERROR ,
88
- "[ZENITH_SMGR] callmemaybe command failed" );
87
+ neon_log (ERROR , "[NEON_SMGR] callmemaybe command failed" );
89
88
}
90
89
PQclear (res );
91
90
}
@@ -96,8 +95,7 @@ zenith_connect()
96
95
{
97
96
PQfinish (pageserver_conn );
98
97
pageserver_conn = NULL ;
99
- zenith_log (ERROR ,
100
- "[ZENITH_SMGR] failed to start dispatcher_loop on pageserver" );
98
+ neon_log (ERROR , "[NEON_SMGR] could not send pagestream command to pageserver" );
101
99
}
102
100
103
101
while (PQisBusy (pageserver_conn ))
@@ -124,14 +122,14 @@ zenith_connect()
124
122
PQfinish (pageserver_conn );
125
123
pageserver_conn = NULL ;
126
124
127
- zenith_log (ERROR , "[ZENITH_SMGR] failed to get handshake from pageserver: %s" ,
128
- msg );
125
+ neon_log (ERROR , "[NEON_SMGR] could not complete handshake with pageserver: %s" ,
126
+ msg );
129
127
}
130
128
}
131
129
}
132
130
133
- // FIXME: when auth is enabled this ptints JWT to logs
134
- zenith_log (LOG , "libpqpagestore : connected to '%s'" , page_server_connstring );
131
+ /* FIXME: when auth is enabled this prints JWT to logs */
132
+ neon_log (LOG , "libpagestore : connected to '%s'" , page_server_connstring );
135
133
136
134
connected = true;
137
135
}
@@ -145,7 +143,7 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
145
143
int ret ;
146
144
147
145
retry :
148
- ret = PQgetCopyData (conn , buffer , 1 /* async */ );
146
+ ret = PQgetCopyData (conn , buffer , 1 /* async */ );
149
147
150
148
if (ret == 0 )
151
149
{
@@ -165,8 +163,8 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
165
163
if (wc & WL_SOCKET_READABLE )
166
164
{
167
165
if (!PQconsumeInput (conn ))
168
- zenith_log (ERROR , "could not get response from pageserver: %s" ,
169
- PQerrorMessage (conn ));
166
+ neon_log (ERROR , "could not get response from pageserver: %s" ,
167
+ PQerrorMessage (conn ));
170
168
}
171
169
172
170
goto retry ;
@@ -177,7 +175,7 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
177
175
178
176
179
177
static ZenithResponse *
180
- zenith_call (ZenithRequest * request )
178
+ pageserver_call (ZenithRequest * request )
181
179
{
182
180
StringInfoData req_buff ;
183
181
StringInfoData resp_buff ;
@@ -194,7 +192,7 @@ zenith_call(ZenithRequest *request)
194
192
}
195
193
196
194
if (!connected )
197
- zenith_connect ();
195
+ pageserver_connect ();
198
196
199
197
req_buff = zm_pack_request (request );
200
198
@@ -203,21 +201,21 @@ zenith_call(ZenithRequest *request)
203
201
*
204
202
* In principle, this could block if the output buffer is full, and we
205
203
* should use async mode and check for interrupts while waiting. In
206
- * practice, our requests are small enough to always fit in the output and
207
- * TCP buffer.
204
+ * practice, our requests are small enough to always fit in the output
205
+ * and TCP buffer.
208
206
*/
209
207
if (PQputCopyData (pageserver_conn , req_buff .data , req_buff .len ) <= 0 || PQflush (pageserver_conn ))
210
208
{
211
- zenith_log (ERROR , "failed to send page request: %s" ,
212
- PQerrorMessage (pageserver_conn ));
209
+ neon_log (ERROR , "failed to send page request: %s" ,
210
+ PQerrorMessage (pageserver_conn ));
213
211
}
214
212
pfree (req_buff .data );
215
213
216
214
if (message_level_is_interesting (PqPageStoreTrace ))
217
215
{
218
216
char * msg = zm_to_string ((ZenithMessage * ) request );
219
217
220
- zenith_log (PqPageStoreTrace , "Sent request: %s" , msg );
218
+ neon_log (PqPageStoreTrace , "sent request: %s" , msg );
221
219
pfree (msg );
222
220
}
223
221
@@ -226,9 +224,9 @@ zenith_call(ZenithRequest *request)
226
224
resp_buff .cursor = 0 ;
227
225
228
226
if (resp_buff .len == -1 )
229
- zenith_log (ERROR , "end of COPY" );
227
+ neon_log (ERROR , "end of COPY" );
230
228
else if (resp_buff .len == -2 )
231
- zenith_log (ERROR , "could not read COPY data: %s" , PQerrorMessage (pageserver_conn ));
229
+ neon_log (ERROR , "could not read COPY data: %s" , PQerrorMessage (pageserver_conn ));
232
230
233
231
resp = zm_unpack_response (& resp_buff );
234
232
PQfreemem (resp_buff .data );
@@ -237,14 +235,9 @@ zenith_call(ZenithRequest *request)
237
235
{
238
236
char * msg = zm_to_string ((ZenithMessage * ) resp );
239
237
240
- zenith_log (PqPageStoreTrace , "Got response: %s" , msg );
238
+ neon_log (PqPageStoreTrace , "got response: %s" , msg );
241
239
pfree (msg );
242
240
}
243
-
244
- /*
245
- * XXX: zm_to_string leak strings. Check with what memory contex all this
246
- * methods are called.
247
- */
248
241
}
249
242
PG_CATCH ();
250
243
{
@@ -257,7 +250,7 @@ zenith_call(ZenithRequest *request)
257
250
*/
258
251
if (connected )
259
252
{
260
- zenith_log (LOG , "dropping connection to page server due to error" );
253
+ neon_log (LOG , "dropping connection to page server due to error" );
261
254
PQfinish (pageserver_conn );
262
255
pageserver_conn = NULL ;
263
256
connected = false;
@@ -290,11 +283,13 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
290
283
PQconninfoOption * conn_options ;
291
284
PQconninfoOption * conn_option ;
292
285
MemoryContext oldcontext ;
286
+
293
287
/*
294
- * Here we substitute password in connection string with an environment variable.
295
- * To simplify things we construct a connection string back with only known options.
296
- * In particular: host port user and password. We do not currently use other options and
297
- * constructing full connstring in an URI shape is quite messy.
288
+ * Here we substitute password in connection string with an environment
289
+ * variable. To simplify things we construct a connection string back with
290
+ * only known options. In particular: host port user and password. We do
291
+ * not currently use other options and constructing full connstring in an
292
+ * URI shape is quite messy.
298
293
*/
299
294
300
295
if (page_server_connstring_raw == NULL || page_server_connstring_raw [0 ] == '\0' )
@@ -321,15 +316,18 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
321
316
*/
322
317
for (conn_option = conn_options ; conn_option -> keyword != NULL ; conn_option ++ )
323
318
{
324
- if (strcmp (conn_option -> keyword , "host" ) == 0 ) {
319
+ if (strcmp (conn_option -> keyword , "host" ) == 0 )
320
+ {
325
321
if (conn_option -> val != NULL && conn_option -> val [0 ] != '\0' )
326
322
host = conn_option -> val ;
327
323
}
328
- else if (strcmp (conn_option -> keyword , "port" ) == 0 ) {
324
+ else if (strcmp (conn_option -> keyword , "port" ) == 0 )
325
+ {
329
326
if (conn_option -> val != NULL && conn_option -> val [0 ] != '\0' )
330
327
port = conn_option -> val ;
331
328
}
332
- else if (strcmp (conn_option -> keyword , "user" ) == 0 ) {
329
+ else if (strcmp (conn_option -> keyword , "user" ) == 0 )
330
+ {
333
331
if (conn_option -> val != NULL && conn_option -> val [0 ] != '\0' )
334
332
user = conn_option -> val ;
335
333
}
@@ -343,7 +341,7 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
343
341
(errcode (ERRCODE_CONNECTION_EXCEPTION ),
344
342
errmsg ("expected placeholder value in pageserver password starting from $ but found: %s" , & conn_option -> val [1 ])));
345
343
346
- zenith_log (LOG , "found auth token placeholder in pageserver conn string %s " , & conn_option -> val [1 ]);
344
+ neon_log (LOG , "found auth token placeholder in pageserver conn string '%s' " , & conn_option -> val [1 ]);
347
345
auth_token = getenv (& conn_option -> val [1 ]);
348
346
if (!auth_token )
349
347
{
@@ -353,12 +351,16 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
353
351
}
354
352
else
355
353
{
356
- zenith_log (LOG , "using auth token from environment passed via env" );
354
+ neon_log (LOG , "using auth token from environment passed via env" );
357
355
}
358
356
}
359
357
}
360
358
}
361
- // allocate connection string in a TopMemoryContext to make sure it is not freed
359
+
360
+ /*
361
+ * allocate connection string in TopMemoryContext to make sure it is not
362
+ * freed
363
+ */
362
364
oldcontext = CurrentMemoryContext ;
363
365
MemoryContextSwitchTo (TopMemoryContext );
364
366
page_server_connstring = psprintf ("postgresql://%s:%s@%s:%s" , user , auth_token ? auth_token : "" , host , port );
@@ -426,15 +428,15 @@ _PG_init(void)
426
428
-1 , -1 , INT_MAX ,
427
429
PGC_SIGHUP ,
428
430
GUC_UNIT_MB ,
429
- NULL , NULL , NULL );
431
+ NULL , NULL , NULL );
430
432
431
433
relsize_hash_init ();
432
434
EmitWarningsOnPlaceholders ("zenith" );
433
435
434
436
if (page_server != NULL )
435
- zenith_log (ERROR , "libpqpagestore already loaded" );
437
+ neon_log (ERROR , "libpagestore already loaded" );
436
438
437
- zenith_log (PqPageStoreTrace , "libpqpagestore already loaded" );
439
+ neon_log (PqPageStoreTrace , "libpagestore already loaded" );
438
440
page_server = & api ;
439
441
440
442
/* substitute password in pageserver_connstring */
@@ -443,18 +445,22 @@ _PG_init(void)
443
445
/* Is there more correct way to pass CustomGUC to postgres code? */
444
446
zenith_timeline_walproposer = zenith_timeline ;
445
447
zenith_tenant_walproposer = zenith_tenant ;
446
- /* Walproposer instructcs safekeeper which pageserver to use for replication */
448
+
449
+ /*
450
+ * Walproposer instructs safekeeper which pageserver to use for
451
+ * replication
452
+ */
447
453
zenith_pageserver_connstring_walproposer = page_server_connstring ;
448
454
449
455
if (wal_redo )
450
456
{
451
- zenith_log (PqPageStoreTrace , "set inmem_smgr hook" );
457
+ neon_log (PqPageStoreTrace , "set inmem_smgr hook" );
452
458
smgr_hook = smgr_inmem ;
453
459
smgr_init_hook = smgr_init_inmem ;
454
460
}
455
461
else if (page_server_connstring && page_server_connstring [0 ])
456
462
{
457
- zenith_log (PqPageStoreTrace , "set zenith_smgr hook" );
463
+ neon_log (PqPageStoreTrace , "set neon_smgr hook" );
458
464
smgr_hook = smgr_zenith ;
459
465
smgr_init_hook = smgr_init_zenith ;
460
466
dbsize_hook = zenith_dbsize ;
0 commit comments