-
Notifications
You must be signed in to change notification settings - Fork 402
/
service.go
139 lines (119 loc) · 3.5 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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package reputation
import (
"context"
"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/multinode/nodes"
"storj.io/storj/private/multinodepb"
)
var (
mon = monkit.Package()
// Error is an error class for reputation service error.
Error = errs.Class("reputation")
// ErrorNoStats is an error class for reputation is not found error.
ErrorNoStats = errs.Class("reputation stats not found")
)
// Service exposes all reputation related logic.
//
// architecture: Service
type Service struct {
log *zap.Logger
dialer rpc.Dialer
nodes nodes.DB
}
// NewService creates new instance of reputation Service.
func NewService(log *zap.Logger, dialer rpc.Dialer, nodes nodes.DB) *Service {
return &Service{
log: log,
dialer: dialer,
nodes: nodes,
}
}
// Stats retrieves node reputation stats list for satellite.
func (service *Service) Stats(ctx context.Context, satelliteID storj.NodeID) (_ []Stats, err error) {
defer mon.Task()(&ctx)(&err)
nodeList, err := service.nodes.List(ctx)
if err != nil {
return nil, Error.Wrap(err)
}
var statsList []Stats
for _, node := range nodeList {
stats, err := service.dialStats(ctx, node, satelliteID)
if err != nil {
if ErrorNoStats.Has(err) {
continue
}
if nodes.ErrNodeNotReachable.Has(err) {
continue
}
return nil, Error.Wrap(err)
}
statsList = append(statsList, stats)
}
return statsList, nil
}
// dialStats dials node and retrieves reputation stats for particular satellite.
func (service *Service) dialStats(ctx context.Context, node nodes.Node, satelliteID storj.NodeID) (_ Stats, 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 Stats{}, nodes.ErrNodeNotReachable.Wrap(err)
}
defer func() {
err = errs.Combine(err, conn.Close())
}()
nodeClient := multinodepb.NewDRPCNodeClient(conn)
req := &multinodepb.ReputationRequest{
Header: &multinodepb.RequestHeader{
ApiKey: node.APISecret,
},
SatelliteId: satelliteID,
}
resp, err := nodeClient.Reputation(ctx, req)
if err != nil {
if rpcstatus.Code(err) == rpcstatus.NotFound {
return Stats{}, ErrorNoStats.New("no stats for %s", satelliteID.String())
}
return Stats{}, Error.Wrap(err)
}
var auditWindows []AuditWindow
for _, window := range resp.Audit.History {
auditWindows = append(auditWindows, AuditWindow{
WindowStart: window.WindowStart,
TotalCount: window.TotalCount,
OnlineCount: window.OnlineCount,
})
}
return Stats{
NodeID: node.ID,
NodeName: node.Name,
Audit: Audit{
TotalCount: resp.Audit.TotalCount,
SuccessCount: resp.Audit.SuccessCount,
Alpha: resp.Audit.Alpha,
Beta: resp.Audit.Beta,
UnknownAlpha: resp.Audit.UnknownAlpha,
UnknownBeta: resp.Audit.UnknownBeta,
Score: resp.Audit.Score,
SuspensionScore: resp.Audit.SuspensionScore,
History: auditWindows,
},
OnlineScore: resp.Online.Score,
DisqualifiedAt: resp.DisqualifiedAt,
SuspendedAt: resp.SuspendedAt,
OfflineSuspendedAt: resp.OfflineSuspendedAt,
OfflineUnderReviewAt: resp.OfflineUnderReviewAt,
VettedAt: resp.VettedAt,
UpdatedAt: resp.UpdatedAt,
JoinedAt: resp.JoinedAt,
}, nil
}