🎨 改进内核任务调度机制提升稳定性 https://github.com/siyuan-note/siyuan/issues/7113

This commit is contained in:
Liang Ding 2023-01-25 20:04:05 +08:00
parent 3f87f3da5e
commit 9b28cdfdf1
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 29 additions and 19 deletions

View file

@ -54,7 +54,7 @@ type dbQueueOperation struct {
func AutoFlushTx() { func AutoFlushTx() {
for { for {
time.Sleep(util.SQLFlushInterval) time.Sleep(util.SQLFlushInterval)
task.PrependTask(task.DatabaseIndexCommit, FlushQueue) task.AppendTask(task.DatabaseIndexCommit, FlushQueue)
} }
} }

View file

@ -52,6 +52,7 @@ func PrependTask(action string, handler interface{}, args ...interface{}) {
return return
} }
cancelTask(action, args...)
taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...) taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...)
} }
@ -64,23 +65,29 @@ func AppendTask(action string, handler interface{}, args ...interface{}) {
return return
} }
cancelTask(action, args...)
taskQueue = append(taskQueue, newTask(action, handler, args...)) taskQueue = append(taskQueue, newTask(action, handler, args...))
} }
func CancelTask(actions ...string) { func cancelTask(action string, args ...interface{}) {
queueLock.Lock()
defer queueLock.Unlock()
for i := len(taskQueue) - 1; i >= 0; i-- { for i := len(taskQueue) - 1; i >= 0; i-- {
task := taskQueue[i] task := taskQueue[i]
for _, action := range actions {
if action == task.Action { if action == task.Action {
if len(task.Args) != len(args) {
continue
}
for j, arg := range args {
if arg != task.Args[j] {
continue
}
}
taskQueue = append(taskQueue[:i], taskQueue[i+1:]...) taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)
break break
} }
} }
} }
}
func newTask(action string, handler interface{}, args ...interface{}) *Task { func newTask(action string, handler interface{}, args ...interface{}) *Task {
return &Task{ return &Task{

View file

@ -35,7 +35,7 @@ import (
var blockTrees = map[string]*BlockTree{} var blockTrees = map[string]*BlockTree{}
var blockTreesLock = sync.Mutex{} var blockTreesLock = sync.Mutex{}
var blockTreesChanged = false var blockTreesChanged = time.Time{}
type BlockTree struct { type BlockTree struct {
ID string // 块 ID ID string // 块 ID
@ -209,7 +209,7 @@ func SetBlockTreePath(tree *parse.Tree) {
b.BoxID, b.Path, b.HPath, b.Updated = tree.Box, tree.Path, tree.HPath, tree.Root.IALAttr("updated") b.BoxID, b.Path, b.HPath, b.Updated = tree.Box, tree.Path, tree.HPath, tree.Root.IALAttr("updated")
} }
} }
blockTreesChanged = true blockTreesChanged = time.Now()
} }
func RemoveBlockTreesByRootID(rootID string) { func RemoveBlockTreesByRootID(rootID string) {
@ -226,7 +226,7 @@ func RemoveBlockTreesByRootID(rootID string) {
for _, id := range ids { for _, id := range ids {
delete(blockTrees, id) delete(blockTrees, id)
} }
blockTreesChanged = true blockTreesChanged = time.Now()
} }
func RemoveBlockTreesByPath(path string) { func RemoveBlockTreesByPath(path string) {
@ -243,7 +243,7 @@ func RemoveBlockTreesByPath(path string) {
for _, id := range ids { for _, id := range ids {
delete(blockTrees, id) delete(blockTrees, id)
} }
blockTreesChanged = true blockTreesChanged = time.Now()
} }
func RemoveBlockTreesByPathPrefix(pathPrefix string) { func RemoveBlockTreesByPathPrefix(pathPrefix string) {
@ -260,7 +260,7 @@ func RemoveBlockTreesByPathPrefix(pathPrefix string) {
for _, id := range ids { for _, id := range ids {
delete(blockTrees, id) delete(blockTrees, id)
} }
blockTreesChanged = true blockTreesChanged = time.Now()
} }
func RemoveBlockTreesByBoxID(boxID string) (ids []string) { func RemoveBlockTreesByBoxID(boxID string) (ids []string) {
@ -276,7 +276,7 @@ func RemoveBlockTreesByBoxID(boxID string) (ids []string) {
for _, id := range ids { for _, id := range ids {
delete(blockTrees, id) delete(blockTrees, id)
} }
blockTreesChanged = true blockTreesChanged = time.Now()
return return
} }
@ -285,7 +285,7 @@ func RemoveBlockTree(id string) {
defer blockTreesLock.Unlock() defer blockTreesLock.Unlock()
delete(blockTrees, id) delete(blockTrees, id)
blockTreesChanged = true blockTreesChanged = time.Now()
} }
func ReindexBlockTree(tree *parse.Tree) { func ReindexBlockTree(tree *parse.Tree) {
@ -306,7 +306,7 @@ func ReindexBlockTree(tree *parse.Tree) {
blockTrees[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath, Updated: tree.Root.IALAttr("updated")} blockTrees[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath, Updated: tree.Root.IALAttr("updated")}
return ast.WalkContinue return ast.WalkContinue
}) })
blockTreesChanged = true blockTreesChanged = time.Now()
} }
func AutoFlushBlockTree() { func AutoFlushBlockTree() {
@ -361,11 +361,14 @@ func InitBlockTree(force bool) {
} }
func SaveBlockTree(force bool) { func SaveBlockTree(force bool) {
if !force && !blockTreesChanged { if !force && blockTreesChanged.IsZero() {
return return
} }
start := time.Now() start := time.Now()
if blockTreesChanged.Before(start.Add(7 * time.Second)) {
return
}
blockTreesLock.Lock() blockTreesLock.Lock()
data, err := msgpack.Marshal(blockTrees) data, err := msgpack.Marshal(blockTrees)
@ -387,5 +390,5 @@ func SaveBlockTree(force bool) {
logging.LogWarnf("save block tree [size=%s] to [%s], elapsed [%.2fs]", humanize.Bytes(uint64(len(data))), util.BlockTreePath, elapsed) logging.LogWarnf("save block tree [size=%s] to [%s], elapsed [%.2fs]", humanize.Bytes(uint64(len(data))), util.BlockTreePath, elapsed)
} }
blockTreesChanged = false blockTreesChanged = time.Time{}
} }