/
announcer.go
314 lines (269 loc) · 7.94 KB
/
announcer.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
package announcer
import (
"context"
"fmt"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
cid2 "github.com/ipfs/go-cid"
kaddht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/make-os/kit/config"
dht3 "github.com/make-os/kit/net/dht"
"github.com/make-os/kit/pkgs/logger"
"github.com/make-os/kit/remote/plumbing"
"github.com/make-os/kit/types/core"
"github.com/make-os/kit/util"
)
const (
ObjTypeAny int = iota
ObjTypeGit
ObjTypeRepoName
)
// ErrDelisted indicates that a task failed to be announced because it was delisted
var ErrDelisted = fmt.Errorf("key delisted")
const (
// KeyReannounceDur is the duration that passes streamer/streamer_test.go:630:between key re-announcement.
KeyReannounceDur = 5 * time.Hour
// ReannouncerInterval is the duration between each reannoucement process.
ReannouncerInterval = 30 * time.Second
)
// MaxRetry is the number of times to try to reannounce a key
var MaxRetry = 3
// Session represents a multi-announcement session.
type Session struct {
a dht3.Announcer
wg *sync.WaitGroup
lck *sync.Mutex
errCount int
}
// NewSession creates an instance of Session
func NewSession(a dht3.Announcer) *Session {
return &Session{a: a, wg: &sync.WaitGroup{}, lck: &sync.Mutex{}}
}
// Announce an object
func (s *Session) Announce(objType int, repo string, key []byte) bool {
s.wg.Add(1)
announced := s.a.Announce(objType, repo, key, func(err error) {
if err != nil {
s.lck.Lock()
s.errCount++
s.lck.Unlock()
}
s.wg.Done()
})
if !announced {
s.wg.Done()
}
return announced
}
// OnDone calls the callback with the number of failed announcements in the session.
func (s *Session) OnDone(cb func(errCount int)) {
s.wg.Wait()
s.lck.Lock()
errCount := s.errCount
s.lck.Unlock()
cb(errCount)
}
// Task represents a key that needs to be announced.
type Task struct {
Type int // The type of object the key represents.
RepoName string // The name of the repository where the object can be found
Key util.Bytes // The unique object key
CheckExistence bool // Indicates that existence check should be performed on the key.
Done func(err error) // Done is called on success or failure
}
func (t *Task) GetID() interface{} {
return t.Key.String()
}
// Announcer implements dht.Announcer.
// It provides the mechanism for announcing keys on the DHT.
// Announcement requests are queued up an concurrently executed by n workers.
// When an announcement fails, it is retried several times.
type Announcer struct {
keepers core.Keepers
log logger.Logger
dht *kaddht.IpfsDHT
checkers *sync.Map
lck *sync.Mutex
queue chan *Task
queued map[string]struct{}
reannouncer *time.Ticker
started bool
stopped bool
}
// New creates an instance of Announcer
func New(cfg *config.AppConfig, dht *kaddht.IpfsDHT, keepers core.Keepers) *Announcer {
rs := &Announcer{
keepers: keepers,
dht: dht,
checkers: &sync.Map{},
lck: &sync.Mutex{},
log: cfg.G().Log.Module("announcer"),
queue: make(chan *Task, 10000),
queued: make(map[string]struct{}),
}
go func() {
config.GetInterrupt().Wait()
rs.Stop()
}()
return rs
}
// addTask adds a new announcement task to the queue.
// Returns true if task was added or false if it already exist in the queue.
func (a *Announcer) addTask(task *Task) bool {
a.lck.Lock()
defer a.lck.Unlock()
if _, ok := a.queued[task.GetID().(string)]; !ok {
a.queue <- task
a.queued[task.GetID().(string)] = struct{}{}
return true
}
return false
}
// GetQueued returns the index containing tasks awaiting processing.
func (a *Announcer) GetQueued() map[string]struct{} {
return a.queued
}
// finishedTask removes the task from the queued index
func (a *Announcer) finishedTask(task *Task) {
a.lck.Lock()
defer a.lck.Unlock()
delete(a.queued, task.GetID().(string))
}
// Announce queues an object to be announced.
// objType is the type of the object.
// repo is the name of the repository where the object can be found.
// key is the unique identifier of the object.
// doneCB is called after successful announcement
// Returns true if object has been successfully queued
func (a *Announcer) Announce(objType int, repo string, key []byte, doneCB func(error)) bool {
task := &Task{Type: objType, RepoName: repo, Key: key, Done: doneCB}
return a.addTask(task)
}
// NewSession creates an instance of Session
func (a *Announcer) NewSession() dht3.Session {
return NewSession(a)
}
// HasTask checks whether there are one or more unprocessed tasks.
func (a *Announcer) HasTask() bool {
return len(a.queue) > 0
}
// QueueSize returns the size of the queue
func (a *Announcer) QueueSize() int {
return len(a.queue)
}
// Start starts the workers.
// Panics if already started.
func (a *Announcer) Start() {
a.lck.Lock()
started := a.started
a.lck.Unlock()
if started {
panic("already started")
}
a.reannouncer = time.NewTicker(ReannouncerInterval)
go func() {
for range a.reannouncer.C {
a.Reannounce()
}
}()
go func() {
for task := range a.queue {
go a.Do(task)
}
}()
a.lck.Lock()
a.started = true
a.lck.Unlock()
}
// IsRunning checks if the announcer is running.
func (a *Announcer) IsRunning() bool {
return a.started
}
// hasStopped checks whether the announcer has been stopped
func (a *Announcer) hasStopped() bool {
a.lck.Lock()
defer a.lck.Unlock()
return a.stopped
}
// getTask returns a task from the queue
func (a *Announcer) getTask() *Task {
return <-a.queue
}
// Stop stops the announcer
func (a *Announcer) Stop() {
a.lck.Lock()
a.stopped = true
a.started = false
if a.reannouncer != nil {
a.reannouncer.Stop()
}
a.lck.Unlock()
}
// keyExist performs existence check for a given task's key.
// If the checker for the object type is not found, the key
// is removed from the announce list
func (a *Announcer) keyExist(task *Task) bool {
cf, ok := a.checkers.Load(task.Type)
if !ok {
a.keepers.DHTKeeper().RemoveFromAnnounceList(task.Key)
return false
}
return cf.(dht3.CheckFunc)(task.RepoName, task.Key)
}
// Do announces the key in the given task.
// After announcement, the key is (re)added to the announce list.
func (a *Announcer) Do(task *Task) (err error) {
if task.Done == nil {
task.Done = func(err error) {}
}
defer func() {
a.finishedTask(task)
task.Done(err)
}()
// If the key's existence needs to be checked, perform check operation.
if task.CheckExistence && !a.keyExist(task) {
err = ErrDelisted
return
}
// Make CID out of the key
var key = task.Key
var cid cid2.Cid
cid, err = dht3.MakeCID(key)
if err != nil {
return
}
// Broadcast as provider of the key to the DHT.
// Allow exponential backoff retries on failure.
err = backoff.Retry(func() error {
ctx, cn := context.WithTimeout(context.Background(), 60*time.Second)
if err := a.dht.Provide(ctx, cid, true); err != nil {
cn()
return err
}
cn()
return nil
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), uint64(MaxRetry)))
if err != nil {
a.log.Error("Failed to announce key", "Err", err, "Key", plumbing.BytesToHex(key))
return
}
// (Re)add the key to the announce list
a.keepers.DHTKeeper().AddToAnnounceList(key, task.RepoName, task.Type, time.Now().Add(KeyReannounceDur).Unix())
a.log.Debug("Successfully announced a key", "Key", plumbing.BytesToHex(key))
return
}
// Reannounce finds keys that need to be re-announced
func (a *Announcer) Reannounce() {
a.keepers.DHTKeeper().IterateAnnounceList(func(key []byte, entry *core.AnnounceListEntry) {
annTime := time.Unix(entry.NextTime, 0)
if time.Now().After(annTime) || time.Now().Equal(annTime) {
a.addTask(&Task{Type: entry.Type, RepoName: entry.Repo, Key: key, CheckExistence: true})
}
})
}
// RegisterChecker allows external caller to register existence checker
// for a given object type. Only one checker per object type.
func (a *Announcer) RegisterChecker(objType int, checker dht3.CheckFunc) {
a.checkers.Store(objType, checker)
}