Rename get_rel_size() to get_relish_size(). Don't bail if relish is not found, just return None and let the caller to decide how to handle this

This commit is contained in:
anastasia
2021-08-06 13:47:40 +03:00
committed by lubennikovaav
parent a368642790
commit e475f82ff1
5 changed files with 44 additions and 28 deletions

View File

@@ -96,9 +96,17 @@ impl<'a> Basebackup<'a> {
// Generate SLRU segment files from repository.
//
fn add_slru_segment(&mut self, slru: SlruKind, segno: u32) -> anyhow::Result<()> {
let nblocks = self
let seg_size = self
.timeline
.get_rel_size(RelishTag::Slru { slru, segno }, self.lsn)?;
.get_relish_size(RelishTag::Slru { slru, segno }, self.lsn)?;
if seg_size == None
{
info!("SLRU segment {}/{:>04X} was truncated", slru.to_str(), segno);
return Ok(());
}
let nblocks = seg_size.unwrap();
let mut slru_buf: Vec<u8> =
Vec::with_capacity(nblocks as usize * pg_constants::BLCKSZ as usize);

View File

@@ -320,18 +320,15 @@ impl Timeline for ObjectTimeline {
/* return Err("could not find page image")?; */
}
/// Get size of relation
fn get_rel_size(&self, rel: RelishTag, lsn: Lsn) -> Result<u32> {
/// Get size of a relish in number of blocks
/// Return None if the relish was truncated
fn get_relish_size(&self, rel: RelishTag, lsn: Lsn) -> Result<Option<u32>> {
if !rel.is_blocky() {
bail!("invalid get_rel_size request for non-blocky relish {}", rel);
bail!("invalid get_relish_size request for non-blocky relish {}", rel);
}
let lsn = self.wait_lsn(lsn)?;
match self.relsize_get_nowait(rel, lsn)? {
Some(nblocks) => Ok(nblocks),
None => bail!("relation {} not found at {}", rel, lsn),
}
return self.relsize_get_nowait(rel, lsn);
}
/// Does relation exist at given LSN?
@@ -471,7 +468,10 @@ impl Timeline for ObjectTimeline {
Ok(())
}
/// Unlink relation. This method is used for marking dropped relations.
/// Unlink relation. This method is used for marking dropped Relishes.
///
/// Note: each SLRU segment in PostgreSQL is considered a separate relish,
/// so we can use unlink to truncate SLRU segments.
fn put_unlink(&self, rel_tag: RelishTag, lsn: Lsn) -> Result<()> {
self.put_relsize_entry(&rel_tag, lsn, RelationSizeEntry::Unlink)?;
@@ -558,10 +558,15 @@ impl Timeline for ObjectTimeline {
/// Adds a relation-wide WAL record (like truncate) to the repository,
/// associating it with all pages started with specified block number
///
/// Note: This is only for regular relation truncations.
/// To truncate SLRU segments use put_unlink() function.
///
fn put_truncation(&self, rel: RelishTag, lsn: Lsn, nblocks: u32) -> Result<()> {
if !rel.is_blocky() {
bail!("invalid truncation for non-blocky relish {}", rel);
}
match rel {
RelishTag::Relation(_) => {},
_ => bail!("invalid truncation for non-rel relish {}", rel)
};
info!("Truncate relation {} to {} blocks at {}", rel, nblocks, lsn);

View File

@@ -266,7 +266,9 @@ impl PageServerHandler {
let n_blocks = SMGR_QUERY_TIME
.with_label_values(&["get_rel_size"])
.observe_closure_duration(|| {
timeline.get_rel_size(tag, req.lsn).unwrap_or(0)
// Return 0 if relation is not found.
// This is what postgres smgr expects.
timeline.get_relish_size(tag, req.lsn).unwrap_or(Some(0)).unwrap_or(0)
});
PagestreamBeMessage::Nblocks(PagestreamStatusResponse { ok: true, n_blocks })

View File

@@ -61,8 +61,8 @@ pub trait Timeline: Send + Sync {
/// Look up given page in the cache.
fn get_page_at_lsn_nowait(&self, tag: RelishTag, blknum: u32, lsn: Lsn) -> Result<Bytes>;
/// Get size of relation
fn get_rel_size(&self, tag: RelishTag, lsn: Lsn) -> Result<u32>;
/// Get size of a relish
fn get_relish_size(&self, tag: RelishTag, lsn: Lsn) -> Result<Option<u32>>;
/// Does relation exist?
fn get_rel_exists(&self, tag: RelishTag, lsn: Lsn) -> Result<bool>;
@@ -98,7 +98,8 @@ pub trait Timeline: Send + Sync {
/// Truncate relation
fn put_truncation(&self, rel: RelishTag, lsn: Lsn, nblocks: u32) -> Result<()>;
/// Unlink relation. This method is used for marking dropped relations.
/// Unlink relish.
/// This method is used for marking dropped relations and truncated SLRU segments
fn put_unlink(&self, tag: RelishTag, lsn: Lsn) -> Result<()>;
/// Put raw data
@@ -338,11 +339,11 @@ mod tests {
// The relation was created at LSN 2, not visible at LSN 1 yet.
assert_eq!(tline.get_rel_exists(TESTREL_A, Lsn(1))?, false);
assert!(tline.get_rel_size(TESTREL_A, Lsn(1)).is_err());
assert!(tline.get_relish_size(TESTREL_A, Lsn(1))?.is_none());
assert_eq!(tline.get_rel_exists(TESTREL_A, Lsn(2))?, true);
assert_eq!(tline.get_rel_size(TESTREL_A, Lsn(2))?, 1);
assert_eq!(tline.get_rel_size(TESTREL_A, Lsn(5))?, 3);
assert_eq!(tline.get_relish_size(TESTREL_A, Lsn(2))?.unwrap(), 1);
assert_eq!(tline.get_relish_size(TESTREL_A, Lsn(5))?.unwrap(), 3);
// Check page contents at each LSN
assert_eq!(
@@ -382,7 +383,7 @@ mod tests {
tline.advance_last_valid_lsn(Lsn(6));
// Check reported size and contents after truncation
assert_eq!(tline.get_rel_size(TESTREL_A, Lsn(6))?, 2);
assert_eq!(tline.get_relish_size(TESTREL_A, Lsn(6))?.unwrap(), 2);
assert_eq!(
tline.get_page_at_lsn(TESTREL_A, 0, Lsn(6))?,
TEST_IMG("foo blk 0 at 3")
@@ -393,7 +394,7 @@ mod tests {
);
// should still see the truncated block with older LSN
assert_eq!(tline.get_rel_size(TESTREL_A, Lsn(5))?, 3);
assert_eq!(tline.get_relish_size(TESTREL_A, Lsn(5))?.unwrap(), 3);
assert_eq!(
tline.get_page_at_lsn(TESTREL_A, 2, Lsn(5))?,
TEST_IMG("foo blk 2 at 5")
@@ -424,7 +425,7 @@ mod tests {
tline.advance_last_valid_lsn(Lsn(lsn));
assert_eq!(
tline.get_rel_size(TESTREL_A, Lsn(lsn))?,
tline.get_relish_size(TESTREL_A, Lsn(lsn))?.unwrap(),
pg_constants::RELSEG_SIZE + 1
);
@@ -433,7 +434,7 @@ mod tests {
tline.put_truncation(TESTREL_A, Lsn(lsn), pg_constants::RELSEG_SIZE)?;
tline.advance_last_valid_lsn(Lsn(lsn));
assert_eq!(
tline.get_rel_size(TESTREL_A, Lsn(lsn))?,
tline.get_relish_size(TESTREL_A, Lsn(lsn))?.unwrap(),
pg_constants::RELSEG_SIZE
);
@@ -442,7 +443,7 @@ mod tests {
tline.put_truncation(TESTREL_A, Lsn(lsn), pg_constants::RELSEG_SIZE - 1)?;
tline.advance_last_valid_lsn(Lsn(lsn));
assert_eq!(
tline.get_rel_size(TESTREL_A, Lsn(lsn))?,
tline.get_relish_size(TESTREL_A, Lsn(lsn))?.unwrap(),
pg_constants::RELSEG_SIZE - 1
);
@@ -505,7 +506,7 @@ mod tests {
TEST_IMG("foobar blk 0 at 2")
);
assert_eq!(newtline.get_rel_size(TESTREL_B, Lsn(4))?, 1);
assert_eq!(newtline.get_relish_size(TESTREL_B, Lsn(4))?.unwrap(), 1);
Ok(())
}

View File

@@ -531,7 +531,7 @@ fn save_xlog_dbase_create(timeline: &dyn Timeline, lsn: Lsn, rec: &XlCreateDatab
assert_eq!(src_rel.spcnode, src_tablespace_id);
assert_eq!(src_rel.dbnode, src_db_id);
let nblocks = timeline.get_rel_size(RelishTag::Relation(src_rel), req_lsn)?;
let nblocks = timeline.get_relish_size(RelishTag::Relation(src_rel), req_lsn)?.unwrap_or(0);
let dst_rel = RelTag {
spcnode: tablespace_id,
dbnode: db_id,