-
Notifications
You must be signed in to change notification settings - Fork 9
/
backfill.go
323 lines (268 loc) · 7.73 KB
/
backfill.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
package consumer
import (
"context"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/bluesky-social/indigo/repo"
"github.com/ericvolp12/bsky-experiments/pkg/consumer/store/store_queries"
"github.com/ipfs/go-cid"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
type BackfillRepoStatus struct {
RepoDid string
Seq int64
State string
DeleteBuffer []*Delete
lk sync.Mutex
}
func (b *BackfillRepoStatus) SetState(state string) {
b.lk.Lock()
b.State = state
b.lk.Unlock()
}
func (b *BackfillRepoStatus) AddDelete(repo, path string) {
b.lk.Lock()
b.DeleteBuffer = append(b.DeleteBuffer, &Delete{repo: repo, path: path})
b.lk.Unlock()
}
func (c *Consumer) FlushBackfillBuffer(ctx context.Context, bf *BackfillRepoStatus) int {
ctx, span := tracer.Start(ctx, "FlushBackfillBuffer")
defer span.End()
log := c.Logger.With("source", "backfill_buffer_flush", "repo", bf.RepoDid)
processed := 0
bf.lk.Lock()
for _, del := range bf.DeleteBuffer {
err := c.HandleDeleteRecord(ctx, del.repo, del.path)
if err != nil {
log.Errorf("failed to handle delete record: %+v", err)
}
backfillDeletesBuffered.WithLabelValues(c.SocketURL).Dec()
processed++
}
bf.DeleteBuffer = []*Delete{}
bf.lk.Unlock()
return processed
}
type RecordJob struct {
RecordPath string
NodeCid cid.Cid
}
type RecordResult struct {
RecordPath string
Error error
}
func (c *Consumer) BackfillProcessor(ctx context.Context) {
ctx, span := tracer.Start(ctx, "BackfillProcessor")
defer span.End()
log := c.Logger.With("source", "backfill_main")
log.Info("starting backfill processor")
// Create a semaphore with a capacity of 50
sem := make(chan struct{}, 50)
for {
select {
case <-ctx.Done():
log.Info("stopping backfill processor")
return
default:
}
// Get the next backfill
var backfill *BackfillRepoStatus
c.BackfillStatus.Range(func(repo string, b *BackfillRepoStatus) bool {
b.lk.Lock()
if b.State == "enqueued" {
backfill = b
b.State = "in_progress"
b.lk.Unlock()
return false
}
b.lk.Unlock()
return true
})
if backfill == nil {
time.Sleep(1 * time.Second)
continue
}
sem <- struct{}{} // Block until there is a slot in the semaphore
go func(b *BackfillRepoStatus) {
// Process the backfill
c.ProcessBackfill(ctx, b.RepoDid)
backfillJobsProcessed.WithLabelValues(c.SocketURL).Inc()
<-sem // Release a slot in the semaphore when the goroutine finishes
}(backfill)
}
}
type instrumentedReader struct {
source io.ReadCloser
counter prometheus.Counter
}
func (r instrumentedReader) Read(b []byte) (int, error) {
n, err := r.source.Read(b)
r.counter.Add(float64(n))
return n, err
}
func (r instrumentedReader) Close() error {
var buf [32]byte
var n int
var err error
for err == nil {
n, err = r.source.Read(buf[:])
r.counter.Add(float64(n))
}
closeerr := r.source.Close()
if err != nil && err != io.EOF {
return err
}
return closeerr
}
func (c *Consumer) ProcessBackfill(ctx context.Context, repoDID string) {
ctx, span := tracer.Start(ctx, "ProcessBackfill")
defer span.End()
start := time.Now()
log := c.Logger.With("source", "backfill", "repo", repoDID)
log.Infof("processing backfill for %s", repoDID)
var url = "https://bsky.network/xrpc/com.atproto.sync.getRepo?did=" + repoDID
// GET and CAR decode the body
client := &http.Client{
Transport: otelhttp.NewTransport(http.DefaultTransport),
Timeout: 15 * time.Minute,
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
log.Errorf("Error creating request: %v", err)
return
}
req.Header.Set("Accept", "application/vnd.ipld.car")
req.Header.Set("User-Agent", "jaz-atproto-backfill/0.0.1")
if c.magicHeaderKey != "" && c.magicHeaderVal != "" {
req.Header.Set(c.magicHeaderKey, c.magicHeaderVal)
}
c.SyncLimiter.Wait(ctx)
resp, err := client.Do(req)
if err != nil {
log.Errorf("Error sending request: %v", err)
return
}
bf, _ := c.BackfillStatus.Load(repoDID)
if resp.StatusCode != http.StatusOK {
log.Errorf("Error response: %v", resp.StatusCode)
reason := "unknown error"
if resp.StatusCode == http.StatusBadRequest {
reason = "repo not found"
}
state := fmt.Sprintf("failed (%s)", reason)
// Mark the backfill as "failed"
bf.SetState(state)
err = c.Store.Queries.UpdateRepoBackfillRecord(ctx, store_queries.UpdateRepoBackfillRecordParams{
Repo: repoDID,
LastBackfill: time.Now(),
SeqStarted: bf.Seq,
State: state,
})
if err != nil {
log.Errorf("failed to update repo backfill record: %+v", err)
}
// Process buffered deletes
c.FlushBackfillBuffer(ctx, bf)
return
}
instrumentedReader := instrumentedReader{
source: resp.Body,
counter: backfillBytesProcessed.WithLabelValues(c.SocketURL),
}
defer instrumentedReader.Close()
r, err := repo.ReadRepoFromCar(ctx, instrumentedReader)
if err != nil {
log.Errorf("Error reading repo: %v", err)
// Mark the backfill as "failed"
bf, _ := c.BackfillStatus.Load(repoDID)
state := "failed (couldn't read repo CAR from response body)"
// Mark the backfill as "failed"
bf.SetState(state)
err = c.Store.Queries.UpdateRepoBackfillRecord(ctx, store_queries.UpdateRepoBackfillRecordParams{
Repo: repoDID,
LastBackfill: time.Now(),
SeqStarted: bf.Seq,
State: state,
})
if err != nil {
log.Errorf("failed to update repo backfill record: %+v", err)
}
// Process buffered deletes
c.FlushBackfillBuffer(ctx, bf)
return
}
numRecords := 0
numRoutines := 50
recordJobs := make(chan RecordJob, numRoutines)
recordResults := make(chan RecordResult, numRoutines)
wg := sync.WaitGroup{}
// Producer routine
go func() {
defer close(recordJobs)
r.ForEach(ctx, "", func(recordPath string, nodeCid cid.Cid) error {
numRecords++
recordJobs <- RecordJob{RecordPath: recordPath, NodeCid: nodeCid}
return nil
})
}()
// Consumer routines
for i := 0; i < numRoutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range recordJobs {
recordCid, rec, err := r.GetRecord(ctx, job.RecordPath)
if err != nil {
log.Errorf("Error getting record: %v", err)
recordResults <- RecordResult{RecordPath: job.RecordPath, Error: err}
continue
}
// Verify that the record cid matches the cid in the event
if recordCid != job.NodeCid {
log.Errorf("mismatch in record and op cid: %s != %s", recordCid, job.NodeCid)
recordResults <- RecordResult{RecordPath: job.RecordPath, Error: err}
continue
}
_, err = c.HandleCreateRecord(ctx, repoDID, job.RecordPath, rec)
if err != nil {
log.Errorf("failed to handle create record: %+v", err)
}
backfillRecordsProcessed.WithLabelValues(c.SocketURL).Inc()
recordResults <- RecordResult{RecordPath: job.RecordPath, Error: err}
}
}()
}
resultWG := sync.WaitGroup{}
resultWG.Add(1)
// Handle results
go func() {
defer resultWG.Done()
for result := range recordResults {
if result.Error != nil {
log.Errorf("Error processing record %s: %v", result.RecordPath, result.Error)
}
}
}()
wg.Wait()
close(recordResults)
resultWG.Wait()
state := "complete"
// Mark the backfill as "complete"
bf.SetState(state)
// Process buffered deletes
numProcessed := c.FlushBackfillBuffer(ctx, bf)
err = c.Store.Queries.UpdateRepoBackfillRecord(ctx, store_queries.UpdateRepoBackfillRecordParams{
Repo: repoDID,
LastBackfill: time.Now(),
SeqStarted: bf.Seq,
State: state,
})
if err != nil {
log.Errorf("failed to update repo backfill record: %+v", err)
}
log.Infow("backfill complete", "buffered_deletes_processed", numProcessed, "records_backfilled", numRecords, "duration", time.Since(start))
}