Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

perf(knowledge_compiler): opt out of real-time retry backoff in failure-path unit tests (#20708)

xugangqiangfc215f299d5a
diff --git a/internal/ingestion/component/knowledge_compiler/common/common_test.go b/internal/ingestion/component/knowledge_compiler/common/common_test.go
index f5250dce10..958850594a 100644
--- a/internal/ingestion/component/knowledge_compiler/common/common_test.go
+++ b/internal/ingestion/component/knowledge_compiler/common/common_test.go
@@ -89,6 +89,21 @@ func TestGenJSONReportsLLMFailure(t *testing.T) {
}
}
+// TestGenJSONRetryDelayOverride verifies the ctx-based retry-delay override
+// shortens the backoff so failure-path tests need not wait out the real ~60s
+// budget. Without the override GenJSON sleeps ~62s per failed call.
+func TestGenJSONRetryDelayOverride(t *testing.T) {
+ ctx := WithRetryDelay(context.Background(), time.Millisecond)
+ start := time.Now()
+ _, err := GenJSON(ctx, failingChat{err: errors.New("llm down")}, ChatRequest{})
+ if err == nil {
+ t.Fatal("expected the transient LLM error to surface")
+ }
+ if time.Since(start) > 5*time.Second {
+ t.Fatalf("override did not shorten retry backoff: %s", time.Since(start))
+ }
+}
+
func TestCompactErrorRedactsCredentials(t *testing.T) {
got := CompactError(errors.New(`status=401 api_key="sk-secret-value" password=topsecret`))
if strings.Contains(got, "sk-secret-value") || strings.Contains(got, "topsecret") {
diff --git a/internal/ingestion/component/knowledge_compiler/common/jsonchat.go b/internal/ingestion/component/knowledge_compiler/common/jsonchat.go
index 645d954fd4..dc4aa9d191 100644
--- a/internal/ingestion/component/knowledge_compiler/common/jsonchat.go
+++ b/internal/ingestion/component/knowledge_compiler/common/jsonchat.go
@@ -33,6 +33,23 @@ const jsonRetryDelay = 2 * time.Second
// retry before giving up.
var fencedJSONRE = regexp.MustCompile("(?s)```(?:json)?\\s*(.*?)\\s*```")
+// retryDelayCtxKey carries a test-only override for jsonRetryDelay. Production
+// code never sets it, so the real backoff budget is preserved at runtime.
+type retryDelayCtxKey struct{}
+
+// WithRetryDelay returns a ctx that makes GenJSON use d as the initial
+// exponential-backoff delay instead of jsonRetryDelay. It exists so tests that
+// exercise the LLM-failure retry path need not wait out the real ~60s budget
+// per failed call. Pass 0 or a negative value to keep the default.
+func WithRetryDelay(ctx context.Context, d time.Duration) context.Context {
+ return context.WithValue(ctx, retryDelayCtxKey{}, d)
+}
+
+func RetryDelayFromCtx(ctx context.Context) (time.Duration, bool) {
+ d, ok := ctx.Value(retryDelayCtxKey{}).(time.Duration)
+ return d, ok && d > 0
+}
+
// GenJSON dispatches a chat call in JSON mode and parses the response into a
// map. It first tries the raw content, then a fenced ```json ... ``` block the
// model may have wrapped around the JSON, and finally the outermost {...} span
@@ -57,6 +74,9 @@ func GenJSON(ctx context.Context, chat ChatInvoker, req ChatRequest, retryMax ..
req.DisableRetry = true
var lastErr error
delay := jsonRetryDelay
+ if d, ok := RetryDelayFromCtx(ctx); ok {
+ delay = d
+ }
failureReporter := RetryFailureReporter{}
for attempt := 0; attempt <= maxRetries; attempt++ {
resp, err := chat.Chat(ctx, req)
diff --git a/internal/ingestion/component/knowledge_compiler/component_test.go b/internal/ingestion/component/knowledge_compiler/component_test.go
index fd604e3261..3657cdc569 100644
--- a/internal/ingestion/component/knowledge_compiler/component_test.go
+++ b/internal/ingestion/component/knowledge_compiler/component_test.go
@@ -11,6 +11,7 @@ import (
"strings"
"sync"
"testing"
+ "time"
"ragflow/internal/agent/runtime"
"ragflow/internal/ingestion/component/globals"
@@ -1096,7 +1097,7 @@ func TestKnowledgeCompiler_Structure_MalformedJSONFailsLoud(t *testing.T) {
if err != nil {
t.Fatalf("NewKnowledgeCompilerComponent: %v", err)
}
- _, err = c.Invoke(context.Background(), nil, map[string]any{
+ _, err = c.Invoke(common.WithRetryDelay(context.Background(), time.Millisecond), nil, map[string]any{
"chunks": []any{map[string]any{"id": "c1", "text": "Alpha is a Beta"}},
"doc_id": "d1",
"tenant_id": "t1",
diff --git a/internal/ingestion/component/knowledge_compiler/structure/chain_test.go b/internal/ingestion/component/knowledge_compiler/structure/chain_test.go
index 308660559f..92d1af3662 100644
--- a/internal/ingestion/component/knowledge_compiler/structure/chain_test.go
+++ b/internal/ingestion/component/knowledge_compiler/structure/chain_test.go
@@ -5,6 +5,7 @@ import (
"errors"
"strings"
"testing"
+ "time"
"ragflow/internal/ingestion/component/knowledge_compiler/common"
)
@@ -126,14 +127,14 @@ func TestValidateAndCorrectChainFailOpen(t *testing.T) {
relRow("r-ag", "Alpha", "Gamma"),
}
deps := common.Deps{Chat: errChainChat{}, Embed: hashEmbedder{dim: 8}}
- out := validateAndCorrectChain(context.Background(), deps, "llm1", rows, nil, TypeList)
+ out := validateAndCorrectChain(common.WithRetryDelay(context.Background(), time.Millisecond), deps, "llm1", rows, nil, TypeList)
if len(out) != len(rows) {
t.Fatalf("fail-open: LLM failure must return the input untouched (%d vs %d)", len(out), len(rows))
}
// Non-chain kinds skip validation entirely (no LLM call possible).
deps2 := common.Deps{Chat: errChainChat{}}
- out2 := validateAndCorrectChain(context.Background(), deps2, "llm1", rows, nil, TypeHypergraph)
+ out2 := validateAndCorrectChain(common.WithRetryDelay(context.Background(), time.Millisecond), deps2, "llm1", rows, nil, TypeHypergraph)
if len(out2) != len(rows) {
t.Fatalf("non-chain kind must pass through untouched")
}
diff --git a/internal/ingestion/component/knowledge_compiler/tree/raptor.go b/internal/ingestion/component/knowledge_compiler/tree/raptor.go
index 0003efc78c..6f1e53a6c7 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/raptor.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/raptor.go
@@ -459,6 +459,16 @@ func buildTree(ctx context.Context, deps common.Deps, llmID, tenantID, docID str
// passed as the user turn so the model emits a one-line title on the first line
// of the summary. maxToken is deliberately not sent as MaxTokens: the provider
// controls its own output budget, while the prompt guides the target length.
+// summarizeBackoff returns the per-attempt backoff unit for summarizeTexts
+// retries. A ctx carrying common.WithRetryDelay shortens it so failure-path
+// tests need not wait out the real ~1s-per-attempt budget.
+func summarizeBackoff(ctx context.Context) time.Duration {
+ if d, ok := common.RetryDelayFromCtx(ctx); ok {
+ return d
+ }
+ return time.Second
+}
+
func summarizeTexts(ctx context.Context, deps common.Deps, llmID, systemText, userText string, maxToken int) (string, error) {
userPrompt := fmt.Sprintf("%s Keep the summary concise and target approximately %d tokens.", userText, maxToken)
for attempt := 0; attempt < raptorMaxRetries; attempt++ {
@@ -466,7 +476,7 @@ func summarizeTexts(ctx context.Context, deps common.Deps, llmID, systemText, us
select {
case <-ctx.Done():
return "", ctx.Err()
- case <-time.After(time.Duration(1+attempt) * time.Second):
+ case <-time.After(time.Duration(attempt+1) * summarizeBackoff(ctx)):
}
}
req := common.ChatRequest{
diff --git a/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go b/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go
index c8a71607dc..9d5af75b64 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go
@@ -4,6 +4,7 @@ import (
"context"
"strings"
"testing"
+ "time"
"ragflow/internal/ingestion/component/knowledge_compiler/common"
"ragflow/internal/tokenizer"
@@ -108,7 +109,7 @@ func TestSummarizeTextsFailsAfterMaxRetries(t *testing.T) {
f := &fakeChat{responses: []*common.ChatResponse{
{Content: "**ERROR** 1"}, {Content: "**ERROR** 2"}, {Content: "**ERROR** 3"},
}}
- if _, err := summarizeTexts(context.Background(), depsWithChat(f), "llm", "sys", "user", 512); err == nil {
+ if _, err := summarizeTexts(common.WithRetryDelay(context.Background(), time.Millisecond), depsWithChat(f), "llm", "sys", "user", 512); err == nil {
t.Fatal("expected error after exhausting retries")
}
if f.calls != raptorMaxRetries {
@@ -167,7 +168,7 @@ func TestBuildTreeNoPanicWhenAllSummariesFail(t *testing.T) {
{Text: "beta", Vector: []float32{0, 1, 0, 0}},
}
var products []common.Product
- if err := buildTree(context.Background(), deps, "llm", "t", "d", chunks, 4, "", common.Param{}, &products, nil); err != nil {
+ if err := buildTree(common.WithRetryDelay(context.Background(), time.Millisecond), deps, "llm", "t", "d", chunks, 4, "", common.Param{}, &products, nil); err != nil {
t.Fatalf("buildTree returned unexpected error: %v", err)
}
if len(products) != 0 {
diff --git a/internal/ingestion/component/knowledge_compiler/wiki/wiki_budget_test.go b/internal/ingestion/component/knowledge_compiler/wiki/wiki_budget_test.go
index d3054f1250..ef11addb5b 100644
--- a/internal/ingestion/component/knowledge_compiler/wiki/wiki_budget_test.go
+++ b/internal/ingestion/component/knowledge_compiler/wiki/wiki_budget_test.go
@@ -502,7 +502,7 @@ func TestRunPlan_ParallelBatchesFirstError(t *testing.T) {
big := strings.Repeat("x", 7000)
boom := errors.New("planning failed")
p := &wikiPipeline{
- ctx: context.Background(),
+ ctx: common.WithRetryDelay(context.Background(), time.Millisecond),
param: topicModeParam(),
deps: common.Deps{
Chat: failPlanChatStub{err: boom},