Skip to content

Commit c7c11a0

Browse files
hlinnakatristan957
authored andcommitted
Misc cleanup in libpagestore.c.
- Fix typos - Change Zenith -> Neon in the ZENITH_SMGR tag that's printed in error messages that is user-visible, and in various function names and comments that are not user-visible. - pgindent - Remove comment about zm_to_string() leaking memory. It doesn't. - Re-word some error messages to match PostgreSQL error message style guide - Cleanup logging style - Don't print JWT token to log
1 parent f7fc3bb commit c7c11a0

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

contrib/neon/libpagestore.c

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*-------------------------------------------------------------------------
22
*
3-
* libpqpagestore.c
3+
* libpagestore.c
44
* Handles network communications with the remote pagestore.
55
*
66
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
@@ -32,25 +32,25 @@ PG_MODULE_MAGIC;
3232

3333
void _PG_init(void);
3434

35-
#define PqPageStoreTrace DEBUG5
35+
#define PageStoreTrace DEBUG5
3636

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__), \
4040
errhidestmt(true), errhidecontext(true)))
4141

4242
bool connected = false;
4343
PGconn *pageserver_conn = NULL;
4444

4545
char *page_server_connstring_raw;
4646

47-
static ZenithResponse *zenith_call(ZenithRequest *request);
47+
static ZenithResponse *pageserver_call(ZenithRequest *request);
4848
page_server_api api = {
49-
.request = zenith_call
49+
.request = pageserver_call
5050
};
5151

5252
static void
53-
zenith_connect()
53+
pageserver_connect()
5454
{
5555
char *query;
5656
int ret;
@@ -67,7 +67,7 @@ zenith_connect()
6767
pageserver_conn = NULL;
6868
ereport(ERROR,
6969
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
70-
errmsg("[ZENITH_SMGR] could not establish connection"),
70+
errmsg(NEON_TAG "could not establish connection to pageserver"),
7171
errdetail_internal("%s", msg)));
7272
}
7373

@@ -77,8 +77,7 @@ zenith_connect()
7777
{
7878
PQfinish(pageserver_conn);
7979
pageserver_conn = NULL;
80-
zenith_log(ERROR,
81-
"[ZENITH_SMGR] failed to start dispatcher_loop on pageserver");
80+
neon_log(ERROR, "could not send pagestream command to pageserver");
8281
}
8382

8483
while (PQisBusy(pageserver_conn))
@@ -105,14 +104,13 @@ zenith_connect()
105104
PQfinish(pageserver_conn);
106105
pageserver_conn = NULL;
107106

108-
zenith_log(ERROR, "[ZENITH_SMGR] failed to get handshake from pageserver: %s",
109-
msg);
107+
neon_log(ERROR, "could not complete handshake with pageserver: %s",
108+
msg);
110109
}
111110
}
112111
}
113112

114-
// FIXME: when auth is enabled this ptints JWT to logs
115-
zenith_log(LOG, "libpqpagestore: connected to '%s'", page_server_connstring);
113+
neon_log(LOG, "libpagestore: connected to '%s'", page_server_connstring_raw);
116114

117115
connected = true;
118116
}
@@ -126,7 +124,7 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
126124
int ret;
127125

128126
retry:
129-
ret = PQgetCopyData(conn, buffer, 1 /* async */);
127+
ret = PQgetCopyData(conn, buffer, 1 /* async */ );
130128

