WAL prefetch (`wal.maxParallel` > 1) names the wrong segments when the WAL segment size is not 16MB
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Start in internal/cnpgi/common/wal.go at gatherWALFilesToRestore (lines 554-556), where NextSegments(parallel, nil, nil) falls back to 16MB; then read NextSegments and WalSegmentsPerFile in internal/cnpgi/common/wal_import.go (lines 158-197). Record the size of a successfully restored WAL segment in the restore path, pass it to NextSegments, and fetch only the requested segment until the size is known. The package tests of gatherWALFilesToRestore at 4MB, 16MB, 64MB, 1GB and unknown size define done.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
When wal.maxParallel is above 1, the WAL restore prefetches the segments that
follow the one PostgreSQL asked for. It always names them as if the cluster used
PostgreSQL's default 16MB WAL segments. A cluster initialized with another size
(bootstrap.initdb.walSegmentSize, PostgreSQL's initdb --wal-segsize) holds a
different number of segments per log file. Near the end of a log, the prefetch then
asks the object store for segment names PostgreSQL never writes, and at sizes below
16MB it names segments of the next log instead of the ones that follow.
Versions: found on v0.15.1 (6216b7d); the same code is in v0.16.0 (cd3b9fa) and in
main (691a620). Line numbers below are for main, where wal.go is the same as
in v0.16.0.
The defect
PostgreSQL names a WAL segment TTTTTTTTLLLLLLLLSSSSSSSS (timeline, log, segment).
A log holds 0x100000000 / wal_segment_size segments, so the last segment of a
log is:
| wal_segment_size | last segment of a log | the segment after ...0000003F |
the segment after ...000000FF |
|---|---|---|---|
| 4MB | 0x3FF |
...00000040 |
...0000000000000100 |
| 16MB (default) | 0xFF |
...00000040 |
...0000000100000000 |
| 64MB | 0x3F |
...0000000100000000 |
(never written) |
| 1GB | 0x3 |
(never written) | (never written) |
gatherWALFilesToRestore builds the prefetch list with NextSegments(parallel, nil, nil),
and NextSegments takes a nil segment size as DefaultWALSegmentSize (16MB):
-
internal/cnpgi/common/wal.go:554-556:// NextSegments would accept postgresVersion and segmentSize, // but we do not have this info here, so we pass nil. segmentList := segment.NextSegments(parallel, nil, nil) -
internal/cnpgi/common/wal_import.go:35:DefaultWALSegmentSize = int64(1 << 24);
:158WalSegmentsPerFileand:170-197NextSegmentsroll to the next log
after segment0xFFFFFFFF / segmentSize(:190), which is right when the size is
given.
The list goes to RestoreList (wal.go:409). A prefetched name that is not in the
archive comes back as not found. When the end-of-wal-stream flag applies
(shouldUseEndOfWALStreamFlag, wal.go:492-505, and isStreamingAvailable,
:507-539: a replica, or the designated primary of a replica cluster whose source
has streaming connection parameters), a not-found result makes
SetEndOfWALStreamFromResults (wal.go:425-435, barman-cloud v0.7.0) set the flag
for that name's timeline. The flag is checked only after a spool miss
(checkEndOfWALStreamFlag, wal.go:387-392 and :565-576), so the next invocation
on that timeline that misses the spool returns ErrEndOfWALStreamReached for the
segment PostgreSQL asks for, even when that segment is in the archive.
In v0.15.1 the nil call is at wal.go:550-552, and the same flow uses one flag for all
timelines: the streaming check at :383 and :495-535, the flag set at :422-427,
the error at :574.
Reproduction
Unit level (run, on main 691a620). I added a table test of
gatherWALFilesToRestore with the segment size given. With the parameter added but
ignored, 4 of the package's 37 specs fail: the 4MB, 64MB and 1GB entries, and the one
for an unknown size. At 64MB, the request for 00000001000000000000003E with
maxParallel: 4 prefetches:
00000001000000000000003E
00000001000000000000003F
000000010000000000000040 <- never written at 64MB
000000010000000000000041 <- never written at 64MB
where the segments PostgreSQL writes after ...3F are 000000010000000100000000 and
000000010000000100000001. The 64MB entry fails as:
Expected [00000001000000000000003E 00000001000000000000003F 000000010000000000000040]
to equal [00000001000000000000003E 00000001000000000000003F 000000010000000100000000]
and likewise at 4MB (...000000FF is followed by 000000010000000100000000
instead of 000000010000000000000100) and at 1GB (...00000003 is followed by
000000010000000000000004).
Cluster level (not run; traced from the code). A spool hit returns before the
prefetch list is built (wal.go:369-379), so only a spool miss starts a new batch of
maxParallel names, and only that batch-starting invocation can ask for wrong names.
Batches start every maxParallel segments, and after a log boundary the first batch
starts at segment 0. At 64MB a log holds 64 segments, so:
- when
maxParalleldoes not divide 64 (3, 5, 6, 7, ...), the last batch of every log
crosses its end: withmaxParallel: 5the batch that starts at...3Casks for
...40; withmaxParallel: 3the batch at...3Fasks for...40and...41; - when it divides 64 (2, 4, 8, ...), a batch crosses the end of a log only if the
restore started at a misaligned segment, and then only once, at the first boundary.
Steps:
- Create a Cluster with
bootstrap.initdb.walSegmentSize: 64and this plugin as
its WAL archive, withwal.maxParallel: 5on the ObjectStore. - Write more than 64 segments of WAL (4GB at 64MB per segment) so that the
archive holds000000010000000000000000to past000000010000000100000000. - Restore WAL from the archive through the plugin: either a new Cluster
bootstrapped withbootstrap.recoveryfrom it, or a replica (or the designated
primary of a replica cluster whose source has streaming connection parameters)
that catches up from the archive. - Expected with v0.16.0 or
main: at each log boundary, the invocation that requests
...3Calso asks the object store for...40, which does not exist. On the
recovery that is one wasted lookup per log. On the replica, the plugin logs "Set
end-of-wal-stream flag as one of the WAL files to be prefetched was not found". The
segments up to...3Fstill come from the spool; the next invocation that misses
it, the restore of000000010000000100000000, which is in the archive, fails with
the end-of-WAL-stream error. PostgreSQL then tries streaming replication before it
callsrestore_commandagain.
At 4MB more of each log is affected. NextSegments rolls to the next log after
0xFF, so every request from ...00000100 to ...000003FF (768 of the log's 1024
segments) misses the spool, is fetched alone, and prefetches segments 0 to
maxParallel - 2 of the next log, which PostgreSQL writes up to 770 segments later.
If the archive already holds the next log, each of those invocations downloads the
same few segments again; if it does not yet (a replica close to the end of the
archive), they are not found, and on a replica that sets the end-of-wal-stream flag.
The fix's idea
The segment size is fixed when the cluster is initialized, and every WAL segment
file is exactly that size. The restore already writes the segment PostgreSQL
requested whole to the destination path before it returns, so the plugin can
learn the size from that file:
- after a successful restore of a regular WAL segment, record the file's size as
the instance's WAL segment size, once it is one PostgreSQL allows (a power of two
from 1MB to 1GB); - name the segments to prefetch by that size (
NextSegments(parallel, nil, &size)); - while the size is not known yet (the first restore after the plugin starts),
fetch the requested segment alone rather than guess.
This costs one invocation without prefetch per plugin process, and needs no new
configuration and no change to the CNPG-I protocol.
Other sources considered:
spec.bootstrap.initdb.walSegmentSize: absent for a cluster bootstrapped by
recoveryorpg_basebackupfrom a source of another size, and for replica
clusters.- Parsing
global/pg_controlinPGDATA: its layout differs between
PostgreSQL versions, andpg_controldatais not in the sidecar image
(containers/Dockerfile.sidecarcopieslibpq5and its dependencies into a
distroless image, not the PostgreSQL client tools). - Passing the size in the CNPG-I WAL restore request: a protocol change, for
information the restored file already holds.
I have this change ready in internal/cnpgi/common/wal.go, with tests of the names at
4MB, 16MB, 64MB and 1GB, of the unknown size, and of the size learned from a restored
file, and can open a PR if the approach works for you.
Related
- #516 (restore breaks with
wal.maxParallel: 8): it does not say which WAL segment
size its clusters use. If it is not 16MB, this may be its cause; I have not confirmed
that. - #1150 (open) adds another caller of
gatherWALFilesToRestore(prefetchAhead),
which would need the same segment size. - The operator's in-tree WAL restore has the same call:
internal/cmd/manager/walrestore/cmd.go:483
in cloudnative-pg.
Workaround
Until a fix ships, keeping wal.maxParallel at 1 on clusters whose WAL segment size
is not 16MB avoids the problem.
- Lingua principale
- Go
- Stelle
- 214
- Fork
- 91
- Merge medio
- 2g 11h
- PR unite (30g)
- 38
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di cloudnative-pg/plugin-barman-cloud
-
Cluster never reconciles when created before its ObjectStore: requeue without RequeueAfter is droppedForse già presa @shanurwan l’ha presa 17 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
cloudnative-pg/plugin-barman-cloud#1236 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
cloudnative-pg/plugin-barman-cloud#1117 · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
Add RBAC aggregation labels to ObjectStore editor/viewer ClusterRolesForse già presa @stefanpeknik l’ha presa 76 giorni fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
cloudnative-pg/plugin-barman-cloud#1102 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 47/100
cloudnative-pg/plugin-barman-cloud#1268 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 55/100
cloudnative-pg/plugin-barman-cloud#1263 ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di cloudnative-pg/plugin-barman-cloud
Issue simili
-
feat(arithmetization): update `arithmetization` to ZkC version `v1.2.38`Forse già presa @DavePearce l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
LFDT-Lineth/lineth-monorepo#4679 ·
I maintainer di solito rispondono entro 1 giorno
-
macOS: MixMusic stalls as the GPU times out drawing frames, and the media keys fail to register at every startForse già presa @fzn0x l’ha presa oggi. Apertabug priority: high
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
funding: failure logs print `<nil>` error and zero channel IDForse già presa @litbot-9000 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
lightningnetwork/lnd#11676 ·
I maintainer di solito rispondono entro 1 giorno
-
effort:s impact:low raised-by:reviewer risk:low
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
medici-finance/assay#8050 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno