Skip to content

Commit 5ee103d

Browse files
authored
Merge pull request #8 from glassflow/factory/TR-1350
TR-1350: a tenant deletes its own repo, DELETE /v1/repos/{id}
2 parents a9fd41b + afb662d commit 5ee103d

5 files changed

Lines changed: 187 additions & 2 deletions

File tree

‎docs/usage.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ curl -X PUT "localhost:8080/v1/repos/<id>/bundle?ref=$(git rev-parse HEAD)&full=
4343

4444
`ref` is the full 40 character lowercase sha of the bundle's `HEAD`. With `full=true` the bundle holds the whole history and the tree is re-indexed, deleting documents of files that are gone. With `full=false` the bundle may be incremental (`git bundle create out.bundle <last-sha>..HEAD`) and only changed files are indexed. If the server no longer has the last indexed commit, or the bundle needs commits it lacks, the answer is 409 (`need_full_bundle`) and the client sends a full bundle. The answer is a report with `indexed_commit`, `files_indexed`, `files_unchanged`, `files_skipped`, `files_deleted` and `chunks`.
4545

46+
`DELETE /v1/repos/{id}` needs an `ingest` key and deletes one of the tenant's repos with everything the admin delete of a source removes: the source, its documents and chunks, its mirror and its checkout on the server. The answer is 204. An unknown id, an id of another tenant and an id that is not a repo are all a 404. While a run of the repo is going (a push, a sync, or a `lore index` Job whose run was written in the last 90 seconds) the answer is 409, and nothing is deleted; try again when the run ends. `curl -X DELETE localhost:8080/v1/repos/<id> -H "Authorization: Bearer $LORE_KEY"`.
47+
4648
`GET /v1/repos/{id}/progress` needs a `read` key and answers with the latest index run of that repo, whichever process ran it (while a run is queued behind another, the queued run is the latest, so the one indexing is not shown until the queued one starts): `{"id", "stage", "commit", "full", "files_done", "files_total", "chunks", "chunks_embedded", "embed_ms", "started_at", "updated_at", "finished_at", "error", "stale"}`. `stage` is `queued` (waiting for another run on the same repo), `cloning` (fetching the commit), `indexing`, `done` or `failed`, and `error` says why a run failed. `files_total` is null until the files to index are known. A run that has not ended and has not been updated for 90 seconds is shown with `"stale": true`: its process most likely died. The stage is left as it was, and the run stays that way until the next run starts. A repo that has no run yet answers 404 `no_index_run`; a repo of another tenant answers the usual 404.
4749

4850
Progress is kept in the Postgres table `index_runs`, one row per run (source, tenant, stage, files done and total, chunks, start and finish times, error), so it survives a restart and a process other than the server can report through it. An indexer writes its counts at most once a second, writes a change of stage or the end of a run at once, and refreshes `updated_at` every 5 seconds while it is alive. If a write fails, the failure is logged and the index carries on. The rows go with their source or tenant when it is deleted.

‎src/http/admin.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,17 @@ pub async fn delete_source(
296296
State(state): State<AppState>,
297297
Path(source_id): Path<Uuid>,
298298
) -> Result<StatusCode, AppError> {
299+
remove_source(&state, source_id).await?;
300+
Ok(StatusCode::NO_CONTENT)
301+
}
302+
303+
/// What deleting a source does, for the admin route and the tenant's `DELETE /v1/repos/{id}`
304+
/// alike: 409 while a run of the source is live, then the row goes (its documents and chunks
305+
/// with it) and so do its mirror and checkout. `NotFound` when no such source exists. The caller
306+
/// has checked that the source is the caller's to delete.
307+
pub(crate) async fn remove_source(state: &AppState, source_id: Uuid) -> Result<(), AppError> {
299308
let running = source_has_live_run(&state.pool, source_id).await?;
300-
let _guards = hold_sources(&state, &[source_id], running, "source")?;
309+
let _guards = hold_sources(state, &[source_id], running, "source")?;
301310
let done = sqlx::query("DELETE FROM sources WHERE id = $1")
302311
.bind(source_id)
303312
.execute(&state.pool)
@@ -307,7 +316,7 @@ pub async fn delete_source(
307316
}
308317
tracing::info!(source = %source_id, "source deleted");
309318
repo::remove_repo_dirs(&state.config.data_dir, &[source_id]).await;
310-
Ok(StatusCode::NO_CONTENT)
319+
Ok(())
311320
}
312321

