WAL prefetch (`wal.maxParallel` > 1) names the wrong segments when the WAL segment size is not 16MB
Mantenedores costumam responder em até 2 dias
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Facilidade para iniciantes
- 45/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- Go
- Estrelas
- 214
- Forks
- 91
- Merge médio
- 2d 11h
- PRs com merge (30d)
- 38
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de cloudnative-pg/plugin-barman-cloud
-
Cluster never reconciles when created before its ObjectStore: requeue without RequeueAfter is droppedTalvez já em andamento @shanurwan assumiu há 17 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
cloudnative-pg/plugin-barman-cloud#1236 ·
Mantenedores costumam responder em até 2 dias
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
cloudnative-pg/plugin-barman-cloud#1117 · 1 reação ·
Mantenedores costumam responder em até 2 dias
-
Add RBAC aggregation labels to ObjectStore editor/viewer ClusterRolesTalvez já em andamento @stefanpeknik assumiu há 76 dias. Aberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 82/100
cloudnative-pg/plugin-barman-cloud#1102 ·
Mantenedores costumam responder em até 2 dias
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 47/100
cloudnative-pg/plugin-barman-cloud#1268 ·
Mantenedores costumam responder em até 2 dias
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 55/100
cloudnative-pg/plugin-barman-cloud#1263 ·
Mantenedores costumam responder em até 2 dias
Todas as issues de cloudnative-pg/plugin-barman-cloud
Issues semelhantes
-
[Bug]: Azure SAS signing uses local time instead of UTC, shifting validity window on non-UTC hostsTalvez já em andamento @huanghaoyuanhhy assumiu hoje. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
zilliztech/milvus-backup#1418 ·
Mantenedores costumam responder em até 1 dia
-
good first issue
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 86/100
eugenioenko/ttt#1909 ·
Mantenedores costumam responder em até 1 dia
-
ColumnIndex max truncation returns a value smaller than the real max when the prefix is all 0xFFAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 84/100
parquet-go/parquet-go#819 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 83/100
-
feat(arithmetization): update `arithmetization` to ZkC version `v1.2.38`Talvez já em andamento @DavePearce assumiu hoje. Aberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 78/100
LFDT-Lineth/lineth-monorepo#4679 ·
Mantenedores costumam responder em até 1 dia