131129
if (ret == 0)
132130
{
@@ -146,8 +144,8 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
146144
if (wc & WL_SOCKET_READABLE)
147145
{
148146
if (!PQconsumeInput(conn))
149-
zenith_log(ERROR, "could not get response from pageserver: %s",
150-
PQerrorMessage(conn));
147+
neon_log(ERROR, "could not get response from pageserver: %s",
148+
PQerrorMessage(conn));
151149
}
152150

153151
goto retry;
@@ -158,7 +156,7 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
158156

159157

160158
static ZenithResponse *
161-
zenith_call(ZenithRequest *request)
159+
pageserver_call(ZenithRequest *request)
162160
{
163161
StringInfoData req_buff;
164162
StringInfoData resp_buff;
@@ -175,7 +173,7 @@ zenith_call(ZenithRequest *request)
175173
}
176174

177175
if (!connected)
178-
zenith_connect();
176+
pageserver_connect();
179177

180178
req_buff = zm_pack_request(request);
181179

@@ -184,21 +182,21 @@ zenith_call(ZenithRequest *request)
184182
*
185183
* In principle, this could block if the output buffer is full, and we
186184
* should use async mode and check for interrupts while waiting. In
187-
* practice, our requests are small enough to always fit in the output and
188-
* TCP buffer.
185+
* practice, our requests are small enough to always fit in the output
186+
* and TCP buffer.
189187
*/
190188
if (PQputCopyData(pageserver_conn, req_buff.data, req_buff.len) <= 0 || PQflush(pageserver_conn))
191189
{
192-
zenith_log(ERROR, "failed to send page request: %s",
193-
PQerrorMessage(pageserver_conn));
190+
neon_log(ERROR, "failed to send page request: %s",
191+
PQerrorMessage(pageserver_conn));
194192
}
195193
pfree(req_buff.data);
196194

197-
if (message_level_is_interesting(PqPageStoreTrace))
195+
if (message_level_is_interesting(PageStoreTrace))
198196
{
199197
char *msg = zm_to_string((ZenithMessage *) request);
200198

201-
zenith_log(PqPageStoreTrace, "Sent request: %s", msg);
199+
neon_log(PageStoreTrace, "sent request: %s", msg);
202200
pfree(msg);
203201
}
204202

@@ -207,25 +205,20 @@ zenith_call(ZenithRequest *request)
207205
resp_buff.cursor = 0;
208206

209207
if (resp_buff.len == -1)
210-
zenith_log(ERROR, "end of COPY");
208+
neon_log(ERROR, "end of COPY");
211209
else if (resp_buff.len == -2)
212-
zenith_log(ERROR, "could not read COPY data: %s", PQerrorMessage(pageserver_conn));
210+
neon_log(ERROR, "could not read COPY data: %s", PQerrorMessage(pageserver_conn));
213211

214212
resp = zm_unpack_response(&resp_buff);
215213
PQfreemem(resp_buff.data);
216214

217-
if (message_level_is_interesting(PqPageStoreTrace))
215+
if (message_level_is_interesting(PageStoreTrace))
218216
{
219217
char *msg = zm_to_string((ZenithMessage *) resp);
220218

221-
zenith_log(PqPageStoreTrace, "Got response: %s", msg);
219+
neon_log(PageStoreTrace, "got response: %s", msg);
222220
pfree(msg);
223221
}
224-
225-
/*
226-
* XXX: zm_to_string leak strings. Check with what memory contex all this
227-
* methods are called.
228-
*/
229222
}
230223
PG_CATCH();
231224
{
@@ -238,7 +231,7 @@ zenith_call(ZenithRequest *request)
238231
*/
239232
if (connected)
240233
{
241-
zenith_log(LOG, "dropping connection to page server due to error");
234+
neon_log(LOG, "dropping connection to page server due to error");
242235
PQfinish(pageserver_conn);
243236
pageserver_conn = NULL;
244237
connected = false;
@@ -271,11 +264,13 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
271264
PQconninfoOption *conn_options;
272265
PQconninfoOption *conn_option;
273266
MemoryContext oldcontext;
267+
274268
/*
275-
* Here we substitute password in connection string with an environment variable.
276-
* To simplify things we construct a connection string back with only known options.
277-
* In particular: host port user and password. We do not currently use other options and
278-
* constructing full connstring in an URI shape is quite messy.
269+
* Here we substitute password in connection string with an environment
270+
* variable. To simplify things we construct a connection string back with
271+
* only known options. In particular: host port user and password. We do
272+
* not currently use other options and constructing full connstring in an
273+
* URI shape is quite messy.
279274
*/
280275

281276
if (page_server_connstring_raw == NULL || page_server_connstring_raw[0] == '\0')
@@ -302,15 +297,18 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
302297
*/
303298
for (conn_option = conn_options; conn_option->keyword != NULL; conn_option++)
304299
{
305-
if (strcmp(conn_option->keyword, "host") == 0) {
300+
if (strcmp(conn_option->keyword, "host") == 0)
301+
{
306302
if (conn_option->val != NULL && conn_option->val[0] != '\0')
307303
host = conn_option->val;
308304
}
309-
else if (strcmp(conn_option->keyword, "port") == 0) {
305+
else if (strcmp(conn_option->keyword, "port") == 0)
306+
{
310307
if (conn_option->val != NULL && conn_option->val[0] != '\0')
311308
port = conn_option->val;
312309
}
313-
else if (strcmp(conn_option->keyword, "user") == 0) {
310+
else if (strcmp(conn_option->keyword, "user") == 0)
311+
{
314312
if (conn_option->val != NULL && conn_option->val[0] != '\0')
315313
user = conn_option->val;
316314
}
@@ -324,7 +322,7 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
324322
(errcode(ERRCODE_CONNECTION_EXCEPTION),
325323
errmsg("expected placeholder value in pageserver password starting from $ but found: %s", &conn_option->val[1])));
326324

327-
zenith_log(LOG, "found auth token placeholder in pageserver conn string %s", &conn_option->val[1]);
325+
neon_log(LOG, "found auth token placeholder in pageserver conn string '%s'", &conn_option->val[1]);
328326
auth_token = getenv(&conn_option->val[1]);
329327
if (!auth_token)
330328
{
@@ -334,12 +332,16 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
334332
}
335333
else
336334
{
337-
zenith_log(LOG, "using auth token from environment passed via env");
335+
neon_log(LOG, "using auth token from environment passed via env");
338336
}
339337
}
340338
}
341339
}
342-
// allocate connection string in a TopMemoryContext to make sure it is not freed
340+
341+
/*
342+
* allocate connection string in TopMemoryContext to make sure it is not
343+
* freed
344+
*/
343345
oldcontext = CurrentMemoryContext;
344346
MemoryContextSwitchTo(TopMemoryContext);
345347
page_server_connstring = psprintf("postgresql://%s:%s@%s:%s", user, auth_token ? auth_token : "", host, port);
@@ -398,15 +400,15 @@ _PG_init(void)
398400
-1, -1, INT_MAX,
399401
PGC_SIGHUP,
400402
GUC_UNIT_MB,
401-
NULL, NULL, NULL);
403+
NULL, NULL, NULL);
402404