313322
/// What the server runs and holds, for the admin page.

‎src/http/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub fn router(state: AppState) -> Router {
9191
"/v1/repos/{id}/bundle",
9292
put(repos::put_bundle).layer(RequestBodyLimitLayer::new(bundle_limit)),
9393
)
94+
.route("/v1/repos/{id}", delete(repos::delete_repo))
9495
.route("/v1/repos/{id}/sync", post(repos::sync_repo))
9596
.route("/v1/repos/{id}/progress", get(repos::repo_progress))
9697
.route(

‎src/http/repos.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use std::error::Error as StdError;
55
use axum::Json;
66
use axum::body::{Body, Bytes};
77
use axum::extract::{Path, State};
8+
use axum::http::StatusCode;
89
use futures::StreamExt;
910
use serde::{Deserialize, Serialize};
1011
use tokio::io::AsyncWriteExt;
1112
use uuid::Uuid;
1213

1314
use crate::auth::{Caller, Scope};
1415
use crate::error::AppError;
16+
use crate::http::admin::remove_source;
1517
use crate::http::{AppJson, AppQuery, AppState};
1618
use crate::repo::progress::{IndexRun, latest_run};
1719
use crate::repo::sync::{SyncReport, check_remote_url, remote_urls, set_remote_url};
@@ -250,6 +252,23 @@ pub async fn sync_repo(
250252
Ok(Json(report))
251253
}
252254

255+
/// Deletes one of the caller's repos with its documents, chunks, mirror and checkout, exactly as
256+
/// the admin delete of a source does. A 409 while a run of the repo is live.
257+
pub async fn delete_repo(
258+
caller: Caller,
259+
State(state): State<AppState>,
260+
Path(id): Path<Uuid>,
261+
) -> Result<StatusCode, AppError> {
262+
caller.require(Scope::Ingest)?;
263+
// Another tenant's source, or a source that is not a repo, looks like a missing one.
264+
let source = get_source(&state.pool, caller.tenant_id, id)
265+
.await?
266+
.filter(|s| s.kind == "repo")
267+
.ok_or(AppError::NotFound)?;
268+
remove_source(&state, source.id).await?;
269+
Ok(StatusCode::NO_CONTENT)
270+
}
271+
253272
/// The latest index run of the repo, whichever process ran it, with `stale` set when the run
254273
/// has not ended and has not been written for 90 seconds.
255274
pub async fn repo_progress(

‎tests/admin_delete.rs‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,160 @@ async fn unknown_ids_are_404(pool: PgPool) {
381381
}
382382
}
383383

384+
/// Documents and chunks of the source, plus its own row.
385+
async fn rows_of_source(pool: &PgPool, source: Uuid) -> i64 {
386+
sqlx::query_scalar(
387+
"SELECT (SELECT count(*) FROM sources WHERE id = $1)
388+
+ (SELECT count(*) FROM documents WHERE source_id = $1)
389+
+ (SELECT count(*) FROM chunks c JOIN documents d ON d.id = c.document_id
390+
WHERE d.source_id = $1)",
391+
)
392+
.bind(source)
393+
.fetch_one(pool)
394+
.await
395+
.unwrap()
396+
}
397+
398+
#[sqlx::test(migrations = "./migrations")]
399+
async fn a_tenant_deletes_its_repo_with_what_the_admin_delete_removes(pool: PgPool) {
400+
let env = env(pool);
401+
let (acme, acme_key, acme_repo) = env.full_tenant("acme").await;
402+
let (other, _, other_repo) = env.full_tenant("other").await;
403+
let other_before = rows_of(&env.pool, other).await;
404+
assert!(rows_of_source(&env.pool, acme_repo).await > 1);
405+
assert!(env.repo_dir(acme_repo).exists());
406+
407+
let path = format!("/v1/repos/{acme_repo}");
408+
let (status, body) = send(&env.router, "DELETE", &path, &acme_key, Vec::new()).await;
409+
assert_eq!(status, StatusCode::NO_CONTENT, "{body}");
410+
assert_eq!(rows_of_source(&env.pool, acme_repo).await, 0);
411+
assert!(!env.repo_dir(acme_repo).exists());
412+
413+
// The tenant keeps its other sources and its key; the repo is gone from its list.
414+
let names: Vec<String> =
415+
sqlx::query_scalar("SELECT name FROM sources WHERE tenant_id = $1 ORDER BY name")
416+
.bind(acme)
417+
.fetch_all(&env.pool)
418+
.await
419+
.unwrap();
420+
assert!(names.contains(&"notes".to_string()), "{names:?}");
421+
let (status, list) = send(&env.router, "GET", "/v1/repos", &acme_key, Vec::new()).await;
422+
assert_eq!(status, StatusCode::OK, "{list}");
423+
assert!(list["repos"].as_array().unwrap().is_empty(), "{list}");
424+
425+
assert_eq!(rows_of(&env.pool, other).await, other_before);
426+
assert!(env.repo_dir(other_repo).exists());
427+
428+
let (status, _) = send(&env.router, "DELETE", &path, &acme_key, Vec::new()).await;
429+
assert_eq!(status, StatusCode::NOT_FOUND);
430+
}
431+
432+
#[sqlx::test(migrations = "./migrations")]
433+
async fn a_tenant_cannot_delete_an_unknown_repo_or_another_tenants(pool: PgPool) {
434+
let env = env(pool);
435+
let (acme, acme_key, _) = env.full_tenant("acme").await;
436+
let (_, _, other_repo) = env.full_tenant("other").await;
437+
let other_before = rows_of_source(&env.pool, other_repo).await;
438+
439+
let unknown = format!("/v1/repos/{}", Uuid::new_v4());
440+
let (status, body) = send(&env.router, "DELETE", &unknown, &acme_key, Vec::new()).await;
441+
assert_eq!(status, StatusCode::NOT_FOUND, "{body}");
442+
443+
let theirs = format!("/v1/repos/{other_repo}");
444+
let (status, body) = send(&env.router, "DELETE", &theirs, &acme_key, Vec::new()).await;
445+
assert_eq!(status, StatusCode::NOT_FOUND, "{body}");
446+
assert_eq!(rows_of_source(&env.pool, other_repo).await, other_before);
447+
assert!(env.repo_dir(other_repo).exists());
448+
449+
// A source that is not a repo (the tenant's notes) is no repo to delete either.
450+
let notes: Uuid =
451+
sqlx::query_scalar("SELECT id FROM sources WHERE tenant_id = $1 AND name = 'notes'")
452+
.bind(acme)
453+
.fetch_one(&env.pool)
454+
.await
455+
.unwrap();
456+
let (status, _) = send(
457+
&env.router,
458+
"DELETE",
459+
&format!("/v1/repos/{notes}"),
460+
&acme_key,
461+
Vec::new(),
462+
)
463+
.await;
464+
assert_eq!(status, StatusCode::NOT_FOUND);
465+
assert!(rows_of_source(&env.pool, notes).await > 0);
466+
}
467+
468+
#[sqlx::test(migrations = "./migrations")]
469+
async fn deleting_a_repo_needs_an_ingest_key_of_its_tenant(pool: PgPool) {
470+
let env = env(pool);
471+
let (tenant, key) = create_tenant_key(&env.router, "acme", &["ingest", "read"]).await;
472+
let (status, created) = create_repo(&env.router, &key, "sample").await;
473+
assert_eq!(status, StatusCode::OK, "{created}");
474+
let source: Uuid = created["id"].as_str().unwrap().parse().unwrap();
475+
let (status, minted) = send(
476+
&env.router,
477+
"POST",
478+
&format!("/v1/admin/tenants/{tenant}/keys"),
479+
"t",
480+
json!({ "scopes": ["read"] }).to_string().into_bytes(),
481+
)
482+
.await;
483+
assert_eq!(status, StatusCode::CREATED, "{minted}");
484+
let read_key = minted["key"].as_str().unwrap().to_string();
485+
486+
let path = format!("/v1/repos/{source}");
487+
for (bearer, want) in [
488+
(read_key.as_str(), StatusCode::FORBIDDEN),
489+
("wrong", StatusCode::UNAUTHORIZED),
490+
] {
491+
let (status, body) = send(&env.router, "DELETE", &path, bearer, Vec::new()).await;
492+
assert_eq!(status, want, "{bearer}: {body}");
493+
}
494+
assert_eq!(rows_of_source(&env.pool, source).await, 1);
495+
}
496+
497+
#[sqlx::test(migrations = "./migrations")]
498+
async fn a_repo_with_a_live_run_is_a_409_for_its_tenant_too(pool: PgPool) {
499+
let env = env(pool);
500+
let (tenant, key) = create_tenant_key(&env.router, "acme", &["ingest", "read"]).await;
501+
let (_, created) = create_repo(&env.router, &key, "sample").await;
502+
let source: Uuid = created["id"].as_str().unwrap().parse().unwrap();
503+
let path = format!("/v1/repos/{source}");
504+
505+
// A run of another process (no lock held here).
506+
insert_run(&env.pool, tenant, source, 5).await;
507+
let (status, body) = send(&env.router, "DELETE", &path, &key, Vec::new()).await;
508+
assert_eq!(status, StatusCode::CONFLICT, "{body}");
509+
assert_eq!(rows_of_source(&env.pool, source).await, 1);
510+
511+
// Once the run has gone stale, the delete goes through.
512+
sqlx::query("UPDATE index_runs SET updated_at = now() - interval '120 seconds'")
513+
.execute(&env.pool)
514+
.await
515+
.unwrap();
516+
let (status, body) = send(&env.router, "DELETE", &path, &key, Vec::new()).await;
517+
assert_eq!(status, StatusCode::NO_CONTENT, "{body}");
518+
assert_eq!(rows_of_source(&env.pool, source).await, 0);
519+
}
520+
521+
#[sqlx::test(migrations = "./migrations")]
522+
async fn a_repo_whose_lock_is_held_is_a_409_for_its_tenant(pool: PgPool) {
523+
let env = env(pool);
524+
let (_, key) = create_tenant_key(&env.router, "acme", &["ingest", "read"]).await;
525+
let (_, created) = create_repo(&env.router, &key, "sample").await;
526+
let source: Uuid = created["id"].as_str().unwrap().parse().unwrap();
527+
let lock = env.state.source_locks.get(source);
528+
let guard = lock.lock().await;
529+
let path = format!("/v1/repos/{source}");
530+
let (status, body) = send(&env.router, "DELETE", &path, &key, Vec::new()).await;
531+
assert_eq!(status, StatusCode::CONFLICT, "{body}");
532+
assert_eq!(rows_of_source(&env.pool, source).await, 1);
533+
drop(guard);
534+
let (status, _) = send(&env.router, "DELETE", &path, &key, Vec::new()).await;
535+
assert_eq!(status, StatusCode::NO_CONTENT);
536+
}
537+
384538
#[sqlx::test(migrations = "./migrations")]
385539
async fn the_cli_deletes_sources_and_tenants(pool: PgPool) {
386540
let env = env(pool);

0 commit comments

Comments
 (0)