-
Notifications
You must be signed in to change notification settings - Fork 89
/
crawler.go
271 lines (225 loc) · 6.44 KB
/
crawler.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
package indexer
import (
"context"
"fmt"
"sync"
comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/models"
"go.opentelemetry.io/otel"
)
type CrawlDispatcher struct {
ingest chan *models.ActorInfo
repoSync chan *crawlWork
catchup chan *crawlWork
complete chan models.Uid
maplk sync.Mutex
todo map[models.Uid]*crawlWork
inProgress map[models.Uid]*crawlWork
doRepoCrawl func(context.Context, *crawlWork) error
concurrency int
}
func NewCrawlDispatcher(repoFn func(context.Context, *crawlWork) error, concurrency int) (*CrawlDispatcher, error) {
if concurrency < 1 {
return nil, fmt.Errorf("must specify a non-zero positive integer for crawl dispatcher concurrency")
}
return &CrawlDispatcher{
ingest: make(chan *models.ActorInfo),
repoSync: make(chan *crawlWork),
complete: make(chan models.Uid),
catchup: make(chan *crawlWork),
doRepoCrawl: repoFn,
concurrency: concurrency,
todo: make(map[models.Uid]*crawlWork),
inProgress: make(map[models.Uid]*crawlWork),
}, nil
}
func (c *CrawlDispatcher) Run() {
go c.mainLoop()
for i := 0; i < c.concurrency; i++ {
go c.fetchWorker()
}
}
type catchupJob struct {
evt *comatproto.SyncSubscribeRepos_Commit
host *models.PDS
user *models.ActorInfo
}
type crawlWork struct {
act *models.ActorInfo
initScrape bool
// for events that come in while this actor's crawl is enqueued
// catchup items are processed during the crawl
catchup []*catchupJob
// for events that come in while this actor is being processed
// next items are processed after the crawl
next []*catchupJob
}
func (c *CrawlDispatcher) mainLoop() {
var nextDispatchedJob *crawlWork
var jobsAwaitingDispatch []*crawlWork
// dispatchQueue represents the repoSync worker channel to which we dispatch crawl work
var dispatchQueue chan *crawlWork
for {
select {
case actorToCrawl := <-c.ingest:
// TODO: max buffer size
crawlJob := c.enqueueJobForActor(actorToCrawl)
if crawlJob == nil {
break
}
if nextDispatchedJob == nil {
nextDispatchedJob = crawlJob
dispatchQueue = c.repoSync
} else {
jobsAwaitingDispatch = append(jobsAwaitingDispatch, crawlJob)
}
case dispatchQueue <- nextDispatchedJob:
c.dequeueJob(nextDispatchedJob)
if len(jobsAwaitingDispatch) > 0 {
nextDispatchedJob = jobsAwaitingDispatch[0]
jobsAwaitingDispatch = jobsAwaitingDispatch[1:]
} else {
nextDispatchedJob = nil
dispatchQueue = nil
}
case catchupJob := <-c.catchup:
// CatchupJobs are for processing events that come in while a crawl is in progress
// They are lower priority than new crawls so we only add them to the queue if there isn't already a job in progress
if nextDispatchedJob == nil {
nextDispatchedJob = catchupJob
dispatchQueue = c.repoSync
} else {
jobsAwaitingDispatch = append(jobsAwaitingDispatch, catchupJob)
}
case uid := <-c.complete:
c.maplk.Lock()
job, ok := c.inProgress[uid]
if !ok {
panic("should not be possible to not have a job in progress we receive a completion signal for")
}
delete(c.inProgress, uid)
// If there are any subsequent jobs for this UID, add it back to the todo list or buffer.
// We're basically pumping the `next` queue into the `catchup` queue and will do this over and over until the `next` queue is empty.
if len(job.next) > 0 {
c.todo[uid] = job
job.initScrape = false
job.catchup = job.next
job.next = nil
if nextDispatchedJob == nil {
nextDispatchedJob = job
dispatchQueue = c.repoSync
} else {
jobsAwaitingDispatch = append(jobsAwaitingDispatch, job)
}
}
c.maplk.Unlock()
}
}
}
// enqueueJobForActor adds a new crawl job to the todo list if there isn't already a job in progress for this actor
func (c *CrawlDispatcher) enqueueJobForActor(ai *models.ActorInfo) *crawlWork {
c.maplk.Lock()
defer c.maplk.Unlock()
_, ok := c.inProgress[ai.Uid]
if ok {
return nil
}
_, has := c.todo[ai.Uid]
if has {
return nil
}
crawlJob := &crawlWork{
act: ai,
initScrape: true,
}
c.todo[ai.Uid] = crawlJob
return crawlJob
}
// dequeueJob removes a job from the todo list and adds it to the inProgress list
func (c *CrawlDispatcher) dequeueJob(job *crawlWork) {
c.maplk.Lock()
defer c.maplk.Unlock()
delete(c.todo, job.act.Uid)
c.inProgress[job.act.Uid] = job
}
func (c *CrawlDispatcher) addToCatchupQueue(catchup *catchupJob) *crawlWork {
catchupEventsEnqueued.Inc()
c.maplk.Lock()
defer c.maplk.Unlock()
// If the actor crawl is enqueued, we can append to the catchup queue which gets emptied during the crawl
job, ok := c.todo[catchup.user.Uid]
if ok {
job.catchup = append(job.catchup, catchup)
return nil
}
// If the actor crawl is in progress, we can append to the nextr queue which gets emptied after the crawl
job, ok = c.inProgress[catchup.user.Uid]
if ok {
job.next = append(job.next, catchup)
return nil
}
// Otherwise, we need to create a new crawl job for this actor and enqueue it
cw := &crawlWork{
act: catchup.user,
catchup: []*catchupJob{catchup},
}
c.todo[catchup.user.Uid] = cw
return cw
}
func (c *CrawlDispatcher) fetchWorker() {
for {
select {
case job := <-c.repoSync:
if err := c.doRepoCrawl(context.TODO(), job); err != nil {
log.Errorf("failed to perform repo crawl of %q: %s", job.act.Did, err)
}
// TODO: do we still just do this if it errors?
c.complete <- job.act.Uid
}
}
}
func (c *CrawlDispatcher) Crawl(ctx context.Context, ai *models.ActorInfo) error {
if ai.PDS == 0 {
panic("must have pds for user in queue")
}
userCrawlsEnqueued.Inc()
ctx, span := otel.Tracer("crawler").Start(ctx, "addToCrawler")
defer span.End()
select {
case c.ingest <- ai:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (c *CrawlDispatcher) AddToCatchupQueue(ctx context.Context, host *models.PDS, u *models.ActorInfo, evt *comatproto.SyncSubscribeRepos_Commit) error {
if u.PDS == 0 {
panic("must have pds for user in queue")
}
catchup := &catchupJob{
evt: evt,
host: host,
user: u,
}
cw := c.addToCatchupQueue(catchup)
if cw == nil {
return nil
}
select {
case c.catchup <- cw:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (c *CrawlDispatcher) RepoInSlowPath(ctx context.Context, uid models.Uid) bool {
c.maplk.Lock()
defer c.maplk.Unlock()
if _, ok := c.todo[uid]; ok {
return true
}
if _, ok := c.inProgress[uid]; ok {
return true
}
return false
}