Skip to content

Commit

Permalink
fix discover forwarder
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art committed Mar 24, 2023
1 parent 1c6df67 commit 613fd0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
21 changes: 14 additions & 7 deletions pkg/networkservice/common/discoverforwarder/metadata.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,16 +24,21 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type selectedForworderKey struct{}
type selectedForwarderKey struct{}

type selectedForwarderVal struct {
name string
active bool
}

func loadForwarderName(ctx context.Context) string {
v, ok := metadata.Map(ctx, false).Load(selectedForworderKey{})
func loadForwarder(ctx context.Context) *selectedForwarderVal {
v, ok := metadata.Map(ctx, false).Load(selectedForwarderKey{})
if !ok {
return ""
return nil
}
return v.(string)
return v.(*selectedForwarderVal)
}

func storeForwarderName(ctx context.Context, v string) {
metadata.Map(ctx, false).Store(selectedForworderKey{}, v)
func storeForwarder(ctx context.Context, v *selectedForwarderVal) {
metadata.Map(ctx, false).Store(selectedForwarderKey{}, v)
}
26 changes: 15 additions & 11 deletions pkg/networkservice/common/discoverforwarder/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ func NewServer(nsClient registry.NetworkServiceRegistryClient, nseClient registr
return result
}

// nolint:gocyclo
func (d *discoverForwarderServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
var forwarderName = loadForwarderName(ctx)
var forwarder = loadForwarder(ctx)
var logger = log.FromContext(ctx).WithField("discoverForwarderServer", "request")

if forwarderName == "" {
if forwarder == nil || !forwarder.active {
ns, err := d.discoverNetworkService(ctx, request.GetConnection().GetNetworkService(), request.GetConnection().GetPayload())
if err != nil {
return nil, err
Expand Down Expand Up @@ -117,7 +118,10 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks

resp, err := next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, u), request.Clone())
if err == nil {
storeForwarderName(ctx, candidate.Name)
storeForwarder(ctx, &selectedForwarderVal{
name: candidate.Name,
active: true,
})
return resp, nil
}
logger.Errorf("forwarder=%v url=%v returned error=%v", candidate.Name, candidate.Url, err.Error())
Expand All @@ -129,18 +133,18 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks

stream, err := d.nseClient.Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: forwarderName,
Name: forwarder.name,
Url: d.nsmgrURL,
},
})
if err != nil {
logger.Errorf("can not open registry nse stream by forwarder name. Error: %v", err.Error())
return nil, errors.Wrapf(err, "failed to find %s on %s", forwarderName, d.nsmgrURL)
return nil, errors.Wrapf(err, "failed to find %s on %s", forwarder.name, d.nsmgrURL)
}

nses := registry.ReadNetworkServiceEndpointList(stream)
if len(nses) == 0 {
storeForwarderName(ctx, "")
storeForwarder(ctx, nil)
return nil, errors.New("forwarder not found")
}

Expand All @@ -152,27 +156,27 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks

conn, err := next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, u), request)
if err != nil {
storeForwarderName(ctx, "")
forwarder.active = false
}
return conn, err
}

func (d *discoverForwarderServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
var forwarderName = loadForwarderName(ctx)
var forwarder = loadForwarder(ctx)
var logger = log.FromContext(ctx).WithField("discoverForwarderServer", "request")
if forwarderName == "" {
if forwarder == nil {
return nil, errors.New("forwarder is not selected")
}

stream, err := d.nseClient.Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: forwarderName,
Name: forwarder.name,
Url: d.nsmgrURL,
},
})
if err != nil {
logger.Errorf("can not open registry nse stream by forwarder name. Error: %v", err.Error())
return nil, errors.Wrapf(err, "failed to find %s on %s", forwarderName, d.nsmgrURL)
return nil, errors.Wrapf(err, "failed to find %s on %s", forwarder.name, d.nsmgrURL)
}

nses := registry.ReadNetworkServiceEndpointList(stream)
Expand Down

0 comments on commit 613fd0f

Please sign in to comment.