Skip to content

Commit 169261a

Browse files
committed
Get rid of the weak reference
1 parent 161a70b commit 169261a

File tree

5 files changed

+35
-46
lines changed

5 files changed

+35
-46
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ reflink-copy = "0.1.24"
4545
irpc = { version = "0.5.0", features = ["rpc", "quinn_endpoint_setup", "message_spans", "stream", "derive"], default-features = false }
4646
iroh-metrics = { version = "0.35" }
4747
entity-manager = { path = "../entity-manager" }
48+
atomic_refcell = "0.1.13"
4849

4950
[dev-dependencies]
5051
clap = { version = "4.5.31", features = ["derive"] }

src/store/fs.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
//! safely shut down as well. Any store refs you are holding will be inoperable
6565
//! after this.
6666
use std::{
67-
collections::{HashMap, HashSet},
6867
fmt, fs,
6968
future::Future,
7069
io::Write,
@@ -93,7 +92,7 @@ use meta::{list_blobs, Snapshot};
9392
use n0_future::{future::yield_now, io};
9493
use nested_enum_utils::enum_conversions;
9594
use range_collections::range_set::RangeSetRange;
96-
use tokio::task::{Id, JoinError, JoinSet};
95+
use tokio::task::{JoinError, JoinSet};
9796
use tracing::{error, instrument, trace};
9897

9998
use crate::{
@@ -117,7 +116,7 @@ use crate::{
117116
},
118117
};
119118
mod bao_file;
120-
use bao_file::{BaoFileHandle, BaoFileHandleWeak};
119+
use bao_file::BaoFileHandle;
121120
mod delete_set;
122121
mod entry_state;
123122
mod import;
@@ -259,7 +258,7 @@ impl HashContext {
259258
&self.ctx.options
260259
}
261260

262-
pub async fn lock(&self) -> tokio::sync::MutexGuard<'_, Option<BaoFileHandleWeak>> {
261+
pub async fn lock(&self) -> tokio::sync::MutexGuard<'_, Option<BaoFileHandle>> {
263262
self.slot.0.lock().await
264263
}
265264

