-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix InitialRegistrationTime in case of reregistration (#1456)
* Fix InitialRegistrationTime in case of reregistration Signed-off-by: Artem Glazychev <artem.glazychev@xored.com> * Add registry metadata Signed-off-by: Artem Glazychev <artem.glazychev@xored.com> * Use metadata for setregistrationtime Signed-off-by: Artem Glazychev <artem.glazychev@xored.com> * linter fix Signed-off-by: Artem Glazychev <artem.glazychev@xored.com> --------- Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
- Loading branch information
1 parent
ad70c1f
commit 63043b2
Showing
15 changed files
with
963 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2023 Cisco 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 setregistrationtime | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
|
||
"google.golang.org/protobuf/reflect/protoreflect" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/utils/metadata" | ||
) | ||
|
||
type key struct{} | ||
|
||
// store sets the initialRegistrationTime stored in per NSE metadata. | ||
func store(ctx context.Context, initialRegistrationTime protoreflect.ProtoMessage) { | ||
metadata.Map(ctx, false).Store(key{}, proto.Clone(initialRegistrationTime)) | ||
} | ||
|
||
// load returns the initialRegistrationTime stored in per NSE metadata, | ||
func load(ctx context.Context) (value *timestamppb.Timestamp, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, false).Load(key{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(*timestamppb.Timestamp) | ||
return value, ok | ||
} | ||
|
||
// deleteTime deletes the initialRegistrationTime stored in per NSE metadata, | ||
func deleteTime(ctx context.Context) { | ||
metadata.Map(ctx, false).Delete(key{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) 2023 Cisco 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 injecterror | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/core/next" | ||
) | ||
|
||
type injectErrorNSServer struct { | ||
registerErrorSupplier, findErrorSupplier, unregisterErrorSupplier *errorSupplier | ||
} | ||
|
||
// NewNetworkServiceRegistryServer returns a server chain element returning error on Register/Find/Unregister on given times | ||
func NewNetworkServiceRegistryServer(opts ...Option) registry.NetworkServiceRegistryServer { | ||
o := &options{ | ||
err: errors.New("error originates in injectErrorNSServer"), | ||
registerErrorTimes: []int{-1}, | ||
findErrorTimes: []int{-1}, | ||
unregisterErrorTimes: []int{-1}, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
|
||
return &injectErrorNSServer{ | ||
registerErrorSupplier: &errorSupplier{ | ||
err: o.err, | ||
errorTimes: o.registerErrorTimes, | ||
}, | ||
findErrorSupplier: &errorSupplier{ | ||
err: o.err, | ||
errorTimes: o.findErrorTimes, | ||
}, | ||
unregisterErrorSupplier: &errorSupplier{ | ||
err: o.err, | ||
errorTimes: o.unregisterErrorTimes, | ||
}, | ||
} | ||
} | ||
|
||
func (c *injectErrorNSServer) Register(ctx context.Context, in *registry.NetworkService) (*registry.NetworkService, error) { | ||
if err := c.registerErrorSupplier.supply(); err != nil { | ||
return nil, err | ||
} | ||
return next.NetworkServiceRegistryServer(ctx).Register(ctx, in) | ||
} | ||
|
||
func (c *injectErrorNSServer) Find(query *registry.NetworkServiceQuery, server registry.NetworkServiceRegistry_FindServer) error { | ||
if err := c.findErrorSupplier.supply(); err != nil { | ||
return err | ||
} | ||
return next.NetworkServiceRegistryServer(server.Context()).Find(query, server) | ||
} | ||
|
||
func (c *injectErrorNSServer) Unregister(ctx context.Context, in *registry.NetworkService) (*empty.Empty, error) { | ||
if err := c.unregisterErrorSupplier.supply(); err != nil { | ||
return nil, err | ||
} | ||
return next.NetworkServiceRegistryServer(ctx).Unregister(ctx, in) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) 2023 Cisco 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 metadata | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/edwarnicke/genericsync" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
) | ||
|
||
type metaDataKey struct{} | ||
|
||
type metaData struct { | ||
client sync.Map | ||
server sync.Map | ||
} | ||
|
||
func store(parent context.Context, id string, mdMap *genericsync.Map[string, *metaData]) context.Context { | ||
if _, ok := parent.Value(metaDataKey{}).(*metaData); !ok { | ||
md, _ := mdMap.LoadOrStore(id, &metaData{}) | ||
return context.WithValue(parent, metaDataKey{}, md) | ||
} | ||
return parent | ||
} | ||
|
||
func load(parent context.Context, id string, mdMap *genericsync.Map[string, *metaData]) context.Context { | ||
if _, ok := parent.Value(metaDataKey{}).(*metaData); !ok { | ||
if md, mdOk := mdMap.Load(id); mdOk { | ||
return context.WithValue(parent, metaDataKey{}, md) | ||
} | ||
} | ||
return parent | ||
} | ||
|
||
func del(parent context.Context, id string, mdMap *genericsync.Map[string, *metaData]) context.Context { | ||
if _, ok := parent.Value(metaDataKey{}).(*metaData); !ok { | ||
if md, ok := mdMap.LoadAndDelete(id); ok { | ||
return context.WithValue(parent, metaDataKey{}, md) | ||
} | ||
return context.WithValue(parent, metaDataKey{}, new(metaData)) | ||
} | ||
return parent | ||
} | ||
|
||
// Map - Return the client (or server) per Connection.Id metadata *sync.Map | ||
func Map(parent context.Context, isClient bool) *sync.Map { | ||
m, ok := parent.Value(metaDataKey{}).(*metaData) | ||
if !ok || m == nil { | ||
panic("please add metadata chain element to your chain") | ||
} | ||
if isClient { | ||
return &m.client | ||
} | ||
return &m.server | ||
} | ||
|
||
// IsClient - returns true if in implements networkservice.NetworkServiceClient | ||
func IsClient(in interface{}) bool { | ||
_, ok := in.(networkservice.NetworkServiceClient) | ||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2023 Cisco 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 metadata provides per nsName/nseName metadata | ||
package metadata |
Oops, something went wrong.