mirror of
https://github.com/neondatabase/neon.git
synced 2026-03-21 09:10:37 +00:00
Compare commits
6 Commits
RemoteExte
...
walredo-cl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17ca2d51d2 | ||
|
|
e72e14e6cf | ||
|
|
f0f6daad23 | ||
|
|
8f71bfe8f9 | ||
|
|
f644009b5c | ||
|
|
669a939fff |
@@ -17,9 +17,11 @@
|
|||||||
//! records. It achieves it by dropping privileges before replaying
|
//! records. It achieves it by dropping privileges before replaying
|
||||||
//! any WAL records, so that even if an attacker hijacks the Postgres
|
//! any WAL records, so that even if an attacker hijacks the Postgres
|
||||||
//! process, he cannot escape out of it.
|
//! process, he cannot escape out of it.
|
||||||
//!
|
|
||||||
use byteorder::{ByteOrder, LittleEndian};
|
mod nonrel;
|
||||||
use bytes::{Buf, BufMut, Bytes, BytesMut};
|
mod request;
|
||||||
|
|
||||||
|
use bytes::Bytes;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use log::*;
|
use log::*;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
@@ -37,21 +39,12 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|||||||
use tokio::process::{ChildStdin, ChildStdout, Command};
|
use tokio::process::{ChildStdin, ChildStdout, Command};
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use zenith_metrics::{register_histogram, register_int_counter, Histogram, IntCounter};
|
use zenith_metrics::{register_histogram, register_int_counter, Histogram, IntCounter};
|
||||||
use zenith_utils::bin_ser::BeSer;
|
|
||||||
use zenith_utils::lsn::Lsn;
|
use zenith_utils::lsn::Lsn;
|
||||||
use zenith_utils::zid::ZTenantId;
|
use zenith_utils::zid::ZTenantId;
|
||||||
|
|
||||||
use crate::relish::*;
|
use crate::relish::*;
|
||||||
use crate::repository::WALRecord;
|
use crate::repository::WALRecord;
|
||||||
use crate::waldecoder::XlMultiXactCreate;
|
|
||||||
use crate::waldecoder::XlXactParsedRecord;
|
|
||||||
use crate::PageServerConf;
|
use crate::PageServerConf;
|
||||||
use postgres_ffi::nonrelfile_utils::mx_offset_to_flags_bitshift;
|
|
||||||
use postgres_ffi::nonrelfile_utils::mx_offset_to_flags_offset;
|
|
||||||
use postgres_ffi::nonrelfile_utils::mx_offset_to_member_offset;
|
|
||||||
use postgres_ffi::nonrelfile_utils::transaction_id_set_status;
|
|
||||||
use postgres_ffi::pg_constants;
|
|
||||||
use postgres_ffi::XLogRecord;
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// `RelTag` + block number (`blknum`) gives us a unique id of the page in the cluster.
|
/// `RelTag` + block number (`blknum`) gives us a unique id of the page in the cluster.
|
||||||
@@ -92,7 +85,7 @@ pub trait WalRedoManager: Send + Sync {
|
|||||||
/// a Repository object without launching the real WAL redo process.
|
/// a Repository object without launching the real WAL redo process.
|
||||||
///
|
///
|
||||||
pub struct DummyRedoManager {}
|
pub struct DummyRedoManager {}
|
||||||
impl crate::walredo::WalRedoManager for DummyRedoManager {
|
impl WalRedoManager for DummyRedoManager {
|
||||||
fn request_redo(
|
fn request_redo(
|
||||||
&self,
|
&self,
|
||||||
_rel: RelishTag,
|
_rel: RelishTag,
|
||||||
@@ -105,7 +98,7 @@ impl crate::walredo::WalRedoManager for DummyRedoManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static TIMEOUT: Duration = Duration::from_secs(20);
|
const TIMEOUT: Duration = Duration::from_secs(20);
|
||||||
|
|
||||||
// Metrics collected on WAL redo operations
|
// Metrics collected on WAL redo operations
|
||||||
//
|
//
|
||||||
@@ -249,166 +242,20 @@ impl PostgresRedoManager {
|
|||||||
process: &mut PostgresRedoProcess,
|
process: &mut PostgresRedoProcess,
|
||||||
request: &WalRedoRequest,
|
request: &WalRedoRequest,
|
||||||
) -> Result<Bytes, WalRedoError> {
|
) -> Result<Bytes, WalRedoError> {
|
||||||
let rel = request.rel;
|
|
||||||
let blknum = request.blknum;
|
|
||||||
let lsn = request.lsn;
|
|
||||||
let base_img = request.base_img.clone();
|
|
||||||
let records = &request.records;
|
|
||||||
|
|
||||||
let nrecords = records.len();
|
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
let apply_result: Result<Bytes, Error>;
|
let apply_result = if let RelishTag::Relation(rel) = request.rel {
|
||||||
if let RelishTag::Relation(rel) = rel {
|
|
||||||
// Relational WAL records are applied using wal-redo-postgres
|
// Relational WAL records are applied using wal-redo-postgres
|
||||||
let buf_tag = BufferTag { rel, blknum };
|
let buf_tag = BufferTag {
|
||||||
apply_result = process.apply_wal_records(buf_tag, base_img, records).await;
|
rel,
|
||||||
|
blknum: request.blknum,
|
||||||
|
};
|
||||||
|
process
|
||||||
|
.apply_wal_records(buf_tag, &request.base_img, &request.records)
|
||||||
|
.await
|
||||||
} else {
|
} else {
|
||||||
// Non-relational WAL records are handled here, with custom code that has the
|
Ok(nonrel::apply_nonrel(request))
|
||||||
// same effects as the corresponding Postgres WAL redo function.
|
};
|
||||||
const ZERO_PAGE: [u8; 8192] = [0u8; 8192];
|
|
||||||
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 {
|
|
||||||
// otherwise initialize page with zeros
|
|
||||||
page.extend_from_slice(&ZERO_PAGE);
|
|
||||||
}
|
|
||||||
// Apply all collected WAL records
|
|
||||||
for record in records {
|
|
||||||
let mut buf = record.rec.clone();
|
|
||||||
|
|
||||||
WAL_REDO_RECORD_COUNTER.inc();
|
|
||||||
|
|
||||||
// 1. Parse XLogRecord struct
|
|
||||||
// FIXME: refactor to avoid code duplication.
|
|
||||||
let xlogrec = XLogRecord::from_bytes(&mut buf);
|
|
||||||
|
|
||||||
//move to main data
|
|
||||||
// TODO probably, we should store some records in our special format
|
|
||||||
// to avoid this weird parsing on replay
|
|
||||||
let skip = (record.main_data_offset - pg_constants::SIZEOF_XLOGRECORD) as usize;
|
|
||||||
if buf.remaining() > skip {
|
|
||||||
buf.advance(skip);
|
|
||||||
}
|
|
||||||
|
|
||||||
if xlogrec.xl_rmid == pg_constants::RM_XACT_ID {
|
|
||||||
// Transaction manager stuff
|
|
||||||
let rec_segno = match rel {
|
|
||||||
RelishTag::Slru { slru, segno } => {
|
|
||||||
assert!(
|
|
||||||
slru == SlruKind::Clog,
|
|
||||||
"Not valid XACT relish tag {:?}",
|
|
||||||
rel
|
|
||||||
);
|
|
||||||
segno
|
|
||||||
}
|
|
||||||
_ => panic!("Not valid XACT relish tag {:?}", rel),
|
|
||||||
};
|
|
||||||
let parsed_xact =
|
|
||||||
XlXactParsedRecord::decode(&mut buf, xlogrec.xl_xid, xlogrec.xl_info);
|
|
||||||
if parsed_xact.info == pg_constants::XLOG_XACT_COMMIT
|
|
||||||
|| parsed_xact.info == pg_constants::XLOG_XACT_COMMIT_PREPARED
|
|
||||||
{
|
|
||||||
transaction_id_set_status(
|
|
||||||
parsed_xact.xid,
|
|
||||||
pg_constants::TRANSACTION_STATUS_COMMITTED,
|
|
||||||
&mut page,
|
|
||||||
);
|
|
||||||
for subxact in &parsed_xact.subxacts {
|
|
||||||
let pageno = *subxact as u32 / pg_constants::CLOG_XACTS_PER_PAGE;
|
|
||||||
let segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
|
|
||||||
let rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
|
|
||||||
// only update xids on the requested page
|
|
||||||
if rec_segno == segno && blknum == rpageno {
|
|
||||||
transaction_id_set_status(
|
|
||||||
*subxact,
|
|
||||||
pg_constants::TRANSACTION_STATUS_COMMITTED,
|
|
||||||
&mut page,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if parsed_xact.info == pg_constants::XLOG_XACT_ABORT
|
|
||||||
|| parsed_xact.info == pg_constants::XLOG_XACT_ABORT_PREPARED
|
|
||||||
{
|
|
||||||
transaction_id_set_status(
|
|
||||||
parsed_xact.xid,
|
|
||||||
pg_constants::TRANSACTION_STATUS_ABORTED,
|
|
||||||
&mut page,
|
|
||||||
);
|
|
||||||
for subxact in &parsed_xact.subxacts {
|
|
||||||
let pageno = *subxact as u32 / pg_constants::CLOG_XACTS_PER_PAGE;
|
|
||||||
let segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
|
|
||||||
let rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
|
|
||||||
// only update xids on the requested page
|
|
||||||
if rec_segno == segno && blknum == rpageno {
|
|
||||||
transaction_id_set_status(
|
|
||||||
*subxact,
|
|
||||||
pg_constants::TRANSACTION_STATUS_ABORTED,
|
|
||||||
&mut page,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if xlogrec.xl_rmid == pg_constants::RM_MULTIXACT_ID {
|
|
||||||
// Multixact operations
|
|
||||||
let info = xlogrec.xl_info & pg_constants::XLR_RMGR_INFO_MASK;
|
|
||||||
if info == pg_constants::XLOG_MULTIXACT_CREATE_ID {
|
|
||||||
let xlrec = XlMultiXactCreate::decode(&mut buf);
|
|
||||||
if let RelishTag::Slru {
|
|
||||||
slru,
|
|
||||||
segno: rec_segno,
|
|
||||||
} = rel
|
|
||||||
{
|
|
||||||
if slru == SlruKind::MultiXactMembers {
|
|
||||||
for i in 0..xlrec.nmembers {
|
|
||||||
let pageno =
|
|
||||||
i / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32;
|
|
||||||
let segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
|
|
||||||
let rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
|
|
||||||
if segno == rec_segno && rpageno == blknum {
|
|
||||||
// update only target block
|
|
||||||
let offset = xlrec.moff + i;
|
|
||||||
let memberoff = mx_offset_to_member_offset(offset);
|
|
||||||
let flagsoff = mx_offset_to_flags_offset(offset);
|
|
||||||
let bshift = mx_offset_to_flags_bitshift(offset);
|
|
||||||
let mut flagsval =
|
|
||||||
LittleEndian::read_u32(&page[flagsoff..flagsoff + 4]);
|
|
||||||
flagsval &= !(((1
|
|
||||||
<< pg_constants::MXACT_MEMBER_BITS_PER_XACT)
|
|
||||||
- 1)
|
|
||||||
<< bshift);
|
|
||||||
flagsval |= xlrec.members[i as usize].status << bshift;
|
|
||||||
LittleEndian::write_u32(
|
|
||||||
&mut page[flagsoff..flagsoff + 4],
|
|
||||||
flagsval,
|
|
||||||
);
|
|
||||||
LittleEndian::write_u32(
|
|
||||||
&mut page[memberoff..memberoff + 4],
|
|
||||||
xlrec.members[i as usize].xid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Multixact offsets SLRU
|
|
||||||
let offs = (xlrec.mid
|
|
||||||
% pg_constants::MULTIXACT_OFFSETS_PER_PAGE as u32
|
|
||||||
* 4) as usize;
|
|
||||||
LittleEndian::write_u32(&mut page[offs..offs + 4], xlrec.moff);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply_result = Ok::<Bytes, Error>(page.freeze());
|
|
||||||
}
|
|
||||||
|
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
|
|
||||||
@@ -416,9 +263,9 @@ impl PostgresRedoManager {
|
|||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"applied {} WAL records in {} ms to reconstruct page image at LSN {}",
|
"applied {} WAL records in {} ms to reconstruct page image at LSN {}",
|
||||||
nrecords,
|
request.records.len(),
|
||||||
duration.as_millis(),
|
duration.as_millis(),
|
||||||
lsn
|
request.lsn
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Err(e) = apply_result {
|
if let Err(e) = apply_result {
|
||||||
@@ -543,12 +390,11 @@ impl PostgresRedoProcess {
|
|||||||
async fn apply_wal_records(
|
async fn apply_wal_records(
|
||||||
&mut self,
|
&mut self,
|
||||||
tag: BufferTag,
|
tag: BufferTag,
|
||||||
base_img: Option<Bytes>,
|
base_img: &Option<Bytes>,
|
||||||
records: &[WALRecord],
|
records: &[WALRecord],
|
||||||
) -> Result<Bytes, std::io::Error> {
|
) -> Result<Bytes, std::io::Error> {
|
||||||
|
let stdin = &mut self.stdin;
|
||||||
let stdout = &mut self.stdout;
|
let stdout = &mut self.stdout;
|
||||||
// Buffer the writes to avoid a lot of small syscalls.
|
|
||||||
let mut stdin = tokio::io::BufWriter::new(&mut self.stdin);
|
|
||||||
|
|
||||||
// We do three things simultaneously: send the old base image and WAL records to
|
// We do three things simultaneously: send the old base image and WAL records to
|
||||||
// the child process's stdin, read the result from child's stdout, and forward any logging
|
// the child process's stdin, read the result from child's stdout, and forward any logging
|
||||||
@@ -558,140 +404,24 @@ impl PostgresRedoProcess {
|
|||||||
// 'f_stdout' below reads the result back. And 'f_stderr', which was spawned into the
|
// 'f_stdout' below reads the result back. And 'f_stderr', which was spawned into the
|
||||||
// tokio runtime in the 'launch' function already, forwards the logging.
|
// tokio runtime in the 'launch' function already, forwards the logging.
|
||||||
let f_stdin = async {
|
let f_stdin = async {
|
||||||
// Send base image, if any. (If the record initializes the page, previous page
|
let buf = request::serialize_request(tag, base_img, records);
|
||||||
// version is not needed.)
|
|
||||||
timeout(
|
|
||||||
TIMEOUT,
|
|
||||||
stdin.write_all(&build_begin_redo_for_block_msg(tag)),
|
|
||||||
)
|
|
||||||
.await??;
|
|
||||||
if base_img.is_some() {
|
|
||||||
timeout(
|
|
||||||
TIMEOUT,
|
|
||||||
stdin.write_all(&build_push_page_msg(tag, base_img.unwrap())),
|
|
||||||
)
|
|
||||||
.await??;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send WAL records.
|
timeout(TIMEOUT, stdin.write_all(&buf)).await??;
|
||||||
for rec in records.iter() {
|
|
||||||
let r = rec.clone();
|
|
||||||
|
|
||||||
WAL_REDO_RECORD_COUNTER.inc();
|
|
||||||
|
|
||||||
stdin
|
|
||||||
.write_all(&build_apply_record_msg(r.lsn, r.rec))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
//debug!("sent WAL record to wal redo postgres process ({:X}/{:X}",
|
|
||||||
// r.lsn >> 32, r.lsn & 0xffff_ffff);
|
|
||||||
}
|
|
||||||
//debug!("sent {} WAL records to wal redo postgres process ({:X}/{:X}",
|
|
||||||
// records.len(), lsn >> 32, lsn & 0xffff_ffff);
|
|
||||||
|
|
||||||
// Send GetPage command to get the result back
|
|
||||||
timeout(TIMEOUT, stdin.write_all(&build_get_page_msg(tag))).await??;
|
|
||||||
timeout(TIMEOUT, stdin.flush()).await??;
|
timeout(TIMEOUT, stdin.flush()).await??;
|
||||||
//debug!("sent GetPage for {}", tag.blknum);
|
|
||||||
Ok::<(), Error>(())
|
Ok::<(), Error>(())
|
||||||
};
|
};
|
||||||
|
|
||||||
// Read back new page image
|
// Read back new page image
|
||||||
let f_stdout = async {
|
let f_stdout = async {
|
||||||
let mut buf = [0u8; 8192];
|
let mut buf = vec![0u8; 8192];
|
||||||
|
|
||||||
timeout(TIMEOUT, stdout.read_exact(&mut buf)).await??;
|
timeout(TIMEOUT, stdout.read_exact(&mut buf)).await??;
|
||||||
//debug!("got response for {}", tag.blknum);
|
//debug!("got response for {}", tag.blknum);
|
||||||
Ok::<[u8; 8192], Error>(buf)
|
Ok::<Vec<u8>, Error>(buf)
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = tokio::try_join!(f_stdout, f_stdin)?;
|
let (buf, _) = tokio::try_join!(f_stdout, f_stdin)?;
|
||||||
|
Ok::<Bytes, Error>(Bytes::from(buf))
|
||||||
let buf = res.0;
|
|
||||||
|
|
||||||
Ok::<Bytes, Error>(Bytes::from(std::vec::Vec::from(buf)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions for constructing messages to send to the postgres WAL redo
|
|
||||||
// process. See vendor/postgres/src/backend/tcop/zenith_wal_redo.c for
|
|
||||||
// explanation of the protocol.
|
|
||||||
|
|
||||||
fn build_begin_redo_for_block_msg(tag: BufferTag) -> Bytes {
|
|
||||||
let len = 4 + 1 + 4 * 4;
|
|
||||||
let mut buf = BytesMut::with_capacity(1 + len);
|
|
||||||
|
|
||||||
buf.put_u8(b'B');
|
|
||||||
buf.put_u32(len as u32);
|
|
||||||
|
|
||||||
// FIXME: this is a temporary hack that should go away when we refactor
|
|
||||||
// the postgres protocol serialization + handlers.
|
|
||||||
//
|
|
||||||
// BytesMut is a dynamic growable buffer, used a lot in tokio code but
|
|
||||||
// not in the std library. To write to a BytesMut from a serde serializer,
|
|
||||||
// we need to either:
|
|
||||||
// - pre-allocate the required buffer space. This is annoying because we
|
|
||||||
// shouldn't care what the exact serialized size is-- that's the
|
|
||||||
// serializer's job.
|
|
||||||
// - Or, we need to create a temporary "writer" (which implements the
|
|
||||||
// `Write` trait). It's a bit awkward, because the writer consumes the
|
|
||||||
// underlying BytesMut, and we need to extract it later with
|
|
||||||
// `into_inner`.
|
|
||||||
let mut writer = buf.writer();
|
|
||||||
tag.ser_into(&mut writer)
|
|
||||||
.expect("serialize BufferTag should always succeed");
|
|
||||||
let buf = writer.into_inner();
|
|
||||||
|
|
||||||
debug_assert!(buf.len() == 1 + len);
|
|
||||||
|
|
||||||
buf.freeze()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_push_page_msg(tag: BufferTag, base_img: Bytes) -> Bytes {
|
|
||||||
assert!(base_img.len() == 8192);
|
|
||||||
|
|
||||||
let len = 4 + 1 + 4 * 4 + base_img.len();
|
|
||||||
let mut buf = BytesMut::with_capacity(1 + len);
|
|
||||||
|
|
||||||
buf.put_u8(b'P');
|
|
||||||
buf.put_u32(len as u32);
|
|
||||||
let mut writer = buf.writer();
|
|
||||||
tag.ser_into(&mut writer)
|
|
||||||
.expect("serialize BufferTag should always succeed");
|
|
||||||
let mut buf = writer.into_inner();
|
|
||||||
buf.put(base_img);
|
|
||||||
|
|
||||||
debug_assert!(buf.len() == 1 + len);
|
|
||||||
|
|
||||||
buf.freeze()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_apply_record_msg(endlsn: Lsn, rec: Bytes) -> Bytes {
|
|
||||||
let len = 4 + 8 + rec.len();
|
|
||||||
let mut buf = BytesMut::with_capacity(1 + len);
|
|
||||||
|
|
||||||
buf.put_u8(b'A');
|
|
||||||
buf.put_u32(len as u32);
|
|
||||||
buf.put_u64(endlsn.0);
|
|
||||||
buf.put(rec);
|
|
||||||
|
|
||||||
debug_assert!(buf.len() == 1 + len);
|
|
||||||
|
|
||||||
buf.freeze()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_get_page_msg(tag: BufferTag) -> Bytes {
|
|
||||||
let len = 4 + 1 + 4 * 4;
|
|
||||||
let mut buf = BytesMut::with_capacity(1 + len);
|
|
||||||
|
|
||||||
buf.put_u8(b'G');
|
|
||||||
buf.put_u32(len as u32);
|
|
||||||
let mut writer = buf.writer();
|
|
||||||
tag.ser_into(&mut writer)
|
|
||||||
.expect("serialize BufferTag should always succeed");
|
|
||||||
let buf = writer.into_inner();
|
|
||||||
|
|
||||||
debug_assert!(buf.len() == 1 + len);
|
|
||||||
|
|
||||||
buf.freeze()
|
|
||||||
}
|
|
||||||
161
pageserver/src/walredo/nonrel.rs
Normal file
161
pageserver/src/walredo/nonrel.rs
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
use byteorder::{ByteOrder, LittleEndian};
|
||||||
|
use bytes::{Buf, Bytes, BytesMut};
|
||||||
|
use postgres_ffi::{
|
||||||
|
nonrelfile_utils::{
|
||||||
|
mx_offset_to_flags_bitshift, mx_offset_to_flags_offset, mx_offset_to_member_offset,
|
||||||
|
transaction_id_set_status,
|
||||||
|
},
|
||||||
|
pg_constants, XLogRecord,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
relish::{RelishTag, SlruKind},
|
||||||
|
waldecoder::{XlMultiXactCreate, XlXactParsedRecord},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::WalRedoRequest;
|
||||||
|
|
||||||
|
pub(super) fn apply_nonrel(request: &WalRedoRequest) -> Bytes {
|
||||||
|
let rel = request.rel;
|
||||||
|
let blknum = request.blknum;
|
||||||
|
|
||||||
|
// Non-relational WAL records are handled here, with custom code that has the
|
||||||
|
// same effects as the corresponding Postgres WAL redo function.
|
||||||
|
const ZERO_PAGE: [u8; 8192] = [0u8; 8192];
|
||||||
|
let mut page = BytesMut::new();
|
||||||
|
if let Some(fpi) = &request.base_img {
|
||||||
|
// If full-page image is provided, then use it...
|
||||||
|
page.extend_from_slice(&fpi[..]);
|
||||||
|
} else {
|
||||||
|
// otherwise initialize page with zeros
|
||||||
|
page.extend_from_slice(&ZERO_PAGE);
|
||||||
|
}
|
||||||
|
// Apply all collected WAL records
|
||||||
|
for record in &request.records {
|
||||||
|
let mut buf = record.rec.clone();
|
||||||
|
|
||||||
|
super::WAL_REDO_RECORD_COUNTER.inc();
|
||||||
|
|
||||||
|
// 1. Parse XLogRecord struct
|
||||||
|
// FIXME: refactor to avoid code duplication.
|
||||||
|
let xlogrec = XLogRecord::from_bytes(&mut buf);
|
||||||
|
|
||||||
|
//move to main data
|
||||||
|
// TODO probably, we should store some records in our special format
|
||||||
|
// to avoid this weird parsing on replay
|
||||||
|
let skip = (record.main_data_offset - pg_constants::SIZEOF_XLOGRECORD) as usize;
|
||||||
|
if buf.remaining() > skip {
|
||||||
|
buf.advance(skip);
|
||||||
|
}
|
||||||
|
|
||||||
|
if xlogrec.xl_rmid == pg_constants::RM_XACT_ID {
|
||||||
|
// Transaction manager stuff
|
||||||
|
let rec_segno = match rel {
|
||||||
|
RelishTag::Slru { slru, segno } => {
|
||||||
|
assert!(
|
||||||
|
slru == SlruKind::Clog,
|
||||||
|
"Not valid XACT relish tag {:?}",
|
||||||
|
rel
|
||||||
|
);
|
||||||
|
segno
|
||||||
|
}
|
||||||
|
_ => panic!("Not valid XACT relish tag {:?}", rel),
|
||||||
|
};
|
||||||
|
let parsed_xact = XlXactParsedRecord::decode(&mut buf, xlogrec.xl_xid, xlogrec.xl_info);
|
||||||
|
if parsed_xact.info == pg_constants::XLOG_XACT_COMMIT
|
||||||
|
|| parsed_xact.info == pg_constants::XLOG_XACT_COMMIT_PREPARED
|
||||||
|
{
|
||||||
|
transaction_id_set_status(
|
||||||
|
parsed_xact.xid,
|
||||||
|
pg_constants::TRANSACTION_STATUS_COMMITTED,
|
||||||
|
&mut page,
|
||||||
|
);
|
||||||
|
for subxact in &parsed_xact.subxacts {
|
||||||
|
let pageno = *subxact as u32 / pg_constants::CLOG_XACTS_PER_PAGE;
|
||||||
|
let segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
|
||||||
|
let rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
|
||||||
|
// only update xids on the requested page
|
||||||
|
if rec_segno == segno && blknum == rpageno {
|
||||||
|
transaction_id_set_status(
|
||||||
|
*subxact,
|
||||||
|
pg_constants::TRANSACTION_STATUS_COMMITTED,
|
||||||
|
&mut page,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if parsed_xact.info == pg_constants::XLOG_XACT_ABORT
|
||||||
|
|| parsed_xact.info == pg_constants::XLOG_XACT_ABORT_PREPARED
|
||||||
|
{
|
||||||
|
transaction_id_set_status(
|
||||||
|
parsed_xact.xid,
|
||||||
|
pg_constants::TRANSACTION_STATUS_ABORTED,
|
||||||
|
&mut page,
|
||||||
|
);
|
||||||
|
for subxact in &parsed_xact.subxacts {
|
||||||
|
let pageno = *subxact as u32 / pg_constants::CLOG_XACTS_PER_PAGE;
|
||||||
|
let segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
|
||||||
|
let rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
|
||||||
|
// only update xids on the requested page
|
||||||
|
if rec_segno == segno && blknum == rpageno {
|
||||||
|
transaction_id_set_status(
|
||||||
|
*subxact,
|
||||||
|
pg_constants::TRANSACTION_STATUS_ABORTED,
|
||||||
|
&mut page,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if xlogrec.xl_rmid == pg_constants::RM_MULTIXACT_ID {
|
||||||
|
// Multixact operations
|
||||||
|
let info = xlogrec.xl_info & pg_constants::XLR_RMGR_INFO_MASK;
|
||||||
|
if info == pg_constants::XLOG_MULTIXACT_CREATE_ID {
|
||||||
|
let xlrec = XlMultiXactCreate::decode(&mut buf);
|
||||||
|
if let RelishTag::Slru {
|
||||||
|
slru,
|
||||||
|
segno: rec_segno,
|
||||||
|
} = rel
|
||||||
|
{
|
||||||
|
if slru == SlruKind::MultiXactMembers {
|
||||||
|
for i in 0..xlrec.nmembers {
|
||||||
|
let pageno = i / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32;
|
||||||
|
let segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT;
|
||||||
|
let rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT;
|
||||||
|
if segno == rec_segno && rpageno == blknum {
|
||||||
|
// update only target block
|
||||||
|
let offset = xlrec.moff + i;
|
||||||
|
let memberoff = mx_offset_to_member_offset(offset);
|
||||||
|
let flagsoff = mx_offset_to_flags_offset(offset);
|
||||||
|
let bshift = mx_offset_to_flags_bitshift(offset);
|
||||||
|
let mut flagsval =
|
||||||
|
LittleEndian::read_u32(&page[flagsoff..flagsoff + 4]);
|
||||||
|
flagsval &= !(((1 << pg_constants::MXACT_MEMBER_BITS_PER_XACT)
|
||||||
|
- 1)
|
||||||
|
<< bshift);
|
||||||
|
flagsval |= xlrec.members[i as usize].status << bshift;
|
||||||
|
LittleEndian::write_u32(
|
||||||
|
&mut page[flagsoff..flagsoff + 4],
|
||||||
|
flagsval,
|
||||||
|
);
|
||||||
|
LittleEndian::write_u32(
|
||||||
|
&mut page[memberoff..memberoff + 4],
|
||||||
|
xlrec.members[i as usize].xid,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Multixact offsets SLRU
|
||||||
|
let offs = (xlrec.mid % pg_constants::MULTIXACT_OFFSETS_PER_PAGE as u32 * 4)
|
||||||
|
as usize;
|
||||||
|
LittleEndian::write_u32(&mut page[offs..offs + 4], xlrec.moff);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
page.freeze()
|
||||||
|
}
|
||||||
86
pageserver/src/walredo/request.rs
Normal file
86
pageserver/src/walredo/request.rs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
///! Functions for constructing messages to send to the postgres WAL redo
|
||||||
|
///! process. See vendor/postgres/src/backend/tcop/zenith_wal_redo.c for
|
||||||
|
///! explanation of the protocol.
|
||||||
|
use bytes::{BufMut, Bytes, BytesMut};
|
||||||
|
use zenith_utils::{bin_ser::BeSer, lsn::Lsn};
|
||||||
|
|
||||||
|
use crate::repository::WALRecord;
|
||||||
|
|
||||||
|
use super::BufferTag;
|
||||||
|
|
||||||
|
pub fn serialize_request(
|
||||||
|
tag: BufferTag,
|
||||||
|
base_img: &Option<Bytes>,
|
||||||
|
records: &[WALRecord],
|
||||||
|
) -> BytesMut {
|
||||||
|
let mut capacity = 1 + BEGIN_REDO_MSG_LEN;
|
||||||
|
if base_img.is_some() {
|
||||||
|
capacity += 1 + PUSH_PAGE_MSG_LEN;
|
||||||
|
}
|
||||||
|
capacity += (1 + APPLY_MSG_HEADER_LEN) * records.len();
|
||||||
|
capacity += records.iter().map(|rec| rec.rec.len()).sum::<usize>();
|
||||||
|
capacity += 1 + GET_PAGE_MSG_LEN;
|
||||||
|
|
||||||
|
let mut buf = BytesMut::with_capacity(capacity);
|
||||||
|
|
||||||
|
build_begin_redo_for_block_msg(&mut buf, tag);
|
||||||
|
|
||||||
|
if let Some(base_img) = base_img.as_ref() {
|
||||||
|
build_push_page_msg(&mut buf, tag, base_img);
|
||||||
|
}
|
||||||
|
|
||||||
|
for record in records {
|
||||||
|
build_apply_record_msg(&mut buf, record.lsn, &record.rec);
|
||||||
|
}
|
||||||
|
|
||||||
|
build_get_page_msg(&mut buf, tag);
|
||||||
|
|
||||||
|
debug_assert_eq!(capacity, buf.len());
|
||||||
|
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
|
||||||
|
const TAG_LEN: usize = 4 * 4;
|
||||||
|
const PAGE_SIZE: usize = 8192;
|
||||||
|
const BEGIN_REDO_MSG_LEN: usize = 4 + 1 + TAG_LEN;
|
||||||
|
const PUSH_PAGE_MSG_LEN: usize = 4 + 1 + TAG_LEN + PAGE_SIZE;
|
||||||
|
const APPLY_MSG_HEADER_LEN: usize = 4 + 8;
|
||||||
|
const GET_PAGE_MSG_LEN: usize = 4 + 1 + TAG_LEN;
|
||||||
|
|
||||||
|
fn build_begin_redo_for_block_msg(buf: &mut BytesMut, tag: BufferTag) {
|
||||||
|
buf.put_u8(b'B');
|
||||||
|
buf.put_u32(BEGIN_REDO_MSG_LEN as u32);
|
||||||
|
|
||||||
|
// TODO tag is serialized multiple times
|
||||||
|
// let's try to serialize it just once
|
||||||
|
// or make the protocol less repetitive
|
||||||
|
tag.ser_into(&mut buf.writer())
|
||||||
|
.expect("serialize BufferTag should always succeed");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_push_page_msg(buf: &mut BytesMut, tag: BufferTag, base_img: &Bytes) {
|
||||||
|
debug_assert_eq!(base_img.len(), PAGE_SIZE);
|
||||||
|
|
||||||
|
buf.put_u8(b'P');
|
||||||
|
buf.put_u32(PUSH_PAGE_MSG_LEN as u32);
|
||||||
|
tag.ser_into(&mut buf.writer())
|
||||||
|
.expect("serialize BufferTag should always succeed");
|
||||||
|
buf.extend(base_img);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_apply_record_msg(buf: &mut BytesMut, endlsn: Lsn, rec: &Bytes) {
|
||||||
|
buf.put_u8(b'A');
|
||||||
|
|
||||||
|
let len = APPLY_MSG_HEADER_LEN + rec.len();
|
||||||
|
buf.put_u32(len as u32);
|
||||||
|
|
||||||
|
buf.put_u64(endlsn.0);
|
||||||
|
buf.extend(rec);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_get_page_msg(buf: &mut BytesMut, tag: BufferTag) {
|
||||||
|
buf.put_u8(b'G');
|
||||||
|
buf.put_u32(GET_PAGE_MSG_LEN as u32);
|
||||||
|
tag.ser_into(&mut buf.writer())
|
||||||
|
.expect("serialize BufferTag should always succeed");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user