403405
relsize_hash_init();
404406
EmitWarningsOnPlaceholders("neon");
405407

406408
if (page_server != NULL)
407-
zenith_log(ERROR, "libpqpagestore already loaded");
409+
neon_log(ERROR, "libpagestore already loaded");
408410

409-
zenith_log(PqPageStoreTrace, "libpqpagestore already loaded");
411+
neon_log(PageStoreTrace, "libpagestore already loaded");
410412
page_server = &api;
411413

412414
/* substitute password in pageserver_connstring */
@@ -415,18 +417,22 @@ _PG_init(void)
415417
/* Is there more correct way to pass CustomGUC to postgres code? */
416418
zenith_timeline_walproposer = zenith_timeline;
417419
zenith_tenant_walproposer = zenith_tenant;
418-
/* Walproposer instructcs safekeeper which pageserver to use for replication */
420+
421+
/*
422+
* Walproposer instructs safekeeper which pageserver to use for
423+
* replication
424+
*/
419425
zenith_pageserver_connstring_walproposer = page_server_connstring;
420426

421427
if (wal_redo)
422428
{
423-
zenith_log(PqPageStoreTrace, "set inmem_smgr hook");
429+
neon_log(PageStoreTrace, "set inmem_smgr hook");
424430
smgr_hook = smgr_inmem;
425431
smgr_init_hook = smgr_init_inmem;
426432
}
427433
else if (page_server_connstring && page_server_connstring[0])
428434
{
429-
zenith_log(PqPageStoreTrace, "set zenith_smgr hook");
435+
neon_log(PageStoreTrace, "set neon_smgr hook");
430436
smgr_hook = smgr_zenith;
431437
smgr_init_hook = smgr_init_zenith;
432438
dbsize_hook = zenith_dbsize;

0 commit comments

Comments
 (0)