@@ -377,14 +376,9 @@ async fn open_bao_file(
377376
/// An entry for each hash, containing a weak reference to a BaoFileHandle
378377
/// wrapped in a tokio mutex so handle creation is sequential.
379378
#[derive(Debug, Clone, Default)]
380-
pub(crate) struct Slot(Arc<tokio::sync::Mutex<Option<BaoFileHandleWeak>>>);
379+
pub(crate) struct Slot(Arc<tokio::sync::Mutex<Option<BaoFileHandle>>>);
381380

382381
impl Slot {
383-
pub async fn is_live(&self) -> bool {
384-
let slot = self.0.lock().await;
385-
slot.as_ref().map(|weak| !weak.is_dead()).unwrap_or(false)
386-
}
387-
388382
/// Get the handle if it exists and is still alive, otherwise load it from the database.
389383
/// If there is nothing in the database, create a new in-memory handle.
390384
///
@@ -396,14 +390,12 @@ impl Slot {
396390
T: Default,
397391
{
398392
let mut slot = self.0.lock().await;
399-
if let Some(weak) = &*slot {
400-
if let Some(handle) = weak.upgrade() {
401-
return Ok((handle, Default::default()));
402-
}
393+
if let Some(handle) = &*slot {
394+
return Ok((handle.clone(), Default::default()));
403395
}
404396
let handle = make().await;
405397
if let Ok((handle, _)) = &handle {
406-
*slot = Some(handle.downgrade());
398+
*slot = Some(handle.clone());
407399
}
408400
handle
409401
}
@@ -845,7 +837,7 @@ async fn finish_import_impl(import_data: ImportEntry, ctx: HashContext) -> io::R
845837
}
846838
}
847839
let guard = ctx.lock().await;
848-
let handle = guard.as_ref().and_then(|x| x.upgrade());
840+
let handle = guard.as_ref().map(|x| x.clone());
849841
// if I do have an existing handle, I have to possibly deal with observers.
850842
// if I don't have an existing handle, there are 2 cases:
851843
// the entry exists in the db, but we don't have a handle

src/store/fs/bao_file.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
io,
55
ops::Deref,
66
path::Path,
7-
sync::{Arc, Weak},
7+
sync::Arc,
88
};
99

1010
use bao_tree::{
@@ -503,27 +503,6 @@ impl BaoFileStorage {
503503
}
504504
}
505505

506-
/// A weak reference to a bao file handle.
507-
#[derive(Debug, Clone)]
508-
pub struct BaoFileHandleWeak(Weak<BaoFileHandleInner>);
509-
510-
impl BaoFileHandleWeak {
511-
/// Upgrade to a strong reference if possible.
512-
pub fn upgrade(&self) -> Option<BaoFileHandle> {
513-
let inner = self.0.upgrade()?;
514-
if let &BaoFileStorage::Poisoned = inner.storage.borrow().deref() {
515-
trace!("poisoned storage, cannot upgrade");
516-
return None;
517-
};
518-
Some(BaoFileHandle(inner))
519-
}
520-
521-
/// True if the handle is definitely dead.
522-
pub fn is_dead(&self) -> bool {
523-
self.0.strong_count() == 0
524-
}
525-
}
526-
527506
/// The inner part of a bao file handle.
528507
pub struct BaoFileHandleInner {
529508
pub(crate) storage: watch::Sender<BaoFileStorage>,
@@ -546,8 +525,8 @@ impl fmt::Debug for BaoFileHandleInner {
546525
#[derive(Debug, Clone, derive_more::Deref)]
547526
pub struct BaoFileHandle(Arc<BaoFileHandleInner>);
548527

549-
impl Drop for BaoFileHandle {
550-
fn drop(&mut self) {
528+
impl BaoFileHandle {
529+
pub fn persist(&mut self) {
551530
self.0.storage.send_if_modified(|guard| {
552531
if Arc::strong_count(&self.0) > 1 {
553532
return false;
@@ -564,6 +543,9 @@ impl Drop for BaoFileHandle {
564543
};
565544
let options = &self.options;
566545
let path = options.path.bitfield_path(&self.hash);
546+
if fs.bitfield.size == 8000000 {
547+
println!("PERSISTING THE TEST CASE FILE to {}", path.display());
548+
}
567549
trace!(
568550
"writing bitfield for hash {} to {}",
569551
self.hash,
@@ -582,6 +564,12 @@ impl Drop for BaoFileHandle {
582564
}
583565
}
584566

567+
impl Drop for BaoFileHandle {
568+
fn drop(&mut self) {
569+
self.persist();
570+
}
571+
}
572+
585573
/// A reader for a bao file, reading just the data.
586574
#[derive(Debug)]
587575
pub struct DataReader(BaoFileHandle);
@@ -753,11 +741,6 @@ impl BaoFileHandle {
753741
self.hash
754742
}
755743

756-
/// Downgrade to a weak reference.
757-
pub fn downgrade(&self) -> BaoFileHandleWeak {
758-
BaoFileHandleWeak(Arc::downgrade(&self.0))
759-
}
760-
761744
/// Write a batch and notify the db
762745
pub(super) async fn write_batch(
763746
&self,

src/store/fs/gc.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ mod tests {
192192
use std::{
193193
io::{self},
194194
path::Path,
195+
time::Duration,
195196
};
196197

197198
use bao_tree::{io::EncodeError, ChunkNum};
@@ -299,10 +300,15 @@ mod tests {
299300
let outboard_path = options.outboard_path(&bh);
300301
let sizes_path = options.sizes_path(&bh);
301302
let bitfield_path = options.bitfield_path(&bh);
303+
tokio::time::sleep(Duration::from_millis(100)).await; // allow for some time for the file to be written
302304
assert!(data_path.exists());
303305
assert!(outboard_path.exists());
304306
assert!(sizes_path.exists());
305-
assert!(bitfield_path.exists());
307+
assert!(
308+
bitfield_path.exists(),
309+
"Bitfield file {} should exist",
310+
bitfield_path.display()
311+
);
306312
gc_run_once(store, &mut live).await?;
307313
assert!(!data_path.exists());
308314
assert!(!outboard_path.exists());

0 commit comments

Comments
 (0)