Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 811b6e3

Browse files
committed
Rework error messages around file handling
Some error messages related to file handling are using the code path context to define their state. For example, 2PC-related errors are referring to "two-phase status files", or "relation mapping file" is used for catalog-to-filenode mapping, however those prove to be difficult to translate, and are not more helpful than just referring to the path of the file being worked on. So simplify all those error messages by just referring to files with their path used. In some cases, like the manipulation of WAL segments, the context is actually helpful so those are kept. Calls to the system function read() have also been rather inconsistent with their error handling sometimes not reporting the number of bytes read, and some other code paths trying to use an errno which has not been set. The in-core functions are using a more consistent pattern with this patch, which checks for both errno if set or if an inconsistent read is happening. So as to care about pluralization when reading an unexpected number of byte(s), "could not read: read %d of %zu" is used as error message, with %d field being the output result of read() and %zu the expected size. This simplifies the work of translators with less variations of the same message. Author: Michael Paquier Reviewed-by: Álvaro Herrera Discussion: https://postgr.es/m/20180520000522.GB1603@paquier.xyz
1 parent c6736ff commit 811b6e3

File tree

12 files changed

+207
-95
lines changed

12 files changed

+207
-95
lines changed

src/backend/access/transam/twophase.c

+22-18
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ ReadTwoPhaseFile(TransactionId xid, bool give_warnings)
12191219
uint32 crc_offset;
12201220
pg_crc32c calc_crc,
12211221
file_crc;
1222+
int r;
12221223

12231224
TwoPhaseFilePath(path, xid);
12241225

@@ -1228,8 +1229,7 @@ ReadTwoPhaseFile(TransactionId xid, bool give_warnings)
12281229
if (give_warnings)
12291230
ereport(WARNING,
12301231
(errcode_for_file_access(),
1231-
errmsg("could not open two-phase state file \"%s\": %m",
1232-
path)));
1232+
errmsg("could not open file \"%s\": %m", path)));
12331233
return NULL;
12341234
}
12351235

@@ -1249,8 +1249,7 @@ ReadTwoPhaseFile(TransactionId xid, bool give_warnings)
12491249
errno = save_errno;
12501250
ereport(WARNING,
12511251
(errcode_for_file_access(),
1252-
errmsg("could not stat two-phase state file \"%s\": %m",
1253-
path)));
1252+
errmsg("could not stat file \"%s\": %m", path)));
12541253
}
12551254
return NULL;
12561255
}
@@ -1277,19 +1276,26 @@ ReadTwoPhaseFile(TransactionId xid, bool give_warnings)
12771276
buf = (char *) palloc(stat.st_size);
12781277

12791278
pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_READ);
1280-
if (read(fd, buf, stat.st_size) != stat.st_size)
1279+
r = read(fd, buf, stat.st_size);
1280+
if (r != stat.st_size)
12811281
{
12821282
int save_errno = errno;
12831283

12841284
pgstat_report_wait_end();
12851285
CloseTransientFile(fd);
12861286
if (give_warnings)
12871287
{
1288-
errno = save_errno;
1289-
ereport(WARNING,
1290-
(errcode_for_file_access(),
1291-
errmsg("could not read two-phase state file \"%s\": %m",
1292-
path)));
1288+
if (r < 0)
1289+
{
1290+
errno = save_errno;
1291+
ereport(WARNING,
1292+
(errcode_for_file_access(),
1293+
errmsg("could not read file \"%s\": %m", path)));
1294+
}
1295+
else
1296+
ereport(WARNING,
1297+
(errmsg("could not read file \"%s\": read %d of %zu",
1298+
path, r, stat.st_size)));
12931299
}
12941300
pfree(buf);
12951301
return NULL;
@@ -1632,8 +1638,7 @@ RemoveTwoPhaseFile(TransactionId xid, bool giveWarning)
16321638
if (errno != ENOENT || giveWarning)
16331639
ereport(WARNING,
16341640
(errcode_for_file_access(),
1635-
errmsg("could not remove two-phase state file \"%s\": %m",
1636-
path)));
1641+
errmsg("could not remove file \"%s\": %m", path)));
16371642
}
16381643

