2021-12-03 09:00:31 -08:00
|
|
|
use {
|
2022-01-27 08:25:13 -08:00
|
|
|
crate::accounts_db::SnapshotStorage,
|
|
|
|
log::*,
|
|
|
|
solana_measure::measure::Measure,
|
|
|
|
solana_sdk::clock::Slot,
|
2022-02-18 21:32:29 -08:00
|
|
|
std::ops::{Bound, Range, RangeBounds},
|
2021-12-03 09:00:31 -08:00
|
|
|
};
|
2021-06-01 13:07:46 -07:00
|
|
|
|
2022-01-27 08:25:13 -08:00
|
|
|
/// Provide access to SnapshotStorages sorted by slot
|
2021-06-01 13:07:46 -07:00
|
|
|
pub struct SortedStorages<'a> {
|
2022-01-27 08:25:13 -08:00
|
|
|
/// range of slots where storages exist (likely sparse)
|
2021-06-01 13:07:46 -07:00
|
|
|
range: Range<Slot>,
|
2022-01-27 08:25:13 -08:00
|
|
|
/// the actual storages. index is (slot - range.start)
|
2021-06-01 13:07:46 -07:00
|
|
|
storages: Vec<Option<&'a SnapshotStorage>>,
|
2021-06-02 16:24:55 -07:00
|
|
|
slot_count: usize,
|
2021-10-25 12:37:50 -07:00
|
|
|
storage_count: usize,
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SortedStorages<'a> {
|
2022-03-29 20:05:47 -07:00
|
|
|
/// containing nothing
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
SortedStorages {
|
|
|
|
range: Range::default(),
|
|
|
|
storages: Vec::default(),
|
|
|
|
slot_count: 0,
|
|
|
|
storage_count: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:47:51 -07:00
|
|
|
/// primary method of retrieving (Slot, SnapshotStorage)
|
2022-10-04 09:11:44 -07:00
|
|
|
pub fn iter_range<R>(&'a self, range: &R) -> SortedStoragesIter<'a>
|
2022-01-27 08:25:13 -08:00
|
|
|
where
|
|
|
|
R: RangeBounds<Slot>,
|
|
|
|
{
|
|
|
|
SortedStoragesIter::new(self, range)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, slot: Slot) -> Option<&SnapshotStorage> {
|
2021-06-01 13:07:46 -07:00
|
|
|
if !self.range.contains(&slot) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let index = (slot - self.range.start) as usize;
|
|
|
|
self.storages[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range_width(&self) -> Slot {
|
|
|
|
self.range.end - self.range.start
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range(&self) -> &Range<Slot> {
|
|
|
|
&self.range
|
|
|
|
}
|
2022-05-12 08:24:03 -07:00
|
|
|
|
|
|
|
pub fn max_slot_inclusive(&self) -> Slot {
|
|
|
|
self.range.end.saturating_sub(1)
|
|
|
|
}
|
2021-06-01 13:07:46 -07:00
|
|
|
|
2021-06-02 16:24:55 -07:00
|
|
|
pub fn slot_count(&self) -> usize {
|
|
|
|
self.slot_count
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|
|
|
|
|
2021-10-25 12:37:50 -07:00
|
|
|
pub fn storage_count(&self) -> usize {
|
|
|
|
self.storage_count
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:24:55 -07:00
|
|
|
// assumptions:
|
|
|
|
// 1. each SnapshotStorage.!is_empty()
|
|
|
|
// 2. SnapshotStorage.first().unwrap().get_slot() is unique from all other SnapshotStorage items.
|
2021-06-01 13:07:46 -07:00
|
|
|
pub fn new(source: &'a [SnapshotStorage]) -> Self {
|
2022-03-30 07:19:03 -07:00
|
|
|
let slots = source.iter().map(|storages| {
|
|
|
|
let first = storages.first();
|
|
|
|
assert!(first.is_some(), "SnapshotStorage.is_empty()");
|
|
|
|
let storage = first.unwrap();
|
|
|
|
storage.slot() // this must be unique. Will be enforced in new_with_slots
|
|
|
|
});
|
|
|
|
Self::new_with_slots(source.iter().zip(slots.into_iter()), None, None)
|
2021-06-02 16:24:55 -07:00
|
|
|
}
|
|
|
|
|
2022-01-27 08:25:13 -08:00
|
|
|
/// create `SortedStorages` from 'source' iterator.
|
|
|
|
/// 'source' contains a SnapshotStorage and its associated slot
|
|
|
|
/// 'source' does not have to be sorted in any way, but is assumed to not have duplicate slot #s
|
2022-03-30 07:19:03 -07:00
|
|
|
pub fn new_with_slots(
|
|
|
|
source: impl Iterator<Item = (&'a SnapshotStorage, Slot)> + Clone,
|
2021-06-14 06:53:07 -07:00
|
|
|
// A slot used as a lower bound, but potentially smaller than the smallest slot in the given 'source' iterator
|
|
|
|
min_slot: Option<Slot>,
|
|
|
|
// highest valid slot. Only matters if source array does not contain a slot >= max_slot_inclusive.
|
|
|
|
// An example is a slot that has accounts in the write cache at slots <= 'max_slot_inclusive' but no storages at those slots.
|
|
|
|
// None => self.range.end = source.1.max() + 1
|
|
|
|
// Some(slot) => self.range.end = std::cmp::max(slot, source.1.max())
|
|
|
|
max_slot_inclusive: Option<Slot>,
|
2021-06-08 12:59:50 -07:00
|
|
|
) -> Self {
|
2021-06-02 16:24:55 -07:00
|
|
|
let mut min = Slot::MAX;
|
|
|
|
let mut max = Slot::MIN;
|
2021-06-14 06:53:07 -07:00
|
|
|
let mut adjust_min_max = |slot| {
|
|
|
|
min = std::cmp::min(slot, min);
|
|
|
|
max = std::cmp::max(slot + 1, max);
|
|
|
|
};
|
|
|
|
// none, either, or both of min/max could be specified
|
|
|
|
if let Some(slot) = min_slot {
|
|
|
|
adjust_min_max(slot);
|
|
|
|
}
|
|
|
|
if let Some(slot) = max_slot_inclusive {
|
|
|
|
adjust_min_max(slot);
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:59:50 -07:00
|
|
|
let mut slot_count = 0;
|
2021-06-02 16:24:55 -07:00
|
|
|
let mut time = Measure::start("get slot");
|
2021-06-08 12:59:50 -07:00
|
|
|
let source_ = source.clone();
|
2021-10-25 12:37:50 -07:00
|
|
|
let mut storage_count = 0;
|
|
|
|
source_.for_each(|(storages, slot)| {
|
|
|
|
storage_count += storages.len();
|
2021-06-08 12:59:50 -07:00
|
|
|
slot_count += 1;
|
2022-03-30 07:19:03 -07:00
|
|
|
adjust_min_max(slot);
|
2021-06-02 16:24:55 -07:00
|
|
|
});
|
2021-06-01 13:07:46 -07:00
|
|
|
time.stop();
|
|
|
|
let mut time2 = Measure::start("sort");
|
|
|
|
let range;
|
|
|
|
let mut storages;
|
|
|
|
if min > max {
|
|
|
|
range = Range::default();
|
|
|
|
storages = vec![];
|
|
|
|
} else {
|
|
|
|
range = Range {
|
|
|
|
start: min,
|
|
|
|
end: max,
|
|
|
|
};
|
|
|
|
let len = max - min;
|
|
|
|
storages = vec![None; len as usize];
|
2021-06-08 12:59:50 -07:00
|
|
|
source.for_each(|(original_storages, slot)| {
|
|
|
|
let index = (slot - min) as usize;
|
|
|
|
assert!(storages[index].is_none(), "slots are not unique"); // we should not encounter the same slot twice
|
|
|
|
storages[index] = Some(original_storages);
|
|
|
|
});
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|
|
|
|
time2.stop();
|
|
|
|
debug!("SortedStorages, times: {}, {}", time.as_us(), time2.as_us());
|
|
|
|
Self {
|
|
|
|
range,
|
|
|
|
storages,
|
2021-06-02 16:24:55 -07:00
|
|
|
slot_count,
|
2021-10-25 12:37:50 -07:00
|
|
|
storage_count,
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:25:13 -08:00
|
|
|
/// Iterator over successive slots in 'storages' within 'range'.
|
|
|
|
/// This enforces sequential access so that random access does not have to be implemented.
|
|
|
|
/// Random access could be expensive with large sparse sets.
|
|
|
|
pub struct SortedStoragesIter<'a> {
|
|
|
|
/// range for the iterator to iterate over (start_inclusive..end_exclusive)
|
|
|
|
range: Range<Slot>,
|
|
|
|
/// the data to return per slot
|
|
|
|
storages: &'a SortedStorages<'a>,
|
|
|
|
/// the slot to be returned the next time 'Next' is called
|
|
|
|
next_slot: Slot,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for SortedStoragesIter<'a> {
|
|
|
|
type Item = (Slot, Option<&'a SnapshotStorage>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let slot = self.next_slot;
|
|
|
|
if slot < self.range.end {
|
|
|
|
// iterator is still in range. Storage may or may not exist at this slot, but the iterator still needs to return the slot
|
|
|
|
self.next_slot += 1;
|
|
|
|
Some((slot, self.storages.get(slot)))
|
|
|
|
} else {
|
|
|
|
// iterator passed the end of the range, so from now on it returns None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SortedStoragesIter<'a> {
|
|
|
|
pub fn new<R: RangeBounds<Slot>>(
|
|
|
|
storages: &'a SortedStorages<'a>,
|
2022-10-04 09:11:44 -07:00
|
|
|
range: &R,
|
2022-01-27 08:25:13 -08:00
|
|
|
) -> SortedStoragesIter<'a> {
|
|
|
|
let storage_range = storages.range();
|
|
|
|
let next_slot = match range.start_bound() {
|
|
|
|
Bound::Unbounded => {
|
|
|
|
storage_range.start // unbounded beginning starts with the min known slot (which is inclusive)
|
|
|
|
}
|
|
|
|
Bound::Included(x) => *x,
|
|
|
|
Bound::Excluded(x) => *x + 1, // make inclusive
|
|
|
|
};
|
|
|
|
let end_exclusive_slot = match range.end_bound() {
|
|
|
|
Bound::Unbounded => {
|
|
|
|
storage_range.end // unbounded end ends with the max known slot (which is exclusive)
|
|
|
|
}
|
|
|
|
Bound::Included(x) => *x + 1, // make exclusive
|
|
|
|
Bound::Excluded(x) => *x,
|
|
|
|
};
|
|
|
|
// Note that the range can be outside the range of known storages.
|
|
|
|
// This is because the storages may not be the only source of valid slots.
|
|
|
|
// The write cache is another source of slots that 'storages' knows nothing about.
|
|
|
|
let range = next_slot..end_exclusive_slot;
|
|
|
|
SortedStoragesIter {
|
|
|
|
range,
|
|
|
|
storages,
|
|
|
|
next_slot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:07:46 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
|
|
|
use super::*;
|
|
|
|
impl<'a> SortedStorages<'a> {
|
|
|
|
pub fn new_debug(source: &[(&'a SnapshotStorage, Slot)], min: Slot, len: usize) -> Self {
|
|
|
|
let mut storages = vec![None; len];
|
|
|
|
let range = Range {
|
|
|
|
start: min,
|
|
|
|
end: min + len as Slot,
|
|
|
|
};
|
2021-06-02 16:24:55 -07:00
|
|
|
let slot_count = source.len();
|
2021-06-01 13:07:46 -07:00
|
|
|
for (storage, slot) in source {
|
|
|
|
storages[*slot as usize] = Some(*storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
range,
|
|
|
|
storages,
|
2021-06-02 16:24:55 -07:00
|
|
|
slot_count,
|
2021-10-25 12:37:50 -07:00
|
|
|
storage_count: 0,
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 20:06:48 -07:00
|
|
|
|
|
|
|
pub fn new_for_tests(storages: &[&'a SnapshotStorage], slots: &[Slot]) -> Self {
|
2022-03-30 07:19:03 -07:00
|
|
|
assert_eq!(storages.len(), slots.len());
|
|
|
|
SortedStorages::new_with_slots(
|
|
|
|
storages.iter().cloned().zip(slots.iter().cloned()),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2022-03-29 20:06:48 -07:00
|
|
|
}
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|
2021-06-02 16:24:55 -07:00
|
|
|
|
2022-01-27 08:25:13 -08:00
|
|
|
#[test]
|
|
|
|
fn test_sorted_storages_range_iter() {
|
2022-03-30 07:19:03 -07:00
|
|
|
let storages = SortedStorages::empty();
|
2022-01-27 08:25:13 -08:00
|
|
|
let check = |(slot, storages): (Slot, Option<&SnapshotStorage>)| {
|
|
|
|
assert!(storages.is_none());
|
|
|
|
slot
|
|
|
|
};
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(0..5).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(..5)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(1..5).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(1..5)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(0..0).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(..)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(0..0).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(1..)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// only item is slot 3
|
|
|
|
let s1 = Vec::new();
|
2022-03-29 20:06:48 -07:00
|
|
|
let storages = SortedStorages::new_for_tests(&[&s1], &[3]);
|
2022-01-27 08:25:13 -08:00
|
|
|
let check = |(slot, storages): (Slot, Option<&SnapshotStorage>)| {
|
|
|
|
assert!(
|
|
|
|
(slot != 3) ^ storages.is_some(),
|
2022-12-06 06:30:06 -08:00
|
|
|
"slot: {slot}, storages: {storages:?}"
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
slot
|
|
|
|
};
|
|
|
|
for start in 0..5 {
|
|
|
|
for end in 0..5 {
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(start..end).collect::<Vec<_>>(),
|
2022-01-27 08:25:13 -08:00
|
|
|
storages
|
2022-10-04 09:11:44 -07:00
|
|
|
.iter_range(&(start..end))
|
2022-01-27 08:25:13 -08:00
|
|
|
.map(check)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(3..5).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(..5)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(1..=3).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(1..)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(3..=3).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(..)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// items in slots 2 and 4
|
|
|
|
let s2 = Vec::with_capacity(2);
|
|
|
|
let s4 = Vec::with_capacity(4);
|
2022-03-29 20:06:48 -07:00
|
|
|
let storages = SortedStorages::new_for_tests(&[&s2, &s4], &[2, 4]);
|
2022-01-27 08:25:13 -08:00
|
|
|
let check = |(slot, storages): (Slot, Option<&SnapshotStorage>)| {
|
|
|
|
assert!(
|
|
|
|
(slot != 2 && slot != 4)
|
|
|
|
^ storages
|
|
|
|
.map(|storages| storages.capacity() == (slot as usize))
|
|
|
|
.unwrap_or(false),
|
2022-12-06 06:30:06 -08:00
|
|
|
"slot: {slot}, storages: {storages:?}"
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
slot
|
|
|
|
};
|
|
|
|
for start in 0..5 {
|
|
|
|
for end in 0..5 {
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(start..end).collect::<Vec<_>>(),
|
2022-01-27 08:25:13 -08:00
|
|
|
storages
|
2022-10-04 09:11:44 -07:00
|
|
|
.iter_range(&(start..end))
|
2022-01-27 08:25:13 -08:00
|
|
|
.map(check)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(2..5).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(..5)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(1..=4).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(1..)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-05 10:05:32 -08:00
|
|
|
(2..=4).collect::<Vec<_>>(),
|
2022-10-04 09:11:44 -07:00
|
|
|
storages.iter_range(&(..)).map(check).collect::<Vec<_>>()
|
2022-01-27 08:25:13 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:24:55 -07:00
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "SnapshotStorage.is_empty()")]
|
|
|
|
fn test_sorted_storages_empty() {
|
|
|
|
SortedStorages::new(&[Vec::new()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "slots are not unique")]
|
|
|
|
fn test_sorted_storages_duplicate_slots() {
|
2022-03-29 20:06:48 -07:00
|
|
|
SortedStorages::new_for_tests(&[&Vec::new(), &Vec::new()], &[0, 0]);
|
2021-06-02 16:24:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sorted_storages_none() {
|
2022-03-30 07:19:03 -07:00
|
|
|
let result = SortedStorages::empty();
|
2021-06-02 16:24:55 -07:00
|
|
|
assert_eq!(result.range, Range::default());
|
|
|
|
assert_eq!(result.slot_count, 0);
|
|
|
|
assert_eq!(result.storages.len(), 0);
|
|
|
|
assert!(result.get(0).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sorted_storages_1() {
|
|
|
|
let vec = vec![];
|
|
|
|
let vec_check = vec.clone();
|
|
|
|
let slot = 4;
|
2022-03-29 20:06:48 -07:00
|
|
|
let vecs = [&vec];
|
|
|
|
let result = SortedStorages::new_for_tests(&vecs, &[slot]);
|
2021-06-02 16:24:55 -07:00
|
|
|
assert_eq!(
|
|
|
|
result.range,
|
|
|
|
Range {
|
|
|
|
start: slot,
|
|
|
|
end: slot + 1
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(result.slot_count, 1);
|
|
|
|
assert_eq!(result.storages.len(), 1);
|
|
|
|
assert_eq!(result.get(slot).unwrap().len(), vec_check.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sorted_storages_2() {
|
|
|
|
let vec = vec![];
|
|
|
|
let vec_check = vec.clone();
|
|
|
|
let slots = [4, 7];
|
2022-03-29 20:06:48 -07:00
|
|
|
let vecs = [&vec, &vec];
|
|
|
|
let result = SortedStorages::new_for_tests(&vecs, &slots);
|
2021-06-02 16:24:55 -07:00
|
|
|
assert_eq!(
|
|
|
|
result.range,
|
|
|
|
Range {
|
|
|
|
start: slots[0],
|
|
|
|
end: slots[1] + 1,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(result.slot_count, 2);
|
|
|
|
assert_eq!(result.storages.len() as Slot, slots[1] - slots[0] + 1);
|
|
|
|
assert!(result.get(0).is_none());
|
|
|
|
assert!(result.get(3).is_none());
|
|
|
|
assert!(result.get(5).is_none());
|
|
|
|
assert!(result.get(6).is_none());
|
|
|
|
assert!(result.get(8).is_none());
|
|
|
|
assert_eq!(result.get(slots[0]).unwrap().len(), vec_check.len());
|
|
|
|
assert_eq!(result.get(slots[1]).unwrap().len(), vec_check.len());
|
|
|
|
}
|
2021-06-01 13:07:46 -07:00
|
|
|
}
|