Skip to content
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
4 changes: 4 additions & 0 deletions changelog.d/1952.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Fixed

- Keep local edits when their sync entry is too large, and show the affected file in daemon status and logs.
- Large disk edits store one copy of the new document when it matches the applied result, so more Private edits fit within the sealed entry limit.
3 changes: 3 additions & 0 deletions runtime/crates/daemon/src/runtime/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub(super) fn run(
mut lock: mdbn_local_host::HostLock,
) {
let GrantSignals { grants, changes } = signals;
rep.set_oversized_file_warning(|path| {
tracing::warn!(path = ?path, code = "too_large", "file change refused by sync");
});
let mut ingest = VecDeque::new();
let mut active_log = None;
let mut last_heartbeat = std::time::Instant::now();
Expand Down
77 changes: 74 additions & 3 deletions runtime/crates/daemon/src/runtime/notices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(super) fn file_notices(rep: &Replica<Store>) -> Vec<crate::control::Notice>
use mdbn_wire::client::HoldReason;
let unsupported = &rep.store().inner().stats.unsupported_paths;
let failures = rep.attachment_ingest_failures();
let edits = rep.refused_files();
let (mut holds, error) = match rep.store().holds() {
Ok(holds) => (holds, None),
Err(_) => (
Expand All @@ -20,7 +21,11 @@ pub(super) fn file_notices(rep: &Replica<Store>) -> Vec<crate::control::Notice>
),
};
holds.sort_by_key(|hold| hold.reason != HoldReason::UnknownProvenance);
let total = unsupported.len() + failures.len() + holds.len() + usize::from(error.is_some());
let total = unsupported.len()
+ failures.len()
+ edits.len()
+ holds.len()
+ usize::from(error.is_some());
let skipped = unsupported
.iter()
.map(|(path, reason)| crate::control::Notice {
Expand All @@ -36,6 +41,7 @@ pub(super) fn file_notices(rep: &Replica<Store>) -> Vec<crate::control::Notice>
.unwrap_or_else(|| problem.code.clone()),
message: problem.message.clone(),
});
let edits = edits.map(|(path, problem, deleted)| edit_refused_notice(path, problem, deleted));
let held = holds.into_iter().map(|hold| {
let collision = rep
.store()
Expand All @@ -62,7 +68,7 @@ pub(super) fn file_notices(rep: &Replica<Store>) -> Vec<crate::control::Notice>
match hold.reason {
HoldReason::Conflict => "Changed here and elsewhere; file held.",
HoldReason::UnknownProvenance => {
"mdbase could not tell which version this file was based on; file held on this computer."
"Changed by a program mdbase does not recognise; file held."
}
HoldReason::DeletedElsewhere => "Deleted on another device; file held.",
HoldReason::ReadOnly => "Changed while read-only; file held.",
Expand All @@ -78,11 +84,46 @@ pub(super) fn file_notices(rep: &Replica<Store>) -> Vec<crate::control::Notice>
}
});
limited_file_notices(
error.into_iter().chain(skipped).chain(refused).chain(held),
error
.into_iter()
.chain(skipped)
.chain(refused)
.chain(edits)
.chain(held),
total,
)
}

/// A file whose change was rejected; the disk keeps what was written there.
fn edit_refused_notice(
path: &str,
problem: &mdbn_wire::client::Problem,
deleted: bool,
) -> crate::control::Notice {
let why = match (problem.code.as_str(), problem.reason.as_deref()) {
(_, Some("refs")) => "it refers to too many files",
(_, Some("attachment_objects_missing")) => "a file it attaches could not be uploaded",
("too_large", _) => "it is too large",
_ => "it is not a valid change",
};
let message = if deleted {
format!(
"This deletion could not be synced ({why}). The file is deleted on this \
device only and still exists elsewhere. Put the file back to sync it again."
)
} else {
format!(
"This change could not be synced ({why}). The content is kept in the file. \
Edit the file to try again."
)
};
crate::control::Notice {
path: Some(path.into()),
code: "edit_refused".into(),
message,
}
}

