Repository navigation
fix: use id and kb_id when deleting dataset metadata - #20880
Conversation
DeleteMetadata keys metadata rows by id/kb_id, not doc_id. Passing doc_id built an invalid single-value term query for a slice, so the delete silently failed and metadata rows were orphaned on dataset deletion.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
qinling0210
left a comment
There was a problem hiding this comment.
Please add the regression test
The diff is a single hunk in one file with no test, even though #20779's testing section describes a TestDeleteDatasetEngineDataDeletesMetadataByDataset that fails before the change.
Nothing in the package currently exercises this function: crud_delete_test.go builds its service via testDatasetUpdateService (internal/service/dataset/update_test.go:1050-1058), which leaves docEngine nil, so deleteDatasetEngineData early-returns and the condition is never passed to the engine. Since the failure modes here are engine-specific and silent (ES 400, Infinity unknown column, Vastbase 0 rows with no error), an assertion on the recorded condition is the only cheap guard we have.
A pure unit test needs no DB or storage — the package already has fakeChatDocEngine in internal/service/dataset/fake_doc_engine_test.go to embed:
type recordingMetadataEngine struct {
fakeChatDocEngine
cond map[string]interface{}
tenant string
}
func (e *recordingMetadataEngine) DeleteMetadata(_ context.Context, cond map[string]interface{}, tenantID string) (int64, error) {
e.cond, e.tenant = cond, tenantID
return 0, nil
}
func TestDeleteDatasetEngineDataDeletesMetadataByDataset(t *testing.T) {
eng := &recordingMetadataEngine{}
svc := &DatasetService{docEngine: eng}
svc.deleteDatasetEngineData(t.Context(), &entity.Knowledgebase{ID: "kb-1", TenantID: "tenant-1"}, []string{"doc-1", "doc-2"})
if got := eng.cond["kb_id"]; got != "kb-1" {
t.Fatalf("metadata condition kb_id = %v, want kb-1", got)
}
if got, ok := eng.cond["id"]; !ok || !reflect.DeepEqual(got, []string{"doc-1", "doc-2"}) {
t.Fatalf("metadata condition id = %v", eng.cond["id"])
}
if _, ok := eng.cond["doc_id"]; ok {
t.Fatalf("metadata condition still uses doc_id: %v", eng.cond)
}
}(Update the id assertion to kb_id if you take the Comment 1 suggestion.)
If you keep the id-list form, an additional engine-level test on buildMetadataQueryFromCondition pinning that a slice under a non-list key becomes a term with an array value would cover the trap that caused this bug.
| common.Logger.Warn(fmt.Sprintf("deleteDataset: failed to delete chunks for kb %s: %v", kb.ID, err)) | ||
| } | ||
| if _, err := d.docEngine.DeleteMetadata(ctx, map[string]interface{}{"doc_id": docIDs}, kb.TenantID); err != nil { | ||
| if _, err := d.docEngine.DeleteMetadata(ctx, map[string]interface{}{"id": docIDs, "kb_id": kb.ID}, kb.TenantID); err != nil { |
There was a problem hiding this comment.
Suggestion: filter by kb_id alone
Verified the row shape on all five engines: metadata rows are {id = document id, kb_id = dataset id, meta_fields} (conf/doc_meta_es_mapping.json, conf/doc_meta_infinity_mapping.json, UpdateMetadata in each engine), so doc_id was indeed the wrong key and this fix works for the reported repro. One follow-up though: this call site is the only one that passes a whole dataset's id list (the sibling DeleteDocumentAllMetadata passes a single document), and that list is unbounded:
deleteDatasetloads documents with an unpagedWhere("kb_id = ?")(crud.go:399-402), sodocIDscan be tens of thousands. ES's defaultindex.max_terms_countis 65536 — a dataset above that still gets a400and leaks rows silently, in exactly the way this PR is fixing.- It cannot clean rows whose document row is already gone (the
RemoveDocumentKeepFilecase in RemoveDocumentKeepFile deletes the document row but not its chunk data or metadata record #20659), whilekb_idcan. - The tenant-deletion path already chunks deletes at 1000 for this reason (
internal/admin/user_delete.go:363-382).
kb_id is stored and indexed on every engine (ES mapping is keyword; OceanBase/Infinity/Vastbase create secondary indexes on it; serenedb accepts it via recognizedFilterKey), so a single-clause condition is valid everywhere and removes the list handling from the picture entirely:
| if _, err := d.docEngine.DeleteMetadata(ctx, map[string]interface{}{"id": docIDs, "kb_id": kb.ID}, kb.TenantID); err != nil { | |
| if _, err := d.docEngine.DeleteMetadata(ctx, map[string]interface{}{"kb_id": kb.ID}, kb.TenantID); err != nil { |
If you go this way, the len(docIDs) == 0 guards (crud.go:436-438 and the early return at crud.go:737-739) become unnecessary for metadata; replacing the early return with if d.docEngine == nil { return } and guarding only the DeleteChunks call would also clean up datasets that still hold orphan rows but have no document rows left.
|
Thanks for the detailed review! I took the kb_id-alone suggestion: the delete call now passes only I double checked the single-value kb_id condition in Also added Ready for re-review whenever you get a moment. |
|
Read through the final diff (head 2a0cd65) against main. Looks right to me.
One note: I could not run |
Fixes #20779
User impact: deleting a dataset removes its chunks but silently leaves rows in the dataset metadata index. The dead document metadata keeps appearing in metadata value lists and filters after the dataset is gone.
Technical cause: deleteDatasetEngineData calls DeleteMetadata with {"doc_id": docIDs}, but metadata rows are keyed by id and kb_id. The engine query builder only gives kb_id and id list treatment; doc_id falls into the default branch which builds a single-value term query for a slice, which Elasticsearch rejects. The error is only logged as a warning, so the failure is silent. The sibling DeleteDocumentAllMetadata correctly passes {"id", "kb_id"}.
Fix: pass {"id": docIDs, "kb_id": kb.ID} to DeleteMetadata, matching the sibling caller. The DeleteChunks call is untouched because chunk rows are genuinely keyed by doc_id.