-
Notifications
You must be signed in to change notification settings - Fork 266
/
scheduling.go
769 lines (666 loc) · 27.3 KB
/
scheduling.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
* 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.
*/
//go:generate mockgen -destination mocks/scheduling_mock.go -source scheduling.go -package mocks
package scheduling
import (
"context"
"fmt"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
commonv1 "d7y.io/api/v2/pkg/apis/common/v1"
commonv2 "d7y.io/api/v2/pkg/apis/common/v2"
schedulerv1 "d7y.io/api/v2/pkg/apis/scheduler/v1"
schedulerv2 "d7y.io/api/v2/pkg/apis/scheduler/v2"
"d7y.io/dragonfly/v2/pkg/container/set"
"d7y.io/dragonfly/v2/pkg/types"
"d7y.io/dragonfly/v2/scheduler/config"
"d7y.io/dragonfly/v2/scheduler/resource"
"d7y.io/dragonfly/v2/scheduler/scheduling/evaluator"
)
type Scheduling interface {
// ScheduleCandidateParents schedules candidate parents to the normal peer.
// Used only in v2 version of the grpc.
ScheduleCandidateParents(context.Context, *resource.Peer, set.SafeSet[string]) error
// ScheduleParentAndCandidateParents schedules a parent and candidate parents to the normal peer.
// Used only in v1 version of the grpc.
ScheduleParentAndCandidateParents(context.Context, *resource.Peer, set.SafeSet[string])
// FindCandidateParents finds candidate parents for the peer.
// Used only in v2 version of the grpc.
FindCandidateParents(context.Context, *resource.Peer, set.SafeSet[string]) ([]*resource.Peer, bool)
// FindParentAndCandidateParents finds a parent and candidate parents for the peer.
// Used only in v1 version of the grpc.
FindParentAndCandidateParents(context.Context, *resource.Peer, set.SafeSet[string]) ([]*resource.Peer, bool)
// FindSuccessParent finds success parent for the peer.
FindSuccessParent(context.Context, *resource.Peer, set.SafeSet[string]) (*resource.Peer, bool)
}
type scheduling struct {
// Evaluator interface.
evaluator evaluator.Evaluator
// Scheduler configuration.
config *config.SchedulerConfig
// Scheduler dynamic configuration.
dynconfig config.DynconfigInterface
}
func New(cfg *config.SchedulerConfig, dynconfig config.DynconfigInterface, pluginDir string, networkTopologyOptions ...evaluator.NetworkTopologyOption) Scheduling {
return &scheduling{
evaluator: evaluator.New(cfg.Algorithm, pluginDir, networkTopologyOptions...),
config: cfg,
dynconfig: dynconfig,
}
}
// ScheduleCandidateParents schedules candidate parents to the normal peer.
// Used only in v2 version of the grpc.
func (s *scheduling) ScheduleCandidateParents(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet[string]) error {
var n int
for {
select {
case <-ctx.Done():
peer.Log.Infof("context was done")
return ctx.Err()
default:
}
// Scheduling will send NeedBackToSourceResponse to peer.
//
// Condition 1: Peer's NeedBackToSource is true.
// Condition 2: Scheduling exceeds the RetryBackToSourceLimit.
if peer.Task.CanBackToSource() {
// Check condition 1:
// Peer's NeedBackToSource is true.
if peer.NeedBackToSource.Load() {
stream, loaded := peer.LoadAnnouncePeerStream()
if !loaded {
peer.Log.Error("load stream failed")
return status.Error(codes.FailedPrecondition, "load stream failed")
}
// Send NeedBackToSourceResponse to peer.
peer.Log.Infof("send NeedBackToSourceResponse, because of peer's NeedBackToSource is %t", peer.NeedBackToSource.Load())
description := fmt.Sprintf("peer's NeedBackToSource is %t", peer.NeedBackToSource.Load())
if err := stream.Send(&schedulerv2.AnnouncePeerResponse{
Response: &schedulerv2.AnnouncePeerResponse_NeedBackToSourceResponse{
NeedBackToSourceResponse: &schedulerv2.NeedBackToSourceResponse{
Description: &description,
},
},
}); err != nil {
peer.Log.Error(err)
return status.Error(codes.FailedPrecondition, err.Error())
}
return nil
}
// Check condition 2:
// The number of retry scheduling is greater than RetryBackToSourceLimit
if n >= s.config.RetryBackToSourceLimit {
stream, loaded := peer.LoadAnnouncePeerStream()
if !loaded {
peer.Log.Error("load stream failed")
return status.Error(codes.FailedPrecondition, "load stream failed")
}
// Send NeedBackToSourceResponse to peer.
peer.Log.Infof("send NeedBackToSourceResponse, because of scheduling exceeded RetryBackToSourceLimit %d", s.config.RetryBackToSourceLimit)
description := "scheduling exceeded RetryBackToSourceLimit"
if err := stream.Send(&schedulerv2.AnnouncePeerResponse{
Response: &schedulerv2.AnnouncePeerResponse_NeedBackToSourceResponse{
NeedBackToSourceResponse: &schedulerv2.NeedBackToSourceResponse{
Description: &description,
},
},
}); err != nil {
peer.Log.Error(err)
return status.Error(codes.FailedPrecondition, err.Error())
}
return nil
}
}
// Scheduling will return schedule failed.
//
// Condition 1: Scheduling exceeds the RetryLimit.
if n >= s.config.RetryLimit {
peer.Log.Errorf("scheduling failed, because of scheduling exceeded RetryLimit %d", s.config.RetryLimit)
return status.Error(codes.FailedPrecondition, "scheduling exceeded RetryLimit")
}
// Scheduling will send NormalTaskResponse to peer.
//
// Condition 1: Scheduling can find candidate parents.
if err := peer.Task.DeletePeerInEdges(peer.ID); err != nil {
peer.Log.Error(err)
return status.Error(codes.Internal, err.Error())
}
// Find candidate parents.
candidateParents, found := s.FindCandidateParents(ctx, peer, blocklist)
if !found {
n++
peer.Log.Infof("scheduling failed in %d times, because of candidate parents not found", n)
// Sleep to avoid hot looping.
time.Sleep(s.config.RetryInterval)
continue
}
// Load AnnouncePeerStream from peer.
stream, loaded := peer.LoadAnnouncePeerStream()
if !loaded {
if err := peer.Task.DeletePeerInEdges(peer.ID); err != nil {
msg := fmt.Sprintf("peer deletes inedges failed: %s", err.Error())
peer.Log.Error(msg)
return status.Error(codes.Internal, msg)
}
peer.Log.Error("load stream failed")
return status.Error(codes.FailedPrecondition, "load stream failed")
}
// Send NormalTaskResponse to peer.
peer.Log.Info("send NormalTaskResponse")
if err := stream.Send(&schedulerv2.AnnouncePeerResponse{
Response: ConstructSuccessNormalTaskResponse(candidateParents),
}); err != nil {
peer.Log.Error(err)
return status.Error(codes.FailedPrecondition, err.Error())
}
// Add edge from parent to peer.
for _, candidateParent := range candidateParents {
if err := peer.Task.AddPeerEdge(candidateParent, peer); err != nil {
peer.Log.Warnf("peer adds edge failed: %s", err.Error())
continue
}
}
peer.Log.Infof("scheduling success in %d times", n+1)
return nil
}
}
// ScheduleParentAndCandidateParents schedules a parent and candidate parents to a peer.
// Used only in v1 version of the grpc.
func (s *scheduling) ScheduleParentAndCandidateParents(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet[string]) {
var n int
for {
select {
case <-ctx.Done():
peer.Log.Infof("context was done")
return
default:
}
// Scheduling will send Code_SchedNeedBackSource to peer.
//
// Condition 1: Peer's NeedBackToSource is true.
// Condition 2: Scheduling exceeds the RetryBackToSourceLimit.
if peer.Task.CanBackToSource() {
// Check condition 1:
// Peer's NeedBackToSource is true.
if peer.NeedBackToSource.Load() {
stream, loaded := peer.LoadReportPieceResultStream()
if !loaded {
peer.Log.Error("load stream failed")
return
}
// Send Code_SchedNeedBackSource to peer.
if err := stream.Send(&schedulerv1.PeerPacket{Code: commonv1.Code_SchedNeedBackSource}); err != nil {
peer.Log.Error(err)
return
}
peer.Log.Infof("send Code_SchedNeedBackSource to peer, because of peer's NeedBackToSource is %t", peer.NeedBackToSource.Load())
if err := peer.FSM.Event(ctx, resource.PeerEventDownloadBackToSource); err != nil {
peer.Log.Errorf("peer fsm event failed: %s", err.Error())
return
}
// If the task state is TaskStateFailed,
// peer back-to-source and reset task state to TaskStateRunning.
if peer.Task.FSM.Is(resource.TaskStateFailed) {
if err := peer.Task.FSM.Event(ctx, resource.TaskEventDownload); err != nil {
peer.Task.Log.Errorf("task fsm event failed: %s", err.Error())
return
}
}
return
}
// Check condition 2:
// The number of retry scheduling is greater than RetryBackToSourceLimit
if n >= s.config.RetryBackToSourceLimit {
stream, loaded := peer.LoadReportPieceResultStream()
if !loaded {
peer.Log.Error("load stream failed")
return
}
// Send Code_SchedNeedBackSource peer.
if err := stream.Send(&schedulerv1.PeerPacket{Code: commonv1.Code_SchedNeedBackSource}); err != nil {
peer.Log.Error(err)
return
}
peer.Log.Infof("send Code_SchedNeedBackSource to peer, because of scheduling exceeded RetryBackToSourceLimit %d", s.config.RetryBackToSourceLimit)
if err := peer.FSM.Event(ctx, resource.PeerEventDownloadBackToSource); err != nil {
peer.Log.Errorf("peer fsm event failed: %s", err.Error())
return
}
// If the task state is TaskStateFailed,
// peer back-to-source and reset task state to TaskStateRunning.
if peer.Task.FSM.Is(resource.TaskStateFailed) {
if err := peer.Task.FSM.Event(ctx, resource.TaskEventDownload); err != nil {
peer.Task.Log.Errorf("task fsm event failed: %s", err.Error())
return
}
}
return
}
}
// Scheduling will send Code_SchedTaskStatusError to peer.
//
// Condition 1: Scheduling exceeds the RetryLimit.
if n >= s.config.RetryLimit {
stream, loaded := peer.LoadReportPieceResultStream()
if !loaded {
peer.Log.Error("load stream failed")
return
}
// Send Code_SchedTaskStatusError to peer.
if err := stream.Send(&schedulerv1.PeerPacket{Code: commonv1.Code_SchedTaskStatusError}); err != nil {
peer.Log.Error(err)
return
}
peer.Log.Errorf("send SchedulePeerFailed to peer, because of scheduling exceeded RetryLimit %d", s.config.RetryLimit)
return
}
// Scheduling will send PeerPacket to peer.
//
// Condition 1: Scheduling can find candidate parents.
if err := peer.Task.DeletePeerInEdges(peer.ID); err != nil {
n++
peer.Log.Errorf("scheduling failed in %d times, because of %s", n, err.Error())
// Sleep to avoid hot looping.
time.Sleep(s.config.RetryInterval)
continue
}
// Find candidate parents.
candidateParents, found := s.FindCandidateParents(ctx, peer, blocklist)
if !found {
n++
peer.Log.Infof("scheduling failed in %d times, because of candidate parents not found", n)
// Sleep to avoid hot looping.
time.Sleep(s.config.RetryInterval)
continue
}
// Load ReportPieceResultStream from peer.
stream, loaded := peer.LoadReportPieceResultStream()
if !loaded {
n++
peer.Log.Errorf("scheduling failed in %d times, because of loading peer stream failed", n)
if err := peer.Task.DeletePeerInEdges(peer.ID); err != nil {
peer.Log.Errorf("peer deletes inedges failed: %s", err.Error())
return
}
return
}
// Send PeerPacket to peer.
peer.Log.Info("send PeerPacket to peer")
if err := stream.Send(ConstructSuccessPeerPacket(peer, candidateParents[0], candidateParents[1:])); err != nil {
n++
peer.Log.Errorf("scheduling failed in %d times, because of %s", n, err.Error())
if err := peer.Task.DeletePeerInEdges(peer.ID); err != nil {
peer.Log.Errorf("peer deletes inedges failed: %s", err.Error())
return
}
return
}
// Add edge from parent to peer.
for _, candidateParent := range candidateParents {
if err := peer.Task.AddPeerEdge(candidateParent, peer); err != nil {
peer.Log.Debugf("peer adds edge failed: %s", err.Error())
continue
}
}
peer.Log.Infof("scheduling success in %d times", n+1)
return
}
}
// FindCandidateParents finds candidate parents for the peer.
func (s *scheduling) FindCandidateParents(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet[string]) ([]*resource.Peer, bool) {
// Only PeerStateReceivedNormal and PeerStateRunning peers need to be rescheduled,
// and other states including the PeerStateBackToSource indicate that
// they have been scheduled.
if !(peer.FSM.Is(resource.PeerStateReceivedNormal) || peer.FSM.Is(resource.PeerStateRunning)) {
peer.Log.Infof("peer state is %s, can not schedule parent", peer.FSM.Current())
return []*resource.Peer{}, false
}
// Find the candidate parent that can be scheduled.
candidateParents := s.filterCandidateParents(peer, blocklist)
if len(candidateParents) == 0 {
peer.Log.Info("can not find candidate parents")
return []*resource.Peer{}, false
}
// Sort candidate parents by evaluation score.
taskTotalPieceCount := peer.Task.TotalPieceCount.Load()
candidateParents = s.evaluator.EvaluateParents(candidateParents, peer, taskTotalPieceCount)
// Get the parents with candidateParentLimit.
candidateParentLimit := config.DefaultSchedulerCandidateParentLimit
if config, err := s.dynconfig.GetSchedulerClusterConfig(); err == nil {
if config.CandidateParentLimit > 0 {
candidateParentLimit = int(config.CandidateParentLimit)
}
}
if len(candidateParents) > candidateParentLimit {
candidateParents = candidateParents[:candidateParentLimit]
}
var parentIDs []string
for _, candidateParent := range candidateParents {
parentIDs = append(parentIDs, candidateParent.ID)
}
peer.Log.Infof("scheduling candidate parents is %#v", parentIDs)
return candidateParents, true
}
// FindParentAndCandidateParents finds a parent and candidate parents for the peer.
func (s *scheduling) FindParentAndCandidateParents(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet[string]) ([]*resource.Peer, bool) {
// Only PeerStateRunning peers need to be rescheduled,
// and other states including the PeerStateBackToSource indicate that
// they have been scheduled.
if !peer.FSM.Is(resource.PeerStateRunning) {
peer.Log.Infof("peer state is %s, can not schedule parent", peer.FSM.Current())
return []*resource.Peer{}, false
}
// Find the candidate parent that can be scheduled.
candidateParents := s.filterCandidateParents(peer, blocklist)
if len(candidateParents) == 0 {
peer.Log.Info("can not find candidate parents")
return []*resource.Peer{}, false
}
// Sort candidate parents by evaluation score.
taskTotalPieceCount := peer.Task.TotalPieceCount.Load()
candidateParents = s.evaluator.EvaluateParents(candidateParents, peer, taskTotalPieceCount)
// Get the parents with candidateParentLimit.
candidateParentLimit := config.DefaultSchedulerCandidateParentLimit
if config, err := s.dynconfig.GetSchedulerClusterConfig(); err == nil {
if config.CandidateParentLimit > 0 {
candidateParentLimit = int(config.CandidateParentLimit)
}
}
if len(candidateParents) > candidateParentLimit {
candidateParents = candidateParents[:candidateParentLimit]
}
var parentIDs []string
for _, candidateParent := range candidateParents {
parentIDs = append(parentIDs, candidateParent.ID)
}
peer.Log.Infof("scheduling candidate parents is %#v", parentIDs)
return candidateParents, true
}
// FindSuccessParent finds success parent for the peer.
func (s *scheduling) FindSuccessParent(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet[string]) (*resource.Peer, bool) {
// Only PeerStateRunning peers need to be rescheduled,
// and other states including the PeerStateBackToSource indicate that
// they have been scheduled.
if !peer.FSM.Is(resource.PeerStateRunning) {
peer.Log.Infof("peer state is %s, can not schedule parent", peer.FSM.Current())
return nil, false
}
// Find the candidate parent that can be scheduled.
candidateParents := s.filterCandidateParents(peer, blocklist)
if len(candidateParents) == 0 {
peer.Log.Info("can not find candidate parents")
return nil, false
}
var successParents []*resource.Peer
for _, candidateParent := range candidateParents {
if candidateParent.FSM.Is(resource.PeerStateSucceeded) {
successParents = append(successParents, candidateParent)
}
}
// Sort candidate parents by evaluation score.
taskTotalPieceCount := peer.Task.TotalPieceCount.Load()
successParents = s.evaluator.EvaluateParents(successParents, peer, taskTotalPieceCount)
peer.Log.Infof("scheduling success parent is %s", successParents[0].ID)
return successParents[0], true
}
// filterCandidateParents filters the candidate parents that can be scheduled.
func (s *scheduling) filterCandidateParents(peer *resource.Peer, blocklist set.SafeSet[string]) []*resource.Peer {
filterParentLimit := config.DefaultSchedulerFilterParentLimit
if config, err := s.dynconfig.GetSchedulerClusterConfig(); err == nil {
if config.FilterParentLimit > 0 {
filterParentLimit = int(config.FilterParentLimit)
}
}
var (
candidateParents []*resource.Peer
candidateParentIDs []string
)
for _, candidateParent := range peer.Task.LoadRandomPeers(uint(filterParentLimit)) {
// Candidate parent is in blocklist.
if blocklist.Contains(candidateParent.ID) {
peer.Log.Debugf("parent %s is not selected because it is in blocklist", candidateParent.ID)
continue
}
// Candidate parent host is not allowed to be the same as the peer host,
// because dfdaemon cannot handle the situation
// where two tasks are downloading and downloading each other.
if peer.Host.ID == candidateParent.Host.ID {
peer.Log.Debugf("parent %s host %s is the same as peer host", candidateParent.ID, candidateParent.Host.ID)
continue
}
// Candidate parent can not find in dag.
inDegree, err := peer.Task.PeerInDegree(candidateParent.ID)
if err != nil {
peer.Log.Debugf("can not find parent %s vertex in dag", candidateParent.ID)
continue
}
// Parent can be parent of the peer:
// Condition 1: Parent has parent.
// Condition 2: Parent has been back-to-source.
// Condition 3: Parent has been succeeded.
// Condition 4: Parent is seed peer.
if candidateParent.Host.Type == types.HostTypeNormal && inDegree == 0 && !candidateParent.FSM.Is(resource.PeerStateBackToSource) &&
!candidateParent.FSM.Is(resource.PeerStateSucceeded) {
peer.Log.Debugf("parent %s is not selected, because its download state is %d %d %s",
candidateParent.ID, inDegree, int(candidateParent.Host.Type), candidateParent.FSM.Current())
continue
}
// Candidate parent is bad node.
if s.evaluator.IsBadNode(candidateParent) {
peer.Log.Debugf("parent %s is not selected because it is bad node", candidateParent.ID)
continue
}
// Candidate parent's free upload is empty.
if candidateParent.Host.FreeUploadCount() <= 0 {
peer.Log.Debugf("parent %s is not selected because its free upload is empty, upload limit is %d, upload count is %d",
candidateParent.ID, candidateParent.Host.ConcurrentUploadLimit.Load(), candidateParent.Host.ConcurrentUploadCount.Load())
continue
}
// Candidate parent can add edge with peer.
if !peer.Task.CanAddPeerEdge(candidateParent.ID, peer.ID) {
peer.Log.Debugf("can not add edge with parent %s", candidateParent.ID)
continue
}
candidateParents = append(candidateParents, candidateParent)
candidateParentIDs = append(candidateParentIDs, candidateParent.ID)
}
peer.Log.Infof("filter candidate parents is %#v", candidateParentIDs)
return candidateParents
}
// ConstructSuccessNormalTaskResponse constructs scheduling successful response of the normal task.
// Used only in v2 version of the grpc.
func ConstructSuccessNormalTaskResponse(candidateParents []*resource.Peer) *schedulerv2.AnnouncePeerResponse_NormalTaskResponse {
var parents []*commonv2.Peer
for _, candidateParent := range candidateParents {
parent := &commonv2.Peer{
Id: candidateParent.ID,
Priority: candidateParent.Priority,
Cost: durationpb.New(candidateParent.Cost.Load()),
State: candidateParent.FSM.Current(),
NeedBackToSource: candidateParent.NeedBackToSource.Load(),
CreatedAt: timestamppb.New(candidateParent.CreatedAt.Load()),
UpdatedAt: timestamppb.New(candidateParent.UpdatedAt.Load()),
}
// Set range to parent.
if candidateParent.Range != nil {
parent.Range = &commonv2.Range{
Start: uint64(candidateParent.Range.Start),
Length: uint64(candidateParent.Range.Length),
}
}
// Set pieces to parent.
candidateParent.Pieces.Range(func(key, value any) bool {
candidateParentPiece, ok := value.(*resource.Piece)
if !ok {
candidateParent.Log.Errorf("invalid piece %s %#v", key, value)
return true
}
piece := &commonv2.Piece{
Number: uint32(candidateParentPiece.Number),
ParentId: &candidateParentPiece.ParentID,
Offset: candidateParentPiece.Offset,
Length: candidateParentPiece.Length,
TrafficType: &candidateParentPiece.TrafficType,
Cost: durationpb.New(candidateParentPiece.Cost),
CreatedAt: timestamppb.New(candidateParentPiece.CreatedAt),
}
if candidateParentPiece.Digest != nil {
piece.Digest = candidateParentPiece.Digest.String()
}
parent.Pieces = append(parent.Pieces, piece)
return true
})
// Set task to parent.
parent.Task = &commonv2.Task{
Id: candidateParent.Task.ID,
Type: candidateParent.Task.Type,
Url: candidateParent.Task.URL,
Tag: &candidateParent.Task.Tag,
Application: &candidateParent.Task.Application,
FilteredQueryParams: candidateParent.Task.FilteredQueryParams,
RequestHeader: candidateParent.Task.Header,
PieceLength: uint32(candidateParent.Task.PieceLength),
ContentLength: uint64(candidateParent.Task.ContentLength.Load()),
PieceCount: uint32(candidateParent.Task.TotalPieceCount.Load()),
SizeScope: candidateParent.Task.SizeScope(),
State: candidateParent.Task.FSM.Current(),
PeerCount: uint32(candidateParent.Task.PeerCount()),
CreatedAt: timestamppb.New(candidateParent.Task.CreatedAt.Load()),
UpdatedAt: timestamppb.New(candidateParent.Task.UpdatedAt.Load()),
}
// Set digest to parent task.
if candidateParent.Task.Digest != nil {
dgst := candidateParent.Task.Digest.String()
parent.Task.Digest = &dgst
}
// Set pieces to parent task.
candidateParent.Task.Pieces.Range(func(key, value any) bool {
taskPiece, ok := value.(*resource.Piece)
if !ok {
candidateParent.Task.Log.Errorf("invalid piece %s %#v", key, value)
return true
}
piece := &commonv2.Piece{
Number: uint32(taskPiece.Number),
ParentId: &taskPiece.ParentID,
Offset: taskPiece.Offset,
Length: taskPiece.Length,
TrafficType: &taskPiece.TrafficType,
Cost: durationpb.New(taskPiece.Cost),
CreatedAt: timestamppb.New(taskPiece.CreatedAt),
}
if taskPiece.Digest != nil {
piece.Digest = taskPiece.Digest.String()
}
parent.Task.Pieces = append(parent.Task.Pieces, piece)
return true
})
// Set host to parent.
parent.Host = &commonv2.Host{
Id: candidateParent.Host.ID,
Type: uint32(candidateParent.Host.Type),
Hostname: candidateParent.Host.Hostname,
Ip: candidateParent.Host.IP,
Port: candidateParent.Host.Port,
DownloadPort: candidateParent.Host.DownloadPort,
Os: candidateParent.Host.OS,
Platform: candidateParent.Host.Platform,
PlatformFamily: candidateParent.Host.PlatformFamily,
PlatformVersion: candidateParent.Host.PlatformVersion,
KernelVersion: candidateParent.Host.KernelVersion,
Cpu: &commonv2.CPU{
LogicalCount: candidateParent.Host.CPU.LogicalCount,
PhysicalCount: candidateParent.Host.CPU.PhysicalCount,
Percent: candidateParent.Host.CPU.Percent,
ProcessPercent: candidateParent.Host.CPU.ProcessPercent,
Times: &commonv2.CPUTimes{
User: candidateParent.Host.CPU.Times.User,
System: candidateParent.Host.CPU.Times.System,
Idle: candidateParent.Host.CPU.Times.Idle,
Nice: candidateParent.Host.CPU.Times.Nice,
Iowait: candidateParent.Host.CPU.Times.Iowait,
Irq: candidateParent.Host.CPU.Times.Irq,
Softirq: candidateParent.Host.CPU.Times.Softirq,
Steal: candidateParent.Host.CPU.Times.Steal,
Guest: candidateParent.Host.CPU.Times.Guest,
GuestNice: candidateParent.Host.CPU.Times.GuestNice,
},
},
Memory: &commonv2.Memory{
Total: candidateParent.Host.Memory.Total,
Available: candidateParent.Host.Memory.Available,
Used: candidateParent.Host.Memory.Used,
UsedPercent: candidateParent.Host.Memory.UsedPercent,
ProcessUsedPercent: candidateParent.Host.Memory.ProcessUsedPercent,
Free: candidateParent.Host.Memory.Free,
},
Network: &commonv2.Network{
TcpConnectionCount: candidateParent.Host.Network.TCPConnectionCount,
UploadTcpConnectionCount: candidateParent.Host.Network.UploadTCPConnectionCount,
Location: &candidateParent.Host.Network.Location,
Idc: &candidateParent.Host.Network.IDC,
},
Disk: &commonv2.Disk{
Total: candidateParent.Host.Disk.Total,
Free: candidateParent.Host.Disk.Free,
Used: candidateParent.Host.Disk.Used,
UsedPercent: candidateParent.Host.Disk.UsedPercent,
InodesTotal: candidateParent.Host.Disk.InodesTotal,
InodesUsed: candidateParent.Host.Disk.InodesUsed,
InodesFree: candidateParent.Host.Disk.InodesFree,
InodesUsedPercent: candidateParent.Host.Disk.InodesUsedPercent,
},
Build: &commonv2.Build{
GitVersion: candidateParent.Host.Build.GitVersion,
GitCommit: &candidateParent.Host.Build.GitCommit,
GoVersion: &candidateParent.Host.Build.GoVersion,
Platform: &candidateParent.Host.Build.Platform,
},
}
parents = append(parents, parent)
}
return &schedulerv2.AnnouncePeerResponse_NormalTaskResponse{
NormalTaskResponse: &schedulerv2.NormalTaskResponse{
CandidateParents: parents,
},
}
}
// ConstructSuccessPeerPacket constructs peer successful packet.
// Used only in v1 version of the grpc.
func ConstructSuccessPeerPacket(peer *resource.Peer, parent *resource.Peer, candidateParents []*resource.Peer) *schedulerv1.PeerPacket {
var parents []*schedulerv1.PeerPacket_DestPeer
for _, candidateParent := range candidateParents {
parents = append(parents, &schedulerv1.PeerPacket_DestPeer{
Ip: candidateParent.Host.IP,
RpcPort: candidateParent.Host.Port,
PeerId: candidateParent.ID,
})
}
return &schedulerv1.PeerPacket{
TaskId: peer.Task.ID,
SrcPid: peer.ID,
MainPeer: &schedulerv1.PeerPacket_DestPeer{
Ip: parent.Host.IP,
RpcPort: parent.Host.Port,
PeerId: parent.ID,
},
CandidatePeers: parents,
Code: commonv1.Code_Success,
}
}