Skip to content

Commit f8b0e73

Browse files
committed
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
1 parent 038b2b9 commit f8b0e73

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

contrib/zenith/libpagestore.c

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
*
1010
* IDENTIFICATION
11-
* contrib/zenith/libpqpagestore.c
11+
* contrib/zenith/libpagestore.c
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -34,23 +34,23 @@ void _PG_init(void);
3434

3535
#define PqPageStoreTrace 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_SMGR] could not establish connection to pageserver"),
7171
errdetail_internal("%s", msg)));
7272
}
7373

@@ -84,8 +84,7 @@ zenith_connect()
8484
{
8585
PQfinish(pageserver_conn);
8686
pageserver_conn = NULL;
87-
zenith_log(ERROR,
88-
"[ZENITH_SMGR] callmemaybe command failed");
87+
neon_log(ERROR, "[NEON_SMGR] callmemaybe command failed");
8988
}
9089
PQclear(res);
9190
}
@@ -96,8 +95,7 @@ zenith_connect()
9695
{
9796
PQfinish(pageserver_conn);
9897
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");
10199
}
102100

103101
while (PQisBusy(pageserver_conn))
@@ -124,14 +122,14 @@ zenith_connect()
124122
PQfinish(pageserver_conn);
125123
pageserver_conn = NULL;
126124

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);
129127
}
130128
}
131129
}
132130

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);
135133

136134
connected = true;
137135
}
@@ -145,7 +143,7 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
145143
int ret;
146144

147145
retry:
148-
ret = PQgetCopyData(conn, buffer, 1 /* async */);
146+
ret = PQgetCopyData(conn, buffer, 1 /* async */ );
149147

150148
if (ret == 0)
151149
{
@@ -165,8 +163,8 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
165163
if (wc & WL_SOCKET_READABLE)
166164
{
167165
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));
170168
}
171169

172170
goto retry;
@@ -177,7 +175,7 @@ call_PQgetCopyData(PGconn *conn, char **buffer)
177175

178176

179177
static ZenithResponse *
180-
zenith_call(ZenithRequest *request)
178+
pageserver_call(ZenithRequest *request)
181179
{
182180
StringInfoData req_buff;
183181
StringInfoData resp_buff;
@@ -194,7 +192,7 @@ zenith_call(ZenithRequest *request)
194192
}
195193

196194
if (!connected)
197-
zenith_connect();
195+
pageserver_connect();
198196

199197
req_buff = zm_pack_request(request);
200198

@@ -203,21 +201,21 @@ zenith_call(ZenithRequest *request)
203201
*
204202
* In principle, this could block if the output buffer is full, and we
205203
* 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.
208206
*/
209207
if (PQputCopyData(pageserver_conn, req_buff.data, req_buff.len) <= 0 || PQflush(pageserver_conn))
210208
{
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));
213211
}
214212
pfree(req_buff.data);
215213

216214
if (message_level_is_interesting(PqPageStoreTrace))
217215
{
218216
char *msg = zm_to_string((ZenithMessage *) request);
219217

220-
zenith_log(PqPageStoreTrace, "Sent request: %s", msg);
218+
neon_log(PqPageStoreTrace, "sent request: %s", msg);
221219
pfree(msg);
222220
}
223221

@@ -226,9 +224,9 @@ zenith_call(ZenithRequest *request)
226224
resp_buff.cursor = 0;
227225

228226
if (resp_buff.len == -1)
229-
zenith_log(ERROR, "end of COPY");
227+
neon_log(ERROR, "end of COPY");
230228
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));
232230

