Skip to content

Commit

Permalink
Add registration time chain element
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 Nov 8, 2021
1 parent c57ed95 commit aadc33c
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/registry/chains/memory/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/expire"
"github.com/networkservicemesh/sdk/pkg/registry/common/memory"
"github.com/networkservicemesh/sdk/pkg/registry/common/proxy"
"github.com/networkservicemesh/sdk/pkg/registry/common/registrationtime"
"github.com/networkservicemesh/sdk/pkg/registry/common/serialize"
"github.com/networkservicemesh/sdk/pkg/registry/common/setlogoption"
"github.com/networkservicemesh/sdk/pkg/registry/common/setpayload"
Expand All @@ -41,6 +42,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU
nseChain := chain.NewNetworkServiceEndpointRegistryServer(
setlogoption.NewNetworkServiceEndpointRegistryServer(map[string]string{}),
serialize.NewNetworkServiceEndpointRegistryServer(),
registrationtime.NewNetworkServiceEndpointRegistryServer(),
expire.NewNetworkServiceEndpointRegistryServer(ctx, expiryDuration),
checkid.NewNetworkServiceEndpointRegistryServer(),
memory.NewNetworkServiceEndpointRegistryServer(),
Expand Down
18 changes: 18 additions & 0 deletions pkg/registry/common/registrationtime/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 registrationtime provides registry server chain elements for initial registration time setting
package registrationtime
56 changes: 56 additions & 0 deletions pkg/registry/common/registrationtime/nse_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 registrationtime

import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/registry"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
)

type regtimeNSEServer struct {}

// NewNetworkServiceEndpointRegistryServer creates a new NetworkServiceServer chain element that sets initial
// registration time.
func NewNetworkServiceEndpointRegistryServer() registry.NetworkServiceEndpointRegistryServer {
return &regtimeNSEServer{}
}

func (r *regtimeNSEServer) Register(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*registry.NetworkServiceEndpoint, error) {
if nse.InitialRegistrationTime == nil {
nse.InitialRegistrationTime = timestamppb.New(clock.FromContext(ctx).Now())
}

reg, err := next.NetworkServiceEndpointRegistryServer(ctx).Register(ctx, nse)
if err != nil {
return nil, err
}
return reg, nil
}

func (r *regtimeNSEServer) Find(q *registry.NetworkServiceEndpointQuery, s registry.NetworkServiceEndpointRegistry_FindServer) error {
return next.NetworkServiceEndpointRegistryServer(s.Context()).Find(q, s)
}

func (r *regtimeNSEServer) Unregister(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*empty.Empty, error) {
return next.NetworkServiceEndpointRegistryServer(ctx).Unregister(ctx, nse)
}
100 changes: 100 additions & 0 deletions pkg/registry/common/registrationtime/nse_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 registrationtime_test

import (
"context"
"testing"
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/networkservicemesh/sdk/pkg/registry/common/registrationtime"
"github.com/networkservicemesh/sdk/pkg/registry/core/streamchannel"

"google.golang.org/protobuf/proto"

"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/sdk/pkg/registry/common/memory"
"github.com/networkservicemesh/sdk/pkg/registry/core/next"
)

func testNSE() *registry.NetworkServiceEndpoint {
return &registry.NetworkServiceEndpoint{
Name: "nse",
Url: "tcp://0.0.0.0",
}
}

func TestRegTimeServer_Register(t *testing.T) {
mem := memory.NewNetworkServiceEndpointRegistryServer()

s := next.NewNetworkServiceEndpointRegistryServer(
registrationtime.NewNetworkServiceEndpointRegistryServer(),
mem,
)

// 1. Register
reg, err := s.Register(context.Background(), testNSE())
require.NoError(t, err)
require.NotNil(t, reg.InitialRegistrationTime)
require.GreaterOrEqual(t, timestamppb.Now().GetSeconds(), reg.InitialRegistrationTime.GetSeconds())

registeredNse := reg.Clone()

nses := find(t, mem, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint),
})

require.Len(t, nses, 1)
require.True(t, proto.Equal(nses[0].InitialRegistrationTime, registeredNse.InitialRegistrationTime))

// 2. Refresh
reg, err = s.Register(context.Background(), reg.Clone())
require.NoError(t, err)
require.NotNil(t, reg.InitialRegistrationTime)
require.True(t, proto.Equal(reg.InitialRegistrationTime, registeredNse.InitialRegistrationTime))

// 3. Unregister
_, err = s.Unregister(context.Background(), reg.Clone())
require.NoError(t, err)

// 4. Register again
reg, err = s.Register(context.Background(), testNSE())
require.NoError(t, err)
require.NotNil(t, reg.InitialRegistrationTime)
require.False(t, proto.Equal(reg.InitialRegistrationTime, registeredNse.InitialRegistrationTime))
}

func find(t *testing.T, mem registry.NetworkServiceEndpointRegistryServer, query *registry.NetworkServiceEndpointQuery) (nses []*registry.NetworkServiceEndpoint) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

ch := make(chan *registry.NetworkServiceEndpointResponse)
go func() {
defer close(ch)
require.NoError(t, mem.Find(query, streamchannel.NewNetworkServiceEndpointFindServer(ctx, ch)))
}()

for nseResp := range ch {
nses = append(nses, nseResp.NetworkServiceEndpoint)
}

return nses
}

0 comments on commit aadc33c

Please sign in to comment.