backfill.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 Tulir Asokan, Sumner Evans
  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 database
  17. import (
  18. "database/sql"
  19. "errors"
  20. "fmt"
  21. "strconv"
  22. "strings"
  23. "sync"
  24. "time"
  25. log "maunium.net/go/maulogger/v2"
  26. "maunium.net/go/mautrix/id"
  27. )
  28. type BackfillType int
  29. const (
  30. BackfillImmediate BackfillType = 0
  31. BackfillForward BackfillType = 100
  32. BackfillDeferred BackfillType = 200
  33. )
  34. func (bt BackfillType) String() string {
  35. switch bt {
  36. case BackfillImmediate:
  37. return "IMMEDIATE"
  38. case BackfillForward:
  39. return "FORWARD"
  40. case BackfillDeferred:
  41. return "DEFERRED"
  42. }
  43. return "UNKNOWN"
  44. }
  45. type BackfillQuery struct {
  46. db *Database
  47. log log.Logger
  48. backfillQueryLock sync.Mutex
  49. }
  50. func (bq *BackfillQuery) New() *Backfill {
  51. return &Backfill{
  52. db: bq.db,
  53. log: bq.log,
  54. Portal: &PortalKey{},
  55. }
  56. }
  57. func (bq *BackfillQuery) NewWithValues(userID id.UserID, backfillType BackfillType, priority int, portal *PortalKey, timeStart *time.Time, maxBatchEvents, maxTotalEvents, batchDelay int) *Backfill {
  58. return &Backfill{
  59. db: bq.db,
  60. log: bq.log,
  61. UserID: userID,
  62. BackfillType: backfillType,
  63. Priority: priority,
  64. Portal: portal,
  65. TimeStart: timeStart,
  66. MaxBatchEvents: maxBatchEvents,
  67. MaxTotalEvents: maxTotalEvents,
  68. BatchDelay: batchDelay,
  69. }
  70. }
  71. const (
  72. getNextBackfillQuery = `
  73. SELECT queue_id, user_mxid, type, priority, portal_jid, portal_receiver, time_start, max_batch_events, max_total_events, batch_delay
  74. FROM backfill_queue
  75. WHERE user_mxid=$1
  76. AND type IN (%s)
  77. AND (
  78. dispatch_time IS NULL
  79. OR (
  80. dispatch_time < current_timestamp - interval '15 minutes'
  81. AND completed_at IS NULL
  82. )
  83. )
  84. ORDER BY type, priority, queue_id
  85. LIMIT 1
  86. `
  87. )
  88. // GetNext returns the next backfill to perform
  89. func (bq *BackfillQuery) GetNext(userID id.UserID, backfillTypes []BackfillType) (backfill *Backfill) {
  90. bq.backfillQueryLock.Lock()
  91. defer bq.backfillQueryLock.Unlock()
  92. types := []string{}
  93. for _, backfillType := range backfillTypes {
  94. types = append(types, strconv.Itoa(int(backfillType)))
  95. }
  96. rows, err := bq.db.Query(fmt.Sprintf(getNextBackfillQuery, strings.Join(types, ",")), userID)
  97. if err != nil || rows == nil {
  98. bq.log.Error(err)
  99. return
  100. }
  101. defer rows.Close()
  102. if rows.Next() {
  103. backfill = bq.New().Scan(rows)
  104. }
  105. return
  106. }
  107. func (bq *BackfillQuery) DeleteAll(userID id.UserID) {
  108. bq.backfillQueryLock.Lock()
  109. defer bq.backfillQueryLock.Unlock()
  110. _, err := bq.db.Exec("DELETE FROM backfill_queue WHERE user_mxid=$1", userID)
  111. if err != nil {
  112. bq.log.Warnfln("Failed to delete backfill queue items for %s: %v", userID, err)
  113. }
  114. }
  115. func (bq *BackfillQuery) DeleteAllForPortal(userID id.UserID, portalKey PortalKey) {
  116. bq.backfillQueryLock.Lock()
  117. defer bq.backfillQueryLock.Unlock()
  118. _, err := bq.db.Exec(`
  119. DELETE FROM backfill_queue
  120. WHERE user_mxid=$1
  121. AND portal_jid=$2
  122. AND portal_receiver=$3
  123. `, userID, portalKey.JID, portalKey.Receiver)
  124. if err != nil {
  125. bq.log.Warnfln("Failed to delete backfill queue items for %s/%s: %v", userID, portalKey.JID, err)
  126. }
  127. }
  128. type Backfill struct {
  129. db *Database
  130. log log.Logger
  131. // Fields
  132. QueueID int
  133. UserID id.UserID
  134. BackfillType BackfillType
  135. Priority int
  136. Portal *PortalKey
  137. TimeStart *time.Time
  138. MaxBatchEvents int
  139. MaxTotalEvents int
  140. BatchDelay int
  141. DispatchTime *time.Time
  142. CompletedAt *time.Time
  143. }
  144. func (b *Backfill) String() string {
  145. return fmt.Sprintf("Backfill{QueueID: %d, UserID: %s, BackfillType: %s, Priority: %d, Portal: %s, TimeStart: %s, MaxBatchEvents: %d, MaxTotalEvents: %d, BatchDelay: %d, DispatchTime: %s, CompletedAt: %s}",
  146. b.QueueID, b.UserID, b.BackfillType, b.Priority, b.Portal, b.TimeStart, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay, b.CompletedAt, b.DispatchTime,
  147. )
  148. }
  149. func (b *Backfill) Scan(row Scannable) *Backfill {
  150. err := row.Scan(&b.QueueID, &b.UserID, &b.BackfillType, &b.Priority, &b.Portal.JID, &b.Portal.Receiver, &b.TimeStart, &b.MaxBatchEvents, &b.MaxTotalEvents, &b.BatchDelay)
  151. if err != nil {
  152. if !errors.Is(err, sql.ErrNoRows) {
  153. b.log.Errorln("Database scan failed:", err)
  154. }
  155. return nil
  156. }
  157. return b
  158. }
  159. func (b *Backfill) Insert() {
  160. b.db.Backfill.backfillQueryLock.Lock()
  161. defer b.db.Backfill.backfillQueryLock.Unlock()
  162. rows, err := b.db.Query(`
  163. INSERT INTO backfill_queue
  164. (user_mxid, type, priority, portal_jid, portal_receiver, time_start, max_batch_events, max_total_events, batch_delay, dispatch_time, completed_at)
  165. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
  166. RETURNING queue_id
  167. `, b.UserID, b.BackfillType, b.Priority, b.Portal.JID, b.Portal.Receiver, b.TimeStart, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay, b.DispatchTime, b.CompletedAt)
  168. defer rows.Close()
  169. if err != nil || !rows.Next() {
  170. b.log.Warnfln("Failed to insert %v/%s with priority %d: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  171. return
  172. }
  173. err = rows.Scan(&b.QueueID)
  174. if err != nil {
  175. b.log.Warnfln("Failed to insert %s/%s with priority %s: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  176. }
  177. }
  178. func (b *Backfill) MarkDispatched() {
  179. b.db.Backfill.backfillQueryLock.Lock()
  180. defer b.db.Backfill.backfillQueryLock.Unlock()
  181. if b.QueueID == 0 {
  182. b.log.Errorf("Cannot mark backfill as dispatched without queue_id. Maybe it wasn't actually inserted in the database?")
  183. return
  184. }
  185. _, err := b.db.Exec("UPDATE backfill_queue SET dispatch_time=$1 WHERE queue_id=$2", time.Now(), b.QueueID)
  186. if err != nil {
  187. b.log.Warnfln("Failed to mark %s/%s as dispatched: %v", b.BackfillType, b.Priority, err)
  188. }
  189. }
  190. func (b *Backfill) MarkDone() {
  191. b.db.Backfill.backfillQueryLock.Lock()
  192. defer b.db.Backfill.backfillQueryLock.Unlock()
  193. if b.QueueID == 0 {
  194. b.log.Errorf("Cannot mark backfill done without queue_id. Maybe it wasn't actually inserted in the database?")
  195. return
  196. }
  197. _, err := b.db.Exec("UPDATE backfill_queue SET completed_at=$1 WHERE queue_id=$2", time.Now(), b.QueueID)
  198. if err != nil {
  199. b.log.Warnfln("Failed to mark %s/%s as complete: %v", b.BackfillType, b.Priority, err)
  200. }
  201. }
  202. func (bq *BackfillQuery) NewBackfillState(userID id.UserID, portalKey *PortalKey) *BackfillState {
  203. return &BackfillState{
  204. db: bq.db,
  205. log: bq.log,
  206. UserID: userID,
  207. Portal: portalKey,
  208. }
  209. }
  210. const (
  211. getBackfillState = `
  212. SELECT user_mxid, portal_jid, portal_receiver, processing_batch, backfill_complete, first_expected_ts
  213. FROM backfill_state
  214. WHERE user_mxid=$1
  215. AND portal_jid=$2
  216. AND portal_receiver=$3
  217. `
  218. )
  219. type BackfillState struct {
  220. db *Database
  221. log log.Logger
  222. // Fields
  223. UserID id.UserID
  224. Portal *PortalKey
  225. ProcessingBatch bool
  226. BackfillComplete bool
  227. FirstExpectedTimestamp uint64
  228. }
  229. func (b *BackfillState) Scan(row Scannable) *BackfillState {
  230. err := row.Scan(&b.UserID, &b.Portal.JID, &b.Portal.Receiver, &b.ProcessingBatch, &b.BackfillComplete, &b.FirstExpectedTimestamp)
  231. if err != nil {
  232. if !errors.Is(err, sql.ErrNoRows) {
  233. b.log.Errorln("Database scan failed:", err)
  234. }
  235. return nil
  236. }
  237. return b
  238. }
  239. func (b *BackfillState) Upsert() {
  240. _, err := b.db.Exec(`
  241. INSERT INTO backfill_state
  242. (user_mxid, portal_jid, portal_receiver, processing_batch, backfill_complete, first_expected_ts)
  243. VALUES ($1, $2, $3, $4, $5, $6)
  244. ON CONFLICT (user_mxid, portal_jid, portal_receiver)
  245. DO UPDATE SET
  246. processing_batch=EXCLUDED.processing_batch,
  247. backfill_complete=EXCLUDED.backfill_complete,
  248. first_expected_ts=EXCLUDED.first_expected_ts`,
  249. b.UserID, b.Portal.JID, b.Portal.Receiver, b.ProcessingBatch, b.BackfillComplete, b.FirstExpectedTimestamp)
  250. if err != nil {
  251. b.log.Warnfln("Failed to insert backfill state for %s: %v", b.Portal.JID, err)
  252. }
  253. }
  254. func (b *BackfillState) SetProcessingBatch(processing bool) {
  255. b.ProcessingBatch = processing
  256. b.Upsert()
  257. }
  258. func (bq *BackfillQuery) GetBackfillState(userID id.UserID, portalKey *PortalKey) (backfillState *BackfillState) {
  259. rows, err := bq.db.Query(getBackfillState, userID, portalKey.JID, portalKey.Receiver)
  260. if err != nil || rows == nil {
  261. bq.log.Error(err)
  262. return
  263. }
  264. defer rows.Close()
  265. if rows.Next() {
  266. backfillState = bq.NewBackfillState(userID, portalKey).Scan(rows)
  267. }
  268. return
  269. }