-
Notifications
You must be signed in to change notification settings - Fork 1
/
dispatcher.go
196 lines (165 loc) · 3.45 KB
/
dispatcher.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
package byterange
import (
"context"
"fmt"
"github.com/Filecoin-Titan/titan-sdk-go/titan"
"github.com/Filecoin-Titan/titan-sdk-go/types"
"github.com/eikenb/pipeat"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
"math"
"math/rand"
"time"
)
type dispatcher struct {
cid cid.Cid
fileSize int64
rangeSize int64
todos JobQueue
workers chan worker
resp chan response
titan *titan.Service
writer *pipeat.PipeWriterAt
reader *pipeat.PipeReaderAt
backoff *backoff
}
type worker struct {
id int
c *types.Client
}
type response struct {
offset int64
data []byte
}
type job struct {
index int
start int64
end int64
retry int
}
type backoff struct {
minDelay time.Duration
maxDelay time.Duration
}
func (b *backoff) next(attempt int) time.Duration {
if attempt < 0 {
return b.minDelay
}
minf := float64(b.minDelay)
durf := minf * math.Pow(1.5, float64(attempt))
durf = durf + rand.Float64()*minf
delay := time.Duration(durf)
if delay > b.maxDelay {
return b.maxDelay
}
return delay
}
func (d *dispatcher) generateJobs() {
count := int64(math.Ceil(float64(d.fileSize) / float64(d.rangeSize)))
for i := int64(0); i < count; i++ {
start := i * d.rangeSize
end := (i + 1) * d.rangeSize
if end > d.fileSize {
end = d.fileSize
}
newJob := &job{
index: int(i),
start: start,
end: end,
}
d.todos.Push(newJob)
}
}
func (d *dispatcher) run(ctx context.Context) {
d.generateJobs()
d.writeData(ctx)
var (
counter int64
finished = make(chan int64, 1)
)
go func() {
for {
select {
case w := <-d.workers:
go func() {
j, ok := d.todos.Pop()
if !ok {
d.workers <- w
return
}
data, err := d.fetch(ctx, w.c, d.cid, j.start, j.end)
if err != nil {
errMsg := fmt.Sprintf("pull data failed : %v", err)
if j.retry > 0 {
log.Debugf("pull data failed (retries: %d): %v", j.retry, err)
<-time.After(d.backoff.next(j.retry))
}
log.Warnf(errMsg)
j.retry++
d.todos.PushFront(j)
d.workers <- w
return
}
dataLen := j.end - j.start
if int64(len(data)) < dataLen {
log.Debugf("unexpected data size, want %d got %d", dataLen, len(data))
d.todos.PushFront(j)
d.workers <- w
return
}
d.workers <- w
d.resp <- response{
data: data[:dataLen],
offset: j.start,
}
finished <- dataLen
}()
case size := <-finished:
counter += size
if counter >= d.fileSize {
return
}
case <-ctx.Done():
return
}
}
}()
return
}
func (d *dispatcher) writeData(ctx context.Context) {
go func() {
defer d.finally()
var count int64
for {
select {
case r := <-d.resp:
_, err := d.writer.WriteAt(r.data, r.offset)
if err != nil {
log.Errorf("write data failed: %v", err)
continue
}
count += int64(len(r.data))
if count >= d.fileSize {
return
}
case <-ctx.Done():
return
}
}
}()
}
func (d *dispatcher) fetch(ctx context.Context, c *types.Client, cid cid.Cid, start, end int64) ([]byte, error) {
_, data, err := d.titan.GetRange(ctx, c, cid, start, end)
if err != nil {
return nil, errors.Errorf("get range failed: %v", err)
}
return data, nil
}
func (d *dispatcher) finally() {
if err := d.titan.EndOfFile(); err != nil {
log.Errorf("end of file failed: %v", err)
}
if err := d.writer.Close(); err != nil {
log.Errorf("close write failed: %v", err)
}
}