-
Notifications
You must be signed in to change notification settings - Fork 43
/
synchronizer.go
390 lines (324 loc) · 9.64 KB
/
synchronizer.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package sync
import (
"container/list"
"context"
"encoding/hex"
"errors"
"fmt"
"sync"
"time"
"github.com/FleekHQ/space-daemon/config"
"github.com/FleekHQ/space-daemon/core/keychain"
"github.com/FleekHQ/space-daemon/core/store"
"github.com/FleekHQ/space-daemon/core/textile/bucket"
"github.com/FleekHQ/space-daemon/core/textile/hub"
"github.com/FleekHQ/space-daemon/core/textile/model"
"github.com/FleekHQ/space-daemon/log"
threadsClient "github.com/textileio/go-threads/api/client"
"github.com/textileio/go-threads/core/thread"
nc "github.com/textileio/go-threads/net/api/client"
bucketsClient "github.com/textileio/textile/v2/api/buckets/client"
)
type GetMirrorBucketFn func(ctx context.Context, slug string) (bucket.BucketInterface, error)
type GetBucketFn func(ctx context.Context, slug string) (bucket.BucketInterface, error)
type GetBucketCtxFn func(ctx context.Context, sDbID string, bucketSlug string, ishub bool, enckey []byte) (context.Context, *thread.ID, error)
type AddBucketListenerFn func(ctx context.Context, bucketSlug string) error
const maxParallelTasks = 16
type synchronizer struct {
taskQueue *list.List
filePinningQueue *list.List
queueHashMap map[string]*Task
st store.Store
model model.Model
syncNeeded chan (bool)
shuttingDownMap map[*list.List]chan (bool)
queueMutexMap map[*list.List]*sync.Mutex
getMirrorBucket GetMirrorBucketFn
getBucket GetBucketFn
getBucketCtx GetBucketCtxFn
addBucketListener AddBucketListenerFn
kc keychain.Keychain
hubAuth hub.HubAuth
hubBuckets *bucketsClient.Client
hubThreads *threadsClient.Client
cfg config.Config
netc *nc.Client
queueWg *sync.WaitGroup
eventNotifier EventNotifier
}
// Creates a new Synchronizer
func New(
st store.Store,
model model.Model,
kc keychain.Keychain,
hubAuth hub.HubAuth,
hb *bucketsClient.Client,
ht *threadsClient.Client,
netc *nc.Client,
cfg config.Config,
getMirrorBucket GetMirrorBucketFn,
getBucket GetBucketFn,
getBucketCtx GetBucketCtxFn,
addBucketListenerFn AddBucketListenerFn,
) *synchronizer {
taskQueue := list.New()
filePinningQueue := list.New()
queueMutexMap := make(map[*list.List]*sync.Mutex)
queueMutexMap[taskQueue] = &sync.Mutex{}
queueMutexMap[filePinningQueue] = &sync.Mutex{}
shuttingDownMap := make(map[*list.List]chan bool)
shuttingDownMap[taskQueue] = make(chan bool)
shuttingDownMap[filePinningQueue] = make(chan bool)
queueWg := &sync.WaitGroup{}
return &synchronizer{
taskQueue: taskQueue,
filePinningQueue: filePinningQueue,
queueHashMap: make(map[string]*Task),
st: st,
model: model,
syncNeeded: make(chan bool),
shuttingDownMap: shuttingDownMap,
queueMutexMap: queueMutexMap,
getMirrorBucket: getMirrorBucket,
getBucket: getBucket,
getBucketCtx: getBucketCtx,
addBucketListener: addBucketListenerFn,
kc: kc,
hubAuth: hubAuth,
hubBuckets: hb,
hubThreads: ht,
cfg: cfg,
netc: netc,
queueWg: queueWg,
}
}
// Notify Textile synchronizer that an add item operation needs to be synced
func (s *synchronizer) NotifyItemAdded(bucket, path string) {
t := newTask(addItemTask, []string{bucket, path})
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
// Notify Textile synchronizer that a remove item operation needs to be synced
func (s *synchronizer) NotifyItemRemoved(bucket, path string) {
t := newTask(removeItemTask, []string{bucket, path})
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyBucketCreated(bucket string, enckey []byte) {
t := newTask(createBucketTask, []string{bucket, hex.EncodeToString(enckey)})
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyBucketBackupOn(bucket string) {
t := newTask(bucketBackupOnTask, []string{bucket})
t.Parallelizable = true
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyBucketBackupOff(bucket string) {
t := newTask(bucketBackupOffTask, []string{bucket})
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyBucketRestore(bucket string) {
t := newTask(bucketRestoreTask, []string{bucket})
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyFileRestore(bucket, path string) {
t := newTask(restoreFileTask, []string{bucket, path})
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyBucketStartup(bucket string) {
s.NotifyBucketRestore(bucket)
s.NotifyBucketBackupOn(bucket) // does nothing if !bucket.Backup
s.notifySyncNeeded()
}
func (s *synchronizer) NotifyIndexItemAdded(bucket, path, dbId string) {
t := newTask(addIndexItemTask, []string{bucket, path, dbId})
t.Parallelizable = true
t.MaxRetries = 2
s.enqueueTask(t, s.taskQueue)
s.notifySyncNeeded()
}
func (s *synchronizer) notifySyncNeeded() {
select {
case s.syncNeeded <- true:
default:
}
}
// Starts the synchronizer, which will constantly be checking if there are syncing tasks pending
func (s *synchronizer) Start(ctx context.Context) {
s.queueWg.Add(2)
// Sync loop
go func() {
s.startSyncLoop(ctx, s.taskQueue)
s.queueWg.Done()
}()
go func() {
s.startSyncLoop(ctx, s.filePinningQueue)
s.queueWg.Done()
}()
}
// Restores a previously initialized queue
func (s *synchronizer) RestoreQueue() error {
if err := s.restoreQueue(); err != nil {
return err
}
return nil
}
func (s *synchronizer) startSyncLoop(ctx context.Context, queue *list.List) {
queueMutex := s.queueMutexMap[queue]
// Initial sync
queueMutex.Lock()
s.sync(ctx, queue)
queueMutex.Unlock()
Loop:
for {
queueMutex.Lock()
timeAfterNextSync := 30 * time.Second
select {
case <-time.After(timeAfterNextSync):
s.sync(ctx, queue)
case <-s.syncNeeded:
s.sync(ctx, queue)
// Break execution in case of shutdown
case <-ctx.Done():
queueMutex.Unlock()
s.Shutdown()
break Loop
case <-s.shuttingDownMap[queue]:
queueMutex.Unlock()
break Loop
}
queueMutex.Unlock()
}
}
func (s *synchronizer) Shutdown() {
s.shuttingDownMap[s.taskQueue] <- true
s.shuttingDownMap[s.filePinningQueue] <- true
s.queueWg.Wait()
if err := s.storeQueue(); err != nil {
log.Error("Error while storing Textile task queue state", err)
}
close(s.shuttingDownMap[s.taskQueue])
close(s.shuttingDownMap[s.filePinningQueue])
close(s.syncNeeded)
}
func (s *synchronizer) String() string {
queues := []*list.List{s.filePinningQueue, s.taskQueue}
res := ""
for _, q := range queues {
res = res + s.queueString(q) + "\n"
}
return res
}
var errMaxRetriesSurpassed = errors.New("max retries surpassed")
func (s *synchronizer) executeTask(ctx context.Context, t *Task) error {
var err error
switch t.Type {
case addItemTask:
err = s.processAddItem(ctx, t)
case removeItemTask:
err = s.processRemoveItem(ctx, t)
case pinFileTask:
err = s.processPinFile(ctx, t)
case unpinFileTask:
err = s.processUnpinFile(ctx, t)
case createBucketTask:
err = s.processCreateBucket(ctx, t)
case bucketBackupOnTask:
err = s.processBucketBackupOn(ctx, t)
case bucketBackupOffTask:
err = s.processBucketBackupOff(ctx, t)
case bucketRestoreTask:
err = s.processBucketRestoreTask(ctx, t)
case restoreFileTask:
err = s.processRestoreFile(ctx, t)
case addIndexItemTask:
err = s.processAddIndexItemTask(ctx, t)
case removeIndexItemTask:
err = s.processRemoveIndexItemTask(ctx, t)
default:
log.Warn("Unexpected action on Textile sync, executeTask")
}
if err != nil {
t.State = taskFailed
t.Retries++
// Remove from queue if it surpassed the max amount of retries
if t.MaxRetries != -1 && t.Retries > t.MaxRetries {
t.State = taskDequeued
return errMaxRetriesSurpassed
}
// Retry task
t.State = taskQueued
} else {
t.State = taskSucceeded
}
return err
}
func (s *synchronizer) sync(ctx context.Context, queue *list.List) error {
queueName := "buckets"
if queue == s.filePinningQueue {
queueName = "file pinning"
}
log.Debug(fmt.Sprintf("Textile sync [%s]: Sync start", queueName))
log.Debug(s.queueString(queue))
parallelTaskCount := 0
ptWg := sync.WaitGroup{}
for curr := queue.Front(); curr != nil; curr = curr.Next() {
task := curr.Value.(*Task)
if task.State != taskQueued {
// If task is already in process or finished, skip
continue
}
log.Debug(fmt.Sprintf("Textile sync [%s]: Processing task %s", queueName, task.Type))
task.State = taskPending
handleExecResult := func(err error) {
if err == nil {
// Task completed successfully
log.Debug(fmt.Sprintf("Textile sync [%s]: task completed succesfully", queueName))
} else {
log.Error(fmt.Sprintf("Textile sync [%s]: task failed", queueName), err)
}
}
if task.Parallelizable && parallelTaskCount < maxParallelTasks {
parallelTaskCount++
ptWg.Add(1)
go func() {
err := s.executeTask(ctx, task)
handleExecResult(err)
parallelTaskCount--
ptWg.Done()
}()
} else {
err := s.executeTask(ctx, task)
handleExecResult(err)
if err != nil {
// Break from the loop (avoid executing next tasks)
return err
}
}
}
ptWg.Wait()
// Remove successful and dequeued tasks from queue
curr := queue.Front()
for curr != nil {
task := curr.Value.(*Task)
next := curr.Next()
switch task.State {
case taskDequeued:
queue.Remove(curr)
case taskSucceeded:
queue.Remove(curr)
default:
}
curr = next
}
log.Debug(fmt.Sprintf("Textile sync [%s]: Sync end", queueName))
return nil
}
func (s *synchronizer) AttachNotifier(notif EventNotifier) {
s.eventNotifier = notif
}