Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable dns resolve #2634

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apis/grpc/v1/payload/payload.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apis/grpc/v1/rpc/errdetails/error_details.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 34 additions & 20 deletions internal/net/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,32 @@
GetDialOption() []DialOption
GetCallOption() []CallOption
GetBackoff() backoff.Backoff
SetDisableResolveDNSAddr(addr string, disabled bool)
ConnectedAddrs() []string
Close(ctx context.Context) error
}

type gRPCClient struct {
addrs map[string]struct{}
poolSize uint64
clientCount uint64
conns sync.Map[string, pool.Conn]
hcDur time.Duration
prDur time.Duration
dialer net.Dialer
enablePoolRebalance bool
resolveDNS bool
dopts []DialOption
copts []CallOption
roccd string // reconnection old connection closing duration
eg errgroup.Group
bo backoff.Backoff
cb circuitbreaker.CircuitBreaker
gbo gbackoff.Config // grpc's original backoff configuration
mcd time.Duration // minimum connection timeout duration
group singleflight.Group[pool.Conn]
crl sync.Map[string, bool] // connection request list
addrs map[string]struct{}
poolSize uint64
clientCount uint64
conns sync.Map[string, pool.Conn]
hcDur time.Duration
prDur time.Duration
dialer net.Dialer
enablePoolRebalance bool
disableResolveDNSAddrs sync.Map[string, bool]
resolveDNS bool
dopts []DialOption
copts []CallOption
roccd string // reconnection old connection closing duration
eg errgroup.Group
bo backoff.Backoff
cb circuitbreaker.CircuitBreaker
gbo gbackoff.Config // grpc's original backoff configuration
mcd time.Duration // minimum connection timeout duration
group singleflight.Group[pool.Conn]
crl sync.Map[string, bool] // connection request list

ech <-chan error
monitorRunning atomic.Bool
Expand Down Expand Up @@ -946,6 +948,12 @@
return g.bo
}

func (g *gRPCClient) SetDisableResolveDNSAddr(addr string, disabled bool) {

Check warning on line 951 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L951

Added line #L951 was not covered by tests
// NOTE: When connecting to multiple locations, it was necessary to switch dynamically, so implementation was added.
// There is no setting for disable on the helm chart side, so I used this implementation.
g.disableResolveDNSAddrs.Store(addr, disabled)

Check warning on line 954 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L954

Added line #L954 was not covered by tests
}

func (g *gRPCClient) Connect(
ctx context.Context, addr string, dopts ...DialOption,
) (conn pool.Conn, err error) {
Expand Down Expand Up @@ -975,7 +983,13 @@
pool.WithAddr(addr),
pool.WithSize(g.poolSize),
pool.WithDialOptions(append(g.dopts, dopts...)...),
pool.WithResolveDNS(g.resolveDNS),
pool.WithResolveDNS(func() bool {
disabled, ok := g.disableResolveDNSAddrs.Load(addr)
if ok && disabled {
return false

Check warning on line 989 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L986-L989

Added lines #L986 - L989 were not covered by tests
}
return g.resolveDNS

Check warning on line 991 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L991

Added line #L991 was not covered by tests
}()),
}
if g.bo != nil {
opts = append(opts, pool.WithBackoff(g.bo))
Expand Down
14 changes: 10 additions & 4 deletions internal/test/mock/grpc/grpc_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ type GRPCClientMock struct {
addr string,
conn *grpc.ClientConn,
copts ...grpc.CallOption) error) error
ConnectFunc func(ctx context.Context, addr string, dopts ...grpc.DialOption) (pool.Conn, error)
DisconnectFunc func(ctx context.Context, addr string) error
IsConnectedFunc func(ctx context.Context, addr string) bool
ConnectedAddrsFunc func() []string
ConnectFunc func(ctx context.Context, addr string, dopts ...grpc.DialOption) (pool.Conn, error)
DisconnectFunc func(ctx context.Context, addr string) error
IsConnectedFunc func(ctx context.Context, addr string) bool
ConnectedAddrsFunc func() []string
SetDisableResolveDNSAddrFunc func(addr string, disabled bool)
}