fn limited_file_notices(
notices: impl Iterator<Item = crate::control::Notice>,
total: usize,
Expand Down Expand Up @@ -112,6 +153,36 @@ fn limited_file_notices(

#[cfg(test)]
mod tests {
#[test]
fn a_refused_edit_is_noticed_on_its_file() {
let problem = mdbn_replica::api::ErrorCode::TooLarge
.problem_with_reason("item_bytes", "the log refused this change");
let notice = super::edit_refused_notice("notes/big.md", &problem, false);
assert_eq!(notice.path.as_deref(), Some("notes/big.md"));
assert_eq!(notice.code, "edit_refused");
assert_eq!(
notice.message,
"This change could not be synced (it is too large). The content is kept in \
the file. Edit the file to try again."
);
let missing = mdbn_replica::api::ErrorCode::NotFound
.problem_with_reason("attachment_objects_missing", "upload it again");
let notice = super::edit_refused_notice("media/a.png", &missing, false);
assert!(
notice
.message
.contains("(a file it attaches could not be uploaded)")
);
let invalid = mdbn_replica::api::ErrorCode::InvalidRequest.problem("refused");
let notice = super::edit_refused_notice("notes/gone.md", &invalid, true);
assert_eq!(
notice.message,
"This deletion could not be synced (it is not a valid change). The file is \
deleted on this device only and still exists elsewhere. Put the file back to \
sync it again."
);
}

#[test]
fn file_notices_bound_work_and_bytes_without_hiding_the_omitted_count() {
let mapped = std::cell::Cell::new(0);
Expand Down
49 changes: 41 additions & 8 deletions runtime/crates/daemon/tests/fixtures/sync_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,47 @@ pub async fn run_private_approval(log: &str, dir: &Path) {
assert!(
matches!(approval(&ra, Op::Pending(sa)).await, Ok(Reply::Pending(list)) if list.is_empty())
);
std::fs::write(dir.join(b.label).join("from-b.md"), "private on B\n").unwrap();
converge(
&dir.join(a.label).join("from-b.md"),
"private on B\n",
Duration::from_secs(60),
)
.await;
println!("PASS: B keyed by A; notes open both ways");
// A real file watcher, two Private replicas, and the sealed log limit.
let path = "from-b.md";
let local = dir.join(b.label).join(path);
let peer = dir.join(a.label).join(path);
let original = format!("{}\n", "x".repeat(99)).repeat(3450);
let edited = format!("{original}{}\n", "y".repeat(99));
let wait = Duration::from_secs(60);
for doc in [&original, &edited] {
std::fs::write(&local, doc).unwrap();
converge_bytes(&peer, doc.as_bytes(), wait).await;
}

let oversized = format!("{edited}{}", "z".repeat(400_000));
std::fs::write(&local, &oversized).unwrap();
tokio::time::timeout(wait, async {
loop {
let (_, notices) = rb.telemetry().await.unwrap();
if notices
.iter()
.any(|n| n.code == "edit_refused" && n.path.as_deref() == Some(path))
{
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
})
.await
.expect("the refused Private edit must be visible in daemon status");
assert!(std::fs::read(&local).unwrap() == oversized.as_bytes());
assert!(std::fs::read(&peer).unwrap() == edited.as_bytes());

std::fs::write(&local, "corrected on B\n").unwrap();
converge_bytes(&peer, b"corrected on B\n", wait).await;
assert!(
!rb.telemetry()
.await
.unwrap()
.1
.iter()
.any(|n| n.code == "edit_refused" && n.path.as_deref() == Some(path))
);
ra.stop().await;
rb.stop().await;
}
Expand Down
1 change: 1 addition & 0 deletions runtime/crates/daemon/tests/store_format_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ fn pending() -> PendingRow {
grant: None,
uploads: Vec::new(),
refs: Vec::new(),
submitted: false,
}
}

Expand Down
1 change: 1 addition & 0 deletions runtime/crates/daemon/tests/web_replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ fn dormant_local_capture_is_rejected_on_ordinary_host_reopen() {
grant: Some(owner),
uploads: Vec::new(),
refs: Vec::new(),
submitted: true,
}],
..Tx::default()
})
Expand Down
1 change: 1 addition & 0 deletions runtime/crates/replica/src/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ pub fn pending_row(order: u64, m: u64) -> PendingRow {
grant: None,
uploads: Vec::new(),
refs: Vec::new(),
submitted: false,
}
}

Expand Down
20 changes: 20 additions & 0 deletions runtime/crates/replica/src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ pub struct Faults {
/// Fault (lost objects): the next this-many appends are refused
/// `refs_missing`, without being performed.
pub refs_missing_appends: u32,
/// Appends carrying an item larger than this are refused `too_large` /
/// `item_bytes`, as the service does, without being performed.
pub item_bytes_limit: Option<usize>,
/// The next appends fail with these errors in order, without being performed.
pub fail_appends: Vec<LogError>,
/// Every `put_object` fails with this error, without being performed.
pub put_object_error: Option<LogError>,
/// Every `put_snapshot` fails with this error, without being performed.
Expand Down Expand Up @@ -691,6 +696,21 @@ impl LogClient for FakeLog {
missing: vec![mdbn_wire::common::B32([0xee; 32])],
});
}
if let LogRequest::Append(p) = &request {
if !self.faults.fail_appends.is_empty() {
return Err(self.faults.fail_appends.remove(0));
}
if let Some(limit) = self.faults.item_bytes_limit
&& p.items.iter().any(|i| i.0.len() > limit)
{
return Err(LogError::Service {
code: LogErrorCode::TooLarge,
reason: Some("item_bytes".into()),
retry_after_ms: None,
missing: Vec::new(),
});
}
}
let lie = self.faults.lie_behind > 0
&& matches!(&request, LogRequest::Read(p) if p.kinds.is_none());
let mut reply = self.service.handle(&self.client, request);
Expand Down
1 change: 1 addition & 0 deletions runtime/crates/replica/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ mod retained_boundary_tests {
grant: None,
uploads: Vec::new(),
refs: Vec::new(),
submitted: false,
}
}

Expand Down
Loading
Loading