Skip to content

Commit 441598c

Browse files
author
SqlRush
committed
style(cluster): clang-format-18 compliance for spec-5.6 sources
The spec-5.6 cluster sources were hand-formatted and diverged from the repo .clang-format under clang-format 18 (the fast-gate version; local was 22 -- the L345 version skew). Reformatted with clang-format 18.1.x; whitespace, brace placement and cast spacing only -- no semantic change. Backend builds clean and the cf cluster_unit binaries pass. Spec: spec-5.6-cf-enqueue-shared-controlfile-authority.md
1 parent e3c070c commit 441598c

19 files changed

Lines changed: 643 additions & 606 deletions

src/backend/cluster/cluster_cf_authority.c

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@
5656
static bool
5757
build_path(char *dst, size_t dstlen, const char *relpath)
5858
{
59-
if (cluster_shared_data_dir == NULL || cluster_shared_data_dir[0] == '\0')
60-
{
59+
if (cluster_shared_data_dir == NULL || cluster_shared_data_dir[0] == '\0') {
6160
if (dstlen > 0)
6261
dst[0] = '\0';
6362
return false;
@@ -101,7 +100,7 @@ ClusterCfValidity
101100
cluster_cf_classify_buffer(const char *buf, size_t len, uint64 expected_sysid)
102101
{
103102
ControlFileData cf;
104-
pg_crc32c crc;
103+
pg_crc32c crc;
105104

106105
if (buf == NULL || len < sizeof(ControlFileData))
107106
return CLUSTER_CF_INVALID_SHORT;
@@ -115,8 +114,7 @@ cluster_cf_classify_buffer(const char *buf, size_t len, uint64 expected_sysid)
115114
return CLUSTER_CF_INVALID_CRC;
116115

117116
/* Foreign byte order: version is a nonzero multiple of 65536. */
118-
if (cf.pg_control_version % 65536 == 0 &&
119-
cf.pg_control_version / 65536 != 0)
117+
if (cf.pg_control_version % 65536 == 0 && cf.pg_control_version / 65536 != 0)
120118
return CLUSTER_CF_INVALID_BYTE_ORDER;
121119

122120
if (expected_sysid != 0 && cf.system_identifier != expected_sysid)
@@ -134,8 +132,7 @@ cluster_cf_classify_buffer(const char *buf, size_t len, uint64 expected_sysid)
134132
* corrupt primary (spec §3.9 T3). Otherwise fail-closed.
135133
*/
136134
ClusterCfReadSource
137-
cluster_cf_decide_source(ClusterCfValidity primary, ClusterCfValidity bak,
138-
bool bak_strict_ok)
135+
cluster_cf_decide_source(ClusterCfValidity primary, ClusterCfValidity bak, bool bak_strict_ok)
139136
{
140137
if (primary == CLUSTER_CF_VALID)
141138
return CLUSTER_CF_SOURCE_PRIMARY;
@@ -174,8 +171,8 @@ cluster_cf_bak_strict_ok(const ControlFileData *bak, uint64 expected_sysid,
174171
static ClusterCfValidity
175172
read_image(const char *path, char *image)
176173
{
177-
int fd;
178-
int r;
174+
int fd;
175+
int r;
179176

180177
if (path == NULL)
181178
return CLUSTER_CF_INVALID_SHORT;
@@ -187,7 +184,7 @@ read_image(const char *path, char *image)
187184
r = read(fd, image, sizeof(ControlFileData));
188185
CloseTransientFile(fd);
189186

190-
if (r != (int) sizeof(ControlFileData))
187+
if (r != (int)sizeof(ControlFileData))
191188
return CLUSTER_CF_INVALID_SHORT;
192189

193190
return cluster_cf_classify_buffer(image, sizeof(ControlFileData), 0);
@@ -203,16 +200,15 @@ read_image(const char *path, char *image)
203200
bool
204201
cluster_cf_authority_read(ControlFileData *out)
205202
{
206-
char primary_img[sizeof(ControlFileData)];
207-
char bak_img[sizeof(ControlFileData)];
203+
char primary_img[sizeof(ControlFileData)];
204+
char bak_img[sizeof(ControlFileData)];
208205
ClusterCfValidity pv;
209206
ClusterCfValidity bv;
210-
bool bak_strict_ok;
207+
bool bak_strict_ok;
211208
ClusterCfReadSource src;
212209

213210
pv = read_image(cluster_cf_shared_path(), primary_img);
214-
if (pv == CLUSTER_CF_VALID)
215-
{
211+
if (pv == CLUSTER_CF_VALID) {
216212
memcpy(out, primary_img, sizeof(ControlFileData));
217213
return true;
218214
}
@@ -230,29 +226,26 @@ cluster_cf_authority_read(ControlFileData *out)
230226
* is skipped here (0); the symlink/migrate gates already reject a foreign
231227
* authority at startup (§3.2 M3).
232228
*/
233-
if (bv == CLUSTER_CF_VALID)
234-
{
229+
if (bv == CLUSTER_CF_VALID) {
235230
ControlFileData bak_cf;
236231

237232
memcpy(&bak_cf, bak_img, sizeof(ControlFileData));
238-
bak_strict_ok = cluster_cf_bak_strict_ok(&bak_cf, 0,
239-
cluster_cf_bak_checkpoint_recoverable(&bak_cf));
240-
}
241-
else
233+
bak_strict_ok
234+
= cluster_cf_bak_strict_ok(&bak_cf, 0, cluster_cf_bak_checkpoint_recoverable(&bak_cf));
235+
} else
242236
bak_strict_ok = false;
243237

244238
src = cluster_cf_decide_source(pv, bv, bak_strict_ok);
245-
switch (src)
246-
{
247-
case CLUSTER_CF_SOURCE_PRIMARY:
248-
memcpy(out, primary_img, sizeof(ControlFileData));
249-
return true;
250-
case CLUSTER_CF_SOURCE_BAK:
251-
cluster_cf_counter_inc(CLUSTER_CF_BAK_FALLBACK);
252-
memcpy(out, bak_img, sizeof(ControlFileData));
253-
return true;
254-
case CLUSTER_CF_SOURCE_FAILCLOSED:
255-
break;
239+
switch (src) {
240+
case CLUSTER_CF_SOURCE_PRIMARY:
241+
memcpy(out, primary_img, sizeof(ControlFileData));
242+
return true;
243+
case CLUSTER_CF_SOURCE_BAK:
244+
cluster_cf_counter_inc(CLUSTER_CF_BAK_FALLBACK);
245+
memcpy(out, bak_img, sizeof(ControlFileData));
246+
return true;
247+
case CLUSTER_CF_SOURCE_FAILCLOSED:
248+
break;
256249
}
257250
return false;
258251
}
@@ -265,39 +258,28 @@ cluster_cf_authority_read(ControlFileData *out)
265258
static void
266259
write_durable(const char *tmp, const char *final, const char *buf)
267260
{
268-
int fd;
261+
int fd;
269262

270263
fd = OpenTransientFile(tmp, O_RDWR | O_CREAT | O_TRUNC | PG_BINARY);
271264
if (fd < 0)
272-
ereport(PANIC,
273-
(errcode_for_file_access(),
274-
errmsg("could not open file \"%s\": %m", tmp)));
265+
ereport(PANIC, (errcode_for_file_access(), errmsg("could not open file \"%s\": %m", tmp)));
275266

276267
errno = 0;
277-
if (write(fd, buf, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE)
278-
{
268+
if (write(fd, buf, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE) {
279269
if (errno == 0)
280270
errno = ENOSPC;
281-
ereport(PANIC,
282-
(errcode_for_file_access(),
283-
errmsg("could not write file \"%s\": %m", tmp)));
271+
ereport(PANIC, (errcode_for_file_access(), errmsg("could not write file \"%s\": %m", tmp)));
284272
}
285273

286274
if (pg_fsync(fd) != 0)
287-
ereport(PANIC,
288-
(errcode_for_file_access(),
289-
errmsg("could not fsync file \"%s\": %m", tmp)));
275+
ereport(PANIC, (errcode_for_file_access(), errmsg("could not fsync file \"%s\": %m", tmp)));
290276

291277
if (CloseTransientFile(fd) != 0)
292-
ereport(PANIC,
293-
(errcode_for_file_access(),
294-
errmsg("could not close file \"%s\": %m", tmp)));
278+
ereport(PANIC, (errcode_for_file_access(), errmsg("could not close file \"%s\": %m", tmp)));
295279

296280
if (durable_rename(tmp, final, PANIC) != 0)
297-
ereport(PANIC,
298-
(errcode_for_file_access(),
299-
errmsg("could not rename file \"%s\" to \"%s\": %m",
300-
tmp, final)));
281+
ereport(PANIC, (errcode_for_file_access(),
282+
errmsg("could not rename file \"%s\" to \"%s\": %m", tmp, final)));
301283
}
302284

303285
/*
@@ -308,18 +290,18 @@ write_durable(const char *tmp, const char *final, const char *buf)
308290
static void
309291
roll_primary_to_bak(const char *primary, const char *bak, const char *baktmp)
310292
{
311-
char buf[PG_CONTROL_FILE_SIZE];
312-
int fd;
313-
int r;
293+
char buf[PG_CONTROL_FILE_SIZE];
294+
int fd;
295+
int r;
314296

315297
fd = OpenTransientFile(primary, O_RDONLY | PG_BINARY);
316298
if (fd < 0)
317-
return; /* first write: no prior primary to preserve */
299+
return; /* first write: no prior primary to preserve */
318300

319301
r = read(fd, buf, PG_CONTROL_FILE_SIZE);
320302
CloseTransientFile(fd);
321303
if (r != PG_CONTROL_FILE_SIZE)
322-
return; /* short/odd primary: don't manufacture a .bak */
304+
return; /* short/odd primary: don't manufacture a .bak */
323305

324306
write_durable(baktmp, bak, buf);
325307
}
@@ -332,24 +314,23 @@ roll_primary_to_bak(const char *primary, const char *bak, const char *baktmp)
332314
void
333315
cluster_cf_authority_write(const ControlFileData *cf)
334316
{
335-
char buffer[PG_CONTROL_FILE_SIZE];
336-
char primary[MAXPGPATH];
337-
char bak[MAXPGPATH];
338-
char tmp[MAXPGPATH];
339-
char baktmp[MAXPGPATH];
317+
char buffer[PG_CONTROL_FILE_SIZE];
318+
char primary[MAXPGPATH];
319+
char bak[MAXPGPATH];
320+
char tmp[MAXPGPATH];
321+
char baktmp[MAXPGPATH];
340322
ControlFileData local;
341323

342-
if (!build_path(primary, sizeof(primary), CLUSTER_CF_REL_PATH) ||
343-
!build_path(bak, sizeof(bak), CLUSTER_CF_BAK_REL_PATH) ||
344-
!build_path(tmp, sizeof(tmp), CLUSTER_CF_TMP_REL_PATH) ||
345-
!build_path(baktmp, sizeof(baktmp), CLUSTER_CF_BAK_TMP_REL_PATH))
346-
ereport(PANIC,
347-
(errmsg("cluster shared_data_dir is not configured")));
324+
if (!build_path(primary, sizeof(primary), CLUSTER_CF_REL_PATH)
325+
|| !build_path(bak, sizeof(bak), CLUSTER_CF_BAK_REL_PATH)
326+
|| !build_path(tmp, sizeof(tmp), CLUSTER_CF_TMP_REL_PATH)
327+
|| !build_path(baktmp, sizeof(baktmp), CLUSTER_CF_BAK_TMP_REL_PATH))
328+
ereport(PANIC, (errmsg("cluster shared_data_dir is not configured")));
348329

349330
/* Recompute CRC over a private copy (cf is const). */
350331
memcpy(&local, cf, sizeof(local));
351332
INIT_CRC32C(local.crc);
352-
COMP_CRC32C(local.crc, (char *) &local, offsetof(ControlFileData, crc));
333+
COMP_CRC32C(local.crc, (char *)&local, offsetof(ControlFileData, crc));
353334
FIN_CRC32C(local.crc);
354335

355336
/* Zero-pad to the full on-disk size, as update_controlfile does. */

src/backend/cluster/cluster_cf_enqueue.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343
* release must not run S6 against a phantom holder. There is one slot per
4444
* mode (X / S); CF is not reentrant within a backend for a given mode.
4545
*/
46-
typedef struct CfHoldState
47-
{
48-
bool held;
49-
bool coordinated;
50-
ClusterLockAcquireRequest req; /* resid + holder + request_id for release */
46+
typedef struct CfHoldState {
47+
bool held;
48+
bool coordinated;
49+
ClusterLockAcquireRequest req; /* resid + holder + request_id for release */
5150
} CfHoldState;
5251

5352
static CfHoldState cf_hold_x;
@@ -117,67 +116,63 @@ cluster_cf_lock(LOCKMODE mode)
117116
cluster_cf_resid_encode(&req.resid);
118117
/* locktag left zeroed: not LOCKTAG_ADVISORY, so normal blocking semantics. */
119118
req.lockmode = mode;
120-
req.op = CLUSTER_LOCK_OP_REQUEST; /* CF never converts (X/S independent) */
119+
req.op = CLUSTER_LOCK_OP_REQUEST; /* CF never converts (X/S independent) */
121120
req.current_mode = NoLock;
122121
req.lockmethod_id = DEFAULT_LOCKMETHOD;
123-
req.dontwait = false; /* block until granted or timeout */
122+
req.dontwait = false; /* block until granted or timeout */
124123
req.sessionLock = false;
125-
req.caller_local_start_ts_ms = (uint64) (GetCurrentTimestamp() / 1000);
124+
req.caller_local_start_ts_ms = (uint64)(GetCurrentTimestamp() / 1000);
126125
/* spec-5.6 Dc4b: bound the CF acquire wait and label it ClusterCfEnqueueWait. */
127126
req.timeout_ms = cluster_cf_enqueue_timeout_ms;
128127
req.wait_event = WAIT_EVENT_CLUSTER_CF_ENQUEUE;
129128

130129
r = cluster_lock_acquire_seven_step(&req);
131130

132-
switch (r)
133-
{
134-
case CLUSTER_LOCK_ACQUIRE_OK_NATIVE:
131+
switch (r) {
132+
case CLUSTER_LOCK_ACQUIRE_OK_NATIVE:
135133

136-
/*
134+
/*
137135
* Cluster/LMS layer inactive (single-node or gate off): no cross-
138136
* node coordination is needed and nothing was registered in the
139137
* GRD. Treat as held but uncoordinated so release is a no-op.
140138
*/
141-
slot->held = true;
142-
slot->coordinated = false;
143-
slot->req = req;
144-
cluster_cf_counter_inc(mode == ExclusiveLock ? CLUSTER_CF_X_ACQUIRE
145-
: CLUSTER_CF_S_ACQUIRE);
146-
return true;
139+
slot->held = true;
140+
slot->coordinated = false;
141+
slot->req = req;
142+
cluster_cf_counter_inc(mode == ExclusiveLock ? CLUSTER_CF_X_ACQUIRE : CLUSTER_CF_S_ACQUIRE);
143+
return true;
147144

148-
case CLUSTER_LOCK_ACQUIRE_NEED_PG_NATIVE_LOCK:
149-
case CLUSTER_LOCK_ACQUIRE_OK_GRANTED:
150-
case CLUSTER_LOCK_ACQUIRE_OK_CONVERTED:
145+
case CLUSTER_LOCK_ACQUIRE_NEED_PG_NATIVE_LOCK:
146+
case CLUSTER_LOCK_ACQUIRE_OK_GRANTED:
147+
case CLUSTER_LOCK_ACQUIRE_OK_CONVERTED:
151148

152-
/*
149+
/*
153150
* Granted at the cluster level. CF has no PG-native heavyweight
154151
* lock to take, so run the S5 promote directly to turn the S3
155152
* reservation into a registered GRD holder (cross-node conflict
156153
* visibility). S5 failure cancels the reservation (S7) and we
157154
* fail closed.
158155
*/
159-
if (cluster_lock_acquire_s5_promote(&req) != CLUSTER_LOCK_ACQUIRE_OK_GRANTED)
160-
{
161-
cluster_cf_counter_inc(CLUSTER_CF_FAILCLOSED);
162-
return false;
163-
}
164-
slot->held = true;
165-
slot->coordinated = true;
166-
slot->req = req; /* holder + request_id for the release */
167-
cluster_cf_counter_inc(mode == ExclusiveLock ? CLUSTER_CF_X_ACQUIRE
168-
: CLUSTER_CF_S_ACQUIRE);
169-
return true;
156+
if (cluster_lock_acquire_s5_promote(&req) != CLUSTER_LOCK_ACQUIRE_OK_GRANTED) {
157+
cluster_cf_counter_inc(CLUSTER_CF_FAILCLOSED);
158+
return false;
159+
}
160+
slot->held = true;
161+
slot->coordinated = true;
162+
slot->req = req; /* holder + request_id for the release */
163+
cluster_cf_counter_inc(mode == ExclusiveLock ? CLUSTER_CF_X_ACQUIRE : CLUSTER_CF_S_ACQUIRE);
164+
return true;
170165

171-
default:
166+
default:
172167

173-
/*
168+
/*
174169
* NOT_AVAIL (try conflict; CF blocks so this is unexpected),
175170
* timeout, LMS-unavailable, deadlock, internal, etc. The lock
176171
* could not be proven held: fail closed. The caller raises the
177172
* appropriate FATAL/ERROR (CF correctness, spec §3.1 A3).
178173
*/
179-
cluster_cf_counter_inc(CLUSTER_CF_FAILCLOSED);
180-
return false;
174+
cluster_cf_counter_inc(CLUSTER_CF_FAILCLOSED);
175+
return false;
181176
}
182177
}
183178

@@ -199,7 +194,7 @@ cluster_cf_unlock(LOCKMODE mode)
199194
* re-derives the resid from a PG LOCKTAG that CF does not have.
200195
*/
201196
if (slot->coordinated)
202-
(void) cluster_lock_acquire_s6_release(&slot->req);
197+
(void)cluster_lock_acquire_s6_release(&slot->req);
203198

204199
slot->held = false;
205200
slot->coordinated = false;

0 commit comments

Comments
 (0)