2020-03-12 07:00:53 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package piecedeletion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-29 15:22:52 +01:00
|
|
|
"strconv"
|
2020-03-12 07:00:53 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-06-25 21:01:51 +01:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2020-03-12 07:00:53 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dialer implements dialing piecestores and sending delete requests with batching and redial threshold.
|
|
|
|
type Dialer struct {
|
|
|
|
log *zap.Logger
|
|
|
|
dialer rpc.Dialer
|
|
|
|
|
|
|
|
requestTimeout time.Duration
|
|
|
|
failThreshold time.Duration
|
|
|
|
piecesPerRequest int
|
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
dialFailed map[storj.NodeID]time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDialer returns a new Dialer.
|
|
|
|
func NewDialer(log *zap.Logger, dialer rpc.Dialer, requestTimeout, failThreshold time.Duration, piecesPerRequest int) *Dialer {
|
|
|
|
return &Dialer{
|
|
|
|
log: log,
|
|
|
|
dialer: dialer,
|
|
|
|
|
|
|
|
requestTimeout: requestTimeout,
|
|
|
|
failThreshold: failThreshold,
|
|
|
|
piecesPerRequest: piecesPerRequest,
|
|
|
|
|
|
|
|
dialFailed: map[storj.NodeID]time.Time{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle tries to send the deletion requests to the specified node.
|
2020-05-20 14:10:25 +01:00
|
|
|
func (dialer *Dialer) Handle(ctx context.Context, node storj.NodeURL, queue Queue) {
|
2020-06-25 21:01:51 +01:00
|
|
|
defer mon.Task()(&ctx, node.ID.String())(nil)
|
2020-03-12 07:00:53 +00:00
|
|
|
defer FailPending(queue)
|
|
|
|
|
|
|
|
if dialer.recentlyFailed(ctx, node) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:17:22 +01:00
|
|
|
client, conn, err := dialPieceStore(ctx, dialer.dialer, node)
|
2020-03-12 07:00:53 +00:00
|
|
|
if err != nil {
|
2020-05-20 14:10:25 +01:00
|
|
|
dialer.log.Debug("failed to dial", zap.Stringer("id", node.ID), zap.Error(err))
|
2020-03-12 07:00:53 +00:00
|
|
|
dialer.markFailed(ctx, node)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := conn.Close(); err != nil {
|
2020-05-20 14:10:25 +01:00
|
|
|
dialer.log.Debug("closing connection failed", zap.Stringer("id", node.ID), zap.Error(err))
|
2020-03-12 07:00:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs, ok := queue.PopAll()
|
2020-06-25 21:01:51 +01:00
|
|
|
// Add metrics on to the span
|
|
|
|
s := monkit.SpanFromCtx(ctx)
|
2020-06-29 15:22:52 +01:00
|
|
|
s.Annotate("delete jobs size", strconv.Itoa(len(jobs)))
|
2020-06-25 21:01:51 +01:00
|
|
|
|
2020-03-12 07:00:53 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(jobs) > 0 {
|
|
|
|
batch, promises, rest := batchJobs(jobs, dialer.piecesPerRequest)
|
2020-06-25 21:01:51 +01:00
|
|
|
// Aggregation metrics
|
|
|
|
mon.IntVal("delete batch size").Observe(int64(len(batch)))
|
|
|
|
// Tracing metrics
|
2020-06-29 15:22:52 +01:00
|
|
|
s.Annotate("delete batch size", strconv.Itoa(len(batch)))
|
2020-06-25 21:01:51 +01:00
|
|
|
|
2020-03-12 07:00:53 +00:00
|
|
|
jobs = rest
|
|
|
|
|
|
|
|
requestCtx, cancel := context.WithTimeout(ctx, dialer.requestTimeout)
|
2020-04-28 14:17:22 +01:00
|
|
|
resp, err := client.DeletePieces(requestCtx, &pb.DeletePiecesRequest{
|
|
|
|
PieceIds: batch,
|
|
|
|
})
|
2020-03-12 07:00:53 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
for _, promise := range promises {
|
|
|
|
if err != nil {
|
|
|
|
promise.Failure()
|
|
|
|
} else {
|
|
|
|
promise.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-20 14:10:25 +01:00
|
|
|
dialer.log.Debug("deletion request failed", zap.Stringer("id", node.ID), zap.Error(err))
|
2020-03-12 07:00:53 +00:00
|
|
|
// don't try to send to this storage node a bit, when the deletion times out
|
|
|
|
if errs2.IsCanceled(err) {
|
|
|
|
dialer.markFailed(ctx, node)
|
|
|
|
}
|
|
|
|
break
|
2020-04-28 14:17:22 +01:00
|
|
|
} else {
|
|
|
|
mon.IntVal("deletion pieces unhandled count").Observe(resp.UnhandledCount)
|
2020-03-12 07:00:53 +00:00
|
|
|
}
|
2020-04-22 15:39:26 +01:00
|
|
|
|
|
|
|
jobs = append(jobs, queue.PopAllWithoutClose()...)
|
2020-03-12 07:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we failed early, remaining jobs should be marked as failures
|
|
|
|
for _, job := range jobs {
|
|
|
|
job.Resolve.Failure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// markFailed marks node as something failed recently, so we shouldn't try again,
|
|
|
|
// for some time.
|
2020-05-20 14:10:25 +01:00
|
|
|
func (dialer *Dialer) markFailed(ctx context.Context, node storj.NodeURL) {
|
2020-03-12 07:00:53 +00:00
|
|
|
dialer.mu.Lock()
|
|
|
|
defer dialer.mu.Unlock()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2020-05-20 14:10:25 +01:00
|
|
|
lastFailed, ok := dialer.dialFailed[node.ID]
|
2020-03-12 07:00:53 +00:00
|
|
|
if !ok || lastFailed.Before(now) {
|
2020-05-20 14:10:25 +01:00
|
|
|
dialer.dialFailed[node.ID] = now
|
2020-03-12 07:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recentlyFailed checks whether a request to node recently failed.
|
2020-05-20 14:10:25 +01:00
|
|
|
func (dialer *Dialer) recentlyFailed(ctx context.Context, node storj.NodeURL) bool {
|
2020-03-12 07:00:53 +00:00
|
|
|
dialer.mu.RLock()
|
2020-05-20 14:10:25 +01:00
|
|
|
lastFailed, ok := dialer.dialFailed[node.ID]
|
2020-03-12 07:00:53 +00:00
|
|
|
dialer.mu.RUnlock()
|
|
|
|
|
|
|
|
// when we recently failed to dial, then fail immediately
|
|
|
|
return ok && time.Since(lastFailed) < dialer.failThreshold
|
|
|
|
}
|
|
|
|
|
|
|
|
func batchJobs(jobs []Job, maxBatchSize int) (pieces []storj.PieceID, promises []Promise, rest []Job) {
|
|
|
|
for i, job := range jobs {
|
|
|
|
if len(pieces) >= maxBatchSize {
|
|
|
|
return pieces, promises, jobs[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
pieces = append(pieces, job.Pieces...)
|
|
|
|
promises = append(promises, job.Resolve)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pieces, promises, nil
|
|
|
|
}
|
2020-04-28 14:17:22 +01:00
|
|
|
|
2020-05-20 14:10:25 +01:00
|
|
|
func dialPieceStore(ctx context.Context, dialer rpc.Dialer, target storj.NodeURL) (pb.DRPCPiecestoreClient, *rpc.Conn, error) {
|
|
|
|
conn, err := dialer.DialNodeURL(ctx, target)
|
2020-04-28 14:17:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return pb.NewDRPCPiecestoreClient(conn), conn, nil
|
|
|
|
}
|