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

[sdk#1111] Fix sendfd issue on URL change #1112

Merged
merged 6 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions pkg/networkservice/chains/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func NewClient(ctx context.Context, clientOpts ...Option) networkservice.Network
opts.refreshClient,
clienturl.NewClient(opts.clientURL),
clientconn.NewClient(opts.cc),
},
append(
opts.additionalFunctionality,
Comment on lines -60 to -61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of URL change dial.Client does the following on Request:

  1. Close old URL.
  2. Request new URL.

If we have sendfd.Client in the chain before it does the following on Request:

  1. insert grpcfd.PerRPCCredentials to the call options.

grpcfd.PerRPCCredentials does the following:

  1. Store fds that needs to be sent.
  2. On first gRPC interaction send them and remember connection as a FDTransceiver.
  3. Cleanup all stored fds.

So it results in the following:

  1. dial closes old URL - PerRPCCredentials sends all fds to the old URL.
  2. dial requests new URL - PerRPCCredentials has nothing to send so it does nothing.

So here are 2 solutions that I can currently see:

  1. Put dial client before additionalFunctionality.
  2. Edit PerRPCCredentials not to capture FDTransceiver and not to cleanup stored fds on send.

We have discussed this with @edwarnicke and so it actually looks like both of these solutions have their pros and cons, but [1] is easier to implement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@denis-tingaikin I discussed this this morning with @Bolodya1997 on slack. It's a subtle bug, but once explained it makes sense.

dial.NewClient(ctx,
dial.WithDialOptions(opts.dialOptions...),
dial.WithDialTimeout(opts.dialTimeout),
),
},
append(
opts.additionalFunctionality,
opts.authorizeClient,
connect.NewClient(),
)...,
Expand Down
55 changes: 0 additions & 55 deletions pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -32,7 +31,6 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count"
"github.com/networkservicemesh/sdk/pkg/tools/clientinfo"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)
Expand Down Expand Up @@ -191,59 +189,6 @@ func Test_ShouldCorrectlyAddEndpointsWithSameNames(t *testing.T) {
}
}

func Test_Local_NoURLUsecase(t *testing.T) {
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
if runtime.GOOS == "windows" {
t.Skip("Unix sockets are not supported under windows, skipping")
return
}
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
UseUnixSockets().
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
SetRegistrySupplier(nil).
Build()

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseReg := defaultRegistryEndpoint(nsReg.Name)
request := defaultRequest(nsReg.Name)
counter := new(count.Server)

domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)

nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, 1, counter.Requests())
require.Equal(t, 5, len(conn.Path.PathSegments))

// Simulate refresh from client
refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

conn2, err := nsc.Request(ctx, refreshRequest)
require.NoError(t, err)
require.NotNil(t, conn2)
require.Equal(t, 5, len(conn2.Path.PathSegments))
require.Equal(t, 2, counter.Requests())

// Close
_, err = nsc.Close(ctx, conn)
require.NoError(t, err)
require.Equal(t, 1, counter.Closes())
}

func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

Expand Down
146 changes: 146 additions & 0 deletions pkg/networkservice/chains/nsmgr/unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//+build !windows

package nsmgr_test

import (
"context"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

func Test_Local_NoURLUsecase(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(ctx, t).
UseUnixSockets().
Build()

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseReg := defaultRegistryEndpoint(nsReg.Name)
request := defaultRequest(nsReg.Name)
counter := new(count.Server)

domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)

nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, 1, counter.Requests())
require.Equal(t, 5, len(conn.Path.PathSegments))

// Simulate refresh from client
refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

conn2, err := nsc.Request(ctx, refreshRequest)
require.NoError(t, err)
require.NotNil(t, conn2)
require.Equal(t, 5, len(conn2.Path.PathSegments))
require.Equal(t, 2, counter.Requests())

// Close
_, err = nsc.Close(ctx, conn)
require.NoError(t, err)
require.Equal(t, 1, counter.Closes())
}

func Test_MultiForwarderSendfd(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("sendfd works only on linux")
}
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

errorServer := injecterror.NewServer(
injecterror.WithRequestErrorTimes(0),
injecterror.WithCloseErrorTimes(),
)
domain := sandbox.NewBuilder(ctx, t).
UseUnixSockets().
SetNodeSetup(func(ctx context.Context, node *sandbox.Node, _ int) {
node.NewNSMgr(ctx, "nsmgr", nil, sandbox.GenerateTestToken, nsmgr.NewServer)
node.NewForwarder(ctx, &registry.NetworkServiceEndpoint{
Name: "forwarder-1",
}, sandbox.GenerateTestToken, errorServer, recvfd.NewServer())
node.NewForwarder(ctx, &registry.NetworkServiceEndpoint{
Name: "forwarder-2",
}, sandbox.GenerateTestToken, errorServer, recvfd.NewServer())
}).
Build()

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseReg := defaultRegistryEndpoint(nsReg.Name)
counter := new(count.Server)

domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)

nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, kernel.NewClient(), sendfd.NewClient())

request := defaultRequest(nsReg.Name)

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, 1, counter.Requests())
require.Equal(t, 5, len(conn.Path.PathSegments))

// Simulate refresh from client
refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

conn2, err := nsc.Request(ctx, refreshRequest)
require.NoError(t, err)
require.NotNil(t, conn2)
require.Equal(t, 5, len(conn2.Path.PathSegments))
require.Equal(t, 2, counter.Requests())

