From 8b3a293bb0d648f545924e815d8d88a58443a78e Mon Sep 17 00:00:00 2001 From: anastasia Date: Wed, 1 Sep 2021 14:24:17 +0300 Subject: [PATCH] Use postgres_ffi bindings instead of custom type definitions. Move several functions to postgres_ffi crate --- pageserver/src/restore_local_repo.rs | 19 ++++-------------- pageserver/src/waldecoder.rs | 9 ++------- pageserver/src/walredo.rs | 23 ++++----------------- postgres_ffi/build.rs | 5 +++++ postgres_ffi/pg_control_ffi.h | 3 +++ postgres_ffi/src/nonrelfile_utils.rs | 30 ++++++++++++++++++++++++++++ postgres_ffi/src/pg_constants.rs | 1 + 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/pageserver/src/restore_local_repo.rs b/pageserver/src/restore_local_repo.rs index 3677d4a3e4..0f846b1738 100644 --- a/pageserver/src/restore_local_repo.rs +++ b/pageserver/src/restore_local_repo.rs @@ -19,6 +19,7 @@ use bytes::{Buf, Bytes}; use crate::relish::*; use crate::repository::*; use crate::waldecoder::*; +use postgres_ffi::nonrelfile_utils::mx_offset_to_member_segment; use postgres_ffi::relfile_utils::*; use postgres_ffi::xlog_utils::*; use postgres_ffi::Oid; @@ -875,18 +876,6 @@ fn save_multixact_create_record( Ok(()) } -#[allow(non_upper_case_globals)] -const MaxMultiXactOffset: u32 = 0xFFFFFFFF; - -#[allow(non_snake_case)] -const fn MXOffsetToMemberPage(xid: u32) -> u32 { - xid / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32 -} -#[allow(non_snake_case)] -const fn MXOffsetToMemberSegment(xid: u32) -> i32 { - (MXOffsetToMemberPage(xid) / pg_constants::SLRU_PAGES_PER_SEGMENT) as i32 -} - fn save_multixact_truncate_record( checkpoint: &mut CheckPoint, timeline: &dyn Timeline, @@ -897,9 +886,9 @@ fn save_multixact_truncate_record( checkpoint.oldestMultiDB = xlrec.oldest_multi_db; // PerformMembersTruncation - let maxsegment: i32 = MXOffsetToMemberSegment(MaxMultiXactOffset); - let startsegment: i32 = MXOffsetToMemberSegment(xlrec.start_trunc_memb); - let endsegment: i32 = MXOffsetToMemberSegment(xlrec.end_trunc_memb); + let maxsegment: i32 = mx_offset_to_member_segment(pg_constants::MAX_MULTIXACT_OFFSET); + let startsegment: i32 = mx_offset_to_member_segment(xlrec.start_trunc_memb); + let endsegment: i32 = mx_offset_to_member_segment(xlrec.end_trunc_memb); let mut segment: i32 = startsegment; // Delete all the segments except the last one. The last segment can still diff --git a/pageserver/src/waldecoder.rs b/pageserver/src/waldecoder.rs index 2d1f24f48c..8cde71b970 100644 --- a/pageserver/src/waldecoder.rs +++ b/pageserver/src/waldecoder.rs @@ -10,17 +10,12 @@ use postgres_ffi::xlog_utils::*; use postgres_ffi::XLogLongPageHeaderData; use postgres_ffi::XLogPageHeaderData; use postgres_ffi::XLogRecord; -use postgres_ffi::{Oid, TransactionId}; +use postgres_ffi::{BlockNumber, OffsetNumber}; +use postgres_ffi::{MultiXactId, MultiXactOffset, MultiXactStatus, Oid, TransactionId}; use std::cmp::min; use thiserror::Error; use zenith_utils::lsn::Lsn; -pub type BlockNumber = u32; -pub type OffsetNumber = u16; -pub type MultiXactId = TransactionId; -pub type MultiXactOffset = u32; -pub type MultiXactStatus = u32; - #[allow(dead_code)] pub struct WalStreamDecoder { lsn: Lsn, diff --git a/pageserver/src/walredo.rs b/pageserver/src/walredo.rs index 84a791526b..ed22ca6f1b 100644 --- a/pageserver/src/walredo.rs +++ b/pageserver/src/walredo.rs @@ -44,9 +44,12 @@ use zenith_utils::zid::ZTenantId; use crate::relish::*; use crate::repository::WALRecord; +use crate::waldecoder::XlMultiXactCreate; use crate::waldecoder::XlXactParsedRecord; -use crate::waldecoder::{MultiXactId, XlMultiXactCreate}; 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; @@ -217,24 +220,6 @@ impl WalRedoManager for PostgresRedoManager { } } -fn mx_offset_to_flags_offset(xid: MultiXactId) -> usize { - ((xid / pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP as u32) as u16 - % pg_constants::MULTIXACT_MEMBERGROUPS_PER_PAGE - * pg_constants::MULTIXACT_MEMBERGROUP_SIZE) as usize -} - -fn mx_offset_to_flags_bitshift(xid: MultiXactId) -> u16 { - (xid as u16) % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP - * pg_constants::MXACT_MEMBER_BITS_PER_XACT -} - -/* Location (byte offset within page) of TransactionId of given member */ -fn mx_offset_to_member_offset(xid: MultiXactId) -> usize { - mx_offset_to_flags_offset(xid) - + (pg_constants::MULTIXACT_FLAGBYTES_PER_GROUP - + (xid as u16 % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP) * 4) as usize -} - impl PostgresRedoManager { /// /// Create a new PostgresRedoManager. diff --git a/postgres_ffi/build.rs b/postgres_ffi/build.rs index 20270d03b5..ac738c7b0f 100644 --- a/postgres_ffi/build.rs +++ b/postgres_ffi/build.rs @@ -61,6 +61,11 @@ fn main() { // // These are the types and constants that we want to generate bindings for // + .allowlist_type("BlockNumber") + .allowlist_type("OffsetNumber") + .allowlist_type("MultiXactId") + .allowlist_type("MultiXactOffset") + .allowlist_type("MultiXactStatus") .allowlist_type("ControlFileData") .allowlist_type("CheckPoint") .allowlist_type("FullTransactionId") diff --git a/postgres_ffi/pg_control_ffi.h b/postgres_ffi/pg_control_ffi.h index 790accf7f0..9877c0de6c 100644 --- a/postgres_ffi/pg_control_ffi.h +++ b/postgres_ffi/pg_control_ffi.h @@ -8,3 +8,6 @@ #include "catalog/pg_control.h" #include "access/xlog_internal.h" +#include "storage/block.h" +#include "storage/off.h" +#include "access/multixact.h" diff --git a/postgres_ffi/src/nonrelfile_utils.rs b/postgres_ffi/src/nonrelfile_utils.rs index a0f1f61c18..b92207cd81 100644 --- a/postgres_ffi/src/nonrelfile_utils.rs +++ b/postgres_ffi/src/nonrelfile_utils.rs @@ -5,6 +5,8 @@ use crate::{pg_constants, transaction_id_precedes}; use bytes::BytesMut; use log::*; +use crate::MultiXactId; + pub fn transaction_id_set_status(xid: u32, status: u8, page: &mut BytesMut) { trace!( "handle_apply_request for RM_XACT_ID-{} (1-commit, 2-abort, 3-sub_commit)", @@ -50,3 +52,31 @@ pub fn slru_may_delete_clogsegment(segpage: u32, cutoff_page: u32) -> bool { clogpage_precedes(segpage, cutoff_page) && clogpage_precedes(seg_last_page, cutoff_page) } + +// Multixact utils + +pub fn mx_offset_to_flags_offset(xid: MultiXactId) -> usize { + ((xid / pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP as u32) as u16 + % pg_constants::MULTIXACT_MEMBERGROUPS_PER_PAGE + * pg_constants::MULTIXACT_MEMBERGROUP_SIZE) as usize +} + +pub fn mx_offset_to_flags_bitshift(xid: MultiXactId) -> u16 { + (xid as u16) % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP + * pg_constants::MXACT_MEMBER_BITS_PER_XACT +} + +/* Location (byte offset within page) of TransactionId of given member */ +pub fn mx_offset_to_member_offset(xid: MultiXactId) -> usize { + mx_offset_to_flags_offset(xid) + + (pg_constants::MULTIXACT_FLAGBYTES_PER_GROUP + + (xid as u16 % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP) * 4) as usize +} + +fn mx_offset_to_member_page(xid: u32) -> u32 { + xid / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32 +} + +pub fn mx_offset_to_member_segment(xid: u32) -> i32 { + (mx_offset_to_member_page(xid) / pg_constants::SLRU_PAGES_PER_SEGMENT) as i32 +} diff --git a/postgres_ffi/src/pg_constants.rs b/postgres_ffi/src/pg_constants.rs index baeed75916..a9f96bd19c 100644 --- a/postgres_ffi/src/pg_constants.rs +++ b/postgres_ffi/src/pg_constants.rs @@ -92,6 +92,7 @@ pub const DB_SHUTDOWNED: u32 = 1; // From multixact.h pub const FIRST_MULTIXACT_ID: u32 = 1; pub const MAX_MULTIXACT_ID: u32 = 0xFFFFFFFF; +pub const MAX_MULTIXACT_OFFSET: u32 = 0xFFFFFFFF; pub const XLOG_MULTIXACT_ZERO_OFF_PAGE: u8 = 0x00; pub const XLOG_MULTIXACT_ZERO_MEM_PAGE: u8 = 0x10;