-
Notifications
You must be signed in to change notification settings - Fork 356
/
walredo.rs
1239 lines (1091 loc) · 45.7 KB
/
walredo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//!
//! WAL redo. This service runs PostgreSQL in a special wal_redo mode
//! to apply given WAL records over an old page image and return new
//! page image.
//!
//! We rely on Postgres to perform WAL redo for us. We launch a
//! postgres process in special "wal redo" mode that's similar to
//! single-user mode. We then pass the previous page image, if any,
//! and all the WAL records we want to apply, to the postgres
//! process. Then we get the page image back. Communication with the
//! postgres process happens via stdin/stdout
//!
//! See pgxn/neon_walredo/walredoproc.c for the other side of
//! this communication.
//!
//! The Postgres process is assumed to be secure against malicious WAL
//! records. It achieves it by dropping privileges before replaying
//! any WAL records, so that even if an attacker hijacks the Postgres
//! process, he cannot escape out of it.
//!
use anyhow::Context;
use byteorder::{ByteOrder, LittleEndian};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use serde::Serialize;
use std::process::Stdio;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use tracing::*;
use utils::crashsafe::path_with_suffix_extension;
use utils::{bin_ser::BeSer, id::TenantId, lsn::Lsn};
use crate::metrics::{
WAL_REDO_BYTES_HISTOGRAM, WAL_REDO_RECORDS_HISTOGRAM, WAL_REDO_RECORD_COUNTER, WAL_REDO_TIME,
};
use crate::pgdatadir_mapping::{key_to_rel_block, key_to_slru_block};
use crate::repository::Key;
use crate::task_mgr::BACKGROUND_RUNTIME;
use crate::walrecord::NeonWalRecord;
use crate::{config::PageServerConf, TEMP_FILE_SUFFIX};
use pageserver_api::reltag::{RelTag, SlruKind};
use postgres_ffi::pg_constants;
use postgres_ffi::relfile_utils::VISIBILITYMAP_FORKNUM;
use postgres_ffi::v14::nonrelfile_utils::{
mx_offset_to_flags_bitshift, mx_offset_to_flags_offset, mx_offset_to_member_offset,
transaction_id_set_status,
};
use postgres_ffi::BLCKSZ;
///
/// `RelTag` + block number (`blknum`) gives us a unique id of the page in the cluster.
///
/// In Postgres `BufferTag` structure is used for exactly the same purpose.
/// [See more related comments here](https://github.com/postgres/postgres/blob/99c5852e20a0987eca1c38ba0c09329d4076b6a0/src/include/storage/buf_internals.h#L91).
///
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize)]
pub struct BufferTag {
pub rel: RelTag,
pub blknum: u32,
}
impl BufferTag {
/// Serialized length
pub const LEN: u32 = RelTag::LEN + 4;
}
///
/// WAL Redo Manager is responsible for replaying WAL records.
///
/// Callers use the WAL redo manager through this abstract interface,
/// which makes it easy to mock it in tests.
pub trait WalRedoManager: Send + Sync {
/// Apply some WAL records.
///
/// The caller passes an old page image, and WAL records that should be
/// applied over it. The return value is a new page image, after applying
/// the reords.
fn request_redo(
&self,
key: Key,
lsn: Lsn,
base_img: Option<Bytes>,
records: Vec<(Lsn, NeonWalRecord)>,
pg_version: u32,
) -> Result<Bytes, WalRedoError>;
}
///
/// This is the real implementation that uses a Postgres process to
/// perform WAL replay. Only one thread can use the process at a time,
/// that is controlled by the Mutex. In the future, we might want to
/// launch a pool of processes to allow concurrent replay of multiple
/// records.
///
pub struct PostgresRedoManager {
conf: &'static PageServerConf,
handle: Handle,
}
/// Can this request be served by neon redo functions
/// or we need to pass it to wal-redo postgres process?
fn can_apply_in_neon(rec: &NeonWalRecord) -> bool {
// Currently, we don't have bespoken Rust code to replay any
// Postgres WAL records. But everything else is handled in neon.
#[allow(clippy::match_like_matches_macro)]
match rec {
NeonWalRecord::Postgres {
will_init: _,
rec: _,
} => false,
_ => true,
}
}
/// An error happened in WAL redo
#[derive(Debug, thiserror::Error)]
pub enum WalRedoError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("cannot perform WAL redo now")]
InvalidState,
#[error("cannot perform WAL redo for this request")]
InvalidRequest,
#[error("cannot perform WAL redo for this record")]
InvalidRecord,
}
///
/// Public interface of WAL redo manager
///
impl WalRedoManager for PostgresRedoManager {
///
/// Request the WAL redo manager to apply some WAL records
///
/// The WAL redo is handled by a separate thread, so this just sends a request
/// to the thread and waits for response.
///
fn request_redo(
&self,
key: Key,
lsn: Lsn,
base_img: Option<Bytes>,
records: Vec<(Lsn, NeonWalRecord)>,
pg_version: u32,
) -> Result<Bytes, WalRedoError> {
if records.is_empty() {
error!("invalid WAL redo request with no records");
return Err(WalRedoError::InvalidRequest);
}
// convert it to an arc to avoid cloning it on batches
let records: Arc<[(Lsn, NeonWalRecord)]> = records.into();
let mut img: Option<Bytes> = base_img;
let mut batch_neon = can_apply_in_neon(&records[0].1);
let mut batch_start = 0;
for i in 1..records.len() {
let rec_neon = can_apply_in_neon(&records[i].1);
if rec_neon != batch_neon {
let result = if batch_neon {
self.apply_batch_neon(key, lsn, img, &records[batch_start..i])
} else {
self.apply_batch_postgres(
key,
lsn,
img,
&records,
(batch_start..i).into(),
self.conf.wal_redo_timeout,
pg_version,
)
};
img = Some(result?);
batch_neon = rec_neon;
batch_start = i;
}
}
// last batch
if batch_neon {
self.apply_batch_neon(key, lsn, img, &records[batch_start..])
} else {
self.apply_batch_postgres(
key,
lsn,
img,
&records,
(batch_start..).into(),
self.conf.wal_redo_timeout,
pg_version,
)
}
}
}
impl PostgresRedoManager {
///
/// Create a new PostgresRedoManager.
///
pub fn new(conf: &'static PageServerConf, tenant_id: TenantId) -> PostgresRedoManager {
// The actual process is launched lazily, on first request.
let (handle, fut) = tokio_postgres_redo(conf, tenant_id, 14);
BACKGROUND_RUNTIME.spawn(fut);
PostgresRedoManager { conf, handle }
}
///
/// Process one request for WAL redo using wal-redo postgres
///
#[allow(clippy::too_many_arguments)]
fn apply_batch_postgres(
&self,
key: Key,
lsn: Lsn,
base_img: Option<Bytes>,
records: &Arc<[(Lsn, NeonWalRecord)]>,
records_range: SliceRange,
wal_redo_timeout: Duration,
_pg_version: u32,
) -> Result<Bytes, WalRedoError> {
let (rel, blknum) = key_to_rel_block(key).or(Err(WalRedoError::InvalidRecord))?;
let start_time = Instant::now();
// Relational WAL records are applied using wal-redo-postgres
let buf_tag = BufferTag { rel, blknum };
let record_count = records_range.sub_slice(records).len() as u64;
let result = BACKGROUND_RUNTIME
.block_on(self.handle.request_redo(Request {
target: buf_tag,
base_img,
records: records.clone(),
records_range,
timeout: wal_redo_timeout,
}))
.map_err(|e| WalRedoError::IoError(std::io::Error::new(std::io::ErrorKind::Other, e)));
let duration = start_time.elapsed();
let len = records.len();
let nbytes = records.iter().fold(0, |acumulator, record| {
acumulator
+ match &record.1 {
NeonWalRecord::Postgres { rec, .. } => rec.len(),
_ => unreachable!("Only PostgreSQL records are accepted in this batch"),
}
});
WAL_REDO_TIME.observe(duration.as_secs_f64());
WAL_REDO_RECORDS_HISTOGRAM.observe(len as f64);
WAL_REDO_BYTES_HISTOGRAM.observe(nbytes as f64);
WAL_REDO_RECORD_COUNTER.inc_by(record_count);
debug!(
"postgres applied {} WAL records ({} bytes) in {} us to reconstruct page image at LSN {}",
len,
nbytes,
duration.as_micros(),
lsn
);
result
}
///
/// Process a batch of WAL records using bespoken Neon code.
///
fn apply_batch_neon(
&self,
key: Key,
lsn: Lsn,
base_img: Option<Bytes>,
records: &[(Lsn, NeonWalRecord)],
) -> Result<Bytes, WalRedoError> {
let start_time = Instant::now();
let mut page = BytesMut::new();
if let Some(fpi) = base_img {
// If full-page image is provided, then use it...
page.extend_from_slice(&fpi[..]);
} else {
// All the current WAL record types that we can handle require a base image.
error!("invalid neon WAL redo request with no base image");
return Err(WalRedoError::InvalidRequest);
}
// Apply all the WAL records in the batch
for (record_lsn, record) in records.iter() {
self.apply_record_neon(key, &mut page, *record_lsn, record)?;
}
// Success!
let end_time = Instant::now();
let duration = end_time.duration_since(start_time);
WAL_REDO_TIME.observe(duration.as_secs_f64());
debug!(
"neon applied {} WAL records in {} ms to reconstruct page image at LSN {}",
records.len(),
duration.as_micros(),
lsn
);
Ok(page.freeze())
}
fn apply_record_neon(
&self,
key: Key,
page: &mut BytesMut,
_record_lsn: Lsn,
record: &NeonWalRecord,
) -> Result<(), WalRedoError> {
match record {
NeonWalRecord::Postgres {
will_init: _,
rec: _,
} => {
error!("tried to pass postgres wal record to neon WAL redo");
return Err(WalRedoError::InvalidRequest);
}
NeonWalRecord::ClearVisibilityMapFlags {
new_heap_blkno,
old_heap_blkno,
flags,
} => {
// sanity check that this is modifying the correct relation
let (rel, blknum) = key_to_rel_block(key).or(Err(WalRedoError::InvalidRecord))?;
assert!(
rel.forknum == VISIBILITYMAP_FORKNUM,
"ClearVisibilityMapFlags record on unexpected rel {}",
rel
);
if let Some(heap_blkno) = *new_heap_blkno {
// Calculate the VM block and offset that corresponds to the heap block.
let map_block = pg_constants::HEAPBLK_TO_MAPBLOCK(heap_blkno);
let map_byte = pg_constants::HEAPBLK_TO_MAPBYTE(heap_blkno);
let map_offset = pg_constants::HEAPBLK_TO_OFFSET(heap_blkno);
// Check that we're modifying the correct VM block.
assert!(map_block == blknum);
// equivalent to PageGetContents(page)
let map = &mut page[pg_constants::MAXALIGN_SIZE_OF_PAGE_HEADER_DATA..];
map[map_byte as usize] &= !(flags << map_offset);
}
// Repeat for 'old_heap_blkno', if any
if let Some(heap_blkno) = *old_heap_blkno {
let map_block = pg_constants::HEAPBLK_TO_MAPBLOCK(heap_blkno);
let map_byte = pg_constants::HEAPBLK_TO_MAPBYTE(heap_blkno);
let map_offset = pg_constants::HEAPBLK_TO_OFFSET(heap_blkno);
assert!(map_block == blknum);
let map = &mut page[pg_constants::MAXALIGN_SIZE_OF_PAGE_HEADER_DATA..];
map[map_byte as usize] &= !(flags << map_offset);
}
}
// Non-relational WAL records are handled here, with custom code that has the
// same effects as the corresponding Postgres WAL redo function.
NeonWalRecord::ClogSetCommitted { xids, timestamp } => {
let (slru_kind, segno, blknum) =
key_to_slru_block(key).or(Err(WalRedoError::InvalidRecord))?;
assert_eq!(
slru_kind,
SlruKind::Clog,
"ClogSetCommitted record with unexpected key {}",
key
);
for &xid in xids {
let pageno = xid as u32 / pg_constants::CLOG_XACTS_PER_PAGE;
let expected_segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
let expected_blknum = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
// Check that we're modifying the correct CLOG block.
assert!(
segno == expected_segno,
"ClogSetCommitted record for XID {} with unexpected key {}",
xid,
key
);
assert!(
blknum == expected_blknum,
"ClogSetCommitted record for XID {} with unexpected key {}",
xid,
key
);
transaction_id_set_status(
xid,
pg_constants::TRANSACTION_STATUS_COMMITTED,
page,
);
}
// Append the timestamp
if page.len() == BLCKSZ as usize + 8 {
page.truncate(BLCKSZ as usize);
}
if page.len() == BLCKSZ as usize {
page.extend_from_slice(×tamp.to_be_bytes());
} else {
warn!(
"CLOG blk {} in seg {} has invalid size {}",
blknum,
segno,
page.len()
);
}
}
NeonWalRecord::ClogSetAborted { xids } => {
let (slru_kind, segno, blknum) =
key_to_slru_block(key).or(Err(WalRedoError::InvalidRecord))?;
assert_eq!(
slru_kind,
SlruKind::Clog,
"ClogSetAborted record with unexpected key {}",
key
);
for &xid in xids {
let pageno = xid as u32 / pg_constants::CLOG_XACTS_PER_PAGE;
let expected_segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
let expected_blknum = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
// Check that we're modifying the correct CLOG block.
assert!(
segno == expected_segno,
"ClogSetAborted record for XID {} with unexpected key {}",
xid,
key
);
assert!(
blknum == expected_blknum,
"ClogSetAborted record for XID {} with unexpected key {}",
xid,
key
);
transaction_id_set_status(xid, pg_constants::TRANSACTION_STATUS_ABORTED, page);
}
}
NeonWalRecord::MultixactOffsetCreate { mid, moff } => {
let (slru_kind, segno, blknum) =
key_to_slru_block(key).or(Err(WalRedoError::InvalidRecord))?;
assert_eq!(
slru_kind,
SlruKind::MultiXactOffsets,
"MultixactOffsetCreate record with unexpected key {}",
key
);
// Compute the block and offset to modify.
// See RecordNewMultiXact in PostgreSQL sources.
let pageno = mid / pg_constants::MULTIXACT_OFFSETS_PER_PAGE as u32;
let entryno = mid % pg_constants::MULTIXACT_OFFSETS_PER_PAGE as u32;
let offset = (entryno * 4) as usize;
// Check that we're modifying the correct multixact-offsets block.
let expected_segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
let expected_blknum = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
assert!(
segno == expected_segno,
"MultiXactOffsetsCreate record for multi-xid {} with unexpected key {}",
mid,
key
);
assert!(
blknum == expected_blknum,
"MultiXactOffsetsCreate record for multi-xid {} with unexpected key {}",
mid,
key
);
LittleEndian::write_u32(&mut page[offset..offset + 4], *moff);
}
NeonWalRecord::MultixactMembersCreate { moff, members } => {
let (slru_kind, segno, blknum) =
key_to_slru_block(key).or(Err(WalRedoError::InvalidRecord))?;
assert_eq!(
slru_kind,
SlruKind::MultiXactMembers,
"MultixactMembersCreate record with unexpected key {}",
key
);
for (i, member) in members.iter().enumerate() {
let offset = moff + i as u32;
// Compute the block and offset to modify.
// See RecordNewMultiXact in PostgreSQL sources.
let pageno = offset / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32;
let memberoff = mx_offset_to_member_offset(offset);
let flagsoff = mx_offset_to_flags_offset(offset);
let bshift = mx_offset_to_flags_bitshift(offset);
// Check that we're modifying the correct multixact-members block.
let expected_segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
let expected_blknum = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
assert!(
segno == expected_segno,
"MultiXactMembersCreate record for offset {} with unexpected key {}",
moff,
key
);
assert!(
blknum == expected_blknum,
"MultiXactMembersCreate record for offset {} with unexpected key {}",
moff,
key
);
let mut flagsval = LittleEndian::read_u32(&page[flagsoff..flagsoff + 4]);
flagsval &= !(((1 << pg_constants::MXACT_MEMBER_BITS_PER_XACT) - 1) << bshift);
flagsval |= member.status << bshift;
LittleEndian::write_u32(&mut page[flagsoff..flagsoff + 4], flagsval);
LittleEndian::write_u32(&mut page[memberoff..memberoff + 4], member.xid);
}
}
}
Ok(())
}
}
/// Serializes the wal redo request into `buffers` with the help of scratch buffer `scratch`.
///
/// The request is combination of `B + P + A* + G`.
///
/// Compared to [`build_vectored_messages`], this implementation builds at most 3 messages if the
/// base version of page is included (it's never copied to conserve the "scratch" space).
fn build_messages(
target: BufferTag,
base_img: Option<Bytes>,
records: &[(Lsn, NeonWalRecord)],
scratch: &mut BytesMut,
buffers: &mut BufQueue,
) {
target.ser_into(&mut scratch.writer()).unwrap();
let tag = scratch.split().freeze();
build_begin_message(&tag, scratch);
if let Some(page) = base_img {
assert_eq!(page.len(), 8192);
build_push_page_header(&tag, scratch);
let out = scratch.split().freeze();
buffers.push(out);
buffers.push(page);
}
for (end_lsn, record) in records {
let (_will_init, rec) = match record {
NeonWalRecord::Postgres { will_init, rec } => (will_init, rec),
_ => unreachable!(),
};
build_apply_record_header(end_lsn, rec.len() as u32, scratch);
buffers.push(scratch.split().freeze());
buffers.push(rec.clone());
}
build_get_page_message(&tag, scratch);
let out = scratch.split().freeze();
buffers.push(out);
}
/// Compared to [`build_messages`] builds many small messages and aiming for vectored write
/// handling the gathering of the already allocated records.
fn build_vectored_messages(
target: BufferTag,
base_img: Option<Bytes>,
records: &[(Lsn, NeonWalRecord)],
scratch: &mut BytesMut,
buffers: &mut BufQueue,
) {
let tag = {
target.ser_into(&mut scratch.writer()).unwrap();
scratch.split().freeze()
};
build_begin_message(&tag, scratch);
if let Some(page) = base_img {
build_push_page_header(&tag, scratch);
buffers.push(scratch.split().freeze());
buffers.push(page);
}
for (end_lsn, record) in records {
let rec = match record {
NeonWalRecord::Postgres { rec, .. } => rec,
_ => unreachable!(),
};
let record_len = rec.len() as u32;
build_apply_record_header(end_lsn, record_len, scratch);
buffers.push(scratch.split().freeze());
buffers.push(rec.clone());
}
build_get_page_message(&tag, scratch);
buffers.push(scratch.split().freeze());
}
fn build_begin_message(tag: &Bytes, scratch: &mut BytesMut) {
scratch.put_u8(b'B');
scratch.put_u32(4 + BufferTag::LEN);
scratch.put(tag.clone());
}
fn build_push_page_header(tag: &Bytes, scratch: &mut BytesMut) {
let page_len = 8192;
scratch.put_u8(b'P');
scratch.put_u32(4 + BufferTag::LEN + page_len);
scratch.put(tag.clone());
}
fn build_apply_record_header(end_lsn: &Lsn, record_len: u32, scratch: &mut BytesMut) {
scratch.put_u8(b'A');
scratch.put_u32(4 + 8 + record_len);
scratch.put_u64(end_lsn.0);
}
fn build_get_page_message(tag: &Bytes, scratch: &mut BytesMut) {
scratch.put_u8(b'G');
scratch.put_u32(4 + BufferTag::LEN);
scratch.put(tag.clone());
}
/// Copypasted for tokio, need #[cfg(unix)]
trait TokioCloseFileDescriptors {
fn close_fds(&mut self) -> &mut tokio::process::Command;
}
impl TokioCloseFileDescriptors for tokio::process::Command {
fn close_fds(&mut self) -> &mut tokio::process::Command {
unsafe {
self.pre_exec(|| {
// SAFETY: Code executed inside pre_exec should have async-signal-safety,
// which means it should be safe to execute inside a signal handler.
// The precise meaning depends on platform. See `man signal-safety`
// for the linux definition.
//
// The set_fds_cloexec_threadsafe function is documented to be
// async-signal-safe.
//
// Aside from this function, the rest of the code is re-entrant and
// doesn't make any syscalls. We're just passing constants.
//
// NOTE: It's easy to indirectly cause a malloc or lock a mutex,
// which is not async-signal-safe. Be careful.
close_fds::set_fds_cloexec_threadsafe(3, &[]);
Ok(())
})
}
}
}
fn tokio_postgres_redo(
conf: &'static PageServerConf,
tenant_id: TenantId,
pg_version: u32,
) -> (
Handle,
impl std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
) {
use tokio::io::AsyncWrite;
// precise sizing for this would be pipe size divided by our average redo request
let expected_inflight = 32;
let (tx, mut rx) = tokio::sync::mpsc::channel::<Payload>(expected_inflight);
let ipc = async move {
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
let mut buffers = BufQueue::default();
let mut scratch = BytesMut::with_capacity(
// without vectoring we aim at 3 messages: begin, page, records + get_page,
// with vectoring this will be very much enough
1024 * 8 * 3,
);
// loop to handle wal-redo process failing in between. additionally tenant_mgr expects that
// walredo does not create the temporary directory until we get the first redo request, so
// postpone creation until we get the first one.
while let Some(first) = rx.recv().await {
// make sure we dont have anything remaining from a past partial write
buffers.clear();
let mut child = launch_walredo(conf, tenant_id, pg_version).await?;
let pid = child
.id()
.expect("pid is present before killing the process");
info!("Launched wal-redo process for {tenant_id}: {pid}");
// we send the external the request in different commands
let mut stdin = child.stdin.take().expect("not taken yet");
// stdout is used to communicate the resulting page
let mut stdout = child.stdout.take().expect("not taken yet");
// used to communicate hopefully utf-8 log messages
let stderr = child.stderr.take().expect("not taken yet");
let mut stderr = tokio::io::BufReader::new(stderr);
let (result_txs, result_rx) = tokio::sync::mpsc::channel(expected_inflight);
let have_vectored_stdin = stdin.is_write_vectored();
let stdin_task = async {
// cancellation safety is probably not an issue, vecdeque could be used to capture
// oneshot senders
let result_txs = result_txs;
let mut buffered = Some(first);
loop {
let (request, response) = {
// TODO: could we somehow manage to keep the request in case we need to
// restart the process? see https://github.com/neondatabase/neon/issues/1700
let next = buffered.take();
let next = if next.is_none() {
rx.recv().await
} else {
next
};
match next {
Some(t) => t,
None => break,
}
};
let records = request.records_range.sub_slice(&request.records);
if have_vectored_stdin {
build_vectored_messages(
request.target,
request.base_img,
records,
&mut scratch,
&mut buffers,
);
} else {
build_messages(
request.target,
request.base_img,
records,
&mut scratch,
&mut buffers,
);
}
let write_res = async {
while buffers.has_remaining() {
futures::future::poll_fn(|cx| {
tokio_util::io::poll_write_buf(
std::pin::Pin::new(&mut stdin),
cx,
&mut buffers,
)
})
.await?;
}
// in general flush is not needed, does nothing on pipes
stdin.flush().await
}
.await;
match write_res {
Ok(()) => {}
Err(e) => {
drop(
response.send(
Err(anyhow::Error::new(e))
.context("Failed to write request to wal-redo"),
),
);
// we can still continue processing already pipelined tasks, if any.
// the stdout task will exit upon seeing we've dropped the result_txs.
return Ok(());
}
}
// because we are pipelining, "start counting" the timeout only after we have
// written everything.
let send_res = result_txs
.send((response, tokio::time::Instant::now() + request.timeout))
.await;
if let Err(tokio::sync::mpsc::error::SendError((response, _))) = send_res {
// write side is already gone, resolve this with a specific error
drop(response.send(Err(anyhow::anyhow!("Failed to read from wal-redo"))));
return Err("stdin: failed to send the response over to read task");
}
}
// the Handle or the request queue sender have been dropped; return Ok(()) to keep
// processing any of already pipelined requests
Ok(())
}
.instrument(info_span!("walredo-stdin"));
#[derive(Debug, thiserror::Error)]
enum StdoutTaskError {
#[error("read failed: {0}")]
ReadFailed(std::io::Error),
#[error("external process stdout was closed")]
StdoutClosed,
#[error("reading the page timed out")]
ReadTimeout,
}
let stdout_task = async {
// TODO: do these pages are put it in a cache? if not, could use a larger buffer
let mut result_rx = result_rx;
let mut page_buf = BytesMut::with_capacity(8192);
while let Some((completion, timeout_at)) = result_rx.recv().await {
let read_page = async {
loop {
let read = stdout
.read_buf(&mut page_buf)
.await
.map_err(StdoutTaskError::ReadFailed)?;
if read == 0 {
return Err(StdoutTaskError::StdoutClosed);
}
if page_buf.remaining() < 8192 {
continue;
}
let page = page_buf.split().freeze();
return Ok(page);
}
};
let res = tokio::time::timeout_at(timeout_at, read_page)
.await
.map_err(|_elapsed| StdoutTaskError::ReadTimeout)
.and_then(|x| x);
match res {
Ok(page) => {
// we don't care about the result, because the caller could be gone
drop(completion.send(Ok(page)));
page_buf.reserve(8192);
}
Err(StdoutTaskError::ReadFailed(e)) => {
drop(
completion
.send(Err(e).context("Failed to read response from wal-redo")),
);
return Err("failed to read from wal-redo stdout");
}
Err(StdoutTaskError::StdoutClosed) => {
drop(
completion
.send(Err(anyhow::anyhow!("wal-redo process closed stdout"))),
);
return Err("failed to read from wal-redo: closed stdout");
}
Err(StdoutTaskError::ReadTimeout) => {
drop(completion.send(Err(anyhow::anyhow!(
"Timed out while waiting for the page"
))));
return Err("reading page timed out");
}
}
}
// in a graceful shutdown, this needs to be an Err to take down the stderr task as
// well.
Err::<(), _>("stdout: all requests processed, ready for shutdown")
}
.instrument(info_span!("walredo-stdout"));
let stderr_task = async {
let mut buffer = Vec::new();
loop {
buffer.clear();
match stderr.read_until(b'\n', &mut buffer).await {
Ok(0) => return Err::<(), _>("stderr: closed"),
Ok(read) => {
let message = String::from_utf8_lossy(&buffer[..read]);
error!("wal-redo-process: {}", message.trim());
}
Err(e) => {
error!("reading stderr failed: {e}");
return Err("stderr: read failed");
}
}
}
};
async {
// ignore the result, it is always Err from one of the tasks, upon which we stop
// advancing the others
let reason = tokio::try_join!(stdin_task, stdout_task, stderr_task);
debug!("wal-redo process tasks exited: {reason:?}");
// dont care if the child has already exited
drop(child.start_kill());
match child.wait().await {
Ok(status) => {
if status.success() {
debug!(?status, "wal-redo process exited successfully");
} else {
warn!(?status, "wal-redo process did not exit successfully");
}
}
Err(e) => {
error!("failed to wait for child process to exit: {e}");
}
}
}
.instrument(info_span!("walredo", pid, %tenant_id))
.await
}
info!(tenant_id = %tenant_id, "wal-redo task exiting");
Ok(())
};
(Handle { tx }, ipc)
}
async fn launch_walredo(
conf: &PageServerConf,
tenant_id: TenantId,
pg_version: u32,
) -> anyhow::Result<tokio::process::Child> {
let datadir = path_with_suffix_extension(
conf.tenant_path(&tenant_id).join("wal-redo-datadir"),
TEMP_FILE_SUFFIX,
);
info!("removing existing data directory: {}", datadir.display());
match tokio::fs::remove_dir_all(&datadir).await {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
other => other.with_context(|| {
format!(
"Failed to cleanup existing wal-redo-datadir at {}",
datadir.display()
)
})?,
}
let pg_bin_dir_path = conf.pg_bin_dir(pg_version)?;
let pg_lib_dir_path = conf.pg_lib_dir(pg_version)?;
info!("running initdb in {}", datadir.display());
let initdb = tokio::process::Command::new(pg_bin_dir_path.join("initdb"))
.arg("-D")
.arg(&datadir)
.arg("-N")
.env_clear()
.env("LD_LIBRARY_PATH", &pg_lib_dir_path)
.env("DYLD_LIBRARY_PATH", &pg_lib_dir_path)
.close_fds()
.output()
.await
.context("Failed to execute initdb for wal-redo")?;
anyhow::ensure!(
initdb.status.success(),
"initdb failed\nstdout: {}\nstderr:\n {}",
String::from_utf8_lossy(&initdb.stdout),
String::from_utf8_lossy(&initdb.stderr)
);
info!("starting walredo process");
tokio::process::Command::new(pg_bin_dir_path.join("postgres"))
.arg("--wal-redo")
.stdin(Stdio::piped())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.env_clear()
.env("LD_LIBRARY_PATH", &pg_lib_dir_path)
.env("DYLD_LIBRARY_PATH", &pg_lib_dir_path)
.env("PGDATA", &datadir)
.close_fds()
// best effort is probably good enough for us
.kill_on_drop(true)
.spawn()