-
Notifications
You must be signed in to change notification settings - Fork 359
fix(memory): ingestion reliability and merge bloat #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slvnlrt
wants to merge
6
commits into
spacedriveapp:main
Choose a base branch
from
slvnlrt:pr/ingestion-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26b2635
fix(ingestion): make chunk completion deterministic
slvnlrt 07eed68
fix(ingestion): bound retries with backoff and quarantine
slvnlrt 8172442
fix(api): ingest delete removes the source file and purges progress
slvnlrt d623988
fix(memory): keep canonical content on merge instead of concatenating
slvnlrt e4a2eec
style: apply rustfmt
slvnlrt fd987e7
fix: address automated review feedback
slvnlrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| -- Retry budget for ingestion: bound retries, back them off, and quarantine | ||
| -- files that keep failing so the poll loop stops re-processing them forever. | ||
| ALTER TABLE ingestion_files ADD COLUMN attempts INTEGER NOT NULL DEFAULT 0; | ||
| ALTER TABLE ingestion_files ADD COLUMN next_attempt_at TIMESTAMP; | ||
| -- status now also takes the terminal value 'quarantined' (no CHECK constraint | ||
| -- exists on this column, so no schema change is needed beyond documenting it). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -221,6 +221,41 @@ pub(super) async fn upload_ingest_file( | |||||||||||||||||||
| Ok(Json(IngestUploadResponse { uploaded })) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Remove an ingest file from the source of truth (disk) and purge its tracking | ||||||||||||||||||||
| /// rows. The disk file is the loop's input; deleting only the DB row lets the | ||||||||||||||||||||
| /// next poll cycle re-discover the file and re-create the row ("reappears"). | ||||||||||||||||||||
| pub(super) async fn purge_ingest_file( | ||||||||||||||||||||
| pool: &sqlx::SqlitePool, | ||||||||||||||||||||
| ingest_dir: &Path, | ||||||||||||||||||||
| content_hash: &str, | ||||||||||||||||||||
| ) -> anyhow::Result<()> { | ||||||||||||||||||||
| // Look up the on-disk filename for this hash, then remove the file. | ||||||||||||||||||||
| if let Some(filename) = sqlx::query_scalar::<_, String>( | ||||||||||||||||||||
| "SELECT filename FROM ingestion_files WHERE content_hash = ?", | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| .bind(content_hash) | ||||||||||||||||||||
| .fetch_optional(pool) | ||||||||||||||||||||
| .await? | ||||||||||||||||||||
| { | ||||||||||||||||||||
| let path = ingest_dir.join(&filename); | ||||||||||||||||||||
| match tokio::fs::remove_file(&path).await { | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this deletes a disk path derived from
Suggested change
|
||||||||||||||||||||
| Ok(()) => {} | ||||||||||||||||||||
| Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} | ||||||||||||||||||||
| Err(e) => return Err(e.into()), | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| sqlx::query("DELETE FROM ingestion_progress WHERE content_hash = ?") | ||||||||||||||||||||
| .bind(content_hash) | ||||||||||||||||||||
| .execute(pool) | ||||||||||||||||||||
| .await?; | ||||||||||||||||||||
| sqlx::query("DELETE FROM ingestion_files WHERE content_hash = ?") | ||||||||||||||||||||
| .bind(content_hash) | ||||||||||||||||||||
| .execute(pool) | ||||||||||||||||||||
| .await?; | ||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Delete a completed ingestion file record from history. | ||||||||||||||||||||
| #[utoipa::path( | ||||||||||||||||||||
| delete, | ||||||||||||||||||||
|
|
@@ -242,15 +277,47 @@ pub(super) async fn delete_ingest_file( | |||||||||||||||||||
| ) -> Result<Json<IngestDeleteResponse>, StatusCode> { | ||||||||||||||||||||
| let pools = state.agent_pools.load(); | ||||||||||||||||||||
| let pool = pools.get(&query.agent_id).ok_or(StatusCode::NOT_FOUND)?; | ||||||||||||||||||||
| let workspaces = state.agent_workspaces.load(); | ||||||||||||||||||||
| let workspace = workspaces.get(&query.agent_id).ok_or(StatusCode::NOT_FOUND)?; | ||||||||||||||||||||
| let ingest_dir = workspace.join("ingest"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| sqlx::query("DELETE FROM ingestion_files WHERE content_hash = ?") | ||||||||||||||||||||
| .bind(&query.content_hash) | ||||||||||||||||||||
| .execute(pool) | ||||||||||||||||||||
| purge_ingest_file(pool, &ingest_dir, &query.content_hash) | ||||||||||||||||||||
| .await | ||||||||||||||||||||
| .map_err(|error| { | ||||||||||||||||||||
| tracing::warn!(%error, "failed to delete ingest file record"); | ||||||||||||||||||||
| tracing::warn!(%error, "failed to purge ingest file"); | ||||||||||||||||||||
| StatusCode::INTERNAL_SERVER_ERROR | ||||||||||||||||||||
| })?; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Ok(Json(IngestDeleteResponse { success: true })) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||
| mod tests { | ||||||||||||||||||||
| use super::*; | ||||||||||||||||||||
| use sqlx::sqlite::SqlitePoolOptions; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||
| async fn test_purge_removes_disk_file_and_rows() { | ||||||||||||||||||||
| let pool = SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await.unwrap(); | ||||||||||||||||||||
| sqlx::migrate!("./migrations").run(&pool).await.unwrap(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| let dir = tempfile::tempdir().unwrap(); | ||||||||||||||||||||
| let ingest_dir = dir.path().to_path_buf(); | ||||||||||||||||||||
| let file = ingest_dir.join("notes.txt"); | ||||||||||||||||||||
| tokio::fs::write(&file, b"hello").await.unwrap(); | ||||||||||||||||||||
| let hash = crate::agent::ingestion::content_hash("hello"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| sqlx::query("INSERT INTO ingestion_files (content_hash, filename, file_size, total_chunks, status) VALUES (?, 'notes.txt', 5, 1, 'failed')") | ||||||||||||||||||||
| .bind(&hash).execute(&pool).await.unwrap(); | ||||||||||||||||||||
| sqlx::query("INSERT INTO ingestion_progress (content_hash, chunk_index, total_chunks, filename) VALUES (?, 0, 1, 'notes.txt')") | ||||||||||||||||||||
| .bind(&hash).execute(&pool).await.unwrap(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| purge_ingest_file(&pool, &ingest_dir, &hash).await.unwrap(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| assert!(!file.exists(), "disk file must be removed"); | ||||||||||||||||||||
| let files: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM ingestion_files WHERE content_hash = ?").bind(&hash).fetch_one(&pool).await.unwrap(); | ||||||||||||||||||||
| let prog: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM ingestion_progress WHERE content_hash = ?").bind(&hash).fetch_one(&pool).await.unwrap(); | ||||||||||||||||||||
| assert_eq!(files, 0, "ingestion_files row must be deleted"); | ||||||||||||||||||||
| assert_eq!(prog, 0, "ingestion_progress rows must be deleted"); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.