backfill.go 9.6 KB

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