Skip to content

Commit

Permalink
Merge pull request autonomys#3295 from autonomys/improve-cache-logging
Browse files Browse the repository at this point in the history
Improve cache logging byi inheriting span in blocking tasks
  • Loading branch information
nazar-pc authored Dec 9, 2024
2 parents 466e3bd + 765b201 commit 9bc35f4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/subspace-farmer/src/disk_piece_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use subspace_farmer_components::file_ext::FileExt;
use thiserror::Error;
use tokio::runtime::Handle;
use tokio::task;
use tracing::{debug, info, warn};
use tracing::{debug, info, warn, Span};

/// How many pieces should be skipped before stopping to check the rest of contents, this allows to
/// not miss most of the pieces after one or two corrupted pieces
Expand Down Expand Up @@ -129,7 +129,10 @@ impl farm::PieceCache for DiskPieceCache {
> {
let this = self.clone();
let (mut sender, receiver) = mpsc::channel(100_000);
let span = Span::current();
let read_contents = task::spawn_blocking(move || {
let _guard = span.enter();

let contents = this.contents();
for (piece_cache_offset, maybe_piece) in contents {
if let Err(error) =
Expand Down Expand Up @@ -175,8 +178,13 @@ impl farm::PieceCache for DiskPieceCache {
offset: PieceCacheOffset,
) -> Result<Option<PieceIndex>, FarmError> {
let piece_cache = self.clone();
let span = Span::current();
Ok(AsyncJoinOnDrop::new(
task::spawn_blocking(move || piece_cache.read_piece_index(offset)),
task::spawn_blocking(move || {
let _guard = span.enter();

piece_cache.read_piece_index(offset)
}),
false,
)
.await??)
Expand All @@ -186,18 +194,28 @@ impl farm::PieceCache for DiskPieceCache {
&self,
offset: PieceCacheOffset,
) -> Result<Option<(PieceIndex, Piece)>, FarmError> {
let span = Span::current();

// TODO: On Windows spawning blocking task that allows concurrent reads causes huge memory
// usage. No idea why it happens, but not spawning anything at all helps for some reason.
// Someone at some point should figure it out and fix, but it will probably be not me
// (Nazar).
// See https://github.com/autonomys/subspace/issues/2813 and linked forum post for details.
// This TODO exists in multiple files
if cfg!(windows) {
Ok(task::block_in_place(|| self.read_piece(offset))?)
Ok(task::block_in_place(|| {
let _guard = span.enter();

self.read_piece(offset)
})?)
} else {
let piece_cache = self.clone();
Ok(AsyncJoinOnDrop::new(
task::spawn_blocking(move || piece_cache.read_piece(offset)),
task::spawn_blocking(move || {
let _guard = span.enter();

piece_cache.read_piece(offset)
}),
false,
)
.await??)
Expand Down

0 comments on commit 9bc35f4

Please sign in to comment.