/
fetcher.go
326 lines (285 loc) · 8.76 KB
/
fetcher.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
package itemsfetcher
import (
"errors"
"math/rand"
"sync"
"time"
"github.com/Ncog-Earth-Chain/forest-base/utils/wlru"
"github.com/Ncog-Earth-Chain/forest-base/utils/workers"
)
/*
* Fetcher is a network agent, which handles basic hash-based items sync.
* The core mechanic is very simple: interested hash arrived => request it.
* Fetcher has additional code to protect itself (and other nodes) against DoS.
*/
var (
errTerminated = errors.New("terminated")
)
// ItemsRequesterFn is a callback type for sending a item retrieval request.
type ItemsRequesterFn func([]interface{}) error
type announceData struct {
time time.Time // Timestamp of the announcement
peer string // Identifier of the peer originating the notification
fetchItems ItemsRequesterFn
}
type announcesBatch struct {
announceData
ids []interface{} // Hashes of the items being announced
}
type fetchingItem struct {
announce announceData
fetchingTime time.Time
}
// Fetcher is responsible for accumulating item announcements from various peers
// and scheduling them for retrieval.
type Fetcher struct {
cfg Config
// Various item channels
notifications chan announcesBatch
receivedItems chan []interface{}
quit chan struct{}
// Callbacks
callback Callback
// Announce states
announces *wlru.Cache // Announced items, scheduled for fetching
fetching map[interface{}]fetchingItem // Announced items, currently fetching
wg sync.WaitGroup
parallelTasks *workers.Workers
}
type Callback struct {
// FilterInterested returns only item which may be requested.
OnlyInterested func(ids []interface{}) []interface{}
Suspend func() bool
}
// New creates a item fetcher to retrieve items based on hash announcements.
func New(cfg Config, callback Callback) *Fetcher {
f := &Fetcher{
cfg: cfg,
notifications: make(chan announcesBatch, cfg.MaxQueuedBatches),
receivedItems: make(chan []interface{}, cfg.MaxQueuedBatches),
quit: make(chan struct{}),
fetching: make(map[interface{}]fetchingItem),
callback: callback,
}
f.announces, _ = wlru.NewWithEvict(uint(cfg.HashLimit), cfg.HashLimit, func(key interface{}, _ interface{}) {
delete(f.fetching, key.(interface{}))
})
f.parallelTasks = workers.New(&f.wg, f.quit, f.cfg.MaxParallelRequests*2)
return f
}
// Start boots up the items fetcher.
func (f *Fetcher) Start() {
f.parallelTasks.Start(f.cfg.MaxParallelRequests)
f.wg.Add(1)
go func() {
defer f.wg.Done()
f.loop()
}()
}
// Stop interrupts the fetcher, canceling all the pending operations.
// Stop waits until all the internal goroutines have finished.
func (f *Fetcher) Stop() {
close(f.quit)
f.parallelTasks.Drain()
f.wg.Wait()
}
// Overloaded returns true if too much items are being requested
func (f *Fetcher) Overloaded() bool {
return len(f.receivedItems) > f.cfg.MaxQueuedBatches*3/4 ||
len(f.notifications) > f.cfg.MaxQueuedBatches*3/4 ||
f.announces.Len() > f.cfg.HashLimit/2
}
// NotifyAnnounces announces the fetcher of the potential availability of a new item in
// the network.
func (f *Fetcher) NotifyAnnounces(peer string, ids []interface{}, time time.Time, fetchItems ItemsRequesterFn) error {
// divide big batch into smaller ones
for start := 0; start < len(ids); start += f.cfg.MaxBatch {
end := len(ids)
if end > start+f.cfg.MaxBatch {
end = start + f.cfg.MaxBatch
}
op := announcesBatch{
announceData: announceData{
time: time,
peer: peer,
fetchItems: fetchItems,
},
ids: ids[start:end],
}
select {
case <-f.quit:
return errTerminated
case f.notifications <- op:
continue
}
}
return nil
}
func (f *Fetcher) NotifyReceived(ids []interface{}) error {
// divide big batch into smaller ones
for start := 0; start < len(ids); start += f.cfg.MaxBatch {
end := len(ids)
if end > start+f.cfg.MaxBatch {
end = start + f.cfg.MaxBatch
}
select {
case <-f.quit:
return errTerminated
case f.receivedItems <- ids[start:end]:
continue
}
}
return nil
}
func (f *Fetcher) getAnnounces(id interface{}) []announceData {
announces, ok := f.announces.Get(id)
if !ok {
return []announceData{}
}
return announces.([]announceData)
}
func (f *Fetcher) processNotification(notification announcesBatch, fetchTimer *time.Timer) {
first := len(f.fetching) == 0
// filter only not known
notification.ids = f.callback.OnlyInterested(notification.ids)
if len(notification.ids) == 0 {
return
}
noFetching := f.callback.Suspend()
toFetch := make([]interface{}, 0, len(notification.ids))
now := time.Now()
for _, id := range notification.ids {
// add new announcement. other peers may already have announced it, so it's an array
anns := f.getAnnounces(id)
anns = append(anns, notification.announceData)
f.announces.Add(id, append(anns, notification.announceData), uint(len(anns)))
// if it wasn't announced before, then schedule for fetching this time
if !noFetching {
if _, ok := f.fetching[id]; !ok {
f.fetching[id] = fetchingItem{
announce: notification.announceData,
fetchingTime: now,
}
toFetch = append(toFetch, id)
}
}
}
if len(toFetch) != 0 {
// Create a closure of the fetch and schedule in on a new thread
fetchItems, hashes := notification.fetchItems, toFetch
_ = f.parallelTasks.Enqueue(func() {
_ = fetchItems(hashes)
})
}
if first && len(f.fetching) != 0 {
f.rescheduleFetch(fetchTimer)
}
}
// Loop is the main fetcher loop, checking and processing various notifications
func (f *Fetcher) loop() {
// Iterate the item fetching until a quit is requested
fetchTimer := time.NewTimer(0)
defer fetchTimer.Stop()
for {
// Wait for an outside item to occur
select {
case <-f.quit:
// Fetcher terminating, abort all operations
return
case notification := <-f.notifications:
f.processNotification(notification, fetchTimer)
case ids := <-f.receivedItems:
for _, id := range ids {
f.forgetHash(id)
}
case <-fetchTimer.C:
now := time.Now()
// At least one item's timer ran out, check for needing retrieval
request := make(map[string][]interface{})
requestFns := make(map[string]ItemsRequesterFn)
// Find not arrived items
all := make([]interface{}, 0, f.announces.Len())
for _, id := range f.announces.Keys() {
all = append(all, id)
}
notArrived := f.callback.OnlyInterested(all)
for _, id := range notArrived {
// Re-fetch not arrived items
announces := f.getAnnounces(id)
if len(announces) == 0 {
continue
}
oldest := announces[0] // first is the oldest
if time.Since(oldest.time) > f.cfg.ForgetTimeout {
// Forget too old announces
f.forgetHash(id)
} else if time.Since(f.fetching[id].fetchingTime) > f.cfg.ArriveTimeout-f.cfg.GatherSlack {
// The item still didn't arrive, queue for fetching from a random peer
announce := announces[rand.Intn(len(announces))]
request[announce.peer] = append(request[announce.peer], id)
requestFns[announce.peer] = announce.fetchItems
f.fetching[id] = fetchingItem{
announce: announce,
fetchingTime: now,
}
}
}
// Forget arrived items.
// It's possible to get here only if item arrived out-of-fetcher, via another channel.
// Also may be possible after a change of an epoch.
notArrivedMap := make(map[interface{}]bool, len(notArrived))
for _, id := range notArrived {
notArrivedMap[id] = true
}
for _, id := range all {
if !notArrivedMap[id] {
f.forgetHash(id)
}
}
// Send out all item requests
for peer, req := range request {
// Create a closure of the fetch and schedule in on a new thread
fetchItems, hashes := requestFns[peer], req
_ = f.parallelTasks.Enqueue(func() {
_ = fetchItems(hashes)
})
}
// Schedule the next fetch if items are still pending
f.rescheduleFetch(fetchTimer)
}
}
}
func maxDuration(a, b time.Duration) time.Duration {
if a > b {
return a
}
return b
}
// rescheduleFetch resets the specified fetch timer to the next announce timeout.
func (f *Fetcher) rescheduleFetch(fetch *time.Timer) {
// Short circuit if no items are announced
if f.announces.Len() == 0 {
return
}
// Otherwise find the earliest expiring announcement
earliest := time.Now()
i := 0
maxChecks := f.cfg.HashLimit / 32
for _, fetch := range f.fetching {
if earliest.After(fetch.fetchingTime) {
earliest = fetch.fetchingTime
}
if i >= maxChecks {
// no need to scan all the entries
break
}
i++
}
// limit minimum duration to prevent spinning too often
fetch.Reset(maxDuration(f.cfg.ArriveTimeout-time.Since(earliest), f.cfg.ArriveTimeout/8))
}
// forgetHash removes all traces of a item announcement from the fetcher's
// internal state.
func (f *Fetcher) forgetHash(id interface{}) {
f.announces.Remove(id) // f.fetching is deleted inside the evict callback
}