Skip to content

Commit

Permalink
fix(iroh-docs): ensure docs db write txn gets closed regularly under …
Browse files Browse the repository at this point in the history
…all circumstances (#2474)

## Description

The docs db uses batching to reduce the number of write transactions.
Before this, under certain circumstances (lots of activity, exclusively
writes) the transaction would remain open for a long time.

This adds logic to modify to make sure that transactions will be
committed when they get too old in any case.

## Breaking Changes

None

## Notes & open questions

## Change checklist

- [x] Self-review.
- [x] ~~Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.~~
- [x] ~~Tests if relevant.~~
- [x] ~~All breaking changes documented.~~
  • Loading branch information
rklaehn authored Jul 8, 2024
1 parent 3002deb commit 235c69c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion iroh-docs/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
};

const ACTION_CAP: usize = 1024;
const MAX_COMMIT_DELAY: Duration = Duration::from_millis(500);
pub(crate) const MAX_COMMIT_DELAY: Duration = Duration::from_millis(500);

#[derive(derive_more::Debug, derive_more::Display)]
enum Action {
Expand Down
15 changes: 12 additions & 3 deletions iroh-docs/src/store/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{
num::NonZeroU64,
ops::Bound,
path::Path,
time::Duration,
};

use anyhow::{anyhow, Result};
Expand All @@ -17,6 +16,7 @@ use rand_core::CryptoRngCore;
use redb::{Database, DatabaseError, ReadableMultimapTable, ReadableTable, ReadableTableMetadata};

use crate::{
actor::MAX_COMMIT_DELAY,
keys::Author,
ranger::{Fingerprint, Range, RangeEntry},
sync::{Entry, EntrySignature, Record, RecordIdentifier, Replica, SignedEntry},
Expand Down Expand Up @@ -188,7 +188,7 @@ impl Store {
TransactionAndTables::new(tx)?
}
CurrentTransaction::Write(w) => {
if w.since.elapsed() > Duration::from_millis(500) {
if w.since.elapsed() > MAX_COMMIT_DELAY {
tracing::debug!("committing transaction because it's too old");
w.commit()?;
let tx = self.db.begin_write()?;
Expand Down Expand Up @@ -224,7 +224,16 @@ impl Store {
let tx = self.db.begin_write()?;
TransactionAndTables::new(tx)?
}
CurrentTransaction::Write(w) => w,
CurrentTransaction::Write(w) => {
if w.since.elapsed() > MAX_COMMIT_DELAY {
tracing::debug!("committing transaction because it's too old");
w.commit()?;
let tx = self.db.begin_write()?;
TransactionAndTables::new(tx)?
} else {
w
}
}
CurrentTransaction::Read(_) => {
let tx = self.db.begin_write()?;
TransactionAndTables::new(tx)?
Expand Down

0 comments on commit 235c69c

Please sign in to comment.