-
Notifications
You must be signed in to change notification settings - Fork 394
/
service.go
401 lines (328 loc) · 10.8 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
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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package nodes
import (
"bytes"
"context"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/rpc"
"storj.io/common/rpc/rpcstatus"
"storj.io/common/storj"
"storj.io/storj/private/multinodepb"
)
var (
mon = monkit.Package()
// Error is an error class for nodes service error.
Error = errs.Class("nodes")
// ErrNodeNotReachable is an error class that indicates that we are not able to establish drpc connection with node.
ErrNodeNotReachable = errs.Class("node is not reachable")
// ErrNodeAPIKeyInvalid is an error class that indicates that we uses wrong api key.
ErrNodeAPIKeyInvalid = errs.Class("node api key is invalid")
)
// Service exposes all nodes related logic.
//
// architecture: Service
type Service struct {
log *zap.Logger
dialer rpc.Dialer
nodes DB
}
// NewService creates new instance of Service.
func NewService(log *zap.Logger, dialer rpc.Dialer, nodes DB) *Service {
return &Service{
log: log,
dialer: dialer,
nodes: nodes,
}
}
// Add adds new node to the system.
func (service *Service) Add(ctx context.Context, node Node) (err error) {
defer mon.Task()(&ctx)(&err)
// trying to connect to node to check its availability.
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Address: node.PublicAddress,
})
if err != nil {
return ErrNodeNotReachable.Wrap(err)
}
defer func() {
err = errs.Combine(err, conn.Close())
}()
nodeClient := multinodepb.NewDRPCNodeClient(conn)
header := &multinodepb.RequestHeader{
ApiKey: node.APISecret[:],
}
// making test request to check node api key.
_, err = nodeClient.Version(ctx, &multinodepb.VersionRequest{Header: header})
if err != nil {
if rpcstatus.Code(err) == rpcstatus.Unauthenticated {
return ErrNodeAPIKeyInvalid.Wrap(err)
}
return Error.Wrap(err)
}
return Error.Wrap(service.nodes.Add(ctx, node))
}
// List returns list of all nodes.
func (service *Service) List(ctx context.Context) (_ []Node, err error) {
defer mon.Task()(&ctx)(&err)
nodes, err := service.nodes.List(ctx)
if err != nil {
return nil, Error.Wrap(err)
}
return nodes, nil
}
// UpdateName will update name of the specified node.
func (service *Service) UpdateName(ctx context.Context, id storj.NodeID, name string) (err error) {
defer mon.Task()(&ctx)(&err)
return Error.Wrap(service.nodes.UpdateName(ctx, id, name))
}
// Get retrieves node by id.
func (service *Service) Get(ctx context.Context, id storj.NodeID) (_ Node, err error) {
defer mon.Task()(&ctx)(&err)
node, err := service.nodes.Get(ctx, id)
if err != nil {
return Node{}, Error.Wrap(err)
}
return node, nil
}
// Remove removes node from the system.
func (service *Service) Remove(ctx context.Context, id storj.NodeID) (err error) {
defer mon.Task()(&ctx)(&err)
return Error.Wrap(service.nodes.Remove(ctx, id))
}
// ListInfos queries node basic info from all nodes via rpc.
func (service *Service) ListInfos(ctx context.Context) (_ []NodeInfo, err error) {
defer mon.Task()(&ctx)(&err)
nodes, err := service.nodes.List(ctx)
if err != nil {
if ErrNoNode.Has(err) {
return []NodeInfo{}, nil
}
return nil, Error.Wrap(err)
}
var infos []NodeInfo
for _, node := range nodes {
info := func() NodeInfo {
nodeInfo := NodeInfo{
ID: node.ID,
Name: node.Name,
}
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Address: node.PublicAddress,
})
if err != nil {
nodeInfo.Status = StatusNotReachable
return nodeInfo
}
defer func() {
err = errs.Combine(err, conn.Close())
}()
nodeClient := multinodepb.NewDRPCNodeClient(conn)
storageClient := multinodepb.NewDRPCStorageClient(conn)
bandwidthClient := multinodepb.NewDRPCBandwidthClient(conn)
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
header := &multinodepb.RequestHeader{
ApiKey: node.APISecret[:],
}
nodeVersion, err := nodeClient.Version(ctx, &multinodepb.VersionRequest{Header: header})
if err != nil {
if rpcstatus.Code(err) == rpcstatus.Unauthenticated {
nodeInfo.Status = StatusUnauthorized
return nodeInfo
}
nodeInfo.Status = StatusStorageNodeInternalError
return nodeInfo
}
lastContact, err := nodeClient.LastContact(ctx, &multinodepb.LastContactRequest{Header: header})
if err != nil {
// TODO: since rpcstatus.Unauthenticated was checked in nodeVersion this sort of error can be caused
// only if new apikey was issued during ListInfos method call.
nodeInfo.Status = StatusStorageNodeInternalError
return nodeInfo
}
diskSpace, err := storageClient.DiskSpace(ctx, &multinodepb.DiskSpaceRequest{Header: header})
if err != nil {
nodeInfo.Status = StatusStorageNodeInternalError
return nodeInfo
}
earned, err := payoutClient.Earned(ctx, &multinodepb.EarnedRequest{Header: header})
if err != nil {
nodeInfo.Status = StatusStorageNodeInternalError
return nodeInfo
}
bandwidthSummaryRequest := &multinodepb.BandwidthMonthSummaryRequest{
Header: header,
}
bandwidthSummary, err := bandwidthClient.MonthSummary(ctx, bandwidthSummaryRequest)
if err != nil {
nodeInfo.Status = StatusStorageNodeInternalError
return nodeInfo
}
nodeInfo.Version = nodeVersion.Version
nodeInfo.LastContact = lastContact.LastContact
nodeInfo.DiskSpaceUsed = diskSpace.GetUsedPieces() + diskSpace.GetUsedTrash()
nodeInfo.DiskSpaceLeft = diskSpace.GetAvailable()
nodeInfo.BandwidthUsed = bandwidthSummary.GetUsed()
nodeInfo.TotalEarned = earned.Total
nodeInfo.Status = nodeStatus(lastContact.LastContact)
return nodeInfo
}()
infos = append(infos, info)
}
return infos, nil
}
// ListInfosSatellite queries node satellite specific info from all nodes via rpc.
func (service *Service) ListInfosSatellite(ctx context.Context, satelliteID storj.NodeID) (_ []NodeInfoSatellite, err error) {
defer mon.Task()(&ctx)(&err)
nodes, err := service.nodes.List(ctx)
if err != nil {
if ErrNoNode.Has(err) {
return []NodeInfoSatellite{}, nil
}
return nil, Error.Wrap(err)
}
var infos []NodeInfoSatellite
for _, node := range nodes {
info := func() NodeInfoSatellite {
nodeInfoSatellite := NodeInfoSatellite{
ID: node.ID,
Name: node.Name,
}
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Address: node.PublicAddress,
})
if err != nil {
nodeInfoSatellite.Status = StatusNotReachable
return nodeInfoSatellite
}
defer func() {
err = errs.Combine(err, conn.Close())
}()
nodeClient := multinodepb.NewDRPCNodeClient(conn)
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
header := &multinodepb.RequestHeader{
ApiKey: node.APISecret[:],
}
nodeVersion, err := nodeClient.Version(ctx, &multinodepb.VersionRequest{Header: header})
if err != nil {
if rpcstatus.Code(err) == rpcstatus.Unauthenticated {
nodeInfoSatellite.Status = StatusUnauthorized
return nodeInfoSatellite
}
nodeInfoSatellite.Status = StatusStorageNodeInternalError
return nodeInfoSatellite
}
lastContact, err := nodeClient.LastContact(ctx, &multinodepb.LastContactRequest{Header: header})
if err != nil {
// TODO: since rpcstatus.Unauthenticated was checked in Version this sort of error can be caused
// only if new apikey was issued during ListInfosSatellite method call.
nodeInfoSatellite.Status = StatusStorageNodeInternalError
return nodeInfoSatellite
}
rep, err := nodeClient.Reputation(ctx, &multinodepb.ReputationRequest{
Header: header,
SatelliteId: satelliteID,
})
if err != nil {
nodeInfoSatellite.Status = StatusStorageNodeInternalError
return nodeInfoSatellite
}
earned, err := payoutClient.Earned(ctx, &multinodepb.EarnedRequest{Header: header})
if err != nil {
nodeInfoSatellite.Status = StatusStorageNodeInternalError
return nodeInfoSatellite
}
nodeInfoSatellite.Version = nodeVersion.Version
nodeInfoSatellite.LastContact = lastContact.LastContact
nodeInfoSatellite.OnlineScore = rep.Online.Score
nodeInfoSatellite.AuditScore = rep.Audit.Score
nodeInfoSatellite.SuspensionScore = rep.Audit.SuspensionScore
nodeInfoSatellite.TotalEarned = earned.Total
nodeInfoSatellite.Status = nodeStatus(lastContact.LastContact)
return nodeInfoSatellite
}()
infos = append(infos, info)
}
return infos, nil
}
// TrustedSatellites returns list of unique trusted satellites node urls.
func (service *Service) TrustedSatellites(ctx context.Context) (_ storj.NodeURLs, err error) {
defer mon.Task()(&ctx)(&err)
listNodes, err := service.nodes.List(ctx)
if err != nil {
return nil, Error.Wrap(err)
}
var trustedSatellites storj.NodeURLs
for _, node := range listNodes {
nodeURLs, err := service.trustedSatellites(ctx, node)
if err != nil {
if ErrNodeNotReachable.Has(err) {
continue
}
return nil, Error.Wrap(err)
}
trustedSatellites = appendUniqueNodeURLs(trustedSatellites, nodeURLs)
}
return trustedSatellites, nil
}
// trustedSatellites retrieves list of trusted satellites node urls for a node.
func (service *Service) trustedSatellites(ctx context.Context, node Node) (_ storj.NodeURLs, err error) {
defer mon.Task()(&ctx)(&err)
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Address: node.PublicAddress,
})
if err != nil {
return storj.NodeURLs{}, ErrNodeNotReachable.Wrap(err)
}
defer func() {
err = errs.Combine(err, conn.Close())
}()
nodeClient := multinodepb.NewDRPCNodeClient(conn)
header := &multinodepb.RequestHeader{
ApiKey: node.APISecret[:],
}
resp, err := nodeClient.TrustedSatellites(ctx, &multinodepb.TrustedSatellitesRequest{Header: header})
if err != nil {
return nil, Error.Wrap(err)
}
var nodeURLs storj.NodeURLs
for _, url := range resp.TrustedSatellites {
nodeURLs = append(nodeURLs, storj.NodeURL{
ID: url.NodeId,
Address: url.GetAddress(),
})
}
return nodeURLs, nil
}
// nodeStatus chooses node status offline or online depends on LastContact.
func nodeStatus(lastContact time.Time) Status {
now := time.Now().UTC()
if now.Sub(lastContact) < time.Hour*3 {
return StatusOnline
}
return StatusOffline
}
// appendUniqueNodeURLs appends unique node urls from incoming slice.
func appendUniqueNodeURLs(slice storj.NodeURLs, nodeURLs storj.NodeURLs) storj.NodeURLs {
for _, nodeURL := range nodeURLs {
slice = appendUniqueNodeURL(slice, nodeURL)
}
return slice
}
// appendUniqueNodeURL appends node url if it is unique.
func appendUniqueNodeURL(slice storj.NodeURLs, nodeURL storj.NodeURL) storj.NodeURLs {
for _, existing := range slice {
if bytes.Equal(existing.ID.Bytes(), nodeURL.ID.Bytes()) {
return slice
}
}
slice = append(slice, nodeURL)
return slice
}