AcctIdx: remove Option from held ranges (#21958)

This commit is contained in:
Jeff Washington (jwash) 2021-12-16 21:22:04 -06:00 committed by GitHub
parent 385efae4b3
commit ba777f4f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 38 deletions

View File

@ -1358,16 +1358,16 @@ mod tests {
let map = map.read().unwrap();
assert_eq!(
map.cache_ranges_held.read().unwrap().to_vec(),
vec![Some(range.clone())]
vec![range.clone()]
);
});
accts.hold_range_in_memory(&range2, true);
idx.account_maps.iter().enumerate().for_each(|(bin, map)| {
let map = map.read().unwrap();
let expected = if bin == 0 {
vec![Some(range.clone()), Some(range2_inclusive.clone())]
vec![range.clone(), range2_inclusive.clone()]
} else {
vec![Some(range.clone())]
vec![range.clone()]
};
assert_eq!(
map.cache_ranges_held.read().unwrap().to_vec(),

View File

@ -25,7 +25,7 @@ use {
},
};
type K = Pubkey;
type CacheRangesHeld = RwLock<Vec<Option<RangeInclusive<Pubkey>>>>;
type CacheRangesHeld = RwLock<Vec<RangeInclusive<Pubkey>>>;
pub type SlotT<T> = (Slot, T);
type InMemMap<T> = HashMap<Pubkey, AccountMapEntry<T>>;
@ -648,27 +648,18 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
// this becomes inclusive - that is ok - we are just roughly holding a range of items.
// inclusive is bigger than exclusive so we may hold 1 extra item worst case
let inclusive_range = Some(start..=end);
let inclusive_range = start..=end;
let mut ranges = self.cache_ranges_held.write().unwrap();
if start_holding {
ranges.push(inclusive_range);
} else {
// find the matching range and delete it since we don't want to hold it anymore
let none = inclusive_range.is_none();
for (i, r) in ranges.iter().enumerate() {
if r.is_none() != none {
continue;
}
if !none {
// neither are none, so check values
if let (Bound::Included(start_found), Bound::Included(end_found)) = r
.as_ref()
.map(|r| (r.start_bound(), r.end_bound()))
.unwrap()
{
if start_found != &start || end_found != &end {
continue;
}
if let (Bound::Included(start_found), Bound::Included(end_found)) =
(r.start_bound(), r.end_bound())
{
if start_found != &start || end_found != &end {
continue;
}
}
// found a match. There may be dups, that's ok, we expect another call to remove the dup.
@ -900,9 +891,6 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
}
let ranges = self.cache_ranges_held.read().unwrap().clone();
if ranges.iter().any(|range| range.is_none()) {
return false; // range said to hold 'all', so not completed
}
let mut removed = 0;
// consider chunking these so we don't hold the write lock too long
@ -926,12 +914,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
continue;
}
if ranges.iter().any(|range| {
range
.as_ref()
.map(|range| range.contains(&k))
.unwrap_or(true) // None means 'full range', so true
}) {
if ranges.iter().any(|range| range.contains(&k)) {
// this item is held in mem by range, so don't remove
completed_scan = false;
continue;
@ -1090,38 +1073,34 @@ mod tests {
bucket.hold_range_in_memory(&range, true);
assert_eq!(
bucket.cache_ranges_held.read().unwrap().to_vec(),
vec![Some(range.clone())]
vec![range.clone()]
);
bucket.hold_range_in_memory(&range, false);
assert!(bucket.cache_ranges_held.read().unwrap().is_empty());
bucket.hold_range_in_memory(&range, true);
assert_eq!(
bucket.cache_ranges_held.read().unwrap().to_vec(),
vec![Some(range.clone())]
vec![range.clone()]
);
bucket.hold_range_in_memory(&range, true);
assert_eq!(
bucket.cache_ranges_held.read().unwrap().to_vec(),
vec![Some(range.clone()), Some(range.clone())]
vec![range.clone(), range.clone()]
);
bucket.hold_range_in_memory(&ranges[0], true);
assert_eq!(
bucket.cache_ranges_held.read().unwrap().to_vec(),
vec![
Some(range.clone()),
Some(range.clone()),
Some(ranges[0].clone())
]
vec![range.clone(), range.clone(), ranges[0].clone()]
);
bucket.hold_range_in_memory(&range, false);
assert_eq!(
bucket.cache_ranges_held.read().unwrap().to_vec(),
vec![Some(range.clone()), Some(ranges[0].clone())]
vec![range.clone(), ranges[0].clone()]
);
bucket.hold_range_in_memory(&range, false);
assert_eq!(
bucket.cache_ranges_held.read().unwrap().to_vec(),
vec![Some(ranges[0].clone())]
vec![ranges[0].clone()]
);
bucket.hold_range_in_memory(&ranges[0].clone(), false);
assert!(bucket.cache_ranges_held.read().unwrap().is_empty());