16391644
/*
@@ -1661,8 +1666,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
16611666
if (fd < 0)
16621667
ereport(ERROR,
16631668
(errcode_for_file_access(),
1664-
errmsg("could not recreate two-phase state file \"%s\": %m",
1665-
path)));
1669+
errmsg("could not recreate file \"%s\": %m", path)));
16661670

16671671
/* Write content and CRC */
16681672
pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_WRITE);
@@ -1677,7 +1681,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
16771681
errno = save_errno ? save_errno : ENOSPC;
16781682
ereport(ERROR,
16791683
(errcode_for_file_access(),
1680-
errmsg("could not write two-phase state file: %m")));
1684+
errmsg("could not write file \"%s\": %m", path)));
16811685
}
16821686
if (write(fd, &statefile_crc, sizeof(pg_crc32c)) != sizeof(pg_crc32c))
16831687
{
@@ -1690,7 +1694,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
16901694
errno = save_errno ? save_errno : ENOSPC;
16911695
ereport(ERROR,
16921696
(errcode_for_file_access(),
1693-
errmsg("could not write two-phase state file: %m")));
1697+
errmsg("could not write file \"%s\": %m", path)));
16941698
}
16951699
pgstat_report_wait_end();
16961700

@@ -1707,14 +1711,14 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
17071711
errno = save_errno;
17081712
ereport(ERROR,
17091713
(errcode_for_file_access(),
1710-
errmsg("could not fsync two-phase state file: %m")));
1714+
errmsg("could not fsync file \"%s\": %m", path)));
17111715
}
17121716
pgstat_report_wait_end();
17131717

17141718
if (CloseTransientFile(fd) != 0)
17151719
ereport(ERROR,
17161720
(errcode_for_file_access(),
1717-
errmsg("could not close two-phase state file: %m")));
1721+
errmsg("could not close file \"%s\": %m", path)));
17181722
}
17191723

