-
Notifications
You must be signed in to change notification settings - Fork 43
/
sqlite.go
427 lines (351 loc) · 8.99 KB
/
sqlite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package db
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/doug-martin/goqu/v9"
"github.com/fsnotify/fsnotify"
"github.com/mattn/go-sqlite3"
"github.com/maxpert/marmot/pool"
"github.com/maxpert/marmot/telemetry"
"github.com/rs/zerolog/log"
)
const snapshotTransactionMode = "exclusive"
var PoolSize = 4
var MarmotPrefix = "__marmot__"
type statsSqliteStreamDB struct {
published telemetry.Counter
pendingPublish telemetry.Gauge
countChanges telemetry.Histogram
scanChanges telemetry.Histogram
}
type SqliteStreamDB struct {
OnChange func(event *ChangeLogEvent) error
pool *pool.SQLitePool
rawConnection *sqlite3.SQLiteConn
publishLock *sync.Mutex
dbPath string
prefix string
watchTablesSchema map[string][]*ColumnInfo
stats *statsSqliteStreamDB
}
type ColumnInfo struct {
Name string `db:"name"`
Type string `db:"type"`
NotNull bool `db:"notnull"`
DefaultValue any `db:"dflt_value"`
PrimaryKeyIndex int `db:"pk"`
IsPrimaryKey bool
}
func RestoreFrom(destPath, bkFilePath string) error {
dnsTpl := "%s?_journal_mode=WAL&_foreign_keys=false&_busy_timeout=30000&_sync=FULL&_txlock=%s"
dns := fmt.Sprintf(dnsTpl, destPath, snapshotTransactionMode)
destDB, dest, err := pool.OpenRaw(dns)
if err != nil {
return err
}
defer dest.Close()
dns = fmt.Sprintf(dnsTpl, bkFilePath, snapshotTransactionMode)
srcDB, src, err := pool.OpenRaw(dns)
if err != nil {
return err
}
defer src.Close()
dgSQL := goqu.New("sqlite", destDB)
sgSQL := goqu.New("sqlite", srcDB)
// Source locking is required so that any lock related metadata is mirrored in destination
// Transacting on both src and dest in immediate mode makes sure nobody
// else is modifying or interacting with DB
err = sgSQL.WithTx(func(dtx *goqu.TxDatabase) error {
return dgSQL.WithTx(func(_ *goqu.TxDatabase) error {
err = copyFile(destPath, bkFilePath)
if err != nil {
return err
}
err = copyFile(destPath+"-shm", bkFilePath+"-shm")
if err != nil {
return err
}
err = copyFile(destPath+"-wal", bkFilePath+"-wal")
if err != nil {
return err
}
return nil
})
})
if err != nil {
return err
}
err = performCheckpoint(dgSQL)
if err != nil {
return err
}
return nil
}
func GetAllDBTables(path string) ([]string, error) {
connectionStr := fmt.Sprintf("%s?_journal_mode=WAL", path)
conn, rawConn, err := pool.OpenRaw(connectionStr)
if err != nil {
return nil, err
}
defer rawConn.Close()
defer conn.Close()
gSQL := goqu.New("sqlite", conn)
names := make([]string, 0)
err = gSQL.WithTx(func(tx *goqu.TxDatabase) error {
return listDBTables(&names, tx)
})
if err != nil {
return nil, err
}
return names, nil
}
func OpenStreamDB(path string) (*SqliteStreamDB, error) {
dbPool, err := pool.NewSQLitePool(fmt.Sprintf("%s?_journal_mode=WAL", path), PoolSize, true)
if err != nil {
return nil, err
}
conn, err := dbPool.Borrow()
if err != nil {
return nil, err
}
defer conn.Return()
err = performCheckpoint(conn.DB())
if err != nil {
return nil, err
}
ret := &SqliteStreamDB{
pool: dbPool,
dbPath: path,
prefix: MarmotPrefix,
publishLock: &sync.Mutex{},
watchTablesSchema: map[string][]*ColumnInfo{},
stats: &statsSqliteStreamDB{
published: telemetry.NewCounter("published", "number of rows published"),
pendingPublish: telemetry.NewGauge("pending_publish", "rows pending publishing"),
countChanges: telemetry.NewHistogram("count_changes", "latency counting changes in microseconds"),
scanChanges: telemetry.NewHistogram("scan_changes", "latency scanning change rows in DB"),
},
}
return ret, nil
}
func (conn *SqliteStreamDB) InstallCDC(tables []string) error {
sqlConn, err := conn.pool.Borrow()
if err != nil {
return err
}
defer sqlConn.Return()
err = sqlConn.DB().WithTx(func(tx *goqu.TxDatabase) error {
for _, n := range tables {
colInfo, err := getTableInfo(tx, n)
if err != nil {
return err
}
conn.watchTablesSchema[n] = colInfo
}
return nil
})
if err != nil {
return err
}
err = conn.installChangeLogTriggers()
if err != nil {
return err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go conn.watchChanges(watcher, conn.dbPath)
return nil
}
func (conn *SqliteStreamDB) RemoveCDC(tables bool) error {
sqlConn, err := conn.pool.Borrow()
if err != nil {
return err
}
defer sqlConn.Return()
log.Info().Msg("Uninstalling all CDC triggers...")
err = removeMarmotTriggers(sqlConn.DB(), conn.prefix)
if err != nil {
return err
}
if tables {
return removeMarmotTables(sqlConn.DB(), conn.prefix)
}
return nil
}
func (conn *SqliteStreamDB) installChangeLogTriggers() error {
if err := conn.initGlobalChangeLog(); err != nil {
return err
}
for tableName := range conn.watchTablesSchema {
err := conn.initTriggers(tableName)
if err != nil {
return err
}
}
return nil
}
func getTableInfo(tx *goqu.TxDatabase, table string) ([]*ColumnInfo, error) {
query := "SELECT name, type, `notnull`, dflt_value, pk FROM pragma_table_info(?)"
stmt, err := tx.Prepare(query)
if err != nil {
return nil, err
}
rows, err := stmt.Query(table)
if err != nil {
return nil, err
}
tableInfo := make([]*ColumnInfo, 0)
hasPrimaryKey := false
for rows.Next() {
if rows.Err() != nil {
return nil, rows.Err()
}
c := ColumnInfo{}
err = rows.Scan(&c.Name, &c.Type, &c.NotNull, &c.DefaultValue, &c.PrimaryKeyIndex)
if err != nil {
return nil, err
}
c.IsPrimaryKey = c.PrimaryKeyIndex > 0
if c.IsPrimaryKey {
hasPrimaryKey = true
}
tableInfo = append(tableInfo, &c)
}
if !hasPrimaryKey {
tableInfo = append(tableInfo, &ColumnInfo{
Name: "rowid",
IsPrimaryKey: true,
Type: "INT",
NotNull: true,
DefaultValue: nil,
})
}
return tableInfo, nil
}
func (conn *SqliteStreamDB) BackupTo(bkFilePath string) error {
sqlDB, rawDB, err := pool.OpenRaw(fmt.Sprintf("%s?mode=ro&_foreign_keys=false&_journal_mode=WAL", conn.dbPath))
if err != nil {
return err
}
defer sqlDB.Close()
defer rawDB.Close()
_, err = rawDB.Exec("VACUUM main INTO ?;", []driver.Value{bkFilePath})
if err != nil {
return err
}
err = rawDB.Close()
if err != nil {
return err
}
err = sqlDB.Close()
if err != nil {
return err
}
// Now since we have separate copy of DB we don't need to deal with WAL journals or foreign keys
// We need to remove all the marmot specific tables, triggers, and vacuum out the junk.
sqlDB, rawDB, err = pool.OpenRaw(fmt.Sprintf("%s?_foreign_keys=false&_journal_mode=TRUNCATE", bkFilePath))
if err != nil {
return err
}
gSQL := goqu.New("sqlite", sqlDB)
err = removeMarmotTriggers(gSQL, conn.prefix)
if err != nil {
return err
}
err = removeMarmotTables(gSQL, conn.prefix)
if err != nil {
return err
}
_, err = gSQL.Exec("VACUUM;")
if err != nil {
return err
}
return nil
}
func (conn *SqliteStreamDB) GetRawConnection() *sqlite3.SQLiteConn {
return conn.rawConnection
}
func (conn *SqliteStreamDB) GetPath() string {
return conn.dbPath
}
func (conn *SqliteStreamDB) WithReadTx(cb func(tx *sql.Tx) error) error {
var tx *sql.Tx = nil
db, _, err := pool.OpenRaw(fmt.Sprintf("%s?_journal_mode=WAL", conn.dbPath))
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer func() {
if r := recover(); r != nil {
log.Error().Any("recover", r).Msg("Recovered read transaction")
}
if tx != nil {
err = tx.Rollback()
if err != nil {
log.Error().Err(err).Msg("Error performing read transaction")
}
}
db.Close()
cancel()
}()
tx, err = db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
return cb(tx)
}
func copyFile(toPath, fromPath string) error {
fi, err := os.OpenFile(fromPath, os.O_RDWR, 0)
if err != nil {
return err
}
defer fi.Close()
fo, err := os.OpenFile(toPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_SYNC, 0)
if err != nil {
return err
}
defer fo.Close()
bytesWritten, err := io.Copy(fo, fi)
log.Debug().
Int64("bytes", bytesWritten).
Str("from", fromPath).
Str("to", toPath).
Msg("File copied...")
return err
}
func listDBTables(names *[]string, gSQL *goqu.TxDatabase) error {
err := gSQL.Select("name").From("sqlite_schema").Where(
goqu.C("type").Eq("table"),
goqu.C("name").NotLike("sqlite_%"),
goqu.C("name").NotLike(MarmotPrefix+"%"),
).ScanVals(names)
if err != nil {
return err
}
return nil
}
func performCheckpoint(gSQL *goqu.Database) error {
rBusy, rLog, rCheckpoint := int64(1), int64(0), int64(0)
log.Debug().Msg("Forcing WAL checkpoint")
for rBusy != 0 {
row := gSQL.QueryRow("PRAGMA wal_checkpoint(truncate);")
err := row.Scan(&rBusy, &rLog, &rCheckpoint)
if err != nil {
return err
}
if rBusy != 0 {
log.Debug().
Int64("busy", rBusy).
Int64("log", rLog).
Int64("checkpoint", rCheckpoint).
Msg("Waiting checkpoint...")
time.Sleep(100 * time.Millisecond)
}
}
return nil
}