fix(knowledge): make finalizing subtask counter reliable + async question fan-out
The finalizing subtask counter (pending_subtasks_count) could get stuck at a non-zero value, leaving knowledge permanently in "finalizing" and never promoted to "completed". Root cause and related hardening: - UpdateKnowledge does a full-row Save; pending_subtasks_count was NOT in the omit list, so any concurrent enrichment subtask that loaded the row, did slow work (LLM call), then saved an unrelated field wrote back the STALE counter, clobbering decrements other subtasks performed in between. Add PendingSubtasksCount to omitFieldsOnUpdate so only the atomic helpers (SetFinalizing / FinalizeSubtask / explicit column writes) ever touch it. - FinalizeSubtask no longer gates the finalizing->completed promote on a separate re-read of the counter. Every caller unconditionally runs the guarded promote UPDATE (WHERE pending_subtasks_count = 0), the single authoritative atomic check, so a racy/stale read can't strand the row. - Decrements run on a context detached from the caller's cancellation (context.WithoutCancel + timeout) so graceful shutdown / preemption can't silently skip a decrement. - Reconcile seeded vs actually-enqueued subtask slots (summary/question/graph) and release any shortfall so un-enqueued planned slots (e.g. graph with NEO4J off) don't strand the row. - Reparse paths reset pending_subtasks_count via an explicit column write since full-row Save now omits it. Question generation is also moved off the synchronous single-task path onto a batched async fan-out (windows of text chunks), each batch independently queued / retried / traced, grouped under a postprocess.question span.
This commit is contained in:
@@ -80,7 +80,12 @@ Please output in the following format (one paragraph per column):
|
||||
- Write descriptions in the same language as the data content`
|
||||
)
|
||||
|
||||
// NewChunkExtractTask creates a new chunk extract task
|
||||
// NewChunkExtractTask creates a new chunk extract task. It returns
|
||||
// (enqueued, err): enqueued is true only when a task was actually placed on
|
||||
// the queue. When NEO4J is disabled the call is a no-op and returns
|
||||
// (false, nil) — callers that seeded a pending-subtask counter for this chunk
|
||||
// MUST release that slot, otherwise the parent knowledge stays stuck in
|
||||
// "finalizing" forever (the graph subtask it's waiting on was never enqueued).
|
||||
func NewChunkExtractTask(
|
||||
ctx context.Context,
|
||||
client interfaces.TaskEnqueuer,
|
||||
@@ -90,10 +95,10 @@ func NewChunkExtractTask(
|
||||
knowledgeID string,
|
||||
attempt int,
|
||||
chunkIndex int,
|
||||
) error {
|
||||
) (bool, error) {
|
||||
if strings.ToLower(os.Getenv("NEO4J_ENABLE")) != "true" {
|
||||
logger.Warn(ctx, "NEO4J is not enabled, skip chunk extract task")
|
||||
return nil
|
||||
return false, nil
|
||||
}
|
||||
taskPayload := types.ExtractChunkPayload{
|
||||
TenantID: tenantID,
|
||||
@@ -106,16 +111,16 @@ func NewChunkExtractTask(
|
||||
langfuse.InjectTracing(ctx, &taskPayload)
|
||||
payload, err := json.Marshal(taskPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
task := asynq.NewTask(types.TypeChunkExtract, payload, asynq.MaxRetry(3))
|
||||
info, err := client.Enqueue(task)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "failed to enqueue task: %v", err)
|
||||
return fmt.Errorf("failed to enqueue task: %v", err)
|
||||
return false, fmt.Errorf("failed to enqueue task: %v", err)
|
||||
}
|
||||
logger.Infof(ctx, "enqueued task: id=%s queue=%s chunk=%s", info.ID, info.Queue, chunkID)
|
||||
return nil
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// NewTableExtractTask creates a new table extract task
|
||||
@@ -237,11 +242,9 @@ func (s *ChunkExtractService) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
// completed (or terminally-failed) per-chunk extract releases its
|
||||
// slot in pending_subtasks_count. KnowledgeID is the new (post-#? )
|
||||
// payload field; legacy in-flight tasks without it are skipped.
|
||||
if (handleErr == nil || isFinalAsynqAttempt(ctx)) && p.KnowledgeID != "" && s.knowledgeRepo != nil {
|
||||
if _, _, ferr := s.knowledgeRepo.FinalizeSubtask(ctx, p.KnowledgeID); ferr != nil {
|
||||
logger.Warnf(ctx, "graph extract: FinalizeSubtask failed for %s: %v", p.KnowledgeID, ferr)
|
||||
}
|
||||
}
|
||||
finalizeSubtaskDetached(ctx, s.knowledgeRepo, p.KnowledgeID,
|
||||
fmt.Sprintf("graph_chunk[%d]", p.ChunkIndex),
|
||||
handleErr, false, isFinalAsynqAttempt(ctx))
|
||||
if gSpan == nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user