mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-03 23:41:17 -05:00
Catalog: a snapshot already in this format is the upgrade done, and the later upgrade's tests and docs are exact
A library left in its old format whose snapshot is in this build's format already (a write that landed and reported a failure) no longer fails every snapshot until it is reopened: it is the upgrade done, with nothing to keep. Tests now cover the files kept at open's length and seq when changes were made before the first attempt, the snapshot that landed after its files were kept, no second copy on a retry, the background worker's result and telling once. The docs say what the kept files hold when a session already added changes in the old format, what going back loses, and that a snapshot at close or a library switch has no one to tell.
This commit is contained in:
@@ -228,7 +228,9 @@ pub struct Journal {
|
||||
behind: Option<u32>,
|
||||
/// What the library was at open while `behind` is set: the `seq` its files held and the
|
||||
/// length of its log. Appends only add to the log after that, so the first `log_len` bytes
|
||||
/// and the snapshot are what an older build left, whatever is appended meanwhile.
|
||||
/// and the snapshot are the library as this session opened it, whatever is appended
|
||||
/// meanwhile (an earlier session of this build may have appended already, while the library
|
||||
/// stayed in its old format: that is in them, and an older build may refuse it).
|
||||
behind_at: (u64, u64),
|
||||
/// The files [`Journal::keep_before_upgrade`] kept for a snapshot that is not written yet,
|
||||
/// and, once that snapshot is, the names for [`Journal::take_kept`].
|
||||
@@ -732,9 +734,12 @@ impl Journal {
|
||||
/// they were then, before a snapshot upgrades it: the snapshot as it is (nothing writes it
|
||||
/// until the upgrade) and the log up to its length at open (what was appended since is this
|
||||
/// build's, which an older build may refuse). The names are those the open would have kept
|
||||
/// them under, so they are written again as they are when a snapshot failed and is tried
|
||||
/// again, and files kept for an upgrade that did not land are never left behind under other
|
||||
/// names. An error means they could not be kept (or read): the snapshot is not written, so
|
||||
/// them under, so files kept for an upgrade that did not land are never left behind under
|
||||
/// other names. Kept once: when a snapshot failed and is tried again, they stay as they are
|
||||
/// (the failed one may have landed all the same, and the snapshot is then no longer what
|
||||
/// could be kept). A snapshot that is already in this build's format (that landed after its
|
||||
/// files were kept, or before them, reporting a failure) is the upgrade done, with nothing
|
||||
/// to keep. An error means they could not be kept (or read): the snapshot is not written, so
|
||||
/// the library stays as it was, with its way back.
|
||||
fn keep_before_upgrade(&mut self) -> Result<()> {
|
||||
let Some(from) = self.behind else { return Ok(()) };
|
||||
@@ -742,12 +747,16 @@ impl Journal {
|
||||
return Ok(());
|
||||
}
|
||||
let snap = self.store.read(SNAPSHOT).map_err(io)?;
|
||||
// (a write of the snapshot that failed after it landed leaves one that is not what was
|
||||
// to be kept under this name)
|
||||
if let Some(bytes) = snap.as_deref()
|
||||
&& serde_json::from_slice::<SnapHeader>(bytes).ok().map(|h| h.version) != Some(from)
|
||||
{
|
||||
return Err(CatalogError::Io(format!("the snapshot is no longer in format v{from}, and can't be kept as it was")));
|
||||
if let Some(bytes) = snap.as_deref() {
|
||||
match serde_json::from_slice::<SnapHeader>(bytes).ok().map(|h| h.version) {
|
||||
Some(v) if v == from => {}
|
||||
// the upgrade is written already: the snapshot after it is a plain one
|
||||
Some(VERSION) => {
|
||||
self.upgraded_later();
|
||||
return Ok(());
|
||||
}
|
||||
_ => return Err(CatalogError::Io(format!("the snapshot is no longer in format v{from}, and can't be kept as it was"))),
|
||||
}
|
||||
}
|
||||
let mut log = self.store.read(LOG).map_err(io)?.unwrap_or_default();
|
||||
let (seq, log_len) = self.behind_at;
|
||||
|
||||
@@ -539,18 +539,19 @@ fn the_files_a_later_snapshot_keeps_are_told_once_it_is_written() {
|
||||
assert!(j.take_kept().is_empty(), "once");
|
||||
}
|
||||
|
||||
/// Given a library left in its old format whose snapshot failed after its files were kept, when
|
||||
/// it goes on (more changes are appended, in this build's ops) and a later snapshot upgrades it,
|
||||
/// Given a library opened in its old format (its upgrade could not be written) that is then
|
||||
/// changed, in this build's ops, before any snapshot is tried, when a later snapshot upgrades it,
|
||||
/// then the files kept are the library as it was at open, under the names the open would have
|
||||
/// given them: nothing piles up, and an older build can read them whatever was appended since.
|
||||
/// given them: not the log as it is now, and nothing piles up. (An older build can read them
|
||||
/// whatever was appended since.)
|
||||
#[test]
|
||||
fn files_kept_by_a_later_snapshot_are_the_library_as_it_was_at_open() {
|
||||
static FULL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
|
||||
let (m, snap, log) = v6_library();
|
||||
let store = Refusing { files: m.clone(), refuse: |name| name == SNAPSHOT && FULL.load(std::sync::atomic::Ordering::SeqCst) };
|
||||
let (mut j, c, _) = Journal::open(Box::new(store)).unwrap();
|
||||
assert!(j.snapshot(&c).is_err());
|
||||
j.append(&[Op::SetLabelName { label: ColorLabel::ALL[0], name: Some("x".into()) }]).unwrap();
|
||||
assert!(m.get(LOG).unwrap().len() > log.len(), "the log has grown");
|
||||
assert!(j.snapshot(&c).is_err(), "still full");
|
||||
FULL.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||
j.snapshot(&c).unwrap();
|
||||
@@ -559,20 +560,89 @@ fn files_kept_by_a_later_snapshot_are_the_library_as_it_was_at_open() {
|
||||
assert_eq!(m.files.lock().unwrap().keys().filter(|k| k.contains(".v6-")).count(), 2, "{:?}", m.files.lock().unwrap().keys());
|
||||
}
|
||||
|
||||
/// Given a library left in its old format whose snapshot write failed after it landed (the share
|
||||
/// dropped the answer), when a later snapshot would keep its files, then the snapshot that is
|
||||
/// there is not kept under the old format's name, and nothing is deleted.
|
||||
/// Given a library left in its old format whose snapshot is in this build's format already (a
|
||||
/// write that landed and reported a failure, before anything was kept), when snapshots are
|
||||
/// written, then each of them works: the upgrade is done, with nothing to keep (the old files
|
||||
/// are gone from under that name), and nothing is refused until the library is reopened.
|
||||
#[test]
|
||||
fn a_snapshot_that_landed_is_not_kept_as_the_old_format() {
|
||||
fn a_snapshot_that_landed_is_the_upgrade_done() {
|
||||
static FULL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
|
||||
let (m, _, log) = v6_library();
|
||||
let store = Refusing { files: m.clone(), refuse: |name| name == SNAPSHOT && FULL.load(std::sync::atomic::Ordering::SeqCst) };
|
||||
let (mut j, c, _) = Journal::open(Box::new(store)).unwrap();
|
||||
// the upgrade landed after all, though the write said it failed
|
||||
m.set(SNAPSHOT, b"{\"format\":\"lightcraft-catalog\",\"version\":99,\"seq\":8,\"catalog\":{}}\n".to_vec());
|
||||
let e = j.snapshot(&c).unwrap_err();
|
||||
assert!(e.to_string().contains("no longer in format"), "{e}");
|
||||
FULL.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||
m.set(SNAPSHOT, format!("{{\"format\":\"lightcraft-catalog\",\"version\":{VERSION},\"seq\":8,\"catalog\":{}}}\n", c.to_snapshot()).into_bytes());
|
||||
j.snapshot(&c).unwrap();
|
||||
j.snapshot(&c).unwrap();
|
||||
assert!(j.take_kept().is_empty());
|
||||
assert_eq!(m.get(LOG), Some(log));
|
||||
assert!(!m.files.lock().unwrap().keys().any(|k| k.starts_with("catalog.snap.v")), "{:?}", m.files.lock().unwrap().keys());
|
||||
assert_eq!(snapshot_version(&m), u64::from(VERSION));
|
||||
assert!(!m.files.lock().unwrap().keys().any(|k| k.contains(".v6-")), "nothing kept from a snapshot that is not the old one");
|
||||
let _ = log;
|
||||
}
|
||||
|
||||
/// Given a library left in its old format, when a snapshot lands after its files were kept but
|
||||
/// reports a failure, and the snapshot is tried again, then the retry goes through, and the files
|
||||
/// told are the old library, not the snapshot that is there now.
|
||||
#[test]
|
||||
fn a_snapshot_that_landed_after_its_files_were_kept_is_tried_again() {
|
||||
static LAND: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
|
||||
struct Landing {
|
||||
inner: Refusing,
|
||||
}
|
||||
impl Store for Landing {
|
||||
fn read(&mut self, name: &str) -> std::io::Result<Option<Vec<u8>>> {
|
||||
self.inner.read(name)
|
||||
}
|
||||
fn write_atomic(&mut self, name: &str, data: &[u8]) -> std::io::Result<()> {
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
if name == SNAPSHOT && LAND.load(SeqCst) == 1 {
|
||||
// lands, and says it did not (the share dropped the answer)
|
||||
self.inner.files.write_atomic(name, data)?;
|
||||
LAND.store(2, SeqCst);
|
||||
return Err(std::io::Error::other("the share went away"));
|
||||
}
|
||||
self.inner.write_atomic(name, data)
|
||||
}
|
||||
fn append(&mut self, name: &str, data: &[u8]) -> std::io::Result<()> {
|
||||
self.inner.append(name, data)
|
||||
}
|
||||
fn truncate(&mut self, name: &str, len: u64) -> std::io::Result<()> {
|
||||
self.inner.truncate(name, len)
|
||||
}
|
||||
fn remove(&mut self, name: &str) -> std::io::Result<()> {
|
||||
self.inner.remove(name)
|
||||
}
|
||||
fn describe(&self) -> String {
|
||||
"landing".into()
|
||||
}
|
||||
}
|
||||
let (m, snap, log) = v6_library();
|
||||
let store =
|
||||
Landing { inner: Refusing { files: m.clone(), refuse: |name| name == SNAPSHOT && LAND.load(std::sync::atomic::Ordering::SeqCst) == 0 } };
|
||||
let (mut j, c, _) = Journal::open(Box::new(store)).unwrap();
|
||||
LAND.store(1, std::sync::atomic::Ordering::SeqCst);
|
||||
assert!(j.snapshot(&c).is_err(), "landed, and said it did not");
|
||||
assert_eq!(snapshot_version(&m), u64::from(VERSION), "it did land");
|
||||
j.snapshot(&c).unwrap();
|
||||
assert_eq!(j.take_kept(), ["catalog.snap.v6-8", "catalog.log.v6-8"]);
|
||||
assert_eq!((m.get("catalog.snap.v6-8"), m.get("catalog.log.v6-8")), (Some(snap), Some(log)), "the old library, not the snapshot that landed");
|
||||
assert!(j.take_kept().is_empty(), "told once");
|
||||
}
|
||||
|
||||
/// Given a library left in its old format whose snapshot failed after its files were kept, when
|
||||
/// the snapshot is tried again (a compaction retries as the log grows), then the files are not
|
||||
/// copied again: a large library is not written out in full at every retry.
|
||||
#[test]
|
||||
fn files_kept_for_a_failed_snapshot_are_not_written_again_on_a_retry() {
|
||||
static FULL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
|
||||
let (m, _, _) = v6_library();
|
||||
let store = Refusing { files: m.clone(), refuse: |name| name == SNAPSHOT && FULL.load(std::sync::atomic::Ordering::SeqCst) };
|
||||
let (mut j, c, _) = Journal::open(Box::new(store)).unwrap();
|
||||
assert!(j.snapshot(&c).is_err());
|
||||
m.set("catalog.snap.v6-8", b"marker".to_vec());
|
||||
assert!(j.snapshot(&c).is_err(), "still full");
|
||||
assert_eq!(m.get("catalog.snap.v6-8"), Some(b"marker".to_vec()), "not written again");
|
||||
FULL.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||
j.snapshot(&c).unwrap();
|
||||
assert_eq!(j.take_kept(), ["catalog.snap.v6-8", "catalog.log.v6-8"]);
|
||||
}
|
||||
|
||||
@@ -771,8 +771,6 @@ impl Session {
|
||||
log::error!("library: compaction: {e}");
|
||||
lib.last_error = Some(format!("compaction: {e}"));
|
||||
}
|
||||
// (a compaction that wrote its snapshot at once, where there is no worker thread)
|
||||
note_upgrade_kept(lib);
|
||||
let due = self.library.as_ref().is_some_and(|l| l.presets_saved.differs(self) && !l.blocked.contains(&"presets.json"));
|
||||
if !due {
|
||||
return Ok(());
|
||||
|
||||
@@ -241,40 +241,77 @@ fn an_upgraded_library_says_where_what_it_was_is_kept() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Given an older library that could not keep its files at open, when a later compaction upgrades
|
||||
/// it (the files can be kept now), then the person is told once where the library as it was is
|
||||
/// kept, as when the open upgrades it.
|
||||
#[test]
|
||||
fn a_later_upgrade_says_where_what_it_was_is_kept() {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-kept-later-{}", std::process::id()));
|
||||
/// An older library (format 6) that could not keep its files at open: a directory sits where the
|
||||
/// first kept file goes.
|
||||
fn library_that_could_not_keep_its_files(name: &str) -> std::path::PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(dir.join("catalog.snap.v6-8")).unwrap();
|
||||
std::fs::write(dir.join("catalog.snap.v6-8").join("in the way"), b"x").unwrap();
|
||||
std::fs::write(dir.join("catalog.snap"), include_str!("../../../catalog/src/fixtures/v6-catalog.snap.json")).unwrap();
|
||||
std::fs::write(dir.join("catalog.log"), include_str!("../../../catalog/src/fixtures/v6-catalog.log.jsonl")).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// The names of the files kept for format 6 in `dir`.
|
||||
fn kept_for_v6(dir: &std::path::Path, prefix: &str) -> Vec<String> {
|
||||
let mut names: Vec<String> = std::fs::read_dir(dir)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap())
|
||||
.filter(|e| e.path().is_file())
|
||||
.map(|e| e.file_name().to_string_lossy().into_owned())
|
||||
.filter(|n| n.starts_with(prefix) && !n.ends_with(".tmp"))
|
||||
.collect();
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
|
||||
/// Given an older library that could not keep its files at open, when a later compaction upgrades
|
||||
/// it (the files can be kept now), then the person is told once where the library as it was is
|
||||
/// kept, as when the open upgrades it, and a later compaction tells nothing more.
|
||||
#[test]
|
||||
fn a_later_upgrade_says_where_what_it_was_is_kept() {
|
||||
let dir = library_that_could_not_keep_its_files("kept-later");
|
||||
let mut s = Session::new();
|
||||
s.open_library(&dir, false).unwrap();
|
||||
assert!(s.take_library_warnings().iter().all(|w| !w.contains("upgraded")), "not upgraded at open");
|
||||
assert!(s.compact_library().is_err(), "its files can't be kept yet");
|
||||
std::fs::remove_dir_all(dir.join("catalog.snap.v6-8")).unwrap();
|
||||
s.compact_library().unwrap();
|
||||
let on_disk = |prefix: &str| {
|
||||
let mut names: Vec<String> = std::fs::read_dir(&dir)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap())
|
||||
.filter(|e| e.path().is_file())
|
||||
.map(|e| e.file_name().to_string_lossy().into_owned())
|
||||
.filter(|n| n.starts_with(prefix) && !n.ends_with(".tmp"))
|
||||
.collect();
|
||||
names.sort();
|
||||
names
|
||||
};
|
||||
let (snap, log) = (on_disk("catalog.snap.v6-"), on_disk("catalog.log.v6-"));
|
||||
let (snap, log) = (kept_for_v6(&dir, "catalog.snap.v6-"), kept_for_v6(&dir, "catalog.log.v6-"));
|
||||
assert_eq!((snap.len(), log.len()), (1, 1), "the files as they were are kept");
|
||||
let told = s.take_library_warnings();
|
||||
assert!(told.iter().any(|w| w.contains(&snap[0]) && w.contains(&log[0]) && w.contains("delete")), "{told:?}");
|
||||
let info = s.execute("library.info", &serde_json::json!({})).unwrap();
|
||||
assert!(info["settingsWarnings"].to_string().contains(&snap[0]), "{info}");
|
||||
s.compact_library().unwrap();
|
||||
assert!(s.take_library_warnings().is_empty(), "told once");
|
||||
s.close_library().unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The same, when the compaction is written by the background worker: the person is told once its
|
||||
/// result is taken (the frame loop's `persist`), not before.
|
||||
#[test]
|
||||
fn a_background_upgrade_says_where_what_it_was_is_kept() {
|
||||
let dir = library_that_could_not_keep_its_files("kept-background");
|
||||
let mut s = Session::new();
|
||||
s.open_library(&dir, false).unwrap();
|
||||
let _ = s.take_library_warnings();
|
||||
std::fs::remove_dir_all(dir.join("catalog.snap.v6-8")).unwrap();
|
||||
s.compact_soon();
|
||||
let started = std::time::Instant::now();
|
||||
let mut told = Vec::new();
|
||||
while told.is_empty() && started.elapsed() < std::time::Duration::from_secs(30) {
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
s.persist().unwrap();
|
||||
told = s.take_library_warnings();
|
||||
}
|
||||
let (snap, log) = (kept_for_v6(&dir, "catalog.snap.v6-"), kept_for_v6(&dir, "catalog.log.v6-"));
|
||||
assert_eq!((snap.len(), log.len()), (1, 1));
|
||||
assert!(told.iter().any(|w| w.contains(&snap[0]) && w.contains(&log[0])), "{told:?}");
|
||||
s.persist().unwrap();
|
||||
assert!(s.take_library_warnings().is_empty(), "told once");
|
||||
s.close_library().unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ Opening a library with a newer LightCraft rewrites it in that version's format,
|
||||
library was, byte for byte, beside it:
|
||||
|
||||
- `catalog.snap.v<format>-<seq>` and `catalog.log.v<format>-<seq>`, where `<format>` is the format they are in and
|
||||
`<seq>` the number of the last change the library held when it was upgraded (a library that had no log, or only a
|
||||
log, has only the one file). `Journal::open` reports their names (`LoadReport::kept`) and logs them.
|
||||
`<seq>` the number of the last change the library held when it was upgraded (or, where the upgrade was left for a
|
||||
later snapshot, when the library was opened; a library that had no log, or only a log, has only the one file). `Journal::open` reports their names (`LoadReport::kept`) and logs them.
|
||||
- The kept files stay unless the library is known to be in its old format still: once the upgrade is written,
|
||||
nothing that fails after it takes them away.
|
||||
- A library whose files can't be kept, or whose upgrade can't be written (a full disk, a folder that can't be written
|
||||
@@ -21,10 +21,12 @@ library was, byte for byte, beside it:
|
||||
disk after a failed write can't be read back to say whether the upgrade was written, the library is refused instead
|
||||
(its way back stays). A library left like that is upgraded by its next snapshot, during the session or when it is
|
||||
closed, and that snapshot keeps the files first too (and is not written when they can't be kept). When a snapshot
|
||||
during the session does that, LightCraft tells where the files are kept, as it does at open (a snapshot at close
|
||||
has no one left to tell: the files are in the folder, named as above). The files kept are the library as it was
|
||||
when it opened (the snapshot, and the log up to where it ended then): what was changed since is in the files that
|
||||
replace them, and never in the kept ones, so an older version opens them whatever was changed. Until then, changes
|
||||
during the session does that, LightCraft tells where the files are kept, as it does at open (a snapshot when the
|
||||
library is closed, or switched away from in Settings, has no one left to tell: the files are in the folder, named
|
||||
as above). The files kept are the library as it was when it opened (the snapshot, and the log up to where it ended
|
||||
then): what was changed since is in the files that replace them, not in the kept ones. (An earlier session of this
|
||||
version may have added changes while the library stayed in its old format: they are in the log it opened with, and
|
||||
an older version may refuse them.) Until then, changes
|
||||
go on being appended to the library as it is, so that they can be saved at all where there is no room for a copy of
|
||||
its files. (If the session ends without that snapshot, the next open keeps the log as it is then, which holds
|
||||
those changes, and tells so; the files kept earlier stay beside it.)
|
||||
@@ -37,5 +39,6 @@ library was, byte for byte, beside it:
|
||||
wanted. The browser build keeps none (it always runs the newest version).
|
||||
|
||||
**To go back**, with LightCraft closed: move `catalog.snap` and `catalog.log` away, then rename the kept pair to
|
||||
`catalog.snap` and `catalog.log`. The older version opens the library as it was when it was upgraded; changes made
|
||||
since, in the newer version, are in the files that were moved away and are not carried back.
|
||||
`catalog.snap` and `catalog.log`. The older version opens the library as it was when it was upgraded (where the upgrade came
|
||||
from a later snapshot: as it was when it was opened, so the changes made between that and the upgrade are not carried
|
||||
back either); changes made since, in the newer version, are in the files that were moved away.
|
||||
|
||||
Reference in New Issue
Block a user