From f598ee696c2caf2facf2c9758486d42a87dbca06 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Wed, 1 Dec 2021 12:17:32 -0600 Subject: [PATCH] AcctIdx: disk bucket cleanup (#21520) --- bucket_map/src/bucket.rs | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/bucket_map/src/bucket.rs b/bucket_map/src/bucket.rs index 0856ddaa8..78e3bd72f 100644 --- a/bucket_map/src/bucket.rs +++ b/bucket_map/src/bucket.rs @@ -185,7 +185,6 @@ impl Bucket { key: &Pubkey, elem_uid: Uid, random: u64, - ref_count: u64, ) -> Result { let ix = Self::bucket_index_ix(index, key, random); for i in ix..ix + index.max_search() { @@ -196,7 +195,9 @@ impl Bucket { index.allocate(ii, elem_uid).unwrap(); let mut elem: &mut IndexEntry = index.get_mut(ii); elem.key = *key; - elem.ref_count = ref_count; + // These will be overwritten after allocation by callers. + // Since this part of the mmapped file could have previously been used by someone else, there can be garbage here. + elem.ref_count = 0; elem.storage_offset = 0; elem.storage_capacity_when_created_pow2 = 0; elem.num_slots = 0; @@ -218,14 +219,8 @@ impl Bucket { Some(elem.ref_count) } - fn create_key(&self, key: &Pubkey, ref_count: u64) -> Result { - Self::bucket_create_key( - &self.index, - key, - IndexEntry::key_uid(key), - self.random, - ref_count, - ) + fn create_key(&self, key: &Pubkey) -> Result { + Self::bucket_create_key(&self.index, key, IndexEntry::key_uid(key), self.random) } pub fn read_value(&self, key: &Pubkey) -> Option<(&[T], RefCount)> { @@ -248,31 +243,26 @@ impl Bucket { let index_entry = self.find_entry_mut(key); let (elem, elem_ix) = match index_entry { None => { - let ii = self.create_key(key, ref_count)?; - let elem = self.index.get_mut(ii); // get_mut here is right? + let ii = self.create_key(key)?; + let elem: &mut IndexEntry = self.index.get_mut(ii); (elem, ii) } - Some(res) => { - if ref_count != res.0.ref_count { - res.0.ref_count = ref_count; - } - res - } + Some(res) => res, }; + elem.ref_count = ref_count; let elem_uid = self.index.uid(elem_ix); let bucket_ix = elem.data_bucket_ix(); let current_bucket = &self.data[bucket_ix as usize]; if best_fit_bucket == bucket_ix && elem.num_slots > 0 { - //in place update + // in place update let elem_loc = elem.data_loc(current_bucket); let slice: &mut [T] = current_bucket.get_mut_cell_slice(elem_loc, data.len() as u64); - //let elem: &mut IndexEntry = self.index.get_mut(elem_ix); assert!(current_bucket.uid(elem_loc) == elem_uid); elem.num_slots = data.len() as u64; slice.clone_from_slice(data); Ok(()) } else { - //need to move the allocation to a best fit spot + // need to move the allocation to a best fit spot let best_bucket = &self.data[best_fit_bucket as usize]; let cap_power = best_bucket.capacity_pow2; let cap = best_bucket.capacity(); @@ -284,7 +274,6 @@ impl Bucket { if elem.num_slots > 0 { current_bucket.free(elem_loc, elem_uid); } - // elem: &mut IndexEntry = self.index.get_mut(elem_ix); elem.storage_offset = ix; elem.storage_capacity_when_created_pow2 = best_bucket.capacity_pow2; elem.num_slots = data.len() as u64; @@ -339,9 +328,7 @@ impl Bucket { let uid = self.index.uid(ix); if UID_UNLOCKED != uid { let elem: &IndexEntry = self.index.get(ix); - let ref_count = 0; // ??? TODO - let new_ix = - Self::bucket_create_key(&index, &elem.key, uid, random, ref_count); + let new_ix = Self::bucket_create_key(&index, &elem.key, uid, random); if new_ix.is_err() { valid = false; break;