WAL prefetch (`wal.maxParallel` > 1) names the wrong segments when the WAL segment size is not 16MB

オープン
#1,259 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
go, postgresql
領域
backend, databases

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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);
    :158 WalSegmentsPerFile and :170-197 NextSegments roll to the next log
    after segment 0xFFFFFFFF / 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 maxParallel does not divide 64 (3, 5, 6, 7, ...), the last batch of every log
    crosses its end: with maxParallel: 5 the batch that starts at ...3C asks for
    ...40; with maxParallel: 3 the batch at ...3F asks for ...40 and ...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:

  1. Create a Cluster with bootstrap.initdb.walSegmentSize: 64 and this plugin as
    its WAL archive, with wal.maxParallel: 5 on the ObjectStore.
  2. Write more than 64 segments of WAL (4GB at 64MB per segment) so that the
    archive holds 000000010000000000000000 to past 000000010000000100000000.
  3. Restore WAL from the archive through the plugin: either a new Cluster
    bootstrapped with bootstrap.recovery from it, or a replica (or the designated
    primary of a replica cluster whose source has streaming connection parameters)
    that catches up from the archive.
  4. Expected with v0.16.0 or main: at each log boundary, the invocation that requests
    ...3C also 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 ...3F still come from the spool; the next invocation that misses
    it, the restore of 000000010000000100000000, which is in the archive, fails with
    the end-of-WAL-stream error. PostgreSQL then tries streaming replication before it
    calls restore_command again.

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
    recovery or pg_basebackup from a source of another size, and for replica
    clusters.
  • Parsing global/pg_control in PGDATA: its layout differs between
    PostgreSQL versions, and pg_controldata is not in the sidecar image
    (containers/Dockerfile.sidecar copies libpq5 and 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.

主要言語
Go
スター
214
フォーク
91
平均マージ
2日 11時間
マージ済み PR(30日)
38

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudnative-pg/plugin-barman-cloud のほかの issue

cloudnative-pg/plugin-barman-cloud の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。