{"record":{"id":"c0ecc8a36fcadc91","repo":"Tencent/WeKnora","slug":"failed-to-list-existing-chunks-w","errorCode":null,"errorMessage":"failed to list existing chunks: %w","messagePattern":"failed to list existing chunks: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/knowledge_faq_import.go","lineNumber":1210,"sourceCode":"\t\t}\n\n\t\t// 将当前条目的标准问和相似问加入批次集合\n\t\tbatchQuestions[meta.StandardQuestion] = true\n\t\tfor _, q := range meta.SimilarQuestions {\n\t\t\tbatchQuestions[q] = true\n\t\t}\n\n\t\thash := types.CalculateFAQContentHash(meta)\n\t\tif hash != \"\" {\n\t\t\tentriesWithHash = append(entriesWithHash, entryWithHash{entry: entry, hash: hash, meta: meta})\n\t\t\tnewHashSet[hash] = true\n\t\t}\n\t}\n\n\t// 查询所有已存在的chunks\n\tallExistingChunks, err := s.chunkRepo.ListAllFAQChunksByKnowledgeID(ctx, tenantID, knowledgeID)\n\tif err != nil {\n\t\treturn nil, nil, 0, fmt.Errorf(\"failed to list existing chunks: %w\", err)\n\t}\n\n\t// 在内存中过滤出匹配新条目hash的chunks，并构建map\n\texistingHashMap := make(map[string]*types.Chunk)\n\tfor _, chunk := range allExistingChunks {\n\t\tif chunk.ContentHash != \"\" && newHashSet[chunk.ContentHash] {\n\t\t\texistingHashMap[chunk.ContentHash] = chunk\n\t\t}\n\t}\n\n\t// 计算需要删除的chunks（数据库中有但新批次中没有的，或hash不匹配的）\n\tchunksToDelete := make([]*types.Chunk, 0)\n\tfor _, chunk := range allExistingChunks {\n\t\tif chunk.ContentHash == \"\" {\n\t\t\t// 如果没有hash，需要删除（可能是旧数据）\n\t\t\tchunksToDelete = append(chunksToDelete, chunk)\n\t\t} else if !newHashSet[chunk.ContentHash] {\n\t\t\t// hash不在新条目中，需要删除","sourceCodeStart":1192,"sourceCodeEnd":1228,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/knowledge_faq_import.go#L1192-L1228","documentation":"calculateReplaceOperations wraps errors from chunkRepo.ListAllFAQChunksByKnowledgeID when loading all existing FAQ chunks to match against incoming entry content hashes. Without this list the replace diff (chunks to delete/update) cannot be computed. The original DB error is preserved via %w.","triggerScenarios":"Replace-mode import reaches the existing-chunk listing step; ListAllFAQChunksByKnowledgeID fails from DB outage, query timeout, context cancellation, or oversized result set causing memory/query limits.","commonSituations":"Very large FAQ knowledge bases making the full-list query slow enough to hit timeouts; DB failover; connection pool exhaustion under concurrent imports.","solutions":["Inspect the wrapped cause in logs","Confirm DB connectivity and query performance for ListAllFAQChunksByKnowledgeID","Add/verify indexes on knowledge_id and tenant_id for the chunks table","Reduce import size or increase timeouts; retry the import"],"exampleFix":"// before\nallExistingChunks, err := s.chunkRepo.ListAllFAQChunksByKnowledgeID(ctx, tenantID, knowledgeID)\nif err != nil {\n\treturn nil, nil, 0, fmt.Errorf(\"failed to list existing chunks: %w\", err)\n}\n// after\nallExistingChunks, err := s.chunkRepo.ListAllFAQChunksByKnowledgeID(ctx, tenantID, knowledgeID)\nif err != nil {\n\treturn nil, nil, 0, fmt.Errorf(\"failed to list existing chunks (knowledge=%s): %w\", knowledgeID, err)\n}","handlingStrategy":"retry","validationCode":"// Pre-flight: confirm the knowledge has listable chunks before replace mode\nchunks, err := chunkRepo.ListAllFAQChunksByKnowledgeID(ctx, tenantID, knowledgeID)\nif err != nil {\n\treturn fmt.Errorf(\"preflight chunk list failed: %w\", err)\n}\nlog.Printf(\"replace import preflight: %d existing chunks\", len(chunks))","typeGuard":null,"tryCatchPattern":"err := runReplaceImport(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"failed to list existing chunks\") {\n\t// transient DB error: bounded retry\n\treturn retryWithBackoff(3, runReplaceImport, ctx, req)\n}\nreturn err","preventionTips":["Ensure indexes on chunks(knowledge_id, tenant_id) so full lists stay fast","Set import context deadlines larger than worst-case chunk listing time","Cap FAQ knowledge base size or paginate chunk listing","Monitor slow query log for ListAllFAQChunksByKnowledgeID"],"tags":["database","faq-import","replace-mode"],"backgroundTag":"faq-import-chunk-list-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}