Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Improve embedding indexing scheduling and connection error recovery https://github.com/siyuan-note/siyuan/issues/19245

Daniel9daa872d84ef
diff --git a/kernel/model/embedding.go b/kernel/model/embedding.go
index 4a8f55faef..cfce9f2153 100644
--- a/kernel/model/embedding.go
+++ b/kernel/model/embedding.go
@@ -54,12 +54,13 @@ const (
// block_embeddings.ignored_type 取值:区分块被跳过未嵌入的原因
embeddingIgnoredNone = 0 // 未忽略(正常嵌入或失败重试中)
- embeddingIgnoredByLen = 1 // 内容长度超限(< 7 或 > 12000 字符)
+ embeddingIgnoredByLen = 1 // 内容长度超限(< 7 或 > 12000 字节)
embeddingIgnoredByConf = 2 // 被 .siyuan/embeddingignore 配置匹配
)
var (
embeddingDirtyCh = make(chan string, 1024)
+ embeddingRetryCh = make(chan struct{}, 1)
embeddingTableOk bool
embeddingIgnoreLoaded bool
@@ -68,6 +69,10 @@ var (
embeddingStop atomic.Bool
+ // 重建与结果写入共用锁及代次,阻止重建前发出的请求回填索引。
+ embeddingResultMu sync.Mutex
+ embeddingGeneration uint64
+
// embeddingErrNotified 标记本轮是否已向用户提示过嵌入失败,避免多个并发 worker 失败时重复弹窗。
// 每次 processPendingEmbeddings 开始时随 embeddingStop 一起重置。
embeddingErrNotified atomic.Bool
@@ -108,7 +113,18 @@ func StartEmbeddingIndexer() {
processPendingEmbeddings()
for {
+ // 本轮服务请求失败后统一退避,避免未发送任务被积压的编辑事件立即再次调度。
+ if embeddingStop.Load() {
+ select {
+ case <-time.After(time.Duration(embeddingBackoffBase) * time.Second):
+ case <-embeddingRetryCh:
+ }
+ processPendingEmbeddings()
+ continue
+ }
select {
+ case <-embeddingRetryCh:
+ processPendingEmbeddings()
case <-embeddingDirtyCh:
processPendingEmbeddings()
case <-time.After(30 * time.Second):
@@ -126,31 +142,38 @@ func PrepareEmbeddingSearch() {
}
type embeddingJob struct {
- texts []string
- blocks []map[string]any
+ texts []string
+ blocks []map[string]any
+ generation uint64
}
func processPendingEmbeddings() {
if !isEmbeddingEnabled() {
return
}
+ processPendingEmbeddingRows(sql.QueryNoLimitArgs, doEmbedAndStore)
+}
+// 查询与执行分开传入,逐页等待所有任务结束后再读取待处理块。
+func processPendingEmbeddingRows(query func(string, ...any) ([]map[string]any, error),
+ embed func([]string, []map[string]any, uint64)) {
embeddingStop.Store(false)
embeddingErrNotified.Store(false)
workCh := make(chan embeddingJob, embeddingMaxConcurrency*2)
+ var jobsWg sync.WaitGroup
var workersWg sync.WaitGroup
for range embeddingMaxConcurrency {
workersWg.Go(func() {
for job := range workCh {
if embeddingStop.Load() {
- // 本轮已熔断(其它 worker 处理失败触发),这些积压 job 里的块不能直接丢弃,
- // 否则它们仍为 e.id IS NULL,下轮被反复捞出却永远不被写行。按失败处理写占位行。
- recordFailedEmbedding(job.blocks, "round stopped due to earlier failure in this round")
+ // 未发送的任务保持待处理状态,不消耗失败重试次数。
+ jobsWg.Done()
continue
}
- doEmbedAndStore(job.texts, job.blocks)
+ embed(job.texts, job.blocks, job.generation)
+ jobsWg.Done()
}
})
}
@@ -158,14 +181,17 @@ func processPendingEmbeddings() {
go func() {
defer close(workCh)
for {
- if embeddingStop.Load() {
+ if embeddingStop.Load() || !isEmbeddingEnabled() {
return
}
// SQL 粗筛下界用最小退避间隔,保证不漏掉到期的重试块;逐块的精确退避在下面 Go 侧再判
now := time.Now().Unix()
cutoff := now - int64(embeddingBackoffBase) // embeddingBackoffBase 单位为秒
- results, err := sql.QueryNoLimitArgs(stmtPendingBlocks, embeddingMaxFailCount, cutoff)
+ embeddingResultMu.Lock()
+ generation := embeddingGeneration
+ results, err := query(stmtPendingBlocks, embeddingMaxFailCount, cutoff)
+ embeddingResultMu.Unlock()
if err != nil {
logging.LogErrorf("query pending embedding blocks failed: %s", err)
return
@@ -177,15 +203,11 @@ func processPendingEmbeddings() {
var texts []string
var blocks []map[string]any
- anySubmitted := false // 本轮是否向 workCh 提交过 job
- backoffSkipped := 0 // 因未到退避时间被跳过的块数(这类块状态不变,下轮还会被捞出)
- minRemaining := int64(embeddingBackoffMax) // 这些块中最近的剩余等待秒数(embeddingBackoffMax 单位为秒)
+ anySubmitted := false // 本轮是否向 workCh 提交过 job
+ backoffSkipped := 0 // 因未到退避时间被跳过的块数(这类块状态不变,下轮还会被捞出)
for _, row := range results {
- id, _ := row["id"].(string)
- rootID, _ := row["root_id"].(string)
box, _ := row["box"].(string)
path, _ := row["path"].(string)
- updated, _ := row["updated"].(string)
content, _ := row["content"].(string)
// 失败过的块按各自 fail_count 精确判断是否到退避时间,未到期则本轮跳过
@@ -198,9 +220,6 @@ func processPendingEmbeddings() {
required := int64(embeddingBackoffFor(int(failCount)) / time.Second)
if elapsed := now - lastTried; elapsed < required {
backoffSkipped++
- if remaining := required - elapsed; remaining < minRemaining {
- minRemaining = remaining
- }
continue // 未到该块的退避时间
}
}
@@ -208,14 +227,12 @@ func processPendingEmbeddings() {
matcher := getEmbeddingIgnoreMatcher()
if nil != matcher && matcher.MatchesPath("/"+box+path) {
// 被 .siyuan/embeddingignore 配置匹配,配置忽略优先于长度忽略
- sql.Exec("INSERT OR IGNORE INTO block_embeddings (id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, ?)",
- id, rootID, box, path, []byte{}, embeddingModel(), 0, updated, embeddingIgnoredByConf)
+ storeIgnoredEmbedding(row, embeddingIgnoredByConf, generation)
continue
}
if len(content) < embeddingMinTextLen || len(content) > embeddingMaxContentLen {
// 内容长度超限(过短或过长),长度忽略
- sql.Exec("INSERT OR IGNORE INTO block_embeddings (id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, ?)",
- id, rootID, box, path, []byte{}, embeddingModel(), 0, updated, embeddingIgnoredByLen)
+ storeIgnoredEmbedding(row, embeddingIgnoredByLen, generation)
continue
}
row["plain_text"] = content
@@ -223,27 +240,24 @@ func processPendingEmbeddings() {
blocks = append(blocks, row)
if len(texts) >= embeddingBatchSize {
- workCh <- embeddingJob{texts: texts, blocks: blocks}
+ jobsWg.Add(1)
+ workCh <- embeddingJob{texts: texts, blocks: blocks, generation: generation}
anySubmitted = true
texts = nil
blocks = nil
}
}
if len(texts) > 0 {
- workCh <- embeddingJob{texts: texts, blocks: blocks}
+ jobsWg.Add(1)
+ workCh <- embeddingJob{texts: texts, blocks: blocks, generation: generation}
anySubmitted = true
}
+ // 等待本页请求及结果写入完成,避免再次查询到仍在处理的块。
+ jobsWg.Wait()
- // 本轮没有提交任何 job,且全部是被退避跳过的块:这些块状态没变,下轮 SQL 还会捞出同样的块,
- // 直接进下一轮会 CPU 忙等 + 高频 DB 查询。sleep 到最近的到期时间再继续,期间检查熔断以便及时退出。
+ // 待重试块尚未到期时交回外层事件循环,允许新编辑立即唤醒,不阻塞到整个退避间隔结束。
if !anySubmitted && backoffSkipped > 0 {
- wait := max(time.Duration(minRemaining)*time.Second, time.Second)
- // 分段 sleep,每秒检查一次 embeddingStop,熔断时尽快退出
- for wait > 0 && !embeddingStop.Load() {
- step := min(wait, time.Second)
- time.Sleep(step)
- wait -= step
- }
+ return
}
}
}()
@@ -294,9 +308,9 @@ func decodeVector(b []byte) []float32 {
return unsafe.Slice((*float32)(unsafe.Pointer(&b[0])), len(b)/embeddingVectorDim)
}
-// recordFailedEmbedding 把一批块标记为失败(fail_count+1,写空 embedding),并熔断本轮 + 提示用户。
+// recordFailedEmbedding 累加仍待处理的块的失败次数,并停止本轮及提示用户。
// 用于 API 调用出错或返回向量数与输入不匹配时的统一失败处理。
-func recordFailedEmbedding(blocks []map[string]any, reason string) {
+func recordFailedEmbedding(blocks []map[string]any, reason, model string) {
embeddingStop.Store(true)
logging.LogErrorf("create embeddings failed (%s), stop this round", reason)
// 多个 worker 可能并发失败,用 CAS 保证本轮只向用户提示一次
@@ -307,51 +321,72 @@ func recordFailedEmbedding(blocks []map[string]any, reason string) {
now := time.Now().Unix()
for _, row := range blocks {
id, _ := row["id"].(string)
- rootID, _ := row["root_id"].(string)
- box, _ := row["box"].(string)
- path, _ := row["path"].(string)
updated, _ := row["updated"].(string)
- // 先确保占位行存在(INSERT OR IGNORE 不覆盖已有行),再累加失败计数
- sql.Exec("INSERT OR IGNORE INTO block_embeddings (id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0)",
- id, rootID, box, path, []byte{}, embeddingModel(), 0, updated)
- sql.Exec("UPDATE block_embeddings SET fail_count = fail_count + 1, last_tried = ?, embedding = ?, model = ?, content_len = 0, ignored_type = 0 WHERE id = ?",
- now, []byte{}, embeddingModel(), id)
+ content, _ := row["content"].(string)
+ // 只记录仍对应原内容的失败,不覆盖已成功的向量。
+ if err := sql.Exec(stmtFailedEmbedding, []byte{}, model, now, id, updated, content); err != nil {
+ logging.LogErrorf("store embedding failure for block [%s]: %s", id, err)
+ }
}
}
-func doEmbedAndStore(texts []string, blocks []map[string]any) {
- vectors, err := util.BatchGetEmbeddings(texts, embeddingKey(), embeddingBaseURL(), embeddingModel(), embeddingDimensions(), embeddingTimeout())
+func doEmbedAndStore(texts []string, blocks []map[string]any, generation uint64) {
+ embeddingResultMu.Lock()
+ current := generation == embeddingGeneration
+ embeddingResultMu.Unlock()
+ if !current || !isEmbeddingEnabled() {
+ return
+ }
+ key, baseURL, model, dimensions := embeddingKey(), embeddingBaseURL(), embeddingModel(), embeddingDimensions()
+ vectors, err := util.BatchGetEmbeddings(texts, key, baseURL, model, dimensions, embeddingTimeout())
+ embeddingResultMu.Lock()
+ defer embeddingResultMu.Unlock()
+ if generation != embeddingGeneration || !isEmbeddingEnabled() || key != embeddingKey() ||
+ baseURL != embeddingBaseURL() || model != embeddingModel() || dimensions != embeddingDimensions() {
+ return
+ }
if err != nil {
// 任何 API 错误(含模型不存在/鉴权失败/限流/网络异常)都熔断本轮,避免连接风暴
- recordFailedEmbedding(blocks, err.Error())
+ recordFailedEmbedding(blocks, err.Error(), model)
return
}
// 部分 OpenAI 兼容 API 会对重复输入去重,返回少于输入数量的向量。此时无法对齐,整批按失败处理,避免越界 panic
if len(vectors) != len(blocks) {
- recordFailedEmbedding(blocks, fmt.Sprintf("count mismatch: requested %d but got %d", len(blocks), len(vectors)))
+ recordFailedEmbedding(blocks, fmt.Sprintf("count mismatch: requested %d but got %d", len(blocks), len(vectors)), model)
return
}
for i, row := range blocks {
id, _ := row["id"].(string)
- rootID, _ := row["root_id"].(string)
- box, _ := row["box"].(string)
- path, _ := row["path"].(string)
updated, _ := row["updated"].(string)
plainText, _ := row["plain_text"].(string)
buf := encodeVector(vectors[i])
- // 成功则整行重写,fail_count/last_tried/ignored_type 复位为 0
- err = sql.Exec("INSERT OR REPLACE INTO block_embeddings (id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0)",
- id, rootID, box, path, buf, embeddingModel(), len(plainText), updated)
+ // 原内容仍存在时才写入,路径取当前值,避免编辑、移动或删除期间回写过期结果。
+ err = sql.Exec(stmtStoreEmbedding, buf, model, len(plainText), id, updated, plainText)
if err != nil {
+ embeddingStop.Store(true)
logging.LogErrorf("store embedding failed for block [%s]: %s", id, err)
}
}
}
+// 忽略标记同样校验代次和内容,防止过期的短文本或路径匹配结果遮蔽新内容。
+func storeIgnoredEmbedding(row map[string]any, ignoredType int, generation uint64) {
+ embeddingResultMu.Lock()
+ defer embeddingResultMu.Unlock()
+ if generation != embeddingGeneration {
+ return
+ }
+ if err := sql.Exec(stmtIgnoreEmbedding, []byte{}, embeddingModel(), ignoredType,
+ row["id"], row["updated"], row["content"], row["box"], row["path"]); err != nil {
+ embeddingStop.Store(true)
+ logging.LogErrorf("store embedding ignore for block [%s]: %s", row["id"], err)
+ }
+}
+
func getEmbeddingIgnoreMatcher() *ignore.GitIgnore {
if embeddingIgnoreLoaded {
return embeddingIgnoreMatcher
@@ -656,18 +691,18 @@ func fullReindexEmbedding() {
return
}
resetEmbeddingIgnoreMatcher()
- if err := sql.Exec("DELETE FROM block_embeddings"); err != nil {
+ if err := clearEmbeddingResults("DELETE FROM block_embeddings"); err != nil {
logging.LogErrorf("clear block_embeddings failed: %s", err)
return
}
logging.LogInfof("embedding vectors cleared, indexer will re-embed all blocks")
// 若后台索引器死循环未运行(用户启动内核时嵌入未启用、随后才开启并点重建),这里补启动。
- // StartEmbeddingIndexer 内部用 CAS 保证只启动一个死循环。已运行则 Publish 唤醒立即补齐,不必等 30s 兜底轮询。
+ // StartEmbeddingIndexer 内部用 CAS 保证只启动一个循环。已运行则通过手动重试通道立即唤醒。
if !embeddingIndexerRunning.Load() {
go StartEmbeddingIndexer()
} else {
- eventbus.Publish(eventbus.EvtEmbeddingDirty, "")
+ signalEmbeddingRetry()
}
}
@@ -688,19 +723,38 @@ func retryFailedEmbedding() {
logging.LogWarnf("block_embeddings table not available, skip retry failed")
return
}
- if err := sql.Exec("DELETE FROM block_embeddings WHERE fail_count > 0"); err != nil {
+ if err := clearEmbeddingResults("DELETE FROM block_embeddings WHERE fail_count > 0"); err != nil {
logging.LogErrorf("delete failed embedding rows failed: %s", err)
return
}
logging.LogInfof("failed embedding rows cleared, indexer will retry these blocks")
// 唤醒常驻索引器立即补齐
if embeddingIndexerRunning.Load() {
- eventbus.Publish(eventbus.EvtEmbeddingDirty, "")
+ signalEmbeddingRetry()
} else {
go StartEmbeddingIndexer()
}
}
+// 手动重建或重试可提前结束服务退避,普通编辑事件仍遵守退避间隔。
+func signalEmbeddingRetry() {
+ select {
+ case embeddingRetryCh <- struct{}{}:
+ default:
+ }
+}
+
+// 清理索引后推进代次,使已经发出的请求结果失效。
+func clearEmbeddingResults(stmt string) error {
+ embeddingResultMu.Lock()
+ defer embeddingResultMu.Unlock()
+ if err := sql.Exec(stmt); err != nil {
+ return err
+ }
+ embeddingGeneration++
+ return nil
+}
+
// EmbeddingStat 嵌入索引进度统计,供设置页展示。
type EmbeddingStat struct {
Total int `json:"total"` // blocks 表总块数(分母)
diff --git a/kernel/model/embedding_queue_test.go b/kernel/model/embedding_queue_test.go
new file mode 100644
index 0000000000..38f7501d58
--- /dev/null
+++ b/kernel/model/embedding_queue_test.go
@@ -0,0 +1,159 @@
+package model
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+)
+
+func prepareEmbeddingQueueTest(t *testing.T) {
+ t.Helper()
+ previousConf := Conf
+ Conf = NewAppConf()
+ Conf.AI = conf.NewAI()
+ Conf.AI.Embedding = &conf.Embedding{Enabled: true, APIKey: "test"}
+ embeddingIgnoreLock.Lock()
+ loaded, matcher := embeddingIgnoreLoaded, embeddingIgnoreMatcher
+ embeddingIgnoreLoaded, embeddingIgnoreMatcher = true, nil
+ embeddingIgnoreLock.Unlock()
+ stop, notified := embeddingStop.Load(), embeddingErrNotified.Load()
+ t.Cleanup(func() {
+ Conf = previousConf
+ embeddingIgnoreLock.Lock()
+ embeddingIgnoreLoaded, embeddingIgnoreMatcher = loaded, matcher
+ embeddingIgnoreLock.Unlock()
+ embeddingStop.Store(stop)
+ embeddingErrNotified.Store(notified)
+ })
+}
+
+func TestEmbeddingQueueWaitsForWrites(t *testing.T) {
+ prepareEmbeddingQueueTest(t)
+ var queries, calls atomic.Int32
+ var written atomic.Bool
+ started, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{})
+ var once sync.Once
+ query := func(string, ...any) ([]map[string]any, error) {
+ queries.Add(1)
+ if written.Load() {
+ return nil, nil
+ }
+ return []map[string]any{{"id": "block", "content": "pending content"}}, nil
+ }
+ go func() {
+ defer close(done)
+ processPendingEmbeddingRows(query, func([]string, []map[string]any, uint64) {
+ calls.Add(1)
+ once.Do(func() { close(started) })
+ <-release
+ written.Store(true)
+ })
+ }()
+ select {
+ case <-started:
+ case <-time.After(5 * time.Second):
+ close(release)
+ t.Fatal("worker did not start")
+ }
+ // 留出调度时间,结果尚未写入时不得再次查询。
+ time.Sleep(50 * time.Millisecond)
+ beforeWrite := queries.Load()
+ close(release)
+ select {
+ case <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("queue did not finish")
+ }
+ if beforeWrite != 1 || queries.Load() != 2 || calls.Load() != 1 {
+ t.Fatalf("before write=%d, queries=%d, calls=%d", beforeWrite, queries.Load(), calls.Load())
+ }
+}
+
+func TestEmbeddingQueueStopsUnsentJobs(t *testing.T) {
+ prepareEmbeddingQueueTest(t)
+ var calls, queries atomic.Int32
+ query := func(string, ...any) ([]map[string]any, error) {
+ queries.Add(1)
+ rows := make([]map[string]any, 100)
+ for i := range rows {
+ rows[i] = map[string]any{"id": fmt.Sprint(i), "content": "pending content"}
+ }
+ return rows, nil
+ }
+ processPendingEmbeddingRows(query, func([]string, []map[string]any, uint64) {
+ embeddingStop.Store(true)
+ calls.Add(1)
+ })
+ if queries.Load() != 1 || calls.Load() < 1 || calls.Load() > embeddingMaxConcurrency {
+ t.Fatalf("queries=%d, calls=%d", queries.Load(), calls.Load())
+ }
+ if embeddingErrNotified.Load() {
+ t.Fatal("unsent jobs must not report an embedding failure")
+ }
+}
+
+func TestEmbeddingBackoffYieldsToNewEdits(t *testing.T) {
+ prepareEmbeddingQueueTest(t)
+ var calls atomic.Int32
+ started := time.Now()
+ processPendingEmbeddingRows(func(string, ...any) ([]map[string]any, error) {
+ return []map[string]any{{"id": "block", "content": "pending content",
+ "fail_count": int64(2), "last_tried": time.Now().Unix() - 31}}, nil
+ }, func([]string, []map[string]any, uint64) {
+ calls.Add(1)
+ })
+ if calls.Load() != 0 || time.Since(started) > time.Second {
+ t.Fatalf("calls=%d, elapsed=%s", calls.Load(), time.Since(started))
+ }
+}
+
+func TestEmbeddingRebuildDiscardsInflightFailure(t *testing.T) {
+ prepareEmbeddingQueueTest(t)
+ started, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{})
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ close(started)
+ <-release
+ http.Error(w, `{"error":{"message":"failed"}}`, http.StatusInternalServerError)
+ }))
+ defer server.Close()
+ Conf.AI.Embedding.BaseURL = server.URL
+ Conf.AI.Embedding.Timeout = 5
+ embeddingResultMu.Lock()
+ generation := embeddingGeneration
+ embeddingResultMu.Unlock()
+ t.Cleanup(func() {
+ embeddingResultMu.Lock()
+ embeddingGeneration = generation
+ embeddingResultMu.Unlock()
+ })
+ embeddingStop.Store(false)
+ embeddingErrNotified.Store(false)
+ go func() {
+ defer close(done)
+ doEmbedAndStore([]string{"content"}, []map[string]any{{"id": "block", "content": "content"}}, generation)
+ }()
+ select {
+ case <-started:
+ case <-time.After(5 * time.Second):
+ close(release)
+ t.Fatal("request did not start")
+ }
+ embeddingResultMu.Lock()
+ embeddingGeneration++
+ embeddingResultMu.Unlock()
+ close(release)
+ select {
+ case <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("request did not finish")
+ }
+ if embeddingStop.Load() || embeddingErrNotified.Load() {
+ t.Fatal("a request from before rebuilding must not stop the new index")
+ }
+}
diff --git a/kernel/model/embedding_store.go b/kernel/model/embedding_store.go
new file mode 100644
index 0000000000..f2d5fb0c38
--- /dev/null
+++ b/kernel/model/embedding_store.go
@@ -0,0 +1,23 @@
+package model
+
+// 写入时在同一 SQL 中验证内容及更新时间,避免查询与写入之间发生编辑而产生过期索引。
+const stmtStoreEmbedding = "INSERT OR REPLACE INTO block_embeddings " +
+ "(id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) " +
+ "SELECT id, root_id, box, path, ?, ?, ?, updated, 0, 0, 0 FROM blocks " +
+ "WHERE id = ? AND updated = ? AND content = ?"
+
+// 失败仅累加空向量记录,成功结果不受迟到的失败影响。
+const stmtFailedEmbedding = "INSERT INTO block_embeddings " +
+ "(id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) " +
+ "SELECT id, root_id, box, path, ?, ?, 0, updated, 1, ?, 0 FROM blocks " +
+ "WHERE id = ? AND updated = ? AND content = ? " +
+ "ON CONFLICT(id) DO UPDATE SET fail_count = block_embeddings.fail_count + 1, " +
+ "last_tried = excluded.last_tried, model = excluded.model, content_len = 0, ignored_type = 0 " +
+ "WHERE length(block_embeddings.embedding) = 0"
+
+const stmtIgnoreEmbedding = "INSERT INTO block_embeddings " +
+ "(id, root_id, box, path, embedding, model, content_len, updated, fail_count, last_tried, ignored_type) " +
+ "SELECT id, root_id, box, path, ?, ?, 0, updated, 0, 0, ? FROM blocks " +
+ "WHERE id = ? AND updated = ? AND content = ? AND box = ? AND path = ? " +
+ "ON CONFLICT(id) DO UPDATE SET fail_count = 0, last_tried = 0, ignored_type = excluded.ignored_type, " +
+ "model = excluded.model, content_len = 0 WHERE length(block_embeddings.embedding) = 0"
diff --git a/kernel/model/embedding_store_test.go b/kernel/model/embedding_store_test.go
new file mode 100644
index 0000000000..b74652b735
--- /dev/null
+++ b/kernel/model/embedding_store_test.go
@@ -0,0 +1,71 @@
+package model
+
+import (
+ "database/sql"
+ "testing"
+
+ _ "github.com/mattn/go-sqlite3"
+)
+
+func TestEmbeddingResultWrites(t *testing.T) {
+ db, err := sql.Open("sqlite3", ":memory:")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ db.SetMaxOpenConns(1)
+ exec := func(stmt string, args ...any) {
+ t.Helper()
+ if _, err := db.Exec(stmt, args...); err != nil {
+ t.Fatal(err)
+ }
+ }
+ exec("CREATE TABLE blocks (id TEXT PRIMARY KEY, root_id TEXT, box TEXT, path TEXT, updated TEXT, content TEXT)")
+ exec("CREATE TABLE block_embeddings (id TEXT PRIMARY KEY, root_id TEXT, box TEXT, path TEXT, embedding BLOB, model TEXT, content_len INTEGER, updated TEXT, fail_count INTEGER, last_tried INTEGER, ignored_type INTEGER)")
+ exec("INSERT INTO blocks VALUES ('id', 'root', 'box', '/path', '1', 'original')")
+ check := func(wantCount, wantFailures, wantBytes int) {
+ t.Helper()
+ var count, failures, bytes int
+ if err := db.QueryRow("SELECT COUNT(*), COALESCE(SUM(fail_count), 0), COALESCE(SUM(length(embedding)), 0) FROM block_embeddings").Scan(&count, &failures, &bytes); err != nil {
+ t.Fatal(err)
+ }
+ if count != wantCount || failures != wantFailures || bytes != wantBytes {
+ t.Fatalf("count=%d, failures=%d, bytes=%d", count, failures, bytes)
+ }
+ }
+ exec(stmtFailedEmbedding, []byte{}, "model", 1, "id", "1", "original")
+ exec(stmtFailedEmbedding, []byte{}, "model", 2, "id", "1", "original")
+ check(1, 2, 0)
+ exec(stmtIgnoreEmbedding, []byte{}, "model", 2, "id", "1", "original", "box", "/path")
+ check(1, 0, 0)
+ var ignored int
+ if err := db.QueryRow("SELECT ignored_type FROM block_embeddings").Scan(&ignored); err != nil || ignored != 2 {
+ t.Fatalf("ignored=%d, error=%v", ignored, err)
+ }
+ exec(stmtStoreEmbedding, []byte{1, 2, 3, 4}, "model", 8, "id", "1", "original")
+ exec(stmtFailedEmbedding, []byte{}, "model", 3, "id", "1", "original")
+ check(1, 0, 4)
+
+ // 同一秒内编辑也必须凭内容拦截旧结果。
+ exec("DELETE FROM block_embeddings")
+ exec("UPDATE blocks SET content = 'edited'")
+ exec(stmtStoreEmbedding, []byte{1}, "model", 8, "id", "1", "original")
+ exec(stmtFailedEmbedding, []byte{}, "model", 4, "id", "1", "original")
+ exec(stmtIgnoreEmbedding, []byte{}, "model", 1, "id", "1", "original", "box", "/path")
+ check(0, 0, 0)
+
+ // 移动后使用当前路径,删除后不恢复索引行。
+ exec("UPDATE blocks SET path = '/moved'")
+ exec(stmtIgnoreEmbedding, []byte{}, "model", 2, "id", "1", "edited", "box", "/path")
+ check(0, 0, 0)
+ exec(stmtStoreEmbedding, []byte{1}, "model", 6, "id", "1", "edited")
+ var path string
+ if err := db.QueryRow("SELECT path FROM block_embeddings").Scan(&path); err != nil || path != "/moved" {
+ t.Fatalf("path=%s, error=%v", path, err)
+ }
+ exec("DELETE FROM blocks")
+ exec("DELETE FROM block_embeddings")
+ exec(stmtStoreEmbedding, []byte{1}, "model", 6, "id", "1", "edited")
+ exec(stmtFailedEmbedding, []byte{}, "model", 5, "id", "1", "edited")
+ check(0, 0, 0)
+}
diff --git a/kernel/util/embedding_test.go b/kernel/util/embedding_test.go
new file mode 100644
index 0000000000..1b4ac429b7
--- /dev/null
+++ b/kernel/util/embedding_test.go
@@ -0,0 +1,106 @@
+package util
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+func TestEmbeddingConnectionRetry(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ failures int32
+ status int
+ wantCalls int32
+ wantError bool
+ }{
+ {"recover", 1, 0, 2, false},
+ {"bounded", 3, 0, 2, true},
+ {"authentication", 1, http.StatusUnauthorized, 1, true},
+ {"contextLimit", 1, http.StatusInternalServerError, 1, true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ var calls atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var request struct {
+ Input []string `json:"input"`
+ Model string `json:"model"`
+ Dimensions int `json:"dimensions"`
+ }
+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil || len(request.Input) != 2 ||
+ request.Input[0] != "first text" || request.Input[1] != "second text" ||
+ request.Model != "test-model" || request.Dimensions != 2 {
+ t.Errorf("unexpected request: %+v, error: %v", request, err)
+ }
+ if calls.Add(1) <= tc.failures {
+ if tc.status != 0 {
+ w.WriteHeader(tc.status)
+ io.WriteString(w, `{"error":{"message":"rejected","type":"server_error"}}`)
+ return
+ }
+ conn, _, err := w.(http.Hijacker).Hijack()
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ conn.Close()
+ return
+ }
+ io.WriteString(w, `{"data":[{"index":0,"embedding":[1,0]},{"index":1,"embedding":[0,1]}]}`)
+ }))
+ defer server.Close()
+ vectors, err := BatchGetEmbeddings([]string{"first text", "second text"}, "key", server.URL+"/v1", "test-model", 2, 5)
+ if (err != nil) != tc.wantError || calls.Load() != tc.wantCalls {
+ t.Fatalf("calls=%d, error=%v", calls.Load(), err)
+ }
+ if !tc.wantError && (len(vectors) != 2 || vectors[0][0] != 1 || vectors[1][1] != 1) {
+ t.Fatalf("unexpected vectors: %v", vectors)
+ }
+ })
+ }
+}
+
+func TestEmbeddingRetrySharesDeadline(t *testing.T) {
+ var calls atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ calls.Add(1)
+ select {
+ case <-time.After(850 * time.Millisecond):
+ conn, _, err := w.(http.Hijacker).Hijack()
+ if err == nil {
+ conn.Close()
+ }
+ case <-r.Context().Done():
+ }
+ }))
+ defer server.Close()
+ _, err := BatchGetEmbeddings([]string{"text"}, "key", server.URL, "model", 0, 1)
+ if !errors.Is(err, context.DeadlineExceeded) || calls.Load() != 1 {
+ t.Fatalf("calls=%d, error=%v", calls.Load(), err)
+ }
+}
+
+func TestRetryableEmbeddingError(t *testing.T) {
+ for _, tc := range []struct {
+ err error
+ want bool
+ }{
+ {nil, false},
+ {io.EOF, false},
+ {&url.Error{Op: "Post", Err: io.EOF}, true},
+ {&url.Error{Op: "Post", Err: errors.New("http: server closed idle connection")}, true},
+ {&url.Error{Op: "Post", Err: context.Canceled}, false},
+ {&url.Error{Op: "Post", Err: context.DeadlineExceeded}, false},
+ } {
+ if got := retryableEmbeddingError(tc.err); got != tc.want {
+ t.Errorf("error=%v: got %v, want %v", tc.err, got, tc.want)
+ }
+ }
+}
diff --git a/kernel/util/openai.go b/kernel/util/openai.go
index 4be0d6b060..823c9002ad 100644
--- a/kernel/util/openai.go
+++ b/kernel/util/openai.go
@@ -482,6 +482,9 @@ func BatchGetEmbeddings(texts []string, apiKey, baseURL, model string, dimension
if 1 > len(texts) {
return
}
+ if timeout < 1 {
+ timeout = 30
+ }
config := openai.DefaultConfig(apiKey)
config.BaseURL = baseURL
@@ -491,11 +494,23 @@ func BatchGetEmbeddings(texts []string, apiKey, baseURL, model string, dimension
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
- resp, err := client.CreateEmbeddings(ctx, openai.EmbeddingRequestStrings{
+ request := openai.EmbeddingRequestStrings{
Input: texts,
Model: openai.EmbeddingModel(model),
Dimensions: dimensions, // 0 时因 omitempty 不发送,等同于用模型默认维度
- })
+ }
+ resp, err := client.CreateEmbeddings(ctx, request)
+ if ctx.Err() == nil && retryableEmbeddingError(err) {
+ // 嵌入计算遇到瞬时连接中断时最多补试一次,等待及补试共享原请求的超时预算。
+ timer := time.NewTimer(200 * time.Millisecond)
+ select {
+ case <-ctx.Done():
+ timer.Stop()
+ err = ctx.Err()
+ case <-timer.C:
+ resp, err = client.CreateEmbeddings(ctx, request)
+ }
+ }
if err != nil {
logging.LogErrorf("create embeddings failed: %s", err)
return
@@ -507,6 +522,17 @@ func BatchGetEmbeddings(texts []string, apiKey, baseURL, model string, dimension
return
}
+// retryableEmbeddingError 仅重试传输层连接中断,不重试接口状态错误、响应解析错误或超时。
+func retryableEmbeddingError(err error) bool {
+ var requestErr *url.Error
+ if !errors.As(err, &requestErr) || requestErr.Timeout() {
+ return false
+ }
+ return errors.Is(requestErr.Err, io.EOF) || errors.Is(requestErr.Err, io.ErrUnexpectedEOF) ||
+ errors.Is(requestErr.Err, syscall.ECONNRESET) || errors.Is(requestErr.Err, syscall.EPIPE) ||
+ requestErr.Err.Error() == "http: server closed idle connection"
+}
+
// rerankDocTextMaxRunes 限制单篇文档送入重排服务的最大 Unicode 字符数,兼顾常见模型的输入上限。
const rerankDocTextMaxRunes = 4000