An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Retain failed live SQL index operations for retry https://github.com/siyuan-note/siyuan/issues/20935
Daniel90683937b4b2
diff --git a/kernel/sql/index_recovery_fts5_test.go b/kernel/sql/index_recovery_fts5_test.goindex 9376a25310..aba13d672e 100644--- a/kernel/sql/index_recovery_fts5_test.go+++ b/kernel/sql/index_recovery_fts5_test.go@@ -5,6 +5,7 @@ package sqlimport ("bytes""context"+ stdsql "database/sql""os""os/exec""path/filepath"@@ -148,25 +149,98 @@ func TestIndexRecoveryLifecycle(t *testing.T) {assertRows(tree)})- t.Run("failed transaction is retried in order", func(t *testing.T) {+ for _, recovered := range []bool{false, true} {+ name := "live"+ if recovered {+ name = "recovered"+ }+ t.Run(name+" failed transaction is retried in order", func(t *testing.T) {+ ClearQueue()+ first, second := newTree(), newTree()+ IndexTreeQueue(first)+ IndexTreeQueue(second)+ if recovered {+ restartQueue()+ }+ if _, err := db.Exec("CREATE TRIGGER reject_index BEFORE INSERT ON blocks BEGIN SELECT RAISE(ABORT, 'reject index'); END"); err != nil {+ t.Fatal(err)+ }+ FlushQueue()+ if len(operationQueue) != 2 || len(loadIndexQueue()) != 2 {+ t.Fatal("failed transaction discarded pending recovery")+ }+ if _, err := db.Exec("DROP TRIGGER reject_index"); err != nil {+ t.Fatal(err)+ }+ FlushQueue()+ assertRows(first)+ assertRows(second)+ })+ }++ t.Run("live begin failure retains ordered work beyond three retries", func(t *testing.T) {ClearQueue()first, second := newTree(), newTree()IndexTreeQueue(first)IndexTreeQueue(second)- restartQueue()- if _, err := db.Exec("CREATE TRIGGER reject_index BEFORE INSERT ON blocks BEGIN SELECT RAISE(ABORT, 'reject index'); END"); err != nil {+ workingDB := db+ closedDB, err := stdsql.Open("sqlite3", ":memory:")+ if err != nil {t.Fatal(err)}+ closedDB.Close()+ db = closedDB+ defer func() { db = workingDB }()+ for range 5 {+ FlushQueue()+ if len(operationQueue) != 2 || operationQueue[0].indexTree != first || operationQueue[1].indexTree != second || len(loadIndexQueue()) != 2 {+ t.Fatal("begin failure lost, reordered or duplicated pending work")+ }+ }+ db = workingDBFlushQueue()- if len(operationQueue) != 2 || len(loadIndexQueue()) != 2 {- t.Fatal("failed transaction discarded pending recovery")+ assertRows(first)+ assertRows(second)+ if len(operationQueue) != 0 || len(loadIndexQueue()) != 0 {+ t.Fatal("successful retry left pending work")}- if _, err := db.Exec("DROP TRIGGER reject_index"); err != nil {+ })++ t.Run("live commit failure preserves journal until retry", func(t *testing.T) {+ ClearQueue()+ tree := newTree()+ // A deferred foreign key fails at COMMIT after all indexing statements+ // succeeded, exercising a different path from statement failure.+ db.SetMaxOpenConns(1)+ defer db.SetMaxOpenConns(20)+ for _, stmt := range []string{+ "PRAGMA foreign_keys = ON",+ "CREATE TABLE index_test_parent (id TEXT PRIMARY KEY)",+ "CREATE TABLE index_test_child (id TEXT REFERENCES index_test_parent(id) DEFERRABLE INITIALLY DEFERRED)",+ "CREATE TRIGGER fail_index_commit AFTER INSERT ON blocks BEGIN INSERT INTO index_test_child VALUES (NEW.id); END",+ } {+ if _, err := db.Exec(stmt); err != nil {+ t.Fatal(err)+ }+ }+ defer db.Exec("PRAGMA foreign_keys = OFF")+ IndexTreeQueue(tree)+ FlushQueue()+ if len(operationQueue) != 1 || len(loadIndexQueue()) != 1 {+ t.Fatal("commit failure discarded live operation or journal")+ }+ var rows int+ if err := db.QueryRow("SELECT COUNT(*) FROM blocks WHERE root_id = ?", tree.ID).Scan(&rows); err != nil || rows != 0 {+ t.Fatalf("failed commit retained partial data: %d, %v", rows, err)+ }+ if _, err := db.Exec("DROP TRIGGER fail_index_commit"); err != nil {t.Fatal(err)}FlushQueue()- assertRows(first)- assertRows(second)+ assertRows(tree)+ if len(operationQueue) != 0 || len(loadIndexQueue()) != 0 {+ t.Fatal("successful commit did not checkpoint journal")+ }})t.Run("embed content is recalculated and searchable after restart", func(t *testing.T) {@@ -229,20 +303,28 @@ func TestIndexRecoveryLifecycle(t *testing.T) {}})- t.Run("locked notebook recovery does not block ordinary documents", func(t *testing.T) {- ClearQueue()- isEncrypted := IsEncryptedBoxFn- defer func() { IsEncryptedBoxFn = isEncrypted }()- lockedBox := "20260922000000-locked1"- IsEncryptedBoxFn = func(id string) bool { return id == lockedBox }- DeleteBoxQueue(lockedBox)- tree := newTree()- IndexTreeQueue(tree)- restartQueue()- FlushQueue()- assertRows(tree)- if len(operationQueue) != 0 || len(loadIndexQueue()) != 0 {- t.Fatal("obsolete locked notebook index blocked recovery")+ for _, recovered := range []bool{false, true} {+ name := "live"+ if recovered {+ name = "recovered"}- })+ t.Run(name+" locked notebook does not block ordinary documents", func(t *testing.T) {+ ClearQueue()+ isEncrypted := IsEncryptedBoxFn+ defer func() { IsEncryptedBoxFn = isEncrypted }()+ lockedBox := "20260922000000-locked1"+ IsEncryptedBoxFn = func(id string) bool { return id == lockedBox }+ DeleteBoxQueue(lockedBox)+ tree := newTree()+ IndexTreeQueue(tree)+ if recovered {+ restartQueue()+ }+ FlushQueue()+ assertRows(tree)+ if len(operationQueue) != 0 || len(loadIndexQueue()) != 0 {+ t.Fatal("obsolete locked notebook index blocked recovery")+ }+ })+ }}diff --git a/kernel/sql/queue.go b/kernel/sql/queue.goindex 7f046fe2e9..edc92bd3d3 100644--- a/kernel/sql/queue.go+++ b/kernel/sql/queue.go@@ -42,8 +42,6 @@ var (dbQueueCond = sync.NewCond(&dbQueueLock))-const maxBeginTxRetries = 3-type dbQueueOperation struct {inQueueTime time.Timeaction string // upsert/delete/delete_id/rename/move/delete_box/delete_box_refs/index/delete_ids/update_block_content/delete_assets/index_node@@ -57,7 +55,6 @@ type dbQueueOperation struct {block *Block // update_block_contentid string // index_noderemoveAssetHashes []string // delete_assets- beginTxRetries uint8recoveryEntry *indexEntryrecoveredIndex bool}@@ -286,13 +283,14 @@ func FlushQueue() {return}recovered := op.recoveryEntry != nil+ boxID := op.boxID()+ if IsEncryptedBoxFn != nil && IsEncryptedBoxFn(boxID) && GetEncryptedDB(boxID) == nil {+ // Locked notebooks rebuild derived indexes on mount. Obsolete live or+ // recovered work must not block indexing ordinary notebooks.+ ops[i] = nil+ continue+ }if recovered {- boxID := op.boxID()- if IsEncryptedBoxFn != nil && IsEncryptedBoxFn(boxID) && GetEncryptedDB(boxID) == nil {- // 锁定笔记本的派生索引由下次解锁挂载后重新建立,旧会话任务不应阻塞普通笔记本。- ops[i] = nil- continue- }if recoveryLute == nil {recoveryLute = lute.New()}@@ -313,18 +311,8 @@ func FlushQueue() {timing.Stage("begin")tx, err := beginTxForBox(op.boxID())if err != nil {- logging.LogWarnf("skip queue operation [%s] for box [%s]: %s", op.action, op.boxID(), err)- if recovered {- return- }- if op.beginTxRetries < maxBeginTxRetries {- op.beginTxRetries++- requeueOperation(op)- } else {- logging.LogErrorf("drop queue operation [%s] for box [%s] after %d retries: %s", op.action, op.boxID(), maxBeginTxRetries, err)- }- ops[i] = nil- continue+ logging.LogWarnf("retain queue operation [%s] for box [%s]: %s", op.action, op.boxID(), err)+ return}groupOpsCurrent[op.action]++@@ -337,11 +325,7 @@ func FlushQueue() {closeTxPreparedStmts(tx)invalidateRefsCacheForOperation(op)logging.LogErrorf("queue operation [%s] failed: %s", op.action, err)- if recovered {- return- }- ops[i] = nil- continue+ return}diagnostic.Stage("commit " + op.action)@@ -349,11 +333,7 @@ func FlushQueue() {if err = commitTx(tx); err != nil {invalidateRefsCacheForOperation(op)logging.LogErrorf("commit tx failed: %s", err)- if recovered {- return- }- ops[i] = nil- continue+ return}invalidateRefsCacheForOperation(op)backlinkChange.addOperation(op)@@ -737,11 +717,3 @@ func appendOperation(op *dbQueueOperation) {appendToIndexQueue(op)eventbus.Publish(eventbus.EvtSQLIndexChanged)}--func requeueOperation(op *dbQueueOperation) {- dbQueueLock.Lock()- defer dbQueueLock.Unlock()- operationQueue = append(operationQueue, op)- appendToIndexQueue(op)- eventbus.Publish(eventbus.EvtSQLIndexChanged)-}