-
Notifications
You must be signed in to change notification settings - Fork 279
/
client_v1.go
332 lines (281 loc) · 10.6 KB
/
client_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
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
/*
* 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/client_v1_mock.go -source client_v1.go -package mocks
package client
import (
"context"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer"
commonv1 "d7y.io/api/pkg/apis/common/v1"
schedulerv1 "d7y.io/api/pkg/apis/scheduler/v1"
"d7y.io/dragonfly/v2/client/config"
logger "d7y.io/dragonfly/v2/internal/dflog"
pkgbalancer "d7y.io/dragonfly/v2/pkg/balancer"
"d7y.io/dragonfly/v2/pkg/resolver"
"d7y.io/dragonfly/v2/pkg/rpc"
"d7y.io/dragonfly/v2/pkg/rpc/common"
)
// GetV1 returns v1 version of the scheduler client.
func GetV1(ctx context.Context, dynconfig config.Dynconfig, opts ...grpc.DialOption) (V1, error) {
// Register resolver and balancer.
resolver.RegisterScheduler(dynconfig)
builder, pickerBuilder := pkgbalancer.NewConsistentHashingBuilder()
balancer.Register(builder)
conn, err := grpc.DialContext(
ctx,
resolver.SchedulerVirtualTarget,
append([]grpc.DialOption{
grpc.WithDefaultServiceConfig(pkgbalancer.BalancerServiceConfig),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
rpc.ConvertErrorUnaryClientInterceptor,
otelgrpc.UnaryClientInterceptor(),
grpc_prometheus.UnaryClientInterceptor,
grpc_zap.UnaryClientInterceptor(logger.GrpcLogger.Desugar()),
grpc_retry.UnaryClientInterceptor(
grpc_retry.WithMax(maxRetries),
grpc_retry.WithBackoff(grpc_retry.BackoffLinear(backoffWaitBetween)),
),
rpc.RefresherUnaryClientInterceptor(dynconfig),
)),
grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
rpc.ConvertErrorStreamClientInterceptor,
otelgrpc.StreamClientInterceptor(),
grpc_prometheus.StreamClientInterceptor,
grpc_zap.StreamClientInterceptor(logger.GrpcLogger.Desugar()),
rpc.RefresherStreamClientInterceptor(dynconfig),
)),
}, opts...)...,
)
if err != nil {
return nil, err
}
return &v1{
SchedulerClient: schedulerv1.NewSchedulerClient(conn),
ClientConn: conn,
Dynconfig: dynconfig,
dialOptions: opts,
ConsistentHashingPickerBuilder: pickerBuilder,
}, nil
}
// GetV1ByAddr returns v2 version of the scheduler client by address.
func GetV1ByAddr(ctx context.Context, target string, opts ...grpc.DialOption) (V1, error) {
conn, err := grpc.DialContext(
ctx,
target,
append([]grpc.DialOption{
grpc.WithDefaultServiceConfig(pkgbalancer.BalancerServiceConfig),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
rpc.ConvertErrorUnaryClientInterceptor,
otelgrpc.UnaryClientInterceptor(),
grpc_prometheus.UnaryClientInterceptor,
grpc_zap.UnaryClientInterceptor(logger.GrpcLogger.Desugar()),
grpc_retry.UnaryClientInterceptor(
grpc_retry.WithMax(maxRetries),
grpc_retry.WithBackoff(grpc_retry.BackoffLinear(backoffWaitBetween)),
),
)),
grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
rpc.ConvertErrorStreamClientInterceptor,
otelgrpc.StreamClientInterceptor(),
grpc_prometheus.StreamClientInterceptor,
grpc_zap.StreamClientInterceptor(logger.GrpcLogger.Desugar()),
)),
}, opts...)...,
)
if err != nil {
return nil, err
}
return &v1{
SchedulerClient: schedulerv1.NewSchedulerClient(conn),
ClientConn: conn,
dialOptions: opts,
}, nil
}
// V1 is the interface for v1 version of the grpc client.
type V1 interface {
// RegisterPeerTask registers a peer into task.
RegisterPeerTask(context.Context, *schedulerv1.PeerTaskRequest, ...grpc.CallOption) (*schedulerv1.RegisterResult, error)
// ReportPieceResult reports piece results and receives peer packets.
ReportPieceResult(context.Context, *schedulerv1.PeerTaskRequest, ...grpc.CallOption) (schedulerv1.Scheduler_ReportPieceResultClient, error)
// ReportPeerResult reports downloading result for the peer.
ReportPeerResult(context.Context, *schedulerv1.PeerResult, ...grpc.CallOption) error
// A peer announces that it has the announced task to other peers.
AnnounceTask(context.Context, *schedulerv1.AnnounceTaskRequest, ...grpc.CallOption) error
// Checks if any peer has the given task.
StatTask(context.Context, *schedulerv1.StatTaskRequest, ...grpc.CallOption) (*schedulerv1.Task, error)
// LeaveTask releases peer in scheduler.
LeaveTask(context.Context, *schedulerv1.PeerTarget, ...grpc.CallOption) error
// AnnounceHost announces host to scheduler.
AnnounceHost(context.Context, *schedulerv1.AnnounceHostRequest, ...grpc.CallOption) error
// LeaveHost releases host in scheduler.
LeaveHost(context.Context, *schedulerv1.LeaveHostRequest, ...grpc.CallOption) error
// SyncProbes sync probes of the host.
SyncProbes(context.Context, *schedulerv1.SyncProbesRequest, ...grpc.CallOption) (schedulerv1.Scheduler_SyncProbesClient, error)
// SyncNetworkTopology sync network topology of the hosts.
SyncNetworkTopology(context.Context, *schedulerv1.SyncNetworkTopologyRequest, ...grpc.CallOption) (schedulerv1.Scheduler_SyncNetworkTopologyClient, error)
// Close tears down the ClientConn and all underlying connections.
Close() error
}
// v1 provides v1 version of the scheduler grpc function.
type v1 struct {
schedulerv1.SchedulerClient
*grpc.ClientConn
config.Dynconfig
dialOptions []grpc.DialOption
*pkgbalancer.ConsistentHashingPickerBuilder
}
// RegisterPeerTask registers a peer into task.
func (v *v1) RegisterPeerTask(ctx context.Context, req *schedulerv1.PeerTaskRequest, opts ...grpc.CallOption) (*schedulerv1.RegisterResult, error) {
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
return v.SchedulerClient.RegisterPeerTask(
context.WithValue(ctx, pkgbalancer.ContextKey, req.TaskId),
req,
opts...,
)
}
// ReportPieceResult reports piece results and receives peer packets.
func (v *v1) ReportPieceResult(ctx context.Context, req *schedulerv1.PeerTaskRequest, opts ...grpc.CallOption) (schedulerv1.Scheduler_ReportPieceResultClient, error) {
stream, err := v.SchedulerClient.ReportPieceResult(
context.WithValue(ctx, pkgbalancer.ContextKey, req.TaskId),
opts...,
)
if err != nil {
return nil, err
}
// Send begin of piece.
return stream, stream.Send(&schedulerv1.PieceResult{
TaskId: req.TaskId,
SrcPid: req.PeerId,
PieceInfo: &commonv1.PieceInfo{
PieceNum: common.BeginOfPiece,
},
})
}
// ReportPeerResult reports downloading result for the peer.
func (v *v1) ReportPeerResult(ctx context.Context, req *schedulerv1.PeerResult, opts ...grpc.CallOption) error {
_, err := v.SchedulerClient.ReportPeerResult(
context.WithValue(ctx, pkgbalancer.ContextKey, req.TaskId),
req,
opts...,
)
return err
}
// A peer announces that it has the announced task to other peers.
func (v *v1) AnnounceTask(ctx context.Context, req *schedulerv1.AnnounceTaskRequest, opts ...grpc.CallOption) error {
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
_, err := v.SchedulerClient.AnnounceTask(
context.WithValue(ctx, pkgbalancer.ContextKey, req.TaskId),
req,
opts...,
)
return err
}
// Checks if any peer has the given task.
func (v *v1) StatTask(ctx context.Context, req *schedulerv1.StatTaskRequest, opts ...grpc.CallOption) (*schedulerv1.Task, error) {
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
return v.SchedulerClient.StatTask(
context.WithValue(ctx, pkgbalancer.ContextKey, req.TaskId),
req,
opts...,
)
}
// LeaveTask releases peer in scheduler.
func (v *v1) LeaveTask(ctx context.Context, req *schedulerv1.PeerTarget, opts ...grpc.CallOption) error {
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
_, err := v.SchedulerClient.LeaveTask(
context.WithValue(ctx, pkgbalancer.ContextKey, req.TaskId),
req,
opts...,
)
return err
}
// AnnounceHost announces host to scheduler.
func (v *v1) AnnounceHost(ctx context.Context, req *schedulerv1.AnnounceHostRequest, opts ...grpc.CallOption) error {
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
circle, err := v.GetCircle()
if err != nil {
return err
}
eg, _ := errgroup.WithContext(ctx)
for _, virtualTaskID := range circle {
virtualTaskID := virtualTaskID
eg.Go(func() error {
if _, err := v.SchedulerClient.AnnounceHost(
context.WithValue(ctx, pkgbalancer.ContextKey, virtualTaskID),
req,
opts...,
); err != nil {
return err
}
return nil
})
}
return eg.Wait()
}
// LeaveHost releases host in all schedulers.
func (v *v1) LeaveHost(ctx context.Context, req *schedulerv1.LeaveHostRequest, opts ...grpc.CallOption) error {
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
circle, err := v.GetCircle()
if err != nil {
return err
}
logger.Infof("leave host circle is %#v", circle)
eg, _ := errgroup.WithContext(ctx)
for _, virtualTaskID := range circle {
virtualTaskID := virtualTaskID
eg.Go(func() error {
if _, err := v.SchedulerClient.LeaveHost(
context.WithValue(ctx, pkgbalancer.ContextKey, virtualTaskID),
req,
opts...,
); err != nil {
return err
}
return nil
})
}
return eg.Wait()
}
// SyncProbes sync probes of the host.
func (v *v1) SyncProbes(ctx context.Context, req *schedulerv1.SyncProbesRequest, opts ...grpc.CallOption) (schedulerv1.Scheduler_SyncProbesClient, error) {
stream, err := v.SchedulerClient.SyncProbes(ctx, opts...)
if err != nil {
return nil, err
}
// Send begin of piece.
return stream, stream.Send(req)
}
// SyncNetworkTopology sync network topology of the hosts.
func (v *v1) SyncNetworkTopology(ctx context.Context, req *schedulerv1.SyncNetworkTopologyRequest, opts ...grpc.CallOption) (schedulerv1.Scheduler_SyncNetworkTopologyClient, error) {
stream, err := v.SchedulerClient.SyncNetworkTopology(ctx, opts...)
if err != nil {
return nil, err
}
// Send begin of piece.
return stream, stream.Send(req)
}