-
Notifications
You must be signed in to change notification settings - Fork 245
/
gc.go
113 lines (96 loc) · 3.37 KB
/
gc.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
package mysql
import (
"context"
"database/sql"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/shopspring/decimal"
"github.com/authzed/spicedb/internal/datastore/common"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/revision"
)
var _ common.GarbageCollector = (*Datastore)(nil)
// TODO (@vroldanbet) dupe from postgres datastore - need to refactor
func (mds *Datastore) Now(ctx context.Context) (time.Time, error) {
// Retrieve the `now` time from the database.
nowSQL, nowArgs, err := getNow.ToSql()
if err != nil {
return time.Time{}, err
}
var now time.Time
err = mds.db.QueryRowContext(ctx, nowSQL, nowArgs...).Scan(&now)
if err != nil {
return time.Time{}, err
}
// This conversion should just be for convenience while debugging --
// MySQL and the driver do properly timezones properly.
return now.UTC(), nil
}
// TODO (@vroldanbet) dupe from postgres datastore - need to refactor
// - main difference is how the PSQL driver handles null values
func (mds *Datastore) TxIDBefore(ctx context.Context, before time.Time) (datastore.Revision, error) {
// Find the highest transaction ID before the GC window.
query, args, err := mds.GetLastRevision.Where(sq.Lt{colTimestamp: before}).ToSql()
if err != nil {
return datastore.NoRevision, err
}
var value sql.NullInt64
err = mds.db.QueryRowContext(ctx, query, args...).Scan(&value)
if err != nil {
return datastore.NoRevision, err
}
if !value.Valid {
log.Ctx(ctx).Debug().Time("before", before).Msg("no stale transactions found in the datastore")
return datastore.NoRevision, nil
}
return revision.NewFromDecimal(decimal.NewFromInt(value.Int64)), nil
}
// TODO (@vroldanbet) dupe from postgres datastore - need to refactor
// - implementation misses metrics
func (mds *Datastore) DeleteBeforeTx(
ctx context.Context,
txID datastore.Revision,
) (removed common.DeletionCounts, err error) {
// Delete any relationship rows with deleted_transaction <= the transaction ID.
removed.Relationships, err = mds.batchDelete(ctx, mds.driver.RelationTuple(), sq.LtOrEq{colDeletedTxn: txID})
if err != nil {
return
}
// Delete all transaction rows with ID < the transaction ID.
//
// We don't delete the transaction itself to ensure there is always at least
// one transaction present.
removed.Transactions, err = mds.batchDelete(ctx, mds.driver.RelationTupleTransaction(), sq.Lt{colID: txID})
if err != nil {
return
}
// Delete any namespace rows with deleted_transaction <= the transaction ID.
removed.Namespaces, err = mds.batchDelete(ctx, mds.driver.Namespace(), sq.LtOrEq{colDeletedTxn: txID})
return
}
// TODO (@vroldanbet) dupe from postgres datastore - need to refactor
// - query was reworked to make it compatible with Vitess
// - API differences with PSQL driver
func (mds *Datastore) batchDelete(ctx context.Context, tableName string, filter sqlFilter) (int64, error) {
query, args, err := sb.Delete(tableName).Where(filter).Limit(batchDeleteSize).ToSql()
if err != nil {
return -1, err
}
var deletedCount int64
for {
cr, err := mds.db.ExecContext(ctx, query, args...)
if err != nil {
return deletedCount, err
}
rowsDeleted, err := cr.RowsAffected()
if err != nil {
return deletedCount, err
}
deletedCount += rowsDeleted
if rowsDeleted < batchDeleteSize {
break
}
}
return deletedCount, nil
}