Skip to content

Commit

Permalink
feat(iroh-bytes): add more context to errors (#2196)
Browse files Browse the repository at this point in the history
## Description

feat(iroh-bytes): add more context to errors

add hash and offset/range as context for LeafHashMismatch and
ParentHashMismatch errors

This is what the log looks like now:

```
2024-04-16T07:17:19.613913Z  WARN iroh_bytes::provider: error: Error {
    context: "hash 17c8e5a70f69d289105553dfa762064e8cc57ffb9355d76508f557e37ef051db offset 9994240",
    source: LeafHashMismatch(
        ChunkNum(0x2620),
    ),
}
2024-04-16T07:17:26.664680Z  INFO magicsock{me=34qdsunbluss4zny}:actor:disco{node=kqn3pxyufuskwsup}: iroh_net::magicsock::node_map::best_addr: clearing best_addr reason=PongTimeout has_relay=true old_addr=192.168.1.131:64604
2024-04-16T07:18:05.507191Z  INFO poll_recv{me=34qdsunbluss4zny}:disco_in{node=kkpbi4eb5h6jcdjz src=Udp(192.168.1.131:62647)}: iroh_net::magicsock::node_map: inserting new node endpoint in NodeMap node=kkpbi4eb5h6jcdjz relay_url=None
2024-04-16T07:18:05.507311Z  INFO poll_recv{me=34qdsunbluss4zny}:disco_in{node=kkpbi4eb5h6jcdjz src=Udp(192.168.1.131:62647)}: iroh_net::magicsock::node_map::endpoint: new direct addr for node addr=192.168.1.131:62647
2024-04-16T07:18:05.510362Z  INFO poll_recv{me=34qdsunbluss4zny}:disco_in{node=kkpbi4eb5h6jcdjz src=Udp(192.168.1.131:62647)}: iroh_net::magicsock::node_map::best_addr: selecting new direct path for endpoint addr=192.168.1.131:62647 latency=2.686417ms trust_for=6.499932542s
2024-04-16T07:18:07.994402Z  INFO iroh_bytes::provider: sent 100390814 bytes in 2.4763055s
2024-04-16T07:18:07.994461Z  WARN iroh_bytes::provider: error: Error {
    context: "hash 370e2b3002be3b38b120f7b3be53da4cf646810e26f8f4a5018247c8188af5b2 range 65536..98304",
    source: Error {
        context: "g4hcwmacxy5trmja66z34u62jt3enaioe34pjjibqjd4qgek6wza",
        source: ParentHashMismatch(
            TreeNode::Branch(79, level=4),
        ),
    },
}
```

<!-- A summary of what this pull request achieves and a rough list of
changes. -->

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates if relevant.
- [ ] Tests if relevant.
  • Loading branch information
rklaehn authored Apr 16, 2024
1 parent 406280c commit d3fec78
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions iroh-bytes/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;

use anyhow::{Context, Result};
use bao_tree::io::fsm::{encode_ranges_validated, Outboard};
use bao_tree::io::EncodeError;
use futures::future::BoxFuture;
use iroh_base::rpc::RpcError;
use iroh_io::stats::{
Expand Down Expand Up @@ -487,34 +488,50 @@ pub enum SentStatus {
NotFound,
}

/// Send a
/// Send a blob to the client.
pub async fn send_blob<D: Map, W: AsyncStreamWriter>(
db: &D,
name: Hash,
hash: Hash,
ranges: &RangeSpec,
mut writer: W,
) -> Result<(SentStatus, u64, SliceReaderStats)> {
match db.get(&name).await? {
match db.get(&hash).await? {
Some(entry) => {
let outboard = entry.outboard().await?;
let size = outboard.tree().size();
let mut file_reader = TrackingSliceReader::new(entry.data_reader().await?);
writer.write(size.to_le_bytes().as_slice()).await?;
let res = encode_ranges_validated(
encode_ranges_validated(
&mut file_reader,
outboard,
&ranges.to_chunk_ranges(),
writer,
)
.await;
debug!("done sending blob {} {:?}", name, res);
res?;
.await
.map_err(|e| encode_error_to_anyhow(e, &hash))?;

Ok((SentStatus::Sent, size, file_reader.stats()))
}
_ => {
debug!("blob not found {}", hex::encode(name));
debug!("blob not found {}", hash.to_hex());
Ok((SentStatus::NotFound, 0, SliceReaderStats::default()))
}
}
}

fn encode_error_to_anyhow(err: EncodeError, hash: &Hash) -> anyhow::Error {
match err {
EncodeError::LeafHashMismatch(x) => anyhow::Error::from(EncodeError::LeafHashMismatch(x))
.context(format!("hash {} offset {}", hash.to_hex(), x.to_bytes())),
EncodeError::ParentHashMismatch(n) => {
let r = n.chunk_range();
anyhow::Error::from(EncodeError::ParentHashMismatch(n)).context(format!(
"hash {} range {}..{}",
hash.to_hex(),
r.start.to_bytes(),
r.end.to_bytes()
))
}
e => anyhow::Error::from(e).context(format!("hash {}", hash.to_hex())),
}
}

0 comments on commit d3fec78

Please sign in to comment.