-
Notifications
You must be signed in to change notification settings - Fork 390
/
service.go
335 lines (296 loc) · 11.4 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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package reputation
import (
"context"
"time"
"go.uber.org/zap"
"storj.io/common/pb"
"storj.io/common/storj"
"storj.io/storj/satellite/nodeevents"
"storj.io/storj/satellite/overlay"
)
// DB is an interface for storing reputation data.
type DB interface {
Update(ctx context.Context, request UpdateRequest, now time.Time) (_ *Info, err error)
Get(ctx context.Context, nodeID storj.NodeID) (*Info, error)
// ApplyUpdates applies multiple updates (defined by the updates
// parameter) to a node's reputations record.
ApplyUpdates(ctx context.Context, nodeID storj.NodeID, updates Mutations, reputationConfig Config, now time.Time) (_ *Info, err error)
// UnsuspendNodeUnknownAudit unsuspends a storage node for unknown audits.
UnsuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID) (err error)
// DisqualifyNode disqualifies a storage node.
DisqualifyNode(ctx context.Context, nodeID storj.NodeID, disqualifiedAt time.Time, reason overlay.DisqualificationReason) (err error)
// SuspendNodeUnknownAudit suspends a storage node for unknown audits.
SuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID, suspendedAt time.Time) (err error)
}
// Info contains all reputation data to be stored in DB.
type Info struct {
AuditSuccessCount int64
TotalAuditCount int64
VettedAt *time.Time
UnknownAuditSuspended *time.Time
OfflineSuspended *time.Time
UnderReview *time.Time
Disqualified *time.Time
DisqualificationReason overlay.DisqualificationReason
OnlineScore float64
AuditHistory *pb.AuditHistory
AuditReputationAlpha float64
AuditReputationBeta float64
UnknownAuditReputationAlpha float64
UnknownAuditReputationBeta float64
}
// Copy creates a deep copy of the Info object.
func (i *Info) Copy() *Info {
i2 := *i
i2.VettedAt = cloneTime(i.VettedAt)
i2.UnknownAuditSuspended = cloneTime(i.UnknownAuditSuspended)
i2.OfflineSuspended = cloneTime(i.OfflineSuspended)
i2.UnderReview = cloneTime(i.UnderReview)
i2.Disqualified = cloneTime(i.Disqualified)
if i.AuditHistory != nil {
i2.AuditHistory = &pb.AuditHistory{
Score: i.AuditHistory.Score,
Windows: make([]*pb.AuditWindow, len(i.AuditHistory.Windows)),
}
for i, window := range i.AuditHistory.Windows {
w := *window
i2.AuditHistory.Windows[i] = &w
}
}
return &i2
}
// Mutations represents changes which should be made to a particular node's
// reputation, in terms of counts and/or timestamps of events which have
// occurred. A Mutations record can be applied to a reputations row without
// prior knowledge of that row's contents.
type Mutations struct {
PositiveResults int
FailureResults int
UnknownResults int
OfflineResults int
OnlineHistory *pb.AuditHistory
}
// Service handles storing node reputation data and updating
// the overlay cache when a node's status changes.
type Service struct {
log *zap.Logger
overlay *overlay.Service
db DB
config Config
}
// NewService creates a new reputation service.
func NewService(log *zap.Logger, overlay *overlay.Service, db DB, config Config) *Service {
return &Service{
log: log,
overlay: overlay,
db: db,
config: config,
}
}
// ApplyAudit receives an audit result and applies it to the relevant node in DB.
func (service *Service) ApplyAudit(ctx context.Context, nodeID storj.NodeID, reputation overlay.ReputationStatus, result AuditType) (err error) {
defer mon.Task()(&ctx)(&err)
// There are some cases where the caller did not get updated reputation-status information.
// (Usually this means the node was offline, disqualified, or exited and we skipped creating an order limit for it.)
var nodeExited bool
if reputation.Email == "" {
dossier, err := service.overlay.Get(ctx, nodeID)
if err != nil {
return err
}
reputation = dossier.Reputation.Status
if dossier.ExitStatus.ExitFinishedAt != nil {
nodeExited = true
}
}
// If the node is disqualified or exited, we do not need to apply the audit, so return nil.
if reputation.Disqualified != nil || nodeExited {
return nil
}
now := time.Now()
statusUpdate, err := service.db.Update(ctx, UpdateRequest{
NodeID: nodeID,
AuditOutcome: result,
Config: service.config,
}, now)
if err != nil {
return err
}
// only update node if its health status has changed, or it's a newly vetted
// node.
// this prevents the need to require caller of ApplyAudit() to always know
// the previous VettedAt time for a node.
// Due to inconsistencies in the precision of time.Now() on different platforms and databases, the time comparison
// for the VettedAt status is done using time values that are truncated to second precision.
changed, repChanges := hasReputationChanged(*statusUpdate, reputation, now)
if changed {
reputationUpdate := &overlay.ReputationUpdate{
Disqualified: statusUpdate.Disqualified,
DisqualificationReason: statusUpdate.DisqualificationReason,
UnknownAuditSuspended: statusUpdate.UnknownAuditSuspended,
OfflineSuspended: statusUpdate.OfflineSuspended,
VettedAt: statusUpdate.VettedAt,
}
err = service.overlay.UpdateReputation(ctx, nodeID, reputation.Email, *reputationUpdate, repChanges)
if err != nil {
return err
}
}
return err
}
// Get returns a node's reputation info from DB.
// If a node is not found in the DB, default reputation information is returned.
func (service *Service) Get(ctx context.Context, nodeID storj.NodeID) (info *Info, err error) {
defer mon.Task()(&ctx)(&err)
info, err = service.db.Get(ctx, nodeID)
if err != nil {
if ErrNodeNotFound.Has(err) {
// if there is no audit reputation for the node, that's fine and we
// return default reputation values
info = &Info{
UnknownAuditReputationAlpha: 1,
AuditReputationAlpha: service.config.InitialAlpha,
AuditReputationBeta: service.config.InitialBeta,
OnlineScore: 1,
}
return info, nil
}
return nil, Error.Wrap(err)
}
return info, nil
}
// TestSuspendNodeUnknownAudit suspends a storage node for unknown audits.
func (service *Service) TestSuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID, suspendedAt time.Time) (err error) {
err = service.db.SuspendNodeUnknownAudit(ctx, nodeID, suspendedAt)
if err != nil {
return err
}
n, err := service.overlay.Get(ctx, nodeID)
if err != nil {
return err
}
update := overlay.ReputationUpdate{
Disqualified: n.Disqualified,
UnknownAuditSuspended: &suspendedAt,
OfflineSuspended: n.OfflineSuspended,
VettedAt: n.Reputation.Status.VettedAt,
}
if n.DisqualificationReason != nil {
update.DisqualificationReason = *n.DisqualificationReason
}
return service.overlay.UpdateReputation(ctx, nodeID, "", update, []nodeevents.Type{nodeevents.UnknownAuditSuspended})
}
// TestDisqualifyNode disqualifies a storage node.
func (service *Service) TestDisqualifyNode(ctx context.Context, nodeID storj.NodeID, reason overlay.DisqualificationReason) (err error) {
disqualifiedAt := time.Now()
err = service.db.DisqualifyNode(ctx, nodeID, disqualifiedAt, reason)
if err != nil {
return err
}
return service.overlay.DisqualifyNode(ctx, nodeID, reason)
}
// TestUnsuspendNodeUnknownAudit unsuspends a storage node for unknown audits.
func (service *Service) TestUnsuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID) (err error) {
err = service.db.UnsuspendNodeUnknownAudit(ctx, nodeID)
if err != nil {
return err
}
n, err := service.overlay.Get(ctx, nodeID)
if err != nil {
return err
}
update := overlay.ReputationUpdate{
Disqualified: n.Disqualified,
UnknownAuditSuspended: nil,
OfflineSuspended: n.OfflineSuspended,
VettedAt: n.Reputation.Status.VettedAt,
}
if n.DisqualificationReason != nil {
update.DisqualificationReason = *n.DisqualificationReason
}
return service.overlay.UpdateReputation(ctx, nodeID, "", update, []nodeevents.Type{nodeevents.UnknownAuditUnsuspended})
}
// TestFlushAllNodeInfo flushes any and all cached information about all
// nodes to the backing store, if the attached reputationDB does any caching
// at all.
func (service *Service) TestFlushAllNodeInfo(ctx context.Context) (err error) {
if db, ok := service.db.(*CachingDB); ok {
return db.FlushAll(ctx)
}
return nil
}
// FlushNodeInfo flushes any cached information about the specified node to
// the backing store, if the attached reputationDB does any caching at all.
func (service *Service) FlushNodeInfo(ctx context.Context, nodeID storj.NodeID) (err error) {
if db, ok := service.db.(*CachingDB); ok {
return db.RequestSync(ctx, nodeID)
}
return nil
}
// Close closes resources.
func (service *Service) Close() error { return nil }
// hasReputationChanged determines if the current node reputation is different from the newly updated reputation. This
// function will only consider the Disqualified, UnknownAudiSuspended and OfflineSuspended statuses for changes.
func hasReputationChanged(updated Info, current overlay.ReputationStatus, now time.Time) (changed bool, repChanges []nodeevents.Type) {
// there is no unDQ, so only update if changed from nil to not nil
if current.Disqualified == nil && updated.Disqualified != nil {
repChanges = append(repChanges, nodeevents.Disqualified)
changed = true
}
if statusChanged(current.UnknownAuditSuspended, updated.UnknownAuditSuspended) {
if updated.UnknownAuditSuspended != nil {
repChanges = append(repChanges, nodeevents.UnknownAuditSuspended)
} else {
repChanges = append(repChanges, nodeevents.UnknownAuditUnsuspended)
}
changed = true
}
if statusChanged(current.OfflineSuspended, updated.OfflineSuspended) {
if updated.OfflineSuspended != nil {
repChanges = append(repChanges, nodeevents.OfflineSuspended)
} else {
repChanges = append(repChanges, nodeevents.OfflineUnsuspended)
}
changed = true
}
// check for newly vetted nodes.
// Due to inconsistencies in the precision of time.Now() on different platforms and databases, the time comparison
// for the VettedAt status is done using time values that are truncated to second precision.
if updated.VettedAt != nil && updated.VettedAt.Truncate(time.Second).Equal(now.Truncate(time.Second)) {
changed = true
}
return changed, repChanges
}
// statusChanged determines if the two given statuses are different.
// a status is considered "different" if it went from nil to not-nil, or not-nil to nil.
// if not-nil and the only difference is the time, this is considered "not changed".
func statusChanged(s1, s2 *time.Time) bool {
return (s1 == nil && s2 != nil) || (s1 != nil && s2 == nil)
}
// UpdateRequestToMutations transforms an UpdateRequest into the equivalent
// Mutations structure, which can be used with ApplyUpdates.
func UpdateRequestToMutations(updateReq UpdateRequest, now time.Time) (Mutations, error) {
updates := Mutations{}
switch updateReq.AuditOutcome {
case AuditSuccess:
updates.PositiveResults = 1
case AuditFailure:
updates.FailureResults = 1
case AuditUnknown:
updates.UnknownResults = 1
case AuditOffline:
updates.OfflineResults = 1
}
updates.OnlineHistory = &pb.AuditHistory{}
err := AddAuditToHistory(updates.OnlineHistory, updateReq.AuditOutcome != AuditOffline, now, updateReq.Config.AuditHistory)
return updates, err
}
func cloneTime(t *time.Time) *time.Time {
if t == nil {
return nil
}
tCopy := *t
return &tCopy
}