-
Notifications
You must be signed in to change notification settings - Fork 281
/
service.go
363 lines (325 loc) · 11 KB
/
service.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
/*
* Copyright 2020 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 core
import (
"context"
"sync"
"time"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/util/workqueue"
"d7y.io/dragonfly/v2/internal/dferrors"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/gc"
"d7y.io/dragonfly/v2/pkg/rpc/base"
"d7y.io/dragonfly/v2/pkg/rpc/base/common"
schedulerRPC "d7y.io/dragonfly/v2/pkg/rpc/scheduler"
"d7y.io/dragonfly/v2/pkg/synclock"
"d7y.io/dragonfly/v2/scheduler/config"
"d7y.io/dragonfly/v2/scheduler/core/scheduler"
"d7y.io/dragonfly/v2/scheduler/metrics"
"d7y.io/dragonfly/v2/scheduler/supervisor"
)
const maxRescheduleTimes = 8
type Options struct {
openTel bool
disableCDN bool
}
type Option func(options *Options)
func WithOpenTel(openTel bool) Option {
return func(options *Options) {
options.openTel = openTel
}
}
func WithDisableCDN(disableCDN bool) Option {
return func(options *Options) {
options.disableCDN = disableCDN
}
}
type SchedulerService struct {
// CDN manager
CDN supervisor.CDN
// task manager
taskManager supervisor.TaskManager
// host manager
hostManager supervisor.HostManager
// Peer manager
peerManager supervisor.PeerManager
sched scheduler.Scheduler
worker worker
monitor *monitor
done chan struct{}
wg sync.WaitGroup
config *config.SchedulerConfig
dynconfig config.DynconfigInterface
metricsConfig *config.MetricsConfig
}
func NewSchedulerService(cfg *config.SchedulerConfig, pluginDir string, metricsConfig *config.MetricsConfig, dynConfig config.DynconfigInterface, gc gc.GC, options ...Option) (*SchedulerService, error) {
ops := &Options{}
for _, op := range options {
op(ops)
}
hostManager := supervisor.NewHostManager()
peerManager, err := supervisor.NewPeerManager(cfg.GC, gc, hostManager)
if err != nil {
return nil, err
}
taskManager, err := supervisor.NewTaskManager(cfg.GC, gc, peerManager)
if err != nil {
return nil, err
}
sched, err := scheduler.Get(cfg.Scheduler).Build(cfg, &scheduler.BuildOptions{
PeerManager: peerManager,
PluginDir: pluginDir,
})
if err != nil {
return nil, errors.Wrapf(err, "build scheduler %v", cfg.Scheduler)
}
work := newEventLoopGroup(cfg.WorkerNum)
downloadMonitor := newMonitor(cfg.OpenMonitor, peerManager)
s := &SchedulerService{
taskManager: taskManager,
hostManager: hostManager,
peerManager: peerManager,
worker: work,
monitor: downloadMonitor,
sched: sched,
config: cfg,
metricsConfig: metricsConfig,
dynconfig: dynConfig,
done: make(chan struct{}),
}
if !ops.disableCDN {
var opts []grpc.DialOption
if ops.openTel {
opts = append(opts, grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()))
}
client, err := supervisor.NewCDNDynmaicClient(dynConfig, opts)
if err != nil {
return nil, errors.Wrap(err, "new refreshable cdn client")
}
cdn := supervisor.NewCDN(client, peerManager, hostManager)
if err != nil {
return nil, errors.Wrap(err, "new cdn manager")
}
s.CDN = cdn
}
return s, nil
}
func (s *SchedulerService) Serve() {
s.wg.Add(2)
wsdq := workqueue.NewNamedDelayingQueue("wait reSchedule parent")
go s.runWorkerLoop(wsdq)
go s.runReScheduleParentLoop(wsdq)
go s.runMonitor()
logger.Debugf("start scheduler service successfully")
}
func (s *SchedulerService) runWorkerLoop(wsdq workqueue.DelayingInterface) {
defer s.wg.Done()
s.worker.start(newState(s.sched, s.peerManager, s.CDN, wsdq))
}
func (s *SchedulerService) runReScheduleParentLoop(wsdq workqueue.DelayingInterface) {
for {
select {
case <-s.done:
wsdq.ShutDown()
return
default:
v, shutdown := wsdq.Get()
if shutdown {
logger.Infof("wait schedule delay queue is shutdown")
break
}
rsPeer := v.(*rsPeer)
peer := rsPeer.peer
wsdq.Done(v)
if rsPeer.times > maxRescheduleTimes {
if peer.CloseChannelWithError(dferrors.Newf(base.Code_SchedNeedBackSource, "reschedule parent for peer %s already reaches max reschedule times",
peer.ID)) == nil {
peer.Task.AddBackToSourcePeer(peer.ID)
}
continue
}
if peer.Task.ContainsBackToSourcePeer(peer.ID) {
logger.WithTaskAndPeerID(peer.Task.ID, peer.ID).Debugf("runReScheduleLoop: peer is back source client, no need to reschedule it")
continue
}
if peer.IsDone() || peer.IsLeave() {
peer.Log().Debugf("runReScheduleLoop: peer has left from waitScheduleParentPeerQueue because peer is done or leave, peer status is %s, "+
"isLeave %t", peer.GetStatus(), peer.IsLeave())
continue
}
s.worker.send(reScheduleParentEvent{rsPeer: rsPeer})
}
}
}
func (s *SchedulerService) runMonitor() {
defer s.wg.Done()
if s.monitor != nil {
s.monitor.start(s.done)
}
}
func (s *SchedulerService) Stop() {
close(s.done)
if s.worker != nil {
s.worker.stop()
}
s.wg.Wait()
}
func (s *SchedulerService) SelectParent(peer *supervisor.Peer) (parent *supervisor.Peer, err error) {
parent, _, hasParent := s.sched.ScheduleParent(peer, sets.NewString())
if !hasParent || parent == nil {
return nil, errors.Errorf("no parent peer available for peer %s", peer.ID)
}
return parent, nil
}
func (s *SchedulerService) GetPeer(id string) (*supervisor.Peer, bool) {
return s.peerManager.Get(id)
}
func (s *SchedulerService) RegisterPeerTask(req *schedulerRPC.PeerTaskRequest, task *supervisor.Task) *supervisor.Peer {
// get or create host
peerHost := req.PeerHost
host, ok := s.hostManager.Get(peerHost.Uuid)
if !ok {
var options []supervisor.HostOption
if clientConfig, ok := s.dynconfig.GetSchedulerClusterClientConfig(); ok {
options = []supervisor.HostOption{
supervisor.WithTotalUploadLoad(clientConfig.LoadLimit),
}
}
host = supervisor.NewClientHost(peerHost.Uuid, peerHost.Ip, peerHost.HostName, peerHost.RpcPort, peerHost.DownPort,
peerHost.SecurityDomain, peerHost.Location, peerHost.Idc, options...)
s.hostManager.Add(host)
}
// get or creat PeerTask
peer, ok := s.peerManager.Get(req.PeerId)
if ok {
logger.Warnf("peer %s has already registered", peer.ID)
return peer
}
peer = supervisor.NewPeer(req.PeerId, task, host)
s.peerManager.Add(peer)
return peer
}
func (s *SchedulerService) GetOrCreateTask(ctx context.Context, task *supervisor.Task) *supervisor.Task {
span := trace.SpanFromContext(ctx)
synclock.Lock(task.ID, true)
task, ok := s.taskManager.GetOrAdd(task)
if ok {
span.SetAttributes(config.AttributeTaskStatus.String(task.GetStatus().String()))
span.SetAttributes(config.AttributeLastTriggerTime.String(task.LastTriggerAt.Load().String()))
if task.LastTriggerAt.Load().Add(s.config.AccessWindow).After(time.Now()) || task.IsHealth() {
span.SetAttributes(config.AttributeNeedSeedCDN.Bool(false))
synclock.UnLock(task.ID, true)
return task
}
} else {
task.Log().Infof("add new task %s", task.ID)
}
synclock.UnLock(task.ID, true)
synclock.Lock(task.ID, false)
defer synclock.UnLock(task.ID, false)
// do trigger
span.SetAttributes(config.AttributeTaskStatus.String(task.GetStatus().String()))
span.SetAttributes(config.AttributeLastTriggerTime.String(task.LastTriggerAt.Load().String()))
if task.IsHealth() {
span.SetAttributes(config.AttributeNeedSeedCDN.Bool(false))
return task
}
task.LastTriggerAt.Store(time.Now())
task.SetStatus(supervisor.TaskStatusRunning)
if s.CDN == nil {
// client back source
span.SetAttributes(config.AttributeClientBackSource.Bool(true))
task.BackToSourceWeight.Store(s.config.BackSourceCount)
return task
}
span.SetAttributes(config.AttributeNeedSeedCDN.Bool(true))
go func() {
if cdnPeer, err := s.CDN.StartSeedTask(ctx, task); err != nil {
// fall back to client back source
task.Log().Errorf("seed task failed: %v", err)
span.AddEvent(config.EventCDNFailBackClientSource, trace.WithAttributes(config.AttributeTriggerCDNError.String(err.Error())))
task.BackToSourceWeight.Store(s.config.BackSourceCount)
if ok = s.worker.send(taskSeedFailEvent{task}); !ok {
logger.Error("send taskSeed fail event failed, eventLoop is shutdown")
}
} else {
if ok = s.worker.send(peerDownloadSuccessEvent{cdnPeer, nil}); !ok {
logger.Error("send taskSeed success event failed, eventLoop is shutdown")
}
logger.Infof("successfully obtain seeds from cdn, task: %#v", task)
}
}()
return task
}
func (s *SchedulerService) HandlePieceResult(ctx context.Context, peer *supervisor.Peer, pieceResult *schedulerRPC.PieceResult) error {
peer.Touch()
if pieceResult.Success && s.metricsConfig != nil && s.metricsConfig.EnablePeerHost {
// TODO parse PieceStyle
metrics.PeerHostTraffic.WithLabelValues("download", peer.Host.UUID, peer.Host.IP).Add(float64(pieceResult.PieceInfo.RangeSize))
if p, ok := s.peerManager.Get(pieceResult.DstPid); ok {
metrics.PeerHostTraffic.WithLabelValues("upload", p.Host.UUID, p.Host.IP).Add(float64(pieceResult.PieceInfo.RangeSize))
} else {
logger.Warnf("dst peer %s not found for pieceResult %#v, pieceInfo %#v", pieceResult.DstPid, pieceResult, pieceResult.PieceInfo)
}
}
if pieceResult.PieceInfo != nil && pieceResult.PieceInfo.PieceNum == common.EndOfPiece {
return nil
} else if pieceResult.PieceInfo != nil && pieceResult.PieceInfo.PieceNum == common.ZeroOfPiece {
s.worker.send(startReportPieceResultEvent{ctx, peer})
return nil
} else if pieceResult.Success {
s.worker.send(peerDownloadPieceSuccessEvent{
ctx: ctx,
peer: peer,
pr: pieceResult,
})
return nil
} else if pieceResult.Code != base.Code_Success {
s.worker.send(peerDownloadPieceFailEvent{
ctx: ctx,
peer: peer,
pr: pieceResult,
})
return nil
}
return nil
}
func (s *SchedulerService) HandlePeerResult(ctx context.Context, peer *supervisor.Peer, peerResult *schedulerRPC.PeerResult) error {
peer.Touch()
if peerResult.Success {
if !s.worker.send(peerDownloadSuccessEvent{peer: peer, peerResult: peerResult}) {
logger.Errorf("send peer download success event failed")
}
} else if !s.worker.send(peerDownloadFailEvent{peer: peer, peerResult: peerResult}) {
logger.Errorf("send peer download fail event failed")
}
return nil
}
func (s *SchedulerService) HandleLeaveTask(ctx context.Context, peer *supervisor.Peer) error {
peer.Touch()
if !s.worker.send(peerLeaveEvent{
ctx: ctx,
peer: peer,
}) {
logger.Errorf("send peer leave event failed")
}
return nil
}