Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit data sent after dht differences #94

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions crates/api/src/op_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,19 @@ pub trait OpStore: 'static + Send + Sync + std::fmt::Debug {
///
/// This must be the timestamp of the op, not the time that we saw the op or chose to store it.
/// The returned ops must be ordered by timestamp, ascending.
///
/// # Returns
///
/// - As many op ids as can be returned within the `limit_bytes` limit, within the arc and time
/// bounds.
/// - The total size of the op data that is pointed to by the returned op ids.
fn retrieve_op_hashes_in_time_slice(
&self,
arc: DhtArc,
start: Timestamp,
end: Timestamp,
) -> BoxFuture<'_, K2Result<Vec<OpId>>>;
limit_bytes: Option<u32>,
ThetaSinner marked this conversation as resolved.
Show resolved Hide resolved
) -> BoxFuture<'_, K2Result<(Vec<OpId>, u32)>>;

/// Retrieve a list of ops by their op ids.
///
Expand Down Expand Up @@ -112,13 +119,13 @@ pub trait OpStore: 'static + Send + Sync + std::fmt::Debug {
///
/// - As many op ids as can be returned within the `limit_bytes` limit.
/// - The total size of the op data that is pointed to by the returned op ids.
/// - A new timestamp to be used for the next query..
/// - A new timestamp to be used for the next query.
fn retrieve_op_ids_bounded(
&self,
arc: DhtArc,
start: Timestamp,
limit_bytes: usize,
) -> BoxFuture<'_, K2Result<(Vec<OpId>, usize, Timestamp)>>;
limit_bytes: u32,
ThetaSinner marked this conversation as resolved.
Show resolved Hide resolved
) -> BoxFuture<'_, K2Result<(Vec<OpId>, u32, Timestamp)>>;

/// Store the combined hash of a time slice.
fn store_slice_hash(
Expand Down
57 changes: 41 additions & 16 deletions crates/core/src/factories/mem_op_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,44 @@ impl OpStore for Kitsune2MemoryOpStore {
arc: DhtArc,
start: Timestamp,
end: Timestamp,
) -> BoxFuture<'_, K2Result<Vec<OpId>>> {
limit_bytes: Option<u32>,
) -> BoxFuture<'_, K2Result<(Vec<OpId>, u32)>> {
Box::pin(async move {
let self_lock = self.read().await;
Ok(self_lock
.op_list
.iter()
.filter(|(_, op)| {
let loc = op.op_id.loc();
op.created_at >= start
&& op.created_at < end
&& arc.contains(loc)
})
.map(|(op_id, _)| op_id.clone())
.collect())

let mut used_bytes = 0;
Ok((
self_lock
.op_list
.iter()
.filter(|(_, op)| {
let loc = op.op_id.loc();
op.created_at >= start
&& op.created_at < end
&& arc.contains(loc)
})
.take_while(|(_, op)| {
let data_len = op.op_data.len() as u32;
match limit_bytes {
Some(limit_bytes) => {
if used_bytes + data_len <= limit_bytes {
used_bytes += data_len;
true
} else {
false
}
}
None => {
// Return an accurate count even if we're not limiting
used_bytes += data_len;
true
}
}
})
.map(|(op_id, _)| op_id.clone())
.collect(),
used_bytes,
))
})
}