17201724
/*

src/backend/access/transam/xlog.c

+27-13
Original file line numberDiff line numberDiff line change
@@ -3408,21 +3408,24 @@ XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
34083408

34093409
if (nread > 0)
34103410
{
3411+
int r;
3412+
34113413
if (nread > sizeof(buffer))
34123414
nread = sizeof(buffer);
34133415
errno = 0;
34143416
pgstat_report_wait_start(WAIT_EVENT_WAL_COPY_READ);
3415-
if (read(srcfd, buffer, nread) != nread)
3417+
r = read(srcfd, buffer, nread);
3418+
if (r != nread)
34163419
{
3417-
if (errno != 0)
3420+
if (r < 0)
34183421
ereport(ERROR,
34193422
(errcode_for_file_access(),
34203423
errmsg("could not read file \"%s\": %m",
34213424
path)));
34223425
else
34233426
ereport(ERROR,
3424-
(errmsg("not enough data in file \"%s\"",
3425-
path)));
3427+
(errmsg("could not read file \"%s\": read %d of %zu",
3428+
path, r, (Size) nread)));
34263429
}
34273430
pgstat_report_wait_end();
34283431
}
@@ -4544,7 +4547,7 @@ ReadControlFile(void)
45444547
if (fd < 0)
45454548
ereport(PANIC,
45464549
(errcode_for_file_access(),
4547-
errmsg("could not open control file \"%s\": %m",
4550+
errmsg("could not open file \"%s\": %m",
45484551
XLOG_CONTROL_FILE)));
45494552

45504553
pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_READ);
@@ -4554,10 +4557,12 @@ ReadControlFile(void)
45544557
if (r < 0)
45554558
ereport(PANIC,
45564559
(errcode_for_file_access(),
4557-
errmsg("could not read from control file: %m")));
4560+
errmsg("could not read file \"%s\": %m",
4561+
XLOG_CONTROL_FILE)));
45584562
else
45594563
ereport(PANIC,
4560-
(errmsg("could not read from control file: read %d bytes, expected %d", r, (int) sizeof(ControlFileData))));
4564+
(errmsg("could not read file \"%s\": read %d of %zu",
4565+
XLOG_CONTROL_FILE, r, sizeof(ControlFileData))));
45614566
}
45624567
pgstat_report_wait_end();
45634568

@@ -11689,6 +11694,7 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
1168911694
int emode = private->emode;
1169011695
uint32 targetPageOff;
1169111696
XLogSegNo targetSegNo PG_USED_FOR_ASSERTS_ONLY;
11697+
int r;
1169211698

1169311699
XLByteToSeg(targetPagePtr, targetSegNo, wal_segment_size);
1169411700
targetPageOff = XLogSegmentOffset(targetPagePtr, wal_segment_size);
@@ -11782,18 +11788,26 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
1178211788
}
1178311789

1178411790
pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
11785-
if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
11791+
r = read(readFile, readBuf, XLOG_BLCKSZ);
11792+
if (r != XLOG_BLCKSZ)
1178611793
{
1178711794
char fname[MAXFNAMELEN];
1178811795
int save_errno = errno;
1178911796

1179011797
pgstat_report_wait_end();
1179111798
XLogFileName(fname, curFileTLI, readSegNo, wal_segment_size);
11792-
errno = save_errno;
11793-
ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
11794-
(errcode_for_file_access(),
11795-
errmsg("could not read from log segment %s, offset %u: %m",
11796-
fname, readOff)));
11799+
if (r < 0)
11800+
{
11801+
errno = save_errno;
11802+
ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
11803+
(errcode_for_file_access(),
11804+
errmsg("could not read from log segment %s, offset %u: %m",
11805+
fname, readOff)));
11806+
}
11807+
else
11808+
ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
11809+
(errmsg("could not read from log segment %s, offset %u: read %d of %zu",
11810+
fname, readOff, r, (Size) XLOG_BLCKSZ)));
1179711811
goto next_record_is_invalid;
1179811812
}
1179911813
pgstat_report_wait_end();

src/backend/replication/logical/origin.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,16 @@ StartupReplicationOrigin(void)
712712
/* verify magic, that is written even if nothing was active */
713713
readBytes = read(fd, &magic, sizeof(magic));
714714
if (readBytes != sizeof(magic))
715-
ereport(PANIC,
716-
(errmsg("could not read file \"%s\": %m",
717-
path)));
715+
{
716+
if (readBytes < 0)
717+
ereport(PANIC,
718+
(errmsg("could not read file \"%s\": %m",
719+
path)));
720+
else
721+
ereport(PANIC,
722+
(errmsg("could not read file \"%s\": read %d of %zu",
723+
path, readBytes, sizeof(magic))));
724+
}
718725
COMP_CRC32C(crc, &magic, sizeof(magic));
719726

720727
if (magic != REPLICATION_STATE_MAGIC)

src/backend/replication/logical/snapbuild.c

+48-20
Original file line numberDiff line numberDiff line change
@@ -1726,11 +1726,18 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
17261726
int save_errno = errno;
17271727

17281728
CloseTransientFile(fd);
1729-
errno = save_errno;
1730-
ereport(ERROR,
1731-
(errcode_for_file_access(),
1732-
errmsg("could not read file \"%s\", read %d of %d: %m",
1733-
path, readBytes, (int) SnapBuildOnDiskConstantSize)));
1729+
1730+
if (readBytes < 0)
1731+
{
1732+
errno = save_errno;
1733+
ereport(ERROR,
1734+
(errcode_for_file_access(),
1735+
errmsg("could not read file \"%s\": %m", path)));
1736+
}
1737+
else
1738+
ereport(ERROR,
1739+
(errmsg("could not read file \"%s\": read %d of %zu",
1740+
path, readBytes, SnapBuildOnDiskConstantSize)));
17341741
}
17351742

17361743
if (ondisk.magic != SNAPBUILD_MAGIC)
@@ -1757,11 +1764,18 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
17571764
int save_errno = errno;
17581765

