-
Notifications
You must be signed in to change notification settings - Fork 402
/
verify.go
406 lines (341 loc) · 12.8 KB
/
verify.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
402
403
404
405
406
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"errors"
"io"
"time"
"github.com/blang/semver"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/errs2"
"storj.io/common/pb"
"storj.io/common/rpc"
"storj.io/common/rpc/rpcpool"
"storj.io/common/rpc/rpcstatus"
"storj.io/common/storj"
"storj.io/common/sync2"
"storj.io/storj/satellite/audit"
"storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/orders"
"storj.io/uplink/private/piecestore"
)
// ErrNodeOffline is returned when it was not possible to contact a node or the node was not responding.
var ErrNodeOffline = errs.Class("node offline")
// ErrNoSuchNode is returned when a node has been disqualified or gracefully exited, or no known node
// has the specified alias. Pieces associated with the given alias should be considered lost.
var ErrNoSuchNode = errs.Class("no such node")
var errWrongNodeVersion = errs.Class("wrong node version")
// VerifierConfig contains configurations for operation.
type VerifierConfig struct {
DialTimeout time.Duration `help:"how long to wait for a successful dial" default:"2s"`
PerPieceTimeout time.Duration `help:"duration to wait per piece download" default:"800ms"`
OrderRetryThrottle time.Duration `help:"how much to wait before retrying order creation" default:"50ms"`
RequestThrottle time.Duration `help:"minimum interval for sending out each request" default:"150ms"`
VersionWithExists string `help:"minimum storage node version with implemented Exists method" default:"v1.69.2"`
}
// NodeVerifier implements segment verification by dialing nodes.
type NodeVerifier struct {
log *zap.Logger
config VerifierConfig
dialer rpc.Dialer
orders *orders.Service
// this is a callback so that problematic pieces can be reported as they are found,
// rather than being kept in a list which might grow unreasonably large.
reportPiece pieceReporterFunc
versionWithExists semver.Version
}
var _ Verifier = (*NodeVerifier)(nil)
// NewVerifier creates a new segment verifier using the specified dialer.
func NewVerifier(log *zap.Logger, dialer rpc.Dialer, orders *orders.Service, config VerifierConfig) *NodeVerifier {
configuredDialer := dialer
if config.DialTimeout > 0 {
configuredDialer.DialTimeout = config.DialTimeout
}
configuredDialer.Pool = rpcpool.New(rpcpool.Options{
Capacity: 1000,
KeyCapacity: 5,
IdleExpiration: 10 * time.Minute,
})
version, err := semver.ParseTolerant(config.VersionWithExists)
if err != nil {
log.Warn("invalid VersionWithExists", zap.String("VersionWithExists", config.VersionWithExists), zap.Error(err))
}
return &NodeVerifier{
log: log,
config: config,
dialer: configuredDialer,
orders: orders,
versionWithExists: version,
}
}
// Verify a collection of segments by attempting to download a byte from each segment from the target node.
func (service *NodeVerifier) Verify(ctx context.Context, alias metabase.NodeAlias, target storj.NodeURL, targetVersion string, segments []*Segment, ignoreThrottle bool) (verifiedCount int, err error) {
verifiedCount, err = service.VerifyWithExists(ctx, alias, target, targetVersion, segments)
// if Exists method is unimplemented or it is wrong node version fallback to download verification
if !methodUnimplemented(err) && !errWrongNodeVersion.Has(err) {
return verifiedCount, err
}
if err != nil {
service.log.Debug("fallback to download method", zap.Error(err))
}
service.log.Debug("verify segments by downloading pieces")
var client *piecestore.Client
defer func() {
if client != nil {
_ = client.Close()
}
}()
const maxDials = 2
dialCount := 0
rateLimiter := newRateLimiter(0, 0)
if !ignoreThrottle {
rateLimiter = newRateLimiter(service.config.RequestThrottle, service.config.RequestThrottle/4)
}
nextRequest := time.Now()
for i, segment := range segments {
nextRequest, err = rateLimiter.next(ctx, nextRequest)
if err != nil {
return i, Error.Wrap(err)
}
for client == nil {
dialCount++
if dialCount > maxDials {
return i, ErrNodeOffline.New("too many redials")
}
client, err = piecestore.Dial(rpcpool.WithForceDial(ctx), service.dialer, target, piecestore.DefaultConfig)
if err != nil {
service.log.Info("failed to dial node",
zap.Stringer("node-id", target.ID),
zap.Error(err))
client = nil
nextRequest, err = rateLimiter.next(ctx, nextRequest)
if err != nil {
return i, Error.Wrap(err)
}
}
}
outcome, err := service.verifySegment(ctx, client, alias, target, segment)
if err != nil {
// we could not do the verification, for a reason that implies we won't be able
// to do any more
return i, Error.Wrap(err)
}
switch outcome {
case audit.OutcomeNodeOffline, audit.OutcomeTimedOut:
_ = client.Close()
client = nil
case audit.OutcomeFailure:
segment.Status.MarkNotFound()
case audit.OutcomeSuccess:
segment.Status.MarkFound()
}
}
return len(segments), nil
}
// verifySegment tries to verify the segment by downloading a single byte from the piece of the segment
// on the specified target node.
func (service *NodeVerifier) verifySegment(ctx context.Context, client *piecestore.Client, alias metabase.NodeAlias, target storj.NodeURL, segment *Segment) (outcome audit.Outcome, err error) {
pieceNum := findPieceNum(segment, alias)
logger := service.log.With(
zap.Stringer("stream-id", segment.StreamID),
zap.Stringer("node-id", target.ID),
zap.Uint64("position", segment.Position.Encode()),
zap.Uint16("piece-num", pieceNum))
defer func() {
// report the outcome of the piece check, if required
if outcome != audit.OutcomeSuccess && service.reportPiece != nil {
reportErr := service.reportPiece(ctx, &segment.VerifySegment, target.ID, int(pieceNum), outcome)
err = errs.Combine(err, reportErr)
}
}()
limit, piecePrivateKey, _, err := service.orders.CreateAuditOrderLimit(ctx, target.ID, pieceNum, segment.RootPieceID, segment.Redundancy.ShareSize)
if err != nil {
logger.Error("failed to create order limit",
zap.Stringer("retrying in", service.config.OrderRetryThrottle),
zap.Error(err))
if !sync2.Sleep(ctx, service.config.OrderRetryThrottle) {
return audit.OutcomeNotPerformed, Error.Wrap(ctx.Err())
}
limit, piecePrivateKey, _, err = service.orders.CreateAuditOrderLimit(ctx, target.ID, pieceNum, segment.RootPieceID, segment.Redundancy.ShareSize)
if err != nil {
return audit.OutcomeNotPerformed, Error.Wrap(err)
}
}
timedCtx, cancel := context.WithTimeout(ctx, service.config.PerPieceTimeout)
defer cancel()
downloader, err := client.Download(timedCtx, limit.GetLimit(), piecePrivateKey, 0, 1)
if err != nil {
logger.Error("download failed", zap.Error(err))
if errs2.IsRPC(err, rpcstatus.NotFound) {
return audit.OutcomeFailure, nil
}
if errs2.IsRPC(err, rpcstatus.Unknown) {
// dial failed -- offline node
return audit.OutcomeNodeOffline, nil
}
return audit.OutcomeUnknownError, nil
}
buf := [1]byte{}
_, errRead := io.ReadFull(downloader, buf[:])
errClose := downloader.Close()
err = errs.Combine(errClose, errRead)
if err != nil {
if errs2.IsRPC(err, rpcstatus.NotFound) {
logger.Info("segment not found", zap.Error(err))
return audit.OutcomeFailure, nil
}
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
logger.Info("failed to get timely response when asking for piece", zap.Error(err))
return audit.OutcomeTimedOut, nil
}
logger.Error("read/close failed", zap.Error(err))
return audit.OutcomeUnknownError, nil
}
logger.Info("download succeeded")
return audit.OutcomeSuccess, nil
}
func findPieceNum(segment *Segment, alias metabase.NodeAlias) uint16 {
for _, p := range segment.AliasPieces {
if p.Alias == alias {
return p.Number
}
}
panic("piece number not found")
}
// VerifyWithExists verifies that the segments exist on the specified node by calling the piecestore Exists
// endpoint if the node version supports it.
func (service *NodeVerifier) VerifyWithExists(ctx context.Context, alias metabase.NodeAlias, target storj.NodeURL, targetVersion string, segments []*Segment) (verifiedCount int, err error) {
defer mon.Task()(&ctx)(&err)
if service.versionWithExists.String() == "" || targetVersion == "" {
return 0, errWrongNodeVersion.New("missing node version or no base version defined")
}
nodeVersion, err := semver.ParseTolerant(targetVersion)
if err != nil {
return 0, errWrongNodeVersion.Wrap(err)
}
if !nodeVersion.GE(service.versionWithExists) {
return 0, errWrongNodeVersion.New("too old version")
}
service.log.Debug("verify segments using Exists method", zap.Stringer("node-id", target.ID))
var conn *rpc.Conn
var client pb.DRPCPiecestoreClient
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
const maxDials = 2
dialCount := 0
for client == nil {
dialCount++
if dialCount > maxDials {
return 0, ErrNodeOffline.New("too many redials")
}
conn, err := service.dialer.DialNodeURL(rpcpool.WithForceDial(ctx), target)
if err != nil {
service.log.Info("failed to dial node",
zap.Stringer("node-id", target.ID),
zap.Error(err))
} else {
client = pb.NewDRPCPiecestoreClient(conn)
}
}
// this is maybe too generous, since Exists should be faster than asking for pieces one by one,
// but seems like a good first try
ctx, cancel := context.WithTimeout(ctx, service.config.PerPieceTimeout*time.Duration(len(segments)))
defer cancel()
err = service.verifySegmentsWithExists(ctx, client, alias, target, segments)
if err != nil {
// we could not do the verification, for a reason that implies we won't be able
// to do any more
return 0, Error.Wrap(err)
}
return len(segments), nil
}
func methodUnimplemented(err error) bool {
return errs2.IsRPC(err, rpcstatus.Unimplemented) || errs2.IsRPC(err, rpcstatus.Unknown)
}
// verifySegmentsWithExists calls the Exists endpoint on the specified target node for each segment.
func (service *NodeVerifier) verifySegmentsWithExists(ctx context.Context, client pb.DRPCPiecestoreClient, alias metabase.NodeAlias, target storj.NodeURL, segments []*Segment) (err error) {
defer mon.Task()(&ctx)(&err)
pieceIds := make([]storj.PieceID, 0, len(segments))
pieceNums := make([]uint16, 0, len(segments))
for _, segment := range segments {
pieceNum := findPieceNum(segment, alias)
pieceNums = append(pieceNums, pieceNum)
pieceId := segment.RootPieceID.Derive(target.ID, int32(pieceNum))
pieceIds = append(pieceIds, pieceId)
}
response, err := client.Exists(ctx, &pb.ExistsRequest{
PieceIds: pieceIds,
})
if err != nil {
return Error.Wrap(err)
}
service.log.Debug("Exists response",
zap.Int("request-count", len(pieceIds)),
zap.Int("missing-count", len(response.Missing)))
if len(response.Missing) == 0 {
for i := range segments {
segments[i].Status.MarkFound()
}
return nil
}
missing := make(map[uint32]struct{}, len(segments))
for _, m := range response.Missing {
missing[m] = struct{}{}
}
for i := range segments {
_, miss := missing[uint32(i)]
if miss {
segments[i].Status.MarkNotFound()
if service.reportPiece != nil {
reportErr := service.reportPiece(ctx, &segments[i].VerifySegment, target.ID, int(pieceNums[i]), audit.OutcomeFailure)
err = errs.Combine(err, reportErr)
}
} else {
segments[i].Status.MarkFound()
}
}
return err
}
// rateLimiter limits the rate of some type of event. It acts like a token
// bucket, allowing for bursting, as long as the _average_ interval between
// events over the lifetime of the rateLimiter is less than or equal to the
// specified averageInterval.
//
// The wait time between events will be at least minInterval, even if an
// event would otherwise have been allowed with bursting.
type rateLimiter struct {
nowFn func() time.Time
sleepFn func(ctx context.Context, t time.Duration) bool
averageInterval time.Duration
minInterval time.Duration
}
// newRateLimiter creates a new rateLimiter. If both arguments are specified
// as 0, the rate limiter will have no effect (next() will always return
// immediately).
func newRateLimiter(averageInterval, minInterval time.Duration) rateLimiter {
return rateLimiter{
nowFn: time.Now,
sleepFn: sync2.Sleep,
averageInterval: averageInterval,
minInterval: minInterval,
}
}
// next() sleeps until the time when the next event should be allowed.
// It should be passed the current time for the first call. After that,
// the value returned by the last call to next() should be passed in
// as its argument.
func (r rateLimiter) next(ctx context.Context, nextTime time.Time) (time.Time, error) {
sleepTime := nextTime.Sub(r.nowFn())
if sleepTime < r.minInterval {
sleepTime = r.minInterval
}
if !r.sleepFn(ctx, sleepTime) {
return time.Time{}, ctx.Err()
}
return nextTime.Add(r.averageInterval), nil
}