feat: add client grpc dial timeout (#1599)

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2022-08-29 10:11:58 +08:00 committed by Gaius
parent 5e1037d72f
commit ecfe467585
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
10 changed files with 31 additions and 15 deletions

View File

@ -239,6 +239,7 @@ type DownloadOption struct {
TotalRateLimit util.RateLimit `mapstructure:"totalRateLimit" yaml:"totalRateLimit"` TotalRateLimit util.RateLimit `mapstructure:"totalRateLimit" yaml:"totalRateLimit"`
PerPeerRateLimit util.RateLimit `mapstructure:"perPeerRateLimit" yaml:"perPeerRateLimit"` PerPeerRateLimit util.RateLimit `mapstructure:"perPeerRateLimit" yaml:"perPeerRateLimit"`
PieceDownloadTimeout time.Duration `mapstructure:"pieceDownloadTimeout" yaml:"pieceDownloadTimeout"` PieceDownloadTimeout time.Duration `mapstructure:"pieceDownloadTimeout" yaml:"pieceDownloadTimeout"`
GRPCDialTimeout time.Duration `mapstructure:"grpcDialTimeout" yaml:"grpcDialTimeout"`
DownloadGRPC ListenOption `mapstructure:"downloadGRPC" yaml:"downloadGRPC"` DownloadGRPC ListenOption `mapstructure:"downloadGRPC" yaml:"downloadGRPC"`
PeerGRPC ListenOption `mapstructure:"peerGRPC" yaml:"peerGRPC"` PeerGRPC ListenOption `mapstructure:"peerGRPC" yaml:"peerGRPC"`
CalculateDigest bool `mapstructure:"calculateDigest" yaml:"calculateDigest"` CalculateDigest bool `mapstructure:"calculateDigest" yaml:"calculateDigest"`

View File

@ -70,6 +70,7 @@ var peerHostConfig = func() *DaemonOption {
DefaultPattern: PatternP2P, DefaultPattern: PatternP2P,
CalculateDigest: true, CalculateDigest: true,
PieceDownloadTimeout: 30 * time.Second, PieceDownloadTimeout: 30 * time.Second,
GRPCDialTimeout: 10 * time.Second,
GetPiecesMaxRetry: 100, GetPiecesMaxRetry: 100,
TotalRateLimit: util.RateLimit{ TotalRateLimit: util.RateLimit{
Limit: rate.Limit(DefaultTotalDownloadLimit), Limit: rate.Limit(DefaultTotalDownloadLimit),

View File

@ -70,6 +70,7 @@ var peerHostConfig = func() *DaemonOption {
DefaultPattern: PatternP2P, DefaultPattern: PatternP2P,
CalculateDigest: true, CalculateDigest: true,
PieceDownloadTimeout: 30 * time.Second, PieceDownloadTimeout: 30 * time.Second,
GRPCDialTimeout: 10 * time.Second,
GetPiecesMaxRetry: 100, GetPiecesMaxRetry: 100,
TotalRateLimit: util.RateLimit{ TotalRateLimit: util.RateLimit{
Limit: rate.Limit(DefaultTotalDownloadLimit), Limit: rate.Limit(DefaultTotalDownloadLimit),

View File

@ -220,7 +220,7 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
} }
peerTaskManager, err := peer.NewPeerTaskManager(host, pieceManager, storageManager, sched, opt.Scheduler, peerTaskManager, err := peer.NewPeerTaskManager(host, pieceManager, storageManager, sched, opt.Scheduler,
opt.Download.PerPeerRateLimit.Limit, opt.Storage.Multiplex, opt.Download.Prefetch, opt.Download.CalculateDigest, opt.Download.PerPeerRateLimit.Limit, opt.Storage.Multiplex, opt.Download.Prefetch, opt.Download.CalculateDigest,
opt.Download.GetPiecesMaxRetry, opt.Download.WatchdogTimeout, credentials) opt.Download.GetPiecesMaxRetry, opt.Download.WatchdogTimeout, credentials, opt.Download.GRPCDialTimeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -31,7 +31,6 @@ import (
"go.uber.org/atomic" "go.uber.org/atomic"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
commonv1 "d7y.io/api/pkg/apis/common/v1" commonv1 "d7y.io/api/pkg/apis/common/v1"
@ -170,8 +169,6 @@ type peerTaskConductor struct {
rg *util.Range rg *util.Range
sourceErrorStatus *status.Status sourceErrorStatus *status.Status
grpcCredentials credentials.TransportCredentials
} }
func (ptm *peerTaskManager) newPeerTaskConductor( func (ptm *peerTaskManager) newPeerTaskConductor(
@ -244,7 +241,6 @@ func (ptm *peerTaskManager) newPeerTaskConductor(
usedTraffic: atomic.NewUint64(0), usedTraffic: atomic.NewUint64(0),
SugaredLoggerOnWith: log, SugaredLoggerOnWith: log,
seed: seed, seed: seed,
grpcCredentials: ptm.grpcCredentials,
parent: parent, parent: parent,
rg: rg, rg: rg,

View File

@ -144,6 +144,7 @@ type peerTaskManager struct {
getPiecesMaxRetry int getPiecesMaxRetry int
grpcCredentials credentials.TransportCredentials grpcCredentials credentials.TransportCredentials
grpcDialTimeout time.Duration
} }
func NewPeerTaskManager( func NewPeerTaskManager(
@ -158,7 +159,8 @@ func NewPeerTaskManager(
calculateDigest bool, calculateDigest bool,
getPiecesMaxRetry int, getPiecesMaxRetry int,
watchdog time.Duration, watchdog time.Duration,
grpcCredentials credentials.TransportCredentials) (TaskManager, error) { grpcCredentials credentials.TransportCredentials,
grpcDialTimeout time.Duration) (TaskManager, error) {
ptm := &peerTaskManager{ ptm := &peerTaskManager{
host: host, host: host,
@ -175,6 +177,7 @@ func NewPeerTaskManager(
calculateDigest: calculateDigest, calculateDigest: calculateDigest,
getPiecesMaxRetry: getPiecesMaxRetry, getPiecesMaxRetry: getPiecesMaxRetry,
grpcCredentials: grpcCredentials, grpcCredentials: grpcCredentials,
grpcDialTimeout: grpcDialTimeout,
} }
return ptm, nil return ptm, nil
} }

View File

@ -40,6 +40,7 @@ import (
"golang.org/x/time/rate" "golang.org/x/time/rate"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
commonv1 "d7y.io/api/pkg/apis/common/v1" commonv1 "d7y.io/api/pkg/apis/common/v1"
@ -313,6 +314,8 @@ func setupMockManager(ctrl *gomock.Controller, ts *testSpec, opt componentsOptio
schedulerOption: config.SchedulerOption{ schedulerOption: config.SchedulerOption{
ScheduleTimeout: scheduleTimeout, ScheduleTimeout: scheduleTimeout,
}, },
grpcDialTimeout: time.Second,
grpcCredentials: insecure.NewCredentials(),
} }
return &mockManager{ return &mockManager{
testSpec: ts, testSpec: ts,

View File

@ -170,13 +170,15 @@ func (s *pieceTaskSyncManager) newPieceTaskSynchronizer(
} }
var credentialOpt grpc.DialOption var credentialOpt grpc.DialOption
if s.peerTaskConductor.grpcCredentials != nil { if s.peerTaskConductor.peerTaskManager.grpcCredentials != nil {
credentialOpt = grpc.WithTransportCredentials(s.peerTaskConductor.grpcCredentials) credentialOpt = grpc.WithTransportCredentials(s.peerTaskConductor.peerTaskManager.grpcCredentials)
} else { } else {
credentialOpt = grpc.WithTransportCredentials(insecure.NewCredentials()) credentialOpt = grpc.WithTransportCredentials(insecure.NewCredentials())
} }
client, err := dfdaemonclient.GetClient(context.Background(), netAddr.String(), credentialOpt) dialCtx, cancel := context.WithTimeout(ctx, s.peerTaskConductor.peerTaskManager.grpcDialTimeout)
client, err := dfdaemonclient.GetClient(dialCtx, netAddr.String(), credentialOpt)
cancel()
if err != nil { if err != nil {
s.peerTaskConductor.Errorf("get dfdaemon client error: %s, dest peer: %s", err, dstPeer.PeerId) s.peerTaskConductor.Errorf("get dfdaemon client error: %s, dest peer: %s", err, dstPeer.PeerId)

View File

@ -24,10 +24,12 @@ import (
"os" "os"
"path" "path"
"testing" "testing"
"time"
"github.com/go-http-utils/headers" "github.com/go-http-utils/headers"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
testifyassert "github.com/stretchr/testify/assert" testifyassert "github.com/stretchr/testify/assert"
"google.golang.org/grpc/credentials/insecure"
commonv1 "d7y.io/api/pkg/apis/common/v1" commonv1 "d7y.io/api/pkg/apis/common/v1"
schedulerv1 "d7y.io/api/pkg/apis/scheduler/v1" schedulerv1 "d7y.io/api/pkg/apis/scheduler/v1"
@ -321,6 +323,8 @@ func TestReuseFilePeerTask(t *testing.T) {
host: &schedulerv1.PeerHost{}, host: &schedulerv1.PeerHost{},
enablePrefetch: tc.enablePrefetch, enablePrefetch: tc.enablePrefetch,
storageManager: sm, storageManager: sm,
grpcDialTimeout: time.Second,
grpcCredentials: insecure.NewCredentials(),
} }
tc.verify(ptm.tryReuseFilePeerTask(context.Background(), tc.request)) tc.verify(ptm.tryReuseFilePeerTask(context.Background(), tc.request))
}) })
@ -698,6 +702,8 @@ func TestReuseStreamPeerTask(t *testing.T) {
host: &schedulerv1.PeerHost{}, host: &schedulerv1.PeerHost{},
enablePrefetch: tc.enablePrefetch, enablePrefetch: tc.enablePrefetch,
storageManager: sm, storageManager: sm,
grpcDialTimeout: time.Second,
grpcCredentials: insecure.NewCredentials(),
} }
tc.verify(ptm.tryReuseStreamPeerTask(context.Background(), tc.request)) tc.verify(ptm.tryReuseStreamPeerTask(context.Background(), tc.request))
}) })

View File

@ -36,6 +36,7 @@ import (
"go.uber.org/atomic" "go.uber.org/atomic"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
commonv1 "d7y.io/api/pkg/apis/common/v1" commonv1 "d7y.io/api/pkg/apis/common/v1"
@ -262,6 +263,8 @@ func TestStreamPeerTask_BackSource_Partial_WithContentLength(t *testing.T) {
schedulerOption: config.SchedulerOption{ schedulerOption: config.SchedulerOption{
ScheduleTimeout: util.Duration{Duration: 10 * time.Minute}, ScheduleTimeout: util.Duration{Duration: 10 * time.Minute},
}, },
grpcDialTimeout: time.Second,
grpcCredentials: insecure.NewCredentials(),
} }
req := &schedulerv1.PeerTaskRequest{ req := &schedulerv1.PeerTaskRequest{
Url: url, Url: url,