-
Notifications
You must be signed in to change notification settings - Fork 266
/
scheduler_server_v1.go
173 lines (147 loc) · 6.03 KB
/
scheduler_server_v1.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
/*
* Copyright 2023 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 rpcserver
import (
"context"
"google.golang.org/protobuf/types/known/emptypb"
commonv1 "d7y.io/api/v2/pkg/apis/common/v1"
schedulerv1 "d7y.io/api/v2/pkg/apis/scheduler/v1"
"d7y.io/dragonfly/v2/pkg/idgen"
"d7y.io/dragonfly/v2/pkg/types"
"d7y.io/dragonfly/v2/scheduler/config"
"d7y.io/dragonfly/v2/scheduler/metrics"
"d7y.io/dragonfly/v2/scheduler/networktopology"
"d7y.io/dragonfly/v2/scheduler/resource"
"d7y.io/dragonfly/v2/scheduler/scheduling"
"d7y.io/dragonfly/v2/scheduler/service"
"d7y.io/dragonfly/v2/scheduler/storage"
)
// schedulerServerV1 is v1 version of the scheduler grpc server.
type schedulerServerV1 struct {
// Service interface.
service *service.V1
}
// newSchedulerServerV1 returns v1 version of the scheduler server.
func newSchedulerServerV1(
cfg *config.Config,
resource resource.Resource,
scheduling scheduling.Scheduling,
dynconfig config.DynconfigInterface,
storage storage.Storage,
networkTopology networktopology.NetworkTopology,
) schedulerv1.SchedulerServer {
return &schedulerServerV1{service.NewV1(cfg, resource, scheduling, dynconfig, storage, networkTopology)}
}
// RegisterPeerTask registers peer and triggers seed peer download task.
func (s *schedulerServerV1) RegisterPeerTask(ctx context.Context, req *schedulerv1.PeerTaskRequest) (*schedulerv1.RegisterResult, error) {
// FIXME Scheudler will not generate task id.
if req.TaskId == "" {
req.TaskId = idgen.TaskIDV1(req.Url, req.UrlMeta)
}
tag := req.UrlMeta.Tag
application := req.UrlMeta.Application
priority := req.UrlMeta.Priority
// Collect RegisterPeerCount metrics.
metrics.RegisterPeerCount.WithLabelValues(priority.String(), commonv1.TaskType_Normal.String(),
tag, application, types.HostTypeNormalName).Inc()
resp, err := s.service.RegisterPeerTask(ctx, req)
if err != nil {
// Collect RegisterPeerFailureCount metrics.
metrics.RegisterPeerFailureCount.WithLabelValues(priority.String(), commonv1.TaskType_Normal.String(),
tag, application, types.HostTypeNormalName).Inc()
}
return resp, err
}
// ReportPieceResult handles the piece information reported by dfdaemon.
func (s *schedulerServerV1) ReportPieceResult(stream schedulerv1.Scheduler_ReportPieceResultServer) error {
// Collect ConcurrentScheduleGauge metrics.
metrics.ConcurrentScheduleGauge.Inc()
defer metrics.ConcurrentScheduleGauge.Dec()
return s.service.ReportPieceResult(stream)
}
// ReportPeerResult handles peer result reported by dfdaemon.
func (s *schedulerServerV1) ReportPeerResult(ctx context.Context, req *schedulerv1.PeerResult) (*emptypb.Empty, error) {
return new(emptypb.Empty), s.service.ReportPeerResult(ctx, req)
}
// AnnounceTask informs scheduler a peer has completed task.
func (s *schedulerServerV1) AnnounceTask(ctx context.Context, req *schedulerv1.AnnounceTaskRequest) (*emptypb.Empty, error) {
// Collect AnnouncePeerCount metrics.
metrics.AnnouncePeerCount.Inc()
if err := s.service.AnnounceTask(ctx, req); err != nil {
// Collect AnnouncePeerFailureCount metrics.
metrics.AnnouncePeerFailureCount.Inc()
return nil, err
}
return new(emptypb.Empty), nil
}
// StatTask checks if the given task exists.
func (s *schedulerServerV1) StatTask(ctx context.Context, req *schedulerv1.StatTaskRequest) (*schedulerv1.Task, error) {
// Collect StatTaskCount metrics.
metrics.StatTaskCount.Inc()
resp, err := s.service.StatTask(ctx, req)
if err != nil {
// Collect StatTaskFailureCount metrics.
metrics.StatTaskFailureCount.Inc()
return nil, err
}
return resp, nil
}
// LeaveTask makes the peer unschedulable.
func (s *schedulerServerV1) LeaveTask(ctx context.Context, req *schedulerv1.PeerTarget) (*emptypb.Empty, error) {
// Collect LeavePeerCount metrics.
metrics.LeavePeerCount.Inc()
if err := s.service.LeaveTask(ctx, req); err != nil {
// Collect LeavePeerFailureCount metrics.
metrics.LeavePeerFailureCount.Inc()
return nil, err
}
return new(emptypb.Empty), nil
}
// AnnounceHost announces host to scheduler.
func (s *schedulerServerV1) AnnounceHost(ctx context.Context, req *schedulerv1.AnnounceHostRequest) (*emptypb.Empty, error) {
// Collect AnnounceHostCount metrics.
metrics.AnnounceHostCount.WithLabelValues(req.Os, req.Platform, req.PlatformFamily, req.PlatformVersion,
req.KernelVersion, req.Build.GitVersion, req.Build.GitCommit, req.Build.GoVersion, req.Build.Platform).Inc()
if err := s.service.AnnounceHost(ctx, req); err != nil {
// Collect AnnounceHostFailureCount metrics.
metrics.AnnounceHostFailureCount.WithLabelValues(req.Os, req.Platform, req.PlatformFamily, req.PlatformVersion,
req.KernelVersion, req.Build.GitVersion, req.Build.GitCommit, req.Build.GoVersion, req.Build.Platform).Inc()
return nil, err
}
return new(emptypb.Empty), nil
}
// LeaveHost releases host in scheduler.
func (s *schedulerServerV1) LeaveHost(ctx context.Context, req *schedulerv1.LeaveHostRequest) (*emptypb.Empty, error) {
// Collect LeaveHostCount metrics.
metrics.LeaveHostCount.Inc()
if err := s.service.LeaveHost(ctx, req); err != nil {
// Collect LeaveHostFailureCount metrics.
metrics.LeaveHostFailureCount.Inc()
return nil, err
}
return new(emptypb.Empty), nil
}
// SyncProbes sync probes of the host.
func (s *schedulerServerV1) SyncProbes(stream schedulerv1.Scheduler_SyncProbesServer) error {
// Collect SyncProbesCount metrics.
metrics.SyncProbesCount.Inc()
if err := s.service.SyncProbes(stream); err != nil {
// Collect SyncProbesFailureCount metrics.
metrics.SyncProbesFailureCount.Inc()
return err
}
return nil
}