17591766
CloseTransientFile(fd);
1760-
errno = save_errno;
1761-
ereport(ERROR,
1762-
(errcode_for_file_access(),
1763-
errmsg("could not read file \"%s\", read %d of %d: %m",
1764-
path, readBytes, (int) sizeof(SnapBuild))));
1767+
1768+
if (readBytes < 0)
1769+
{
1770+
errno = save_errno;
1771+
ereport(ERROR,
1772+
(errcode_for_file_access(),
1773+
errmsg("could not read file \"%s\": %m", path)));
1774+
}
1775+
else
1776+
ereport(ERROR,
1777+
(errmsg("could not read file \"%s\": read %d of %zu",
1778+
path, readBytes, sizeof(SnapBuild))));
17651779
}
17661780
COMP_CRC32C(checksum, &ondisk.builder, sizeof(SnapBuild));
17671781

@@ -1777,11 +1791,18 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
17771791
int save_errno = errno;
17781792

17791793
CloseTransientFile(fd);
1780-
errno = save_errno;
1781-
ereport(ERROR,
1782-
(errcode_for_file_access(),
1783-
errmsg("could not read file \"%s\", read %d of %d: %m",
1784-
path, readBytes, (int) sz)));
1794+
1795+
if (readBytes < 0)
1796+
{
1797+
errno = save_errno;
1798+
ereport(ERROR,
1799+
(errcode_for_file_access(),
1800+
errmsg("could not read file \"%s\": %m", path)));
1801+
}
1802+
else
1803+
ereport(ERROR,
1804+
(errmsg("could not read file \"%s\": read %d of %zu",
1805+
path, readBytes, sz)));
17851806
}
17861807
COMP_CRC32C(checksum, ondisk.builder.was_running.was_xip, sz);
17871808

@@ -1796,11 +1817,18 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
17961817
int save_errno = errno;
17971818

17981819
CloseTransientFile(fd);
1799-
errno = save_errno;
1800-
ereport(ERROR,
1801-
(errcode_for_file_access(),
1802-
errmsg("could not read file \"%s\", read %d of %d: %m",
1803-
path, readBytes, (int) sz)));
1820+
1821+
if (readBytes < 0)
1822+
{
1823+
errno = save_errno;
1824+
ereport(ERROR,
1825+
(errcode_for_file_access(),
1826+
errmsg("could not read file \"%s\": %m", path)));
1827+
}
1828+
else
1829+
ereport(ERROR,
1830+
(errmsg("could not read file \"%s\": read %d of %zu",
1831+
path, readBytes, sz)));
18041832
}
18051833
COMP_CRC32C(checksum, ondisk.builder.committed.xip, sz);
18061834

src/backend/replication/slot.c

+17-9
Original file line numberDiff line numberDiff line change
@@ -1414,11 +1414,15 @@ RestoreSlotFromDisk(const char *name)
14141414

14151415
CloseTransientFile(fd);
14161416
errno = saved_errno;
1417-
ereport(PANIC,
1418-
(errcode_for_file_access(),
1419-
errmsg("could not read file \"%s\", read %d of %u: %m",
1420-
path, readBytes,
1421-
(uint32) ReplicationSlotOnDiskConstantSize)));
1417+
if (readBytes < 0)
1418+
ereport(PANIC,
1419+
(errcode_for_file_access(),
1420+
errmsg("could not read file \"%s\": %m", path)));
1421+
else
1422+
ereport(PANIC,
1423+
(errmsg("could not read file \"%s\": read %d of %zu",
1424+
path, readBytes,
1425+
ReplicationSlotOnDiskConstantSize)));
14221426
}
14231427

14241428
/* verify magic */
@@ -1454,10 +1458,14 @@ RestoreSlotFromDisk(const char *name)
14541458

14551459
CloseTransientFile(fd);
14561460
errno = saved_errno;
1457-
ereport(PANIC,
1458-
(errcode_for_file_access(),
1459-
errmsg("could not read file \"%s\", read %d of %u: %m",
1460-
path, readBytes, cp.length)));
1461+
if (readBytes < 0)
1462+
ereport(PANIC,
1463+
(errcode_for_file_access(),
1464+
errmsg("could not read file \"%s\": %m", path)));
1465+
else
1466+
ereport(PANIC,
1467+
(errmsg("could not read file \"%s\": read %d of %zu",
1468+
path, readBytes, (Size) cp.length)));
14611469
}
14621470

14631471
CloseTransientFile(fd);

0 commit comments

Comments
 (0)