-
Notifications
You must be signed in to change notification settings - Fork 9
/
gormstore.go
223 lines (183 loc) · 4.12 KB
/
gormstore.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
package backfill
import (
"context"
"fmt"
"sync"
"time"
"github.com/ipfs/go-cid"
typegen "github.com/whyrusleeping/cbor-gen"
"gorm.io/gorm"
)
type Gormjob struct {
repo string
state string
lk sync.Mutex
bufferedOps map[string][]*bufferedOp
dbj *GormDBJob
db *gorm.DB
createdAt time.Time
updatedAt time.Time
}
type GormDBJob struct {
gorm.Model
Repo string `gorm:"unique;index"`
State string `gorm:"index"`
}
// Gormstore is a gorm-backed implementation of the Backfill Store interface
type Gormstore struct {
lk sync.RWMutex
jobs map[string]*Gormjob
db *gorm.DB
}
func NewGormstore(db *gorm.DB) *Gormstore {
return &Gormstore{
jobs: make(map[string]*Gormjob),
db: db,
}
}
func (s *Gormstore) LoadJobs(ctx context.Context) error {
limit := 20_000
offset := 0
s.lk.Lock()
defer s.lk.Unlock()
for {
var dbjobs []*GormDBJob
// Load all jobs from the database
if err := s.db.Limit(limit).Offset(offset).Find(&dbjobs).Error; err != nil {
return err
}
if len(dbjobs) == 0 {
break
}
offset += len(dbjobs)
// Convert them to in-memory jobs
for i := range dbjobs {
dbj := dbjobs[i]
j := &Gormjob{
repo: dbj.Repo,
state: dbj.State,
bufferedOps: map[string][]*bufferedOp{},
createdAt: dbj.CreatedAt,
updatedAt: dbj.UpdatedAt,
dbj: dbj,
db: s.db,
}
s.jobs[dbj.Repo] = j
}
}
return nil
}
func (s *Gormstore) EnqueueJob(repo string) error {
// Persist the job to the database
dbj := &GormDBJob{
Repo: repo,
State: StateEnqueued,
}
if err := s.db.Create(dbj).Error; err != nil {
if err == gorm.ErrDuplicatedKey {
return nil
}
return err
}
s.lk.Lock()
defer s.lk.Unlock()
// Convert it to an in-memory job
if _, ok := s.jobs[repo]; ok {
// The DB create should have errored if the job already existed, but just in case
return fmt.Errorf("job already exists for repo %s", repo)
}
j := &Gormjob{
repo: repo,
createdAt: time.Now(),
updatedAt: time.Now(),
state: StateEnqueued,
bufferedOps: map[string][]*bufferedOp{},
dbj: dbj,
db: s.db,
}
s.jobs[repo] = j
return nil
}
func (s *Gormstore) BufferOp(ctx context.Context, repo, kind, path string, rec *typegen.CBORMarshaler, cid *cid.Cid) (bool, error) {
s.lk.RLock()
// If the job doesn't exist, we can't buffer an op for it
j, ok := s.jobs[repo]
s.lk.RUnlock()
if !ok {
return false, ErrJobNotFound
}
j.lk.Lock()
defer j.lk.Unlock()
switch j.state {
case StateComplete:
return false, ErrJobComplete
case StateInProgress:
// keep going and buffer the op
default:
return false, nil
}
j.bufferedOps[path] = append(j.bufferedOps[path], &bufferedOp{
kind: kind,
rec: rec,
cid: cid,
})
j.updatedAt = time.Now()
return true, nil
}
func (s *Gormstore) GetJob(ctx context.Context, repo string) (Job, error) {
s.lk.RLock()
defer s.lk.RUnlock()
j, ok := s.jobs[repo]
if !ok || j == nil {
return nil, nil
}
return j, nil
}
func (s *Gormstore) GetNextEnqueuedJob(ctx context.Context) (Job, error) {
s.lk.RLock()
defer s.lk.RUnlock()
for _, j := range s.jobs {
if j.State() == StateEnqueued {
return j, nil
}
}
return nil, nil
}
func (j *Gormjob) Repo() string {
return j.repo
}
func (j *Gormjob) State() string {
j.lk.Lock()
defer j.lk.Unlock()
return j.state
}
func (j *Gormjob) SetState(ctx context.Context, state string) error {
j.lk.Lock()
defer j.lk.Unlock()
j.state = state
j.updatedAt = time.Now()
// Persist the job to the database
j.dbj.State = state
return j.db.Save(j.dbj).Error
}
func (j *Gormjob) FlushBufferedOps(ctx context.Context, fn func(kind, path string, rec *typegen.CBORMarshaler, cid *cid.Cid) error) error {
j.lk.Lock()
defer j.lk.Unlock()
for path, ops := range j.bufferedOps {
for _, op := range ops {
if err := fn(op.kind, path, op.rec, op.cid); err != nil {
return err
}
}
}
j.bufferedOps = map[string][]*bufferedOp{}
j.state = StateComplete
return nil
}
func (j *Gormjob) ClearBufferedOps(ctx context.Context) error {
j.lk.Lock()
defer j.lk.Unlock()
j.bufferedOps = map[string][]*bufferedOp{}
j.updatedAt = time.Now()
return nil
}