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

discoverServer: handle Close #571

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 35 additions & 10 deletions pkg/networkservice/chains/nsmgr/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"net/url"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -62,7 +63,8 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
NetworkServiceNames: []string{"my-service-remote"},
}

_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr)
counter := &counterServer{}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, counter)
require.NoError(t, err)

request := &networkservice.NetworkServiceRequest{
Expand All @@ -79,7 +81,7 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
nsc, err := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[1].NSMgr.URL)
require.NoError(t, err)

conn, err := nsc.Request(ctx, request)
conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)

Expand All @@ -88,14 +90,18 @@ func TestNSMGR_RemoteUsecase(t *testing.T) {
// Simulate refresh from client.

refreshRequest := request.Clone()
refreshRequest.GetConnection().Context = conn.Context
refreshRequest.GetConnection().Mechanism = conn.Mechanism
refreshRequest.GetConnection().NetworkServiceEndpointName = conn.NetworkServiceEndpointName
refreshRequest.Connection = conn.Clone()

conn, err = nsc.Request(ctx, refreshRequest)
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, 8, len(conn.Path.PathSegments))

// Close.
e, err := nsc.Close(ctx, conn)
require.NoError(t, err)
require.NotNil(t, e)
require.Equal(t, int32(1), atomic.LoadInt32(&counter.Closes))
}

func TestNSMGR_LocalUsecase(t *testing.T) {
Expand All @@ -114,7 +120,8 @@ func TestNSMGR_LocalUsecase(t *testing.T) {
Name: "final-endpoint",
NetworkServiceNames: []string{"my-service-remote"},
}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr)
counter := &counterServer{}
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr, counter)
require.NoError(t, err)

nsc, err := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)
Expand All @@ -130,7 +137,7 @@ func TestNSMGR_LocalUsecase(t *testing.T) {
Context: &networkservice.ConnectionContext{},
},
}
conn, err := nsc.Request(ctx, request)
conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)

Expand All @@ -139,14 +146,18 @@ func TestNSMGR_LocalUsecase(t *testing.T) {
// Simulate refresh from client.

refreshRequest := request.Clone()
refreshRequest.GetConnection().Context = conn.Context
refreshRequest.GetConnection().Mechanism = conn.Mechanism
refreshRequest.GetConnection().NetworkServiceEndpointName = conn.NetworkServiceEndpointName
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))

// Close.
e, err := nsc.Close(ctx, conn)
require.NoError(t, err)
require.NotNil(t, e)
require.Equal(t, int32(1), atomic.LoadInt32(&counter.Closes))
}

func TestNSMGR_PassThroughRemote(t *testing.T) {
Expand Down Expand Up @@ -318,3 +329,17 @@ func (p *passThroughClient) Close(ctx context.Context, conn *networkservice.Conn
conn = conn.Clone()
return next.Client(ctx).Close(ctx, conn, opts...)
}

type counterServer struct {
Requests, Closes int32
}

func (c *counterServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
atomic.AddInt32(&c.Requests, 1)
return next.Server(ctx).Request(ctx, request)
}

func (c *counterServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) {
atomic.AddInt32(&c.Closes, 1)
return next.Server(ctx).Close(ctx, connection)
}
47 changes: 32 additions & 15 deletions pkg/networkservice/common/discover/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ func NewServer(nsClient registry.NetworkServiceRegistryClient, nseClient registr
func (d *discoverCandidatesServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
nseName := request.GetConnection().GetNetworkServiceEndpointName()
if nseName != "" {
nseStream, err := d.nseClient.Find(context.Background(), &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: nseName,
},
})
if err != nil {
return nil, err
}
nseList := registry.ReadNetworkServiceEndpointList(nseStream)
if len(nseList) == 0 {
return nil, errors.Errorf("network service endpoint %s is not found", nseName)
}
u, err := url.Parse(nseList[0].Url)
u, err := d.urlByNseName(nseName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -100,6 +88,35 @@ func (d *discoverCandidatesServer) Request(ctx context.Context, request *network
return next.Server(ctx).Request(ctx, request)
}

func (d *discoverCandidatesServer) Close(context.Context, *networkservice.Connection) (*empty.Empty, error) {
return &empty.Empty{}, nil
func (d *discoverCandidatesServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
nseName := conn.GetNetworkServiceEndpointName()
if nseName == "" {
// If it's an existing connection, the NSE name should be set. Otherwise, it's probably an API misuse.
return nil, errors.Errorf("network_service_endpoint_name is not set")
}
u, err := d.urlByNseName(nseName)
if err != nil {
return nil, err
}
return next.Server(ctx).Close(clienturlctx.WithClientURL(ctx, u), conn)
}

func (d *discoverCandidatesServer) urlByNseName(nseName string) (*url.URL, error) {
nseStream, err := d.nseClient.Find(context.Background(), &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: nseName,
},
})
if err != nil {
return nil, err
}
nseList := registry.ReadNetworkServiceEndpointList(nseStream)
if len(nseList) == 0 {
return nil, errors.Errorf("network service endpoint %s is not found", nseName)
}
u, err := url.Parse(nseList[0].Url)
if err != nil {
return nil, err
}
return u, nil
}