// OrderedRangeConcurrent calls the OrderedRangeConcurrentFunc object.
Expand Down Expand Up @@ -70,3 +71,8 @@ func (gc *GRPCClientMock) Disconnect(ctx context.Context, addr string) error {
func (gc *GRPCClientMock) IsConnected(ctx context.Context, addr string) bool {
return gc.IsConnectedFunc(ctx, addr)
}

// SetDisableResolveDNSAddr calls the SetDisableResolveDNSAddr object.
func (gc *GRPCClientMock) SetDisableResolveDNSAddr(addr string, disabled bool) {
gc.SetDisableResolveDNSAddrFunc(addr, disabled)
}
13 changes: 12 additions & 1 deletion pkg/gateway/mirror/service/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import (
"context"
"reflect"
"strings"

"github.com/vdaas/vald/internal/client/v1/client/mirror"
"github.com/vdaas/vald/internal/errors"
Expand Down Expand Up @@ -143,11 +144,21 @@
if target == "" {
return nil, errors.ErrTargetNotFound
}
return g.client.GRPCClient().Do(g.ForwardedContext(ctx, g.podName), target,
fctx := g.ForwardedContext(ctx, g.podName)
res, err = g.client.GRPCClient().Do(fctx, target,

Check warning on line 148 in pkg/gateway/mirror/service/gateway.go

View check run for this annotation

Codecov / codecov/patch

pkg/gateway/mirror/service/gateway.go#L147-L148

Added lines #L147 - L148 were not covered by tests
func(ictx context.Context, conn *grpc.ClientConn, copts ...grpc.CallOption) (any, error) {
return f(ictx, target, NewMirrorClient(conn), copts...)
},
)
if err != nil {
return g.client.GRPCClient().RoundRobin(fctx, func(ictx context.Context, conn *grpc.ClientConn, copts ...grpc.CallOption) (any, error) {
if strings.EqualFold(conn.Target(), target) {
return nil, errors.ErrTargetNotFound

Check warning on line 156 in pkg/gateway/mirror/service/gateway.go

View check run for this annotation

Codecov / codecov/patch

pkg/gateway/mirror/service/gateway.go#L153-L156

Added lines #L153 - L156 were not covered by tests
}
return f(ictx, conn.Target(), NewMirrorClient(conn), copts...)

Check warning on line 158 in pkg/gateway/mirror/service/gateway.go

View check run for this annotation

Codecov / codecov/patch

pkg/gateway/mirror/service/gateway.go#L158

Added line #L158 was not covered by tests
})
}
return res, nil

Check warning on line 161 in pkg/gateway/mirror/service/gateway.go

View check run for this annotation

Codecov / codecov/patch

pkg/gateway/mirror/service/gateway.go#L161

Added line #L161 was not covered by tests
}

// DoMulti performs a gRPC operation on multiple targets using the provided function.
Expand Down
1 change: 1 addition & 0 deletions pkg/gateway/mirror/service/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func (m *mirr) Connect(ctx context.Context, targets ...*payload.Mirror_Target) e
if !m.isSelfMirrorAddr(addr) && !m.isGatewayAddr(addr) {
_, ok := m.addrs.Load(addr)
if !ok || !m.IsConnected(ctx, addr) {
m.gateway.GRPCClient().SetDisableResolveDNSAddr(addr, true)
_, err := m.gateway.GRPCClient().Connect(ctx, addr)
if err != nil {
m.addrs.Delete(addr)
Expand Down
6 changes: 6 additions & 0 deletions pkg/gateway/mirror/service/mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func Test_mirr_Connect(t *testing.T) {
ConnectFunc: func(_ context.Context, _ string, _ ...grpc.DialOption) (conn pool.Conn, err error) {
return conn, err
},
SetDisableResolveDNSAddrFunc: func(addr string, disabled bool) {},
}
},
},
Expand Down Expand Up @@ -118,6 +119,7 @@ func Test_mirr_Connect(t *testing.T) {
ConnectFunc: func(_ context.Context, _ string, _ ...grpc.DialOption) (pool.Conn, error) {
return nil, errors.New("missing port in address")
},
SetDisableResolveDNSAddrFunc: func(addr string, disabled bool) {},
}
},
},
Expand Down Expand Up @@ -221,6 +223,7 @@ func Test_mirr_Disconnect(t *testing.T) {
DisconnectFunc: func(_ context.Context, _ string) error {
return nil
},
SetDisableResolveDNSAddrFunc: func(addr string, disabled bool) {},
}
},
},
Expand Down Expand Up @@ -252,6 +255,7 @@ func Test_mirr_Disconnect(t *testing.T) {
DisconnectFunc: func(_ context.Context, _ string) error {
return errors.New("missing port in address")
},
SetDisableResolveDNSAddrFunc: func(addr string, disabled bool) {},
}
},
},
Expand Down Expand Up @@ -373,6 +377,7 @@ func Test_mirr_MirrorTargets(t *testing.T) {
IsConnectedFunc: func(_ context.Context, addr string) bool {
return connected[addr]
},
SetDisableResolveDNSAddrFunc: func(addr string, disabled bool) {},
}
},
},
Expand Down Expand Up @@ -498,6 +503,7 @@ func Test_mirr_connectedOtherMirrorAddrs(t *testing.T) {
IsConnectedFunc: func(_ context.Context, addr string) bool {
return connected[addr]
},
SetDisableResolveDNSAddrFunc: func(addr string, disabled bool) {},
}
},
},
Expand Down
Loading