233231
resp = zm_unpack_response(&resp_buff);
234232
PQfreemem(resp_buff.data);
@@ -237,14 +235,9 @@ zenith_call(ZenithRequest *request)
237235
{
238236
char *msg = zm_to_string((ZenithMessage *) resp);
239237

240-
zenith_log(PqPageStoreTrace, "Got response: %s", msg);
238+
neon_log(PqPageStoreTrace, "got response: %s", msg);
241239
pfree(msg);
242240
}
243-
244-
/*
245-
* XXX: zm_to_string leak strings. Check with what memory contex all this
246-
* methods are called.
247-
*/
248241
}
249242
PG_CATCH();
250243
{
@@ -257,7 +250,7 @@ zenith_call(ZenithRequest *request)
257250
*/
258251
if (connected)
259252
{
260-
zenith_log(LOG, "dropping connection to page server due to error");
253+
neon_log(LOG, "dropping connection to page server due to error");
261254
PQfinish(pageserver_conn);
262255
pageserver_conn = NULL;
263256
connected = false;
@@ -290,11 +283,13 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
290283
PQconninfoOption *conn_options;
291284
PQconninfoOption *conn_option;
292285
MemoryContext oldcontext;
286+
293287
/*
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.
298293
*/
299294

300295
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)
321316
*/
322317
for (conn_option = conn_options; conn_option->keyword != NULL; conn_option++)
323318
{
324-
if (strcmp(conn_option->keyword, "host") == 0) {
319+
if (strcmp(conn_option->keyword, "host") == 0)
320+
{
325321
if (conn_option->val != NULL && conn_option->val[0] != '\0')
326322
host = conn_option->val;
327323
}
328-
else if (strcmp(conn_option->keyword, "port") == 0) {
324+
else if (strcmp(conn_option->keyword, "port") == 0)
325+
{
329326
if (conn_option->val != NULL && conn_option->val[0] != '\0')
330327
port = conn_option->val;
331328
}
332-
else if (strcmp(conn_option->keyword, "user") == 0) {
329+
else if (strcmp(conn_option->keyword, "user") == 0)
330+
{
333331
if (conn_option->val != NULL && conn_option->val[0] != '\0')
334332
user = conn_option->val;
335333
}
@@ -343,7 +341,7 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
343341
(errcode(ERRCODE_CONNECTION_EXCEPTION),
344342
errmsg("expected placeholder value in pageserver password starting from $ but found: %s", &conn_option->val[1])));
345343

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]);
347345
auth_token = getenv(&conn_option->val[1]);
348346
if (!auth_token)
349347
{
@@ -353,12 +351,16 @@ substitute_pageserver_password(const char *page_server_connstring_raw)
353351
}
354352
else
355353
{
356-
zenith_log(LOG, "using auth token from environment passed via env");
354+
neon_log(LOG, "using auth token from environment passed via env");
357355
}
358356
}
359357
}
360358
}
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+
*/
362364
oldcontext = CurrentMemoryContext;
363365
MemoryContextSwitchTo(TopMemoryContext);
364366
page_server_connstring = psprintf("postgresql://%s:%s@%s:%s", user, auth_token ? auth_token : "", host, port);
@@ -426,15 +428,15 @@ _PG_init(void)
426428
-1, -1, INT_MAX,
427429
PGC_SIGHUP,
428430
GUC_UNIT_MB,
429-
NULL, NULL, NULL);
431+
NULL, NULL, NULL);
430432

431433
relsize_hash_init();
432434
EmitWarningsOnPlaceholders("zenith");
433435

434436
if (page_server != NULL)
435-
zenith_log(ERROR, "libpqpagestore already loaded");
437+
neon_log(ERROR, "libpagestore already loaded");
436438

437-
zenith_log(PqPageStoreTrace, "libpqpagestore already loaded");
439+
neon_log(PqPageStoreTrace, "libpagestore already loaded");
438440
page_server = &api;
439441

440442
/* substitute password in pageserver_connstring */
@@ -443,18 +445,22 @@ _PG_init(void)
443445
/* Is there more correct way to pass CustomGUC to postgres code? */
444446
zenith_timeline_walproposer = zenith_timeline;
445447
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+
*/
447453
zenith_pageserver_connstring_walproposer = page_server_connstring;
448454

449455
if (wal_redo)
450456
{
451-
zenith_log(PqPageStoreTrace, "set inmem_smgr hook");
457+
neon_log(PqPageStoreTrace, "set inmem_smgr hook");
452458
smgr_hook = smgr_inmem;
453459
smgr_init_hook = smgr_init_inmem;
454460
}
455461
else if (page_server_connstring && page_server_connstring[0])
456462
{
457-
zenith_log(PqPageStoreTrace, "set zenith_smgr hook");
463+
neon_log(PqPageStoreTrace, "set neon_smgr hook");
458464
smgr_hook = smgr_zenith;
459465
smgr_init_hook = smgr_init_zenith;
460466
dbsize_hook = zenith_dbsize;

0 commit comments

Comments
 (0)