Skip to content

Commit

Permalink
fix: align_up seg_buf
Browse files Browse the repository at this point in the history
  • Loading branch information
Fischer0522 committed Sep 26, 2024
1 parent be0ed86 commit d0c9319
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
14 changes: 12 additions & 2 deletions core/src/layers/5-disk/block_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl AllocTable {
let seg_log_res = store.open_log_in(BUCKET_SEGMENT_TABLE);
let segment_table = match seg_log_res {
Ok(seg_log) => {
let mut buf = Buf::alloc(segment_nums * Segment::ser_size())?;
let mut buf = Buf::alloc(seg_log.nblocks())?;
seg_log.read(0 as BlockId, buf.as_mut())?;
recover_segment_table(segment_nums, buf.as_slice(), bitmap)?
}
Expand Down Expand Up @@ -288,14 +288,18 @@ impl AllocTable {
// Serialize the segment table, [valid_block, free_space]
// Persist the serialized segment table to `SEG` log
let mut ser_seg_buf = vec![0; Segment::ser_size() * segment_table_len];
let mut ser_len = 0;
self.segment_table
.iter()
.enumerate()
.try_for_each(|(idx, segment)| {
let offset = idx * Segment::ser_size();
let segment_buf = &mut ser_seg_buf[offset..offset + Segment::ser_size()];
segment.to_slice(segment_buf)
ser_len += segment.to_slice(segment_buf)?;
Ok::<_, Error>(())
})?;

ser_seg_buf.resize(align_up(ser_len, BLOCK_SIZE), 0);
// Persist the serialized block validity table to `BVT` log
// and GC any old `BVT` logs and `BAL` logs
let mut tx = store.new_tx();
Expand All @@ -306,6 +310,12 @@ impl AllocTable {
}
}

if let Ok(seg_log_ids) = store.list_logs_in(BUCKET_SEGMENT_TABLE) {
for seg_log_id in seg_log_ids {
store.delete_log(seg_log_id)?;
}
}

let bvt_log = store.create_log(BUCKET_BLOCK_VALIDITY_TABLE)?;
bvt_log.append(BufRef::try_from(&ser_buf[..]).unwrap())?;

Expand Down
6 changes: 3 additions & 3 deletions core/src/layers/5-disk/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ mod tests {
#[test]
fn simple_data_migration() {
init_logger();
let nblocks = 64 * SEGMENT_SIZE;
let nblocks = 128 * SEGMENT_SIZE;
let mem_disk = MemDisk::create(nblocks).unwrap();
let greedy_victim_policy = GreedyVictimPolicy {};
let root_key = AeadKey::random();
Expand Down Expand Up @@ -630,7 +630,7 @@ mod tests {
#[test]
fn batch_data_migration() {
init_logger();
let nblocks = 64 * SEGMENT_SIZE;
let nblocks = 128 * SEGMENT_SIZE;
let mem_disk = MemDisk::create(nblocks).unwrap();
let greedy_victim_policy = GreedyVictimPolicy {};
let root_key = AeadKey::random();
Expand Down Expand Up @@ -679,7 +679,7 @@ mod tests {
#[test]
fn multi_segment_migration() {
init_logger();
let nblocks = 64 * SEGMENT_SIZE;
let nblocks = 128 * SEGMENT_SIZE;
let mem_disk = MemDisk::create(nblocks * 5 / 4).unwrap();
let greedy_victim_policy = GreedyVictimPolicy {};
let root_key = AeadKey::random();
Expand Down
9 changes: 5 additions & 4 deletions core/src/layers/5-disk/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ impl Segment {
}

impl Segment {
pub fn to_slice(&self, buf: &mut [u8]) -> Result<()> {
pub fn to_slice(&self, buf: &mut [u8]) -> Result<usize> {
let valid_blocks = self.num_valid_blocks();
let free_space = self.free_space();
let data = [valid_blocks, free_space];
postcard::to_slice::<[usize; 2]>(&data, buf)
.map_err(|_| Error::with_msg(InvalidArgs, "serialize segment failed"))?;
Ok(())
let ser_len = postcard::to_slice::<[usize; 2]>(&data, buf)
.map_err(|_| Error::with_msg(InvalidArgs, "serialize segment failed"))?
.len();
Ok(ser_len)
}

pub fn recover(
Expand Down

0 comments on commit d0c9319

Please sign in to comment.