Expand Down Expand Up @@ -244,8 +268,8 @@ impl OpStore for Kitsune2MemoryOpStore {
&self,
arc: DhtArc,
start: Timestamp,
limit_bytes: usize,
) -> BoxFuture<'_, K2Result<(Vec<OpId>, usize, Timestamp)>> {
limit_bytes: u32,
) -> BoxFuture<'_, K2Result<(Vec<OpId>, u32, Timestamp)>> {
Box::pin(async move {
let new_start = Timestamp::now();

Expand All @@ -269,8 +293,9 @@ impl OpStore for Kitsune2MemoryOpStore {
let op_ids = candidate_ops
.into_iter()
.take_while(|op| {
if total_bytes + op.op_data.len() <= limit_bytes {
total_bytes += op.op_data.len();
let data_len = op.op_data.len() as u32;
if total_bytes + data_len <= limit_bytes {
total_bytes += data_len;
true
} else {
last_op_timestamp = Some(op.stored_at);
Expand Down
101 changes: 63 additions & 38 deletions crates/dht/src/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ pub trait DhtApi: 'static + Send + Sync + std::fmt::Debug {
their_snapshot: DhtSnapshot,
our_previous_snapshot: Option<DhtSnapshot>,
arc_set: ArcSet,
) -> BoxFut<'_, K2Result<DhtSnapshotNextAction>>;
max_op_data_bytes: u32,
) -> BoxFut<'_, K2Result<(DhtSnapshotNextAction, u32)>>;
}

impl DhtApi for Dht {
Expand Down Expand Up @@ -221,7 +222,8 @@ impl DhtApi for Dht {
their_snapshot: DhtSnapshot,
our_previous_snapshot: Option<DhtSnapshot>,
arc_set: ArcSet,
) -> BoxFut<'_, K2Result<DhtSnapshotNextAction>> {
max_op_data_bytes: u32,
) -> BoxFut<'_, K2Result<(DhtSnapshotNextAction, u32)>> {
Box::pin(async move {
if arc_set.covered_sector_count() == 0 {
return Err(K2Error::other("No arcs to snapshot"));
Expand Down Expand Up @@ -318,29 +320,34 @@ impl DhtApi for Dht {
// hashes. In the case that we produce a most detailed snapshot type, we can send the list
// of op hashes at the same time.
match our_snapshot.compare(&their_snapshot) {
SnapshotDiff::Identical => Ok(DhtSnapshotNextAction::Identical),
SnapshotDiff::Identical => {
Ok((DhtSnapshotNextAction::Identical, 0))
}
SnapshotDiff::CannotCompare => {
Ok(DhtSnapshotNextAction::CannotCompare)
Ok((DhtSnapshotNextAction::CannotCompare, 0))
}
SnapshotDiff::DiscMismatch => {
Ok(DhtSnapshotNextAction::NewSnapshot(
SnapshotDiff::DiscMismatch => Ok((
DhtSnapshotNextAction::NewSnapshot(
self.snapshot_disc_sectors(&arc_set).await?,
))
}
SnapshotDiff::DiscSectorMismatches(mismatched_sectors) => {
Ok(DhtSnapshotNextAction::NewSnapshot(
),
0,
)),
SnapshotDiff::DiscSectorMismatches(mismatched_sectors) => Ok((
DhtSnapshotNextAction::NewSnapshot(
self.snapshot_disc_sector_details(
mismatched_sectors,
&arc_set,
self.store.clone(),
)
.await?,
))
}
),
0,
)),
SnapshotDiff::DiscSectorSliceMismatches(
mismatched_slice_indices,
) => {
let mut out = Vec::new();
let mut used_bytes = 0;
for (sector_index, missing_slices) in
mismatched_slice_indices
{
Expand Down Expand Up @@ -369,36 +376,46 @@ impl DhtApi for Dht {
continue;
};

out.extend(
self.store
.retrieve_op_hashes_in_time_slice(
arc, start, end,
)
.await?,
);
let (op_ids, ub) = self
.store
.retrieve_op_hashes_in_time_slice(
arc,
start,
end,
Some(max_op_data_bytes - used_bytes),
)
.await?;
out.extend(op_ids);

used_bytes += ub;
}
}

Ok(if is_final {
DhtSnapshotNextAction::HashList(out)
(DhtSnapshotNextAction::HashList(out), used_bytes)
} else {
DhtSnapshotNextAction::NewSnapshotAndHashList(
our_snapshot,
out,
(
DhtSnapshotNextAction::NewSnapshotAndHashList(
our_snapshot,
out,
),
used_bytes,
)
})
}
SnapshotDiff::RingMismatches(mismatched_rings) => {
Ok(DhtSnapshotNextAction::NewSnapshot(
SnapshotDiff::RingMismatches(mismatched_rings) => Ok((
DhtSnapshotNextAction::NewSnapshot(
self.snapshot_ring_sector_details(
mismatched_rings,
&arc_set,
)?,
))
}
),
0,
)),
SnapshotDiff::RingSectorMismatches(mismatched_sectors) => {
let mut out = Vec::new();

let mut used_bytes = 0;
for (ring_index, missing_sectors) in mismatched_sectors {
for sector_index in missing_sectors {
let Ok(arc) = self
Expand All @@ -425,22 +442,30 @@ impl DhtApi for Dht {
continue;
};

out.extend(
self.store
.retrieve_op_hashes_in_time_slice(
arc, start, end,
)
.await?,
);
let (op_ids, ub) = self
.store
.retrieve_op_hashes_in_time_slice(
arc,
start,
end,
Some(max_op_data_bytes - used_bytes),
)
.await?;
out.extend(op_ids);

used_bytes += ub;
}
}

Ok(if is_final {
DhtSnapshotNextAction::HashList(out)
(DhtSnapshotNextAction::HashList(out), used_bytes)
} else {
DhtSnapshotNextAction::NewSnapshotAndHashList(
our_snapshot,
out,
(
DhtSnapshotNextAction::NewSnapshotAndHashList(
our_snapshot,
out,
),
used_bytes,
)
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/dht/src/dht/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async fn cannot_handle_snapshot_with_empty_arc_set() {
snapshot,
None,
ArcSet::new(vec![DhtArc::Empty]).unwrap(),
1_000,
)
.await
.unwrap_err();
Expand Down
29 changes: 20 additions & 9 deletions crates/dht/src/dht/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ impl DhtSyncHarness {
self.dht.snapshot_minimal(arc_set.clone()).await?;
match other
.dht
.handle_snapshot(initial_snapshot, None, arc_set.clone())
.handle_snapshot(initial_snapshot, None, arc_set.clone(), 1_000)
.await?
.0
{
DhtSnapshotNextAction::Identical => Ok(true),
_ => Ok(false),
Expand All @@ -134,8 +135,9 @@ impl DhtSyncHarness {
// Send it to the other agent and have them diff against it
let outcome = other
.dht
.handle_snapshot(initial_snapshot, None, arc_set.clone())
.await?;
.handle_snapshot(initial_snapshot, None, arc_set.clone(), 1_000)
.await?
.0;

match outcome {
DhtSnapshotNextAction::Identical => {
Expand Down Expand Up @@ -192,8 +194,9 @@ impl DhtSyncHarness {
// coming back to us
let outcome = self
.dht
.handle_snapshot(snapshot, None, arc_set.clone())
.await?;
.handle_snapshot(snapshot, None, arc_set.clone(), 1_000)
.await?
.0;

let our_details_snapshot = match outcome {
DhtSnapshotNextAction::NewSnapshot(new_snapshot) => new_snapshot,
Expand Down Expand Up @@ -225,8 +228,10 @@ impl DhtSyncHarness {
our_details_snapshot.clone(),
None,
arc_set.clone(),
1_000,
)
.await?;
.await?
.0;

let (snapshot, hash_list_from_other) = match outcome {
DhtSnapshotNextAction::NewSnapshotAndHashList(
Expand Down Expand Up @@ -260,8 +265,10 @@ impl DhtSyncHarness {
snapshot,
Some(our_details_snapshot),
arc_set.clone(),
1000,
)
.await?;
.await?
.0;

let hash_list_from_self = match outcome {
DhtSnapshotNextAction::HashList(hash_list) => hash_list,
Expand Down Expand Up @@ -316,8 +323,10 @@ impl DhtSyncHarness {
other_details_snapshot.clone(),
None,
arc_set.clone(),
1_000,
)
.await?;
.await?
.0;

let (snapshot, hash_list_from_self) = match outcome {
DhtSnapshotNextAction::Identical => {
Expand Down Expand Up @@ -349,8 +358,10 @@ impl DhtSyncHarness {
snapshot,
Some(other_details_snapshot),
arc_set.clone(),
1_000,
)
.await?;
.await?
.0;

let hash_list_from_other = match outcome {
DhtSnapshotNextAction::Identical => {
Expand Down
Loading