queue.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package sql
  17. import (
  18. "path"
  19. "sync"
  20. "time"
  21. "github.com/88250/lute/parse"
  22. "github.com/emirpasic/gods/sets/hashset"
  23. "github.com/siyuan-note/eventbus"
  24. "github.com/siyuan-note/logging"
  25. "github.com/siyuan-note/siyuan/kernel/task"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. var (
  29. operationQueue []*treeQueueOperation
  30. upsertTreeQueueLock = sync.Mutex{}
  31. txLock = sync.Mutex{}
  32. )
  33. type treeQueueOperation struct {
  34. inQueueTime time.Time
  35. action string // upsert/delete/delete_id/rename
  36. upsertTree *parse.Tree // upsert
  37. removeTreeBox, removeTreePath string // delete
  38. removeTreeIDBox, removeTreeID string // delete_id
  39. renameTree *parse.Tree // rename
  40. renameTreeOldHPath string // rename
  41. }
  42. func AutoFlushTx() {
  43. for {
  44. time.Sleep(util.SQLFlushInterval)
  45. task.PrependTask(task.DatabaseIndex, FlushQueue)
  46. }
  47. }
  48. func WaitForWritingDatabase() {
  49. var printLog bool
  50. var lastPrintLog bool
  51. for i := 0; isWritingDatabase(); i++ {
  52. time.Sleep(50 * time.Millisecond)
  53. if 200 < i && !printLog { // 10s 后打日志
  54. logging.LogWarnf("database is writing: \n%s", logging.ShortStack())
  55. printLog = true
  56. }
  57. if 1200 < i && !lastPrintLog { // 60s 后打日志
  58. logging.LogWarnf("database is still writing")
  59. lastPrintLog = true
  60. }
  61. }
  62. }
  63. func isWritingDatabase() bool {
  64. time.Sleep(util.SQLFlushInterval + 50*time.Millisecond)
  65. if 0 < len(operationQueue) || util.IsMutexLocked(&txLock) {
  66. return true
  67. }
  68. return false
  69. }
  70. func IsEmptyQueue() bool {
  71. return 1 > len(operationQueue) && !util.IsMutexLocked(&txLock)
  72. }
  73. func ClearQueue() {
  74. upsertTreeQueueLock.Lock()
  75. defer upsertTreeQueueLock.Unlock()
  76. operationQueue = nil
  77. }
  78. func FlushQueue() {
  79. ops := mergeUpsertTrees()
  80. if 1 > len(ops) {
  81. return
  82. }
  83. txLock.Lock()
  84. defer txLock.Unlock()
  85. start := time.Now()
  86. tx, err := BeginTx()
  87. if nil != err {
  88. return
  89. }
  90. context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
  91. boxes := hashset.New()
  92. for _, op := range ops {
  93. switch op.action {
  94. case "upsert":
  95. tree := op.upsertTree
  96. if err = upsertTree(tx, tree, context); nil != err {
  97. logging.LogErrorf("upsert tree [%s] into database failed: %s", tree.Box+tree.Path, err)
  98. }
  99. boxes.Add(op.upsertTree.Box)
  100. case "delete":
  101. batchDeleteByPathPrefix(tx, op.removeTreeBox, op.removeTreePath)
  102. boxes.Add(op.removeTreeBox)
  103. case "delete_id":
  104. DeleteByRootID(tx, op.removeTreeID)
  105. boxes.Add(op.removeTreeIDBox)
  106. case "rename":
  107. batchUpdateHPath(tx, op.renameTree.Box, op.renameTree.ID, op.renameTreeOldHPath, op.renameTree.HPath)
  108. updateRootContent(tx, path.Base(op.renameTree.HPath), op.renameTree.Root.IALAttr("updated"), op.renameTree.ID)
  109. boxes.Add(op.renameTree.Box)
  110. default:
  111. logging.LogErrorf("unknown operation [%s]", op.action)
  112. }
  113. }
  114. CommitTx(tx)
  115. elapsed := time.Now().Sub(start).Milliseconds()
  116. if 5000 < elapsed {
  117. logging.LogInfof("op tx [%dms]", elapsed)
  118. }
  119. }
  120. func mergeUpsertTrees() (ops []*treeQueueOperation) {
  121. upsertTreeQueueLock.Lock()
  122. defer upsertTreeQueueLock.Unlock()
  123. ops = operationQueue
  124. operationQueue = nil
  125. return
  126. }
  127. func UpsertTreeQueue(tree *parse.Tree) {
  128. upsertTreeQueueLock.Lock()
  129. defer upsertTreeQueueLock.Unlock()
  130. newOp := &treeQueueOperation{upsertTree: tree, inQueueTime: time.Now(), action: "upsert"}
  131. for i, op := range operationQueue {
  132. if "upsert" == op.action && op.upsertTree.ID == tree.ID { // 相同树则覆盖
  133. operationQueue[i] = newOp
  134. return
  135. }
  136. }
  137. operationQueue = append(operationQueue, newOp)
  138. }
  139. func RenameTreeQueue(tree *parse.Tree, oldHPath string) {
  140. upsertTreeQueueLock.Lock()
  141. defer upsertTreeQueueLock.Unlock()
  142. newOp := &treeQueueOperation{
  143. renameTree: tree,
  144. renameTreeOldHPath: oldHPath,
  145. inQueueTime: time.Now(),
  146. action: "rename"}
  147. for i, op := range operationQueue {
  148. if "rename" == op.action && op.renameTree.ID == tree.ID { // 相同树则覆盖
  149. operationQueue[i] = newOp
  150. return
  151. }
  152. }
  153. operationQueue = append(operationQueue, newOp)
  154. }
  155. func RemoveTreeQueue(box, rootID string) {
  156. upsertTreeQueueLock.Lock()
  157. defer upsertTreeQueueLock.Unlock()
  158. var tmp []*treeQueueOperation
  159. // 将已有的 upsert 操作去重
  160. for _, op := range operationQueue {
  161. if "upsert" == op.action && op.upsertTree.ID != rootID {
  162. tmp = append(tmp, op)
  163. }
  164. }
  165. operationQueue = tmp
  166. newOp := &treeQueueOperation{removeTreeIDBox: box, removeTreeID: rootID, inQueueTime: time.Now(), action: "delete_id"}
  167. operationQueue = append(operationQueue, newOp)
  168. }
  169. func RemoveTreePathQueue(treeBox, treePathPrefix string) {
  170. upsertTreeQueueLock.Lock()
  171. defer upsertTreeQueueLock.Unlock()
  172. var tmp []*treeQueueOperation
  173. // 将已有的 upsert 操作去重
  174. for _, op := range operationQueue {
  175. if "upsert" == op.action && (op.removeTreeBox != treeBox || op.upsertTree.Path != treePathPrefix) {
  176. tmp = append(tmp, op)
  177. }
  178. }
  179. operationQueue = tmp
  180. newOp := &treeQueueOperation{removeTreeBox: treeBox, removeTreePath: treePathPrefix, inQueueTime: time.Now(), action: "delete"}
  181. operationQueue = append(operationQueue, newOp)
  182. }