mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-25 20:33:50 +00:00
Fixed corrupted vector stores after an older version of AI Studio wrote to them
This commit is contained in:
2 files changed
+147
-9
No files matched your search
@@ -0,0 +1,28 @@
|
|||||||
|
# Qdrant Edge Tracker Journal
|
||||||
|
|
||||||
|
- Status: Active
|
||||||
|
- Introduced: 2026-11-25
|
||||||
|
- Remove after: 2027-05-25, once organizations no longer go back to AI Studio v26.11.1 or older, the last versions with qdrant-edge 0.8
|
||||||
|
- Code references:
|
||||||
|
- `runtime/src/qdrant_edge_database.rs` (`remove_tracker_journals`, `tracker_journals`, `TRACKER_JOURNAL_FILE`, `unload_store`, `Drop for QdrantEdgeDatabase`)
|
||||||
|
|
||||||
|
## User Impact
|
||||||
|
|
||||||
|
Since qdrant-edge 0.9.0, every storage of a vector store notes its pointer writes in the file `tracker_journal.dat` before it writes them, and applies this journal again whenever it opens, which repairs pointers a crash tore during a flush. After a flush, Qdrant Edge keeps the journal until it holds about 1,000 writes. qdrant-edge 0.8.0, which AI Studio v26.11.1 and older use, knows no journal and leaves it alone.
|
||||||
|
|
||||||
|
Someone may go back from a newer version of AI Studio to v26.11.1 or older, e.g., because their organization rolls back an update, or because a developer runs a development build and the installed release on the same data directory. When the older version then writes to a store, e.g., while it indexes a changed file, the journal still holds the pointers from before. The next load under qdrant-edge 0.9 sets them again, so they point into freed blocks. In a test with a real store of 318 MB, loading the store then failed with "Failed to decompress LZ4 value", or Qdrant Edge panicked while it applied the WAL. Either way, the store is lost until its embeddings are rebuilt.
|
||||||
|
|
||||||
|
## Compatibility Behavior
|
||||||
|
|
||||||
|
Whenever AI Studio unloads a store, it flushes it, closes it, and then removes every `tracker_journal.dat` below the store's directory. AI Studio unloads every store when it quits, installs an update, or restarts after restoring a backup, and it unloads a store before copying it into a backup, so backups contain no journal either. After a successful flush, the journal has nothing left to repair. When the flush fails, AI Studio keeps the store loaded and leaves its journals in place, since Qdrant Edge then still needs them.
|
||||||
|
|
||||||
|
When the operating system ends AI Studio without letting it quit, the journals stay. The next start of a version with qdrant-edge 0.9 applies them, which repairs what the crash may have torn, and AI Studio removes the journals again when it unloads the store. Only going back to an older version right after such a hard end can still break a store.
|
||||||
|
|
||||||
|
In the test, removing the journals before the older version wrote to the store, or before the newer version loaded it again, kept all payloads, filters, and points intact.
|
||||||
|
|
||||||
|
## Removal Checklist
|
||||||
|
|
||||||
|
- Remove `remove_tracker_journals`, `tracker_journals`, and the constant `TRACKER_JOURNAL_FILE`.
|
||||||
|
- Remove the call of `remove_tracker_journals` from `unload_store`. Keep `unload_store` and `Drop for QdrantEdgeDatabase` only if they still do more than flushing and closing the stores, which dropping a shard does by itself.
|
||||||
|
- Remove the tests `unloading_a_store_removes_its_tracker_journals_and_keeps_its_points` and `a_backup_copies_a_store_without_its_tracker_journals`.
|
||||||
|
- Update this document's status to `Removed`.
|
||||||
@@ -37,6 +37,9 @@ const WAL_DIRECTORY: &str = "wal";
|
|||||||
const WAL_FIRST_INDEX_FILE: &str = "first-index";
|
const WAL_FIRST_INDEX_FILE: &str = "first-index";
|
||||||
const WAL_FIRST_INDEX_FILE_TEMP: &str = "first-index.tmp";
|
const WAL_FIRST_INDEX_FILE_TEMP: &str = "first-index.tmp";
|
||||||
|
|
||||||
|
/// The file in which a storage of Qdrant Edge notes its pointer writes, see `remove_tracker_journals`.
|
||||||
|
const TRACKER_JOURNAL_FILE: &str = "tracker_journal.dat";
|
||||||
|
|
||||||
/// The directory below the data directory in which a backup collects its copies, one directory per
|
/// The directory below the data directory in which a backup collects its copies, one directory per
|
||||||
/// backup. The app removes a backup's directory once the backup is written.
|
/// backup. The app removes a backup's directory once the backup is written.
|
||||||
const BACKUP_STAGING_DIRECTORY: &str = ".backup-staging";
|
const BACKUP_STAGING_DIRECTORY: &str = ".backup-staging";
|
||||||
@@ -678,10 +681,9 @@ impl QdrantEdgeDatabase {
|
|||||||
|
|
||||||
/// Copies one store into the directory of a backup's copies, and returns how many bytes it copied.
|
/// Copies one store into the directory of a backup's copies, and returns how many bytes it copied.
|
||||||
///
|
///
|
||||||
/// A loaded store is flushed and unloaded first: everything it holds is on the disk then, and
|
/// A loaded store is unloaded first, see `unload_store`: everything it holds is on the disk then,
|
||||||
/// nothing changes its files while they are copied, since the lock of the database is held all
|
/// and nothing changes its files while they are copied, since the lock of the database is held
|
||||||
/// the while. The next request loads the store again. A store which was not created completely
|
/// all the while. A store which was not created completely is not copied.
|
||||||
/// is not copied.
|
|
||||||
fn copy_store_for_backup(&mut self, store_name: &str, data_directory: &Path, backup_id: &str) -> QdrantEdgeResult<u64> {
|
fn copy_store_for_backup(&mut self, store_name: &str, data_directory: &Path, backup_id: &str) -> QdrantEdgeResult<u64> {
|
||||||
let staging_path = backup_staging_path(data_directory, backup_id)?;
|
let staging_path = backup_staging_path(data_directory, backup_id)?;
|
||||||
if !staging_path.is_dir() {
|
if !staging_path.is_dir() {
|
||||||
@@ -693,11 +695,7 @@ impl QdrantEdgeDatabase {
|
|||||||
return Ok(0);
|
return Ok(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(shard) = self.shards.get(store_name) {
|
self.unload_store(store_name)?;
|
||||||
shard.flush()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
drop(self.shards.remove(store_name));
|
|
||||||
let relative_path = path.strip_prefix(data_directory).map_err(|_| {
|
let relative_path = path.strip_prefix(data_directory).map_err(|_| {
|
||||||
format!("The vector store '{store_name}' at '{}' does not lie below the data directory '{}'.", path.display(), data_directory.display())
|
format!("The vector store '{store_name}' at '{}' does not lie below the data directory '{}'.", path.display(), data_directory.display())
|
||||||
})?;
|
})?;
|
||||||
@@ -710,6 +708,21 @@ impl QdrantEdgeDatabase {
|
|||||||
Ok(copied_bytes)
|
Ok(copied_bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Unloads a store: flushes it, closes it, and removes its journals, see `remove_tracker_journals`.
|
||||||
|
///
|
||||||
|
/// When the flush fails, the store stays loaded, and its journals stay as well, since Qdrant Edge
|
||||||
|
/// then still needs them to repair the store. The next request loads an unloaded store again.
|
||||||
|
fn unload_store(&mut self, store_name: &str) -> QdrantEdgeResult<()> {
|
||||||
|
let Some(shard) = self.shards.get(store_name) else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
shard.flush()?;
|
||||||
|
drop(self.shards.remove(store_name));
|
||||||
|
remove_tracker_journals(&self.store_path(store_name)?, store_name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn delete_store(&mut self, store_name: &str) -> QdrantEdgeResult<()> {
|
fn delete_store(&mut self, store_name: &str) -> QdrantEdgeResult<()> {
|
||||||
self.shards.remove(store_name);
|
self.shards.remove(store_name);
|
||||||
|
|
||||||
@@ -726,6 +739,19 @@ impl QdrantEdgeDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for QdrantEdgeDatabase {
|
||||||
|
// Unloads every store, e.g., when AI Studio quits, installs an update, or restarts after a
|
||||||
|
// restore, see unload_store.
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let store_names = self.shards.keys().cloned().collect::<Vec<_>>();
|
||||||
|
for store_name in store_names {
|
||||||
|
if let Err(error) = self.unload_store(&store_name) {
|
||||||
|
warn!(Source = "Qdrant Edge"; "Could not flush vector store '{store_name}' while unloading it, so its journals stay for Qdrant Edge to repair the store: {error}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn store_directory_name(store_name: &str) -> String {
|
fn store_directory_name(store_name: &str) -> String {
|
||||||
let stable_id = store_name.trim_prefix("rag_");
|
let stable_id = store_name.trim_prefix("rag_");
|
||||||
format!("store_{stable_id}")
|
format!("store_{stable_id}")
|
||||||
@@ -1267,6 +1293,47 @@ fn acknowledge_legacy_wal(store_path: &Path, store_name: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Temporary compatibility shim until 2027-05-25:
|
||||||
|
// documentation/compatibility-shims/2026-11-qdrant-edge-tracker-journal.md
|
||||||
|
//
|
||||||
|
// Since qdrant-edge 0.9, every storage of a store notes its pointer writes in a journal before it
|
||||||
|
// writes them, and applies the journal again whenever it opens, which repairs pointers a crash tore.
|
||||||
|
// Older versions of AI Studio use qdrant-edge 0.8, which knows no journal: when one of them writes
|
||||||
|
// to the store, the journal keeps the pointers from before, and the next load under 0.9 sets them
|
||||||
|
// again. They then point into freed blocks, so loading or searching the store fails. Once a flush
|
||||||
|
// succeeded, the journal has nothing left to repair. The store must not be loaded while this runs.
|
||||||
|
fn remove_tracker_journals(store_path: &Path, store_name: &str) {
|
||||||
|
let journals = match tracker_journals(store_path) {
|
||||||
|
Ok(journals) => journals,
|
||||||
|
Err(error) => {
|
||||||
|
warn!(Source = "Qdrant Edge"; "Could not look for the journals of vector store '{store_name}', which breaks the store should an older version of AI Studio write to it: {error}");
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
for journal in journals {
|
||||||
|
if let Err(error) = fs::remove_file(&journal) {
|
||||||
|
warn!(Source = "Qdrant Edge"; "Could not remove the journal '{}' of vector store '{store_name}', which breaks the store should an older version of AI Studio write to it: {error}", journal.display());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every journal of Qdrant Edge below a directory, see `remove_tracker_journals`.
|
||||||
|
fn tracker_journals(path: &Path) -> std::io::Result<Vec<PathBuf>> {
|
||||||
|
let mut journals = Vec::new();
|
||||||
|
for entry in fs::read_dir(path)? {
|
||||||
|
let entry = entry?;
|
||||||
|
let file_type = entry.file_type()?;
|
||||||
|
if file_type.is_dir() {
|
||||||
|
journals.extend(tracker_journals(&entry.path())?);
|
||||||
|
} else if file_type.is_file() && entry.file_name() == TRACKER_JOURNAL_FILE {
|
||||||
|
journals.push(entry.path());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(journals)
|
||||||
|
}
|
||||||
|
|
||||||
fn validate_vector_size(vector_size: usize) -> QdrantEdgeResult<()> {
|
fn validate_vector_size(vector_size: usize) -> QdrantEdgeResult<()> {
|
||||||
if vector_size == 0 {
|
if vector_size == 0 {
|
||||||
return Err("Vector size must be greater than zero.".into());
|
return Err("Vector size must be greater than zero.".into());
|
||||||
@@ -1877,6 +1944,49 @@ mod tests {
|
|||||||
fs::remove_dir_all(test_directory).unwrap();
|
fs::remove_dir_all(test_directory).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An older version of AI Studio which writes to the store in between would otherwise break it,
|
||||||
|
/// cf. remove_tracker_journals.
|
||||||
|
#[test]
|
||||||
|
fn unloading_a_store_removes_its_tracker_journals_and_keeps_its_points() {
|
||||||
|
let test_directory = test_directory("journal-unload");
|
||||||
|
let database = store_with_one_point_per_axis(&test_directory);
|
||||||
|
let store_path = database.store_path(SEARCH_STORE).unwrap();
|
||||||
|
assert!(!tracker_journals(&store_path).unwrap().is_empty(), "the flush after inserting leaves a journal behind, or this test checks nothing");
|
||||||
|
|
||||||
|
drop(database);
|
||||||
|
assert!(tracker_journals(&store_path).unwrap().is_empty());
|
||||||
|
|
||||||
|
let mut database = QdrantEdgeDatabase::new(test_directory.clone());
|
||||||
|
let found = database.search_embedding(SEARCH_STORE, search_vector(), 3, None, &[]).unwrap();
|
||||||
|
assert_eq!(found_point_ids(&found), vec![POINT_X, POINT_Y, POINT_Z]);
|
||||||
|
|
||||||
|
drop(database);
|
||||||
|
fs::remove_dir_all(test_directory).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_backup_copies_a_store_without_its_tracker_journals() {
|
||||||
|
let data_directory = test_directory("backup-journal");
|
||||||
|
let base_path = data_directory.join("databases").join("vector_database");
|
||||||
|
let mut database = store_with_one_point_per_axis(&base_path);
|
||||||
|
let store_path = database.store_path(SEARCH_STORE).unwrap();
|
||||||
|
assert!(!tracker_journals(&store_path).unwrap().is_empty(), "the flush after inserting leaves a journal behind, or this test checks nothing");
|
||||||
|
|
||||||
|
let preparation = database.prepare_backup(&data_directory).unwrap();
|
||||||
|
database.copy_store_for_backup(SEARCH_STORE, &data_directory, &preparation.backup_id).unwrap();
|
||||||
|
let copy_base_path = PathBuf::from(&preparation.staging_path).join("databases").join("vector_database");
|
||||||
|
assert!(tracker_journals(&store_path).unwrap().is_empty(), "the store was unloaded for the copy");
|
||||||
|
assert!(tracker_journals(©_base_path).unwrap().is_empty(), "the copy takes no journal along");
|
||||||
|
|
||||||
|
let mut copy = QdrantEdgeDatabase::new(copy_base_path);
|
||||||
|
let found = copy.search_embedding(SEARCH_STORE, search_vector(), 3, None, &[]).unwrap();
|
||||||
|
assert_eq!(found_point_ids(&found), vec![POINT_X, POINT_Y, POINT_Z]);
|
||||||
|
|
||||||
|
drop(copy);
|
||||||
|
drop(database);
|
||||||
|
fs::remove_dir_all(data_directory).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
async fn a_request_waiting_for_the_database_leaves_the_runtime_free() {
|
async fn a_request_waiting_for_the_database_leaves_the_runtime_free() {
|
||||||
let request = optimize_qdrant_edge_store(
|
let request = optimize_qdrant_edge_store(
|
||||||
|
|||||||
Reference in new issue
Block a user