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

fix: registry/refresh clients doesn't refresh NSEs registration in case of floating interdomain #1004

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
10 changes: 5 additions & 5 deletions pkg/registry/common/refresh/nse_registry_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ func (c *refreshNSEClient) Register(ctx context.Context, nse *registry.NetworkSe
}

if reg.ExpirationTime != nil {
cancel, err = c.startRefresh(ctx, reg.Clone(), expirationDuration)
refreshNSE := nse.Clone()
refreshNSE.ExpirationTime = reg.ExpirationTime

cancel, err = c.startRefresh(ctx, refreshNSE, expirationDuration)
if err != nil {
if _, unregisterErr := next.NetworkServiceEndpointRegistryServer(ctx).Unregister(ctx, reg); unregisterErr != nil {
logger.Errorf("failed to unregister endpoint on error: %s %s", reg.Name, unregisterErr.Error())
}
return nil, err
}

c.nseCancels.Store(reg.Name, cancel)
c.nseCancels.Store(refreshNSE.Name, cancel)
}

return reg, err
Expand Down Expand Up @@ -117,9 +120,6 @@ func (c *refreshNSEClient) startRefresh(ctx context.Context, nse *registry.Netwo
defer cancel()

reg, err := next.NetworkServiceEndpointRegistryClient(ctx).Register(registerCtx, nse.Clone())
if err == nil && reg.Name != nse.Name {
err = errors.Errorf("renamed to %s", reg.Name)
}
if err != nil {
logger.Errorf("failed to refresh endpoint registration: %s %s", nse.Name, err.Error())
return
Expand Down
51 changes: 51 additions & 0 deletions pkg/registry/common/refresh/nse_registry_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (

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

"github.com/networkservicemesh/sdk/pkg/registry/common/null"
"github.com/networkservicemesh/sdk/pkg/registry/common/refresh"
"github.com/networkservicemesh/sdk/pkg/registry/common/serialize"
"github.com/networkservicemesh/sdk/pkg/registry/core/next"
"github.com/networkservicemesh/sdk/pkg/registry/utils/checks/checknse"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
"github.com/networkservicemesh/sdk/pkg/tools/clockmock"
"github.com/networkservicemesh/sdk/pkg/tools/interdomain"
)

const (
Expand All @@ -44,13 +46,62 @@ const (
testTick = testWait / 100
)

type injectNSERegisterClient struct {
registry.NetworkServiceEndpointRegistryClient
register func(context.Context, *registry.NetworkServiceEndpoint, ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error)
}

func (c *injectNSERegisterClient) Register(ctx context.Context, nse *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) {
return c.register(ctx, nse, opts...)
}

func testNSE(clockTime clock.Clock) *registry.NetworkServiceEndpoint {
return &registry.NetworkServiceEndpoint{
Name: "nse-1",
ExpirationTime: timestamppb.New(clockTime.Now().Add(expireTimeout)),
}
}

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

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

clockMock := clockmock.New(ctx)
ctx = clock.WithClock(ctx, clockMock)

var registerCount int32

client := next.NewNetworkServiceEndpointRegistryClient(
serialize.NewNetworkServiceEndpointRegistryClient(),
refresh.NewNetworkServiceEndpointRegistryClient(ctx),
&injectNSERegisterClient{
NetworkServiceEndpointRegistryClient: null.NewNetworkServiceEndpointRegistryClient(),
register: func(c context.Context, nse *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) {
resp := nse.Clone()
resp.Name = interdomain.Target(nse.Name)
require.Equal(t, "nse-1@my.domain.com", nse.Name)
atomic.AddInt32(&registerCount, 1)
return next.NetworkServiceEndpointRegistryClient(ctx).Register(ctx, resp, opts...)
},
},
)

nse := testNSE(clockMock)
nse.Name = "nse-1@my.domain.com"
reg, err := client.Register(ctx, nse)
require.NoError(t, err)

clockMock.Add(expireTimeout)
require.Eventually(t, func() bool {
return atomic.LoadInt32(&registerCount) > 1
}, testWait, testTick)

_, err = client.Unregister(ctx, reg)
require.NoError(t, err)
}

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

Expand Down