-
Notifications
You must be signed in to change notification settings - Fork 280
/
client.go
199 lines (169 loc) · 6.5 KB
/
client.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
/*
* 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 client
import (
"context"
"fmt"
"sync"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/dfnet"
"d7y.io/dragonfly/v2/pkg/idgen"
"d7y.io/dragonfly/v2/pkg/rpc"
"d7y.io/dragonfly/v2/pkg/rpc/base"
"d7y.io/dragonfly/v2/pkg/rpc/dfdaemon"
)
var _ DaemonClient = (*daemonClient)(nil)
func GetClientByAddr(addrs []dfnet.NetAddr, opts ...grpc.DialOption) (DaemonClient, error) {
if len(addrs) == 0 {
return nil, errors.New("address list of daemon is empty")
}
dc := &daemonClient{
rpc.NewConnection(context.Background(), "daemon-static", addrs, []rpc.ConnOption{
rpc.WithConnExpireTime(60 * time.Second),
rpc.WithDialOption(opts),
}),
}
return dc, nil
}
var once sync.Once
var elasticDaemonClient *daemonClient
func GetElasticClientByAddrs(addrs []dfnet.NetAddr, opts ...grpc.DialOption) (DaemonClient, error) {
once.Do(func() {
elasticDaemonClient = &daemonClient{
rpc.NewConnection(context.Background(), "daemon-elastic", make([]dfnet.NetAddr, 0), []rpc.ConnOption{
rpc.WithConnExpireTime(60 * time.Second),
rpc.WithDialOption(opts),
}),
}
})
err := elasticDaemonClient.Connection.AddServerNodes(addrs)
if err != nil {
return nil, err
}
return elasticDaemonClient, nil
}
// DaemonClient see dfdaemon.DaemonClient
type DaemonClient interface {
Download(ctx context.Context, req *dfdaemon.DownRequest, opts ...grpc.CallOption) (*DownResultStream, error)
GetPieceTasks(ctx context.Context, addr dfnet.NetAddr, ptr *base.PieceTaskRequest, opts ...grpc.CallOption) (*base.PiecePacket, error)
SyncPieceTasks(ctx context.Context, addr dfnet.NetAddr, ptr *base.PieceTaskRequest, opts ...grpc.CallOption) (dfdaemon.Daemon_SyncPieceTasksClient, error)
CheckHealth(ctx context.Context, target dfnet.NetAddr, opts ...grpc.CallOption) error
StatTask(ctx context.Context, req *dfdaemon.StatTaskRequest, opts ...grpc.CallOption) error
ImportTask(ctx context.Context, req *dfdaemon.ImportTaskRequest, opts ...grpc.CallOption) error
ExportTask(ctx context.Context, req *dfdaemon.ExportTaskRequest, opts ...grpc.CallOption) error
DeleteTask(ctx context.Context, req *dfdaemon.DeleteTaskRequest, opts ...grpc.CallOption) error
Close() error
}
type daemonClient struct {
*rpc.Connection
}
func (dc *daemonClient) getDaemonClient(key string, stick bool) (dfdaemon.DaemonClient, string, error) {
clientConn, err := dc.Connection.GetClientConn(key, stick)
if err != nil {
return nil, "", err
}
return dfdaemon.NewDaemonClient(clientConn), clientConn.Target(), nil
}
func (dc *daemonClient) getDaemonClientWithTarget(target string) (dfdaemon.DaemonClient, error) {
conn, err := dc.Connection.GetClientConnByTarget(target)
if err != nil {
return nil, err
}
return dfdaemon.NewDaemonClient(conn), nil
}
func (dc *daemonClient) Download(ctx context.Context, req *dfdaemon.DownRequest, opts ...grpc.CallOption) (*DownResultStream, error) {
req.Uuid = uuid.New().String()
// generate taskID
taskID := idgen.TaskID(req.Url, req.UrlMeta)
return newDownResultStream(ctx, dc, taskID, req, opts)
}
func (dc *daemonClient) GetPieceTasks(ctx context.Context, target dfnet.NetAddr, ptr *base.PieceTaskRequest, opts ...grpc.CallOption) (*base.PiecePacket,
error) {
client, err := dc.getDaemonClientWithTarget(target.GetEndpoint())
if err != nil {
return nil, err
}
return client.GetPieceTasks(ctx, ptr, opts...)
}
func (dc *daemonClient) SyncPieceTasks(ctx context.Context, target dfnet.NetAddr, ptr *base.PieceTaskRequest, opts ...grpc.CallOption) (dfdaemon.Daemon_SyncPieceTasksClient, error) {
client, err := dc.getDaemonClientWithTarget(target.GetEndpoint())
if err != nil {
return nil, err
}
syncClient, err := client.SyncPieceTasks(ctx, opts...)
if err != nil {
logger.WithTaskID(ptr.TaskId).Infof("SyncPieceTasks: invoke daemon node %s SyncPieceTasks failed: %v", target, err)
return nil, err
}
return syncClient, syncClient.Send(ptr)
}
func (dc *daemonClient) CheckHealth(ctx context.Context, target dfnet.NetAddr, opts ...grpc.CallOption) (err error) {
_, err = rpc.ExecuteWithRetry(func() (interface{}, error) {
client, err := dc.getDaemonClientWithTarget(target.GetEndpoint())
if err != nil {
return nil, fmt.Errorf("failed to connect server %s: %v", target.GetEndpoint(), err)
}
return client.CheckHealth(ctx, new(emptypb.Empty), opts...)
}, 0.2, 2.0, 3, nil)
if err != nil {
logger.Infof("CheckHealth: invoke daemon node %s CheckHealth failed: %v", target, err)
return
}
return
}
func (dc *daemonClient) StatTask(ctx context.Context, req *dfdaemon.StatTaskRequest, opts ...grpc.CallOption) error {
// StatTask is a latency sensitive operation, so we don't retry & wait for daemon to start,
// we assume daemon is already running.
taskID := idgen.TaskID(req.Cid, req.UrlMeta)
client, _, err := dc.getDaemonClient(taskID, false)
if err != nil {
return err
}
_, err = client.StatTask(ctx, req, opts...)
return err
}
func (dc *daemonClient) ImportTask(ctx context.Context, req *dfdaemon.ImportTaskRequest, opts ...grpc.CallOption) error {
taskID := idgen.TaskID(req.Cid, req.UrlMeta)
client, _, err := dc.getDaemonClient(taskID, false)
if err != nil {
return err
}
_, err = client.ImportTask(ctx, req, opts...)
return err
}
func (dc *daemonClient) ExportTask(ctx context.Context, req *dfdaemon.ExportTaskRequest, opts ...grpc.CallOption) error {
taskID := idgen.TaskID(req.Cid, req.UrlMeta)
client, _, err := dc.getDaemonClient(taskID, false)
if err != nil {
return err
}
_, err = client.ExportTask(ctx, req, opts...)
return err
}
func (dc *daemonClient) DeleteTask(ctx context.Context, req *dfdaemon.DeleteTaskRequest, opts ...grpc.CallOption) error {
taskID := idgen.TaskID(req.Cid, req.UrlMeta)
client, _, err := dc.getDaemonClient(taskID, false)
if err != nil {
return err
}
_, err = client.DeleteTask(ctx, req, opts...)
return err
}