-
Notifications
You must be signed in to change notification settings - Fork 262
/
traffic_shaper.go
271 lines (243 loc) · 7.92 KB
/
traffic_shaper.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
/*
* Copyright 2022 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package peer
import (
"sync"
"time"
"go.uber.org/atomic"
"golang.org/x/time/rate"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/math"
)
const (
TypePlainTrafficShaper = "plain"
TypeSamplingTrafficShaper = "sampling"
)
// TrafficShaper allocates bandwidth for running tasks dynamically
type TrafficShaper interface {
// Start starts the TrafficShaper
Start()
// Stop stops the TrafficShaper
Stop()
// AddTask starts managing the new task
AddTask(taskID string, ptc *peerTaskConductor)
// RemoveTask removes completed task
RemoveTask(taskID string)
// Record records task's used bandwidth
Record(taskID string, n int)
// GetBandwidth gets the total download bandwidth in the past second
GetBandwidth() int64
}
func NewTrafficShaper(trafficShaperType string, totalRateLimit rate.Limit, computePieceSize func(int64) uint32) TrafficShaper {
var ts TrafficShaper
switch trafficShaperType {
case TypeSamplingTrafficShaper:
ts = NewSamplingTrafficShaper(totalRateLimit, computePieceSize)
case TypePlainTrafficShaper:
ts = NewPlainTrafficShaper()
default:
logger.Warnf("type \"%s\" doesn't exist, use plain traffic shaper instead", trafficShaperType)
ts = NewPlainTrafficShaper()
}
return ts
}
type plainTrafficShaper struct {
// total used bandwidth in the past second
lastSecondBandwidth *atomic.Int64
// total used bandwidth in the current second
usingBandWidth *atomic.Int64
stopCh chan struct{}
}
func NewPlainTrafficShaper() TrafficShaper {
return &plainTrafficShaper{
lastSecondBandwidth: atomic.NewInt64(0),
usingBandWidth: atomic.NewInt64(0),
stopCh: make(chan struct{}),
}
}
func (ts *plainTrafficShaper) Start() {
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
ts.lastSecondBandwidth.Store(ts.usingBandWidth.Swap(0))
case <-ts.stopCh:
return
}
}
}()
}
func (ts *plainTrafficShaper) Stop() {
close(ts.stopCh)
}
func (ts *plainTrafficShaper) AddTask(_ string, _ *peerTaskConductor) {
}
func (ts *plainTrafficShaper) RemoveTask(_ string) {
}
func (ts *plainTrafficShaper) Record(_ string, n int) {
ts.usingBandWidth.Add(int64(n))
}
func (ts *plainTrafficShaper) GetBandwidth() int64 {
return ts.lastSecondBandwidth.Load()
}
type taskEntry struct {
ptc *peerTaskConductor
pieceSize uint32
// used bandwidth in the past second
lastSecondBandwidth *atomic.Int64
// need bandwidth in the next second
needBandwidth int64
// indicates if the bandwidth need to be updated, tasks added within one second don't need to be updated
needUpdate bool
}
type samplingTrafficShaper struct {
*logger.SugaredLoggerOnWith
sync.RWMutex
computePieceSize func(int64) uint32
totalRateLimit rate.Limit
// total used bandwidth in the past second
lastSecondBandwidth *atomic.Int64
// total used bandwidth in the current second
usingBandWidth *atomic.Int64
tasks map[string]*taskEntry
stopCh chan struct{}
}
func NewSamplingTrafficShaper(totalRateLimit rate.Limit, computePieceSize func(int64) uint32) TrafficShaper {
log := logger.With("component", "TrafficShaper")
return &samplingTrafficShaper{
SugaredLoggerOnWith: log,
computePieceSize: computePieceSize,
totalRateLimit: totalRateLimit,
lastSecondBandwidth: atomic.NewInt64(0),
usingBandWidth: atomic.NewInt64(0),
tasks: make(map[string]*taskEntry),
stopCh: make(chan struct{}),
}
}
func (ts *samplingTrafficShaper) Start() {
go func() {
// update bandwidth of all running tasks every second
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
ts.lastSecondBandwidth.Store(ts.usingBandWidth.Swap(0))
ts.updateLimit()
case <-ts.stopCh:
return
}
}
}()
}
func (ts *samplingTrafficShaper) Stop() {
close(ts.stopCh)
}
// updateLimit updates every task's limit every second
func (ts *samplingTrafficShaper) updateLimit() {
var totalNeedBandwidth int64
var totalLeastBandwidth int64
ts.RLock()
defer ts.RUnlock()
// compute overall remaining length of all tasks
for _, te := range ts.tasks {
oldLimit := int64(te.ptc.limiter.Limit())
needBandwidth := te.lastSecondBandwidth.Swap(0)
if !te.needUpdate {
// if this task is added within 1 second, don't reduce its limit this time
te.needUpdate = true
needBandwidth = math.Max(needBandwidth, oldLimit)
}
if contentLength := te.ptc.contentLength.Load(); contentLength > 0 {
remainingLength := contentLength - te.ptc.completedLength.Load()
needBandwidth = math.Min(remainingLength, needBandwidth)
}
// delta bandwidth, make sure it's larger than 0
needBandwidth = math.Max(needBandwidth-int64(te.pieceSize), int64(0))
te.needBandwidth = needBandwidth
totalNeedBandwidth += needBandwidth
totalLeastBandwidth += int64(te.pieceSize)
}
// allocate delta bandwidth for tasks
for _, te := range ts.tasks {
// diffLimit indicates the difference between the allocated bandwidth and pieceSize
var diffLimit float64
// make sure new limit is not smaller than pieceSize
diffLimit = math.Max(
(float64(ts.totalRateLimit)-float64(totalLeastBandwidth))*(float64(te.needBandwidth)/float64(totalNeedBandwidth)), 0)
te.ptc.limiter.SetLimit(rate.Limit(diffLimit + float64(te.pieceSize)))
ts.Debugf("period update limit, task %s, need bandwidth %d, diff rate limit %f", te.ptc.taskID, te.needBandwidth, diffLimit)
}
}
func (ts *samplingTrafficShaper) AddTask(taskID string, ptc *peerTaskConductor) {
ts.Lock()
defer ts.Unlock()
nTasks := len(ts.tasks)
if nTasks == 0 {
nTasks++
}
pieceSize := ts.computePieceSize(ptc.contentLength.Load())
limit := rate.Limit(math.Max(float64(ts.totalRateLimit)/float64(nTasks), float64(pieceSize)))
// make sure bandwidth is not smaller than pieceSize
ptc.limiter.SetLimit(limit)
ts.tasks[taskID] = &taskEntry{ptc: ptc, lastSecondBandwidth: atomic.NewInt64(0), pieceSize: pieceSize}
var totalNeedRateLimit rate.Limit
for _, te := range ts.tasks {
totalNeedRateLimit += te.ptc.limiter.Limit()
}
ratio := ts.totalRateLimit / totalNeedRateLimit
// reduce all running tasks' bandwidth
for _, te := range ts.tasks {
// make sure bandwidth is not smaller than pieceSize
newLimit := math.Max(ratio*te.ptc.limiter.Limit(), rate.Limit(te.pieceSize))
te.ptc.limiter.SetLimit(newLimit)
ts.Debugf("a task added, task %s rate limit updated to %f", te.ptc.taskID, newLimit)
}
}
func (ts *samplingTrafficShaper) RemoveTask(taskID string) {
ts.Lock()
defer ts.Unlock()
var limit rate.Limit
if task, ok := ts.tasks[taskID]; ok {
limit = task.ptc.limiter.Limit()
} else {
ts.Debugf("the task %s is already removed", taskID)
return
}
delete(ts.tasks, taskID)
ratio := ts.totalRateLimit / (ts.totalRateLimit - limit)
// increase all running tasks' bandwidth
for _, te := range ts.tasks {
newLimit := ratio * te.ptc.limiter.Limit()
te.ptc.limiter.SetLimit(newLimit)
ts.Debugf("a task removed, task %s rate limit updated to %f", te.ptc.taskID, newLimit)
}
}
func (ts *samplingTrafficShaper) Record(taskID string, n int) {
ts.usingBandWidth.Add(int64(n))
ts.RLock()
if task, ok := ts.tasks[taskID]; ok {
task.lastSecondBandwidth.Add(int64(n))
} else {
ts.Warnf("the task %s is not found when record it", taskID)
}
ts.RUnlock()
}
func (ts *samplingTrafficShaper) GetBandwidth() int64 {
return ts.lastSecondBandwidth.Load()
}