// Close
_, err = nsc.Close(ctx, conn)
require.NoError(t, err)
require.Equal(t, 1, counter.Closes())
}
2 changes: 1 addition & 1 deletion pkg/networkservice/common/discover/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (d *discoverCandidatesServer) Request(ctx context.Context, request *network

delay := defaultDiscoverDelay
for ctx.Err() == nil {
resp, err := next.Server(ctx).Request(WithCandidates(ctx, nses, ns), request)
resp, err := next.Server(ctx).Request(WithCandidates(ctx, nses, ns), request.Clone())
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
return resp, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/interpose/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (l *interposeServer) Request(ctx context.Context, request *networkservice.N
endpointURL: clientURL,
interposeNSEURL: crossNSEURL,
})
result, err = next.Server(crossCTX).Request(crossCTX, request)
result, err = next.Server(crossCTX).Request(crossCTX, request.Clone())
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Errorf("failed to request cross NSE %v err: %v", crossNSEURL, err)
return true
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/mechanisms/recvfd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewClient() networkservice.NetworkServiceClient {
func (r *recvFDClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
// Get the grpcfd.FDRecver
rpcCredentials := grpcfd.PerRPCCredentials(grpcfd.PerRPCCredentialsFromCallOptions(opts...))
opts = append(opts, grpc.PerRPCCredentials(rpcCredentials))
opts = append([]grpc.CallOption{grpc.PerRPCCredentials(rpcCredentials)}, opts...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change the order of call options?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault, I did a mistake during the testing, there is no need for this.

recv, _ := grpcfd.FromPerRPCCredentials(rpcCredentials)

// Call the next Client in the chain
Expand Down Expand Up @@ -76,7 +76,7 @@ func (r *recvFDClient) Request(ctx context.Context, request *networkservice.Netw
func (r *recvFDClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
// Get the grpcfd.FDRecver
rpcCredentials := grpcfd.PerRPCCredentials(grpcfd.PerRPCCredentialsFromCallOptions(opts...))
opts = append(opts, grpc.PerRPCCredentials(rpcCredentials))
opts = append([]grpc.CallOption{grpc.PerRPCCredentials(rpcCredentials)}, opts...)
Bolodya1997 marked this conversation as resolved.
Show resolved Hide resolved
recv, _ := grpcfd.FromPerRPCCredentials(rpcCredentials)

// Get the fileMap
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/mechanisms/sendfd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewClient() networkservice.NetworkServiceClient {
func (s *sendFDClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
// Get the grpcfd.FDSender
rpcCredentials := grpcfd.PerRPCCredentials(grpcfd.PerRPCCredentialsFromCallOptions(opts...))
opts = append(opts, grpc.PerRPCCredentials(rpcCredentials))
opts = append([]grpc.CallOption{grpc.PerRPCCredentials(rpcCredentials)}, opts...)
Bolodya1997 marked this conversation as resolved.
Show resolved Hide resolved
sender, _ := grpcfd.FromPerRPCCredentials(rpcCredentials)

// Iterate over mechanisms
Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *sendFDClient) Request(ctx context.Context, request *networkservice.Netw

func (s *sendFDClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
rpcCredentials := grpcfd.PerRPCCredentials(grpcfd.PerRPCCredentialsFromCallOptions(opts...))
opts = append(opts, grpc.PerRPCCredentials(rpcCredentials))
opts = append([]grpc.CallOption{grpc.PerRPCCredentials(rpcCredentials)}, opts...)
Bolodya1997 marked this conversation as resolved.
Show resolved Hide resolved
sender, _ := grpcfd.FromPerRPCCredentials(rpcCredentials)

// Send the FD and swap the FileURL for an InodeURL
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/roundrobin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *selectEndpointServer) Request(ctx context.Context, request *networkserv
}
ctx = clienturlctx.WithClientURL(ctx, u)
request.GetConnection().NetworkServiceEndpointName = endpoint.Name
resp, err := next.Server(ctx).Request(ctx, request)
resp, err := next.Server(ctx).Request(ctx, request.Clone())
if err == nil {
return resp, err
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/networkservice/common/roundrobin/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package roundrobin_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/discover"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/roundrobin"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/switchcase"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
)

const (
nse1 = "nse-1"
nse2 = "nse-2"
ns = "ns"
)

func TestSelectEndpointServer_CleanRequest(t *testing.T) {
Bolodya1997 marked this conversation as resolved.
Show resolved Hide resolved
t.Cleanup(func() { goleak.VerifyNone(t) })

var flag bool
Copy link
Member

@denis-tingaikin denis-tingaikin Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a more understandable name?

Suggested change
var flag bool
var TODO bool

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with hasBeenRequested.

s := next.NewNetworkServiceServer(
roundrobin.NewServer(),
switchcase.NewServer(
&switchcase.ServerCase{
Condition: func(_ context.Context, conn *networkservice.Connection) bool {
return conn.NetworkServiceEndpointName == nse1
},
Server: next.NewNetworkServiceServer(
checkrequest.NewServer(t, func(_ *testing.T, request *networkservice.NetworkServiceRequest) {
flag = true
request.Connection.Labels[nse1] = nse1
}),
injecterror.NewServer(),
),
},
),
)

ctx := discover.WithCandidates(context.Background(), []*registry.NetworkServiceEndpoint{
{
Name: nse1,
Url: "unix://" + nse1,
NetworkServiceNames: []string{ns},
},
{
Name: nse2,
Url: "unix://" + nse2,
NetworkServiceNames: []string{ns},
},
}, &registry.NetworkService{Name: ns})

conn, err := s.Request(ctx, &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Labels: map[string]string{nse2: nse2},
},
})
require.NoError(t, err)

require.True(t, flag)
require.Equal(t, nse2, conn.NetworkServiceEndpointName)
require.Equal(t, map[string]string{nse2: nse2}, conn.Labels)
}