/
consumer.go
339 lines (293 loc) · 9.41 KB
/
consumer.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
package consumer
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"os"
"strings"
"sync"
"time"
comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/atproto/data"
"github.com/goccy/go-json"
"github.com/bluesky-social/indigo/events"
"github.com/bluesky-social/indigo/repo"
"github.com/bluesky-social/indigo/repomgr"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
// Consumer is the consumer of the firehose
type Consumer struct {
SocketURL string
Progress *Progress
Emit func(context.Context, Event) error
}
// Progress is the cursor for the consumer
type Progress struct {
LastSeq int64 `json:"last_seq"`
LastSeqProcessedAt time.Time `json:"last_seq_processed_at"`
lk sync.RWMutex
path string
}
func (p *Progress) Update(seq int64, processedAt time.Time) {
p.lk.Lock()
defer p.lk.Unlock()
p.LastSeq = seq
p.LastSeqProcessedAt = processedAt
}
func (p *Progress) Get() (int64, time.Time) {
p.lk.RLock()
defer p.lk.RUnlock()
return p.LastSeq, p.LastSeqProcessedAt
}
var tracer = otel.Tracer("consumer")
// WriteCursor writes the cursor to file
func (c *Consumer) WriteCursor(ctx context.Context) error {
ctx, span := tracer.Start(ctx, "WriteCursor")
defer span.End()
// Marshal the cursor JSON
seq, processedAt := c.Progress.Get()
p := Progress{
LastSeq: seq,
LastSeqProcessedAt: processedAt,
}
data, err := json.Marshal(&p)
if err != nil {
return fmt.Errorf("failed to marshal cursor JSON: %+v", err)
}
// Write the cursor JSON to disk
err = os.WriteFile(c.Progress.path, data, 0644)
if err != nil {
return fmt.Errorf("failed to write cursor to file: %+v", err)
}
return nil
}
// ReadCursor reads the cursor from file
func (c *Consumer) ReadCursor(ctx context.Context) error {
ctx, span := tracer.Start(ctx, "ReadCursor")
defer span.End()
// Read the cursor from disk
data, err := os.ReadFile(c.Progress.path)
if err != nil {
return fmt.Errorf("failed to read cursor from file: %w", err)
}
// Unmarshal the cursor JSON
err = json.Unmarshal(data, c.Progress)
if err != nil {
return fmt.Errorf("failed to unmarshal cursor JSON: %w", err)
}
return nil
}
// NewConsumer creates a new consumer
func NewConsumer(
ctx context.Context,
socketURL string,
progPath string,
emit func(context.Context, Event) error,
) (*Consumer, error) {
c := Consumer{
SocketURL: socketURL,
Progress: &Progress{
LastSeq: -1,
path: progPath,
},
Emit: emit,
}
// Check to see if the cursor exists
err := c.ReadCursor(context.Background())
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("failed to read cursor from file: %+v", err)
}
slog.Warn("cursor not found on disk, starting from live")
}
return &c, nil
}
// HandleStreamEvent handles a stream event from the firehose
func (c *Consumer) HandleStreamEvent(ctx context.Context, xe *events.XRPCStreamEvent) error {
ctx, span := tracer.Start(ctx, "HandleStreamEvent")
defer span.End()
switch {
case xe.RepoCommit != nil:
eventsProcessedCounter.WithLabelValues("repo_commit", c.SocketURL).Inc()
if xe.RepoCommit.TooBig {
slog.Warn("repo commit too big", "repo", xe.RepoCommit.Repo, "seq", xe.RepoCommit.Seq, "rev", xe.RepoCommit.Rev)
return nil
}
return c.HandleRepoCommit(ctx, xe.RepoCommit)
case xe.RepoHandle != nil:
eventsProcessedCounter.WithLabelValues("repo_handle", c.SocketURL).Inc()
now := time.Now()
c.Progress.Update(xe.RepoHandle.Seq, now)
// Parse time from the event time string
t, err := time.Parse(time.RFC3339, xe.RepoHandle.Time)
if err != nil {
slog.Error("error parsing time", "error", err)
return nil
}
// Emit handle update
e := Event{
Did: xe.RepoHandle.Did,
Seq: xe.RepoHandle.Seq,
OpType: EvtUpdateRecord,
}
err = c.Emit(ctx, e)
if err != nil {
slog.Error("failed to emit json", "error", err)
}
lastEvtCreatedAtGauge.WithLabelValues(c.SocketURL).Set(float64(t.UnixNano()))
lastEvtProcessedAtGauge.WithLabelValues(c.SocketURL).Set(float64(now.UnixNano()))
lastEvtCreatedEvtProcessedGapGauge.WithLabelValues(c.SocketURL).Set(float64(now.Sub(t).Seconds()))
lastSeqGauge.WithLabelValues(c.SocketURL).Set(float64(xe.RepoHandle.Seq))
case xe.RepoInfo != nil:
eventsProcessedCounter.WithLabelValues("repo_info", c.SocketURL).Inc()
case xe.RepoMigrate != nil:
eventsProcessedCounter.WithLabelValues("repo_migrate", c.SocketURL).Inc()
now := time.Now()
c.Progress.Update(xe.RepoHandle.Seq, now)
// Parse time from the event time string
t, err := time.Parse(time.RFC3339, xe.RepoMigrate.Time)
if err != nil {
slog.Error("error parsing time", "error", err)
return nil
}
lastEvtCreatedAtGauge.WithLabelValues(c.SocketURL).Set(float64(t.UnixNano()))
lastEvtProcessedAtGauge.WithLabelValues(c.SocketURL).Set(float64(now.UnixNano()))
lastEvtCreatedEvtProcessedGapGauge.WithLabelValues(c.SocketURL).Set(float64(now.Sub(t).Seconds()))
lastSeqGauge.WithLabelValues(c.SocketURL).Set(float64(xe.RepoHandle.Seq))
case xe.RepoTombstone != nil:
eventsProcessedCounter.WithLabelValues("repo_tombstone", c.SocketURL).Inc()
case xe.LabelInfo != nil:
eventsProcessedCounter.WithLabelValues("label_info", c.SocketURL).Inc()
case xe.LabelLabels != nil:
eventsProcessedCounter.WithLabelValues("label_labels", c.SocketURL).Inc()
case xe.Error != nil:
eventsProcessedCounter.WithLabelValues("error", c.SocketURL).Inc()
}
return nil
}
// HandleRepoCommit handles a repo commit event from the firehose and processes the records
func (c *Consumer) HandleRepoCommit(ctx context.Context, evt *comatproto.SyncSubscribeRepos_Commit) error {
ctx, span := tracer.Start(ctx, "HandleRepoCommit")
defer span.End()
processedAt := time.Now()
c.Progress.Update(evt.Seq, processedAt)
lastSeqGauge.WithLabelValues(c.SocketURL).Set(float64(evt.Seq))
log := slog.With("repo", evt.Repo, "seq", evt.Seq, "commit", evt.Commit.String())
span.AddEvent("Read Repo From Car")
rr, err := repo.ReadRepoFromCar(ctx, bytes.NewReader(evt.Blocks))
if err != nil {
log.Error("failed to read repo from car", "error", err)
return nil
}
if evt.Rebase {
log.Debug("rebase")
rebasesProcessedCounter.WithLabelValues(c.SocketURL).Inc()
}
// Parse time from the event time string
evtCreatedAt, err := time.Parse(time.RFC3339, evt.Time)
if err != nil {
log.Error("error parsing time", "error", err)
return nil
}
lastEvtCreatedAtGauge.WithLabelValues(c.SocketURL).Set(float64(evtCreatedAt.UnixNano()))
lastEvtProcessedAtGauge.WithLabelValues(c.SocketURL).Set(float64(processedAt.UnixNano()))
lastEvtCreatedEvtProcessedGapGauge.WithLabelValues(c.SocketURL).Set(float64(processedAt.Sub(evtCreatedAt).Seconds()))
for _, op := range evt.Ops {
collection := strings.Split(op.Path, "/")[0]
rkey := strings.Split(op.Path, "/")[1]
ek := repomgr.EventKind(op.Action)
log = log.With("action", op.Action, "collection", collection)
opsProcessedCounter.WithLabelValues(op.Action, collection, c.SocketURL).Inc()
// recordURI := "at://" + evt.Repo + "/" + op.Path
span.SetAttributes(attribute.String("repo", evt.Repo))
span.SetAttributes(attribute.String("collection", collection))
span.SetAttributes(attribute.String("rkey", rkey))
span.SetAttributes(attribute.Int64("seq", evt.Seq))
span.SetAttributes(attribute.String("event_kind", op.Action))
switch ek {
case repomgr.EvtKindCreateRecord:
if op.Cid == nil {
log.Error("update record op missing cid")
break
}
rcid, recB, err := rr.GetRecordBytes(ctx, op.Path)
if err != nil {
log.Error("failed to get record bytes", "error", err)
break
}
if rcid.String() != op.Cid.String() {
log.Error("record cid mismatch", "expected", *op.Cid, "actual", rcid)
break
}
rec, err := data.UnmarshalCBOR(*recB)
if err != nil {
return fmt.Errorf("failed to unmarshal record: %w", err)
}
e := Event{
Did: evt.Repo,
Seq: evt.Seq,
OpType: EvtCreateRecord,
Collection: collection,
RKey: rkey,
Record: rec,
}
err = c.Emit(ctx, e)
if err != nil {
log.Error("failed to emit json", "error", err)
break
}
case repomgr.EvtKindUpdateRecord:
if op.Cid == nil {
log.Error("update record op missing cid")
break
}
rcid, recB, err := rr.GetRecordBytes(ctx, op.Path)
if err != nil {
log.Error("failed to get record bytes", "error", err)
break
}
if rcid.String() != op.Cid.String() {
log.Error("record cid mismatch", "expected", *op.Cid, "actual", rcid)
break
}
rec, err := data.UnmarshalCBOR(*recB)
if err != nil {
return fmt.Errorf("failed to unmarshal record: %w", err)
}
e := Event{
Did: evt.Repo,
Seq: evt.Seq,
OpType: EvtUpdateRecord,
Collection: collection,
RKey: rkey,
Record: rec,
}
err = c.Emit(ctx, e)
if err != nil {
log.Error("failed to emit json", "error", err)
break
}
case repomgr.EvtKindDeleteRecord:
// Emit the delete
e := Event{
Did: evt.Repo,
Seq: evt.Seq,
OpType: EvtDeleteRecord,
Collection: collection,
RKey: rkey,
}
err = c.Emit(ctx, e)
if err != nil {
log.Error("failed to emit json", "error", err)
break
}
default:
log.Warn("unknown event kind from op action", "kind", op.Action)
}
}
eventProcessingDurationHistogram.WithLabelValues(c.SocketURL).Observe(time.Since(processedAt).Seconds())
return nil
}