-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add cleanup chain element #1246
Merged
denis-tingaikin
merged 2 commits into
networkservicemesh:main
from
glazychev-art:cleanup
Mar 16, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2022 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 cleanup_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"go.uber.org/goleak" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/begin" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/cleanup" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
func TestCleanUp_CtxDone(t *testing.T) { | ||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||
chainCtx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
counter := new(count.Client) | ||
|
||
client := chain.NewNetworkServiceClient( | ||
begin.NewClient(), | ||
metadata.NewClient(), | ||
cleanup.NewClient(chainCtx), | ||
counter, | ||
) | ||
req := &networkservice.NetworkServiceRequest{ | ||
Connection: &networkservice.Connection{Id: "nsc-1"}, | ||
} | ||
_, err := client.Request(context.Background(), req) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, counter.Requests()) | ||
require.Equal(t, 0, counter.Closes()) | ||
cancel() | ||
|
||
require.Eventually(t, func() bool { | ||
return counter.Closes() == 1 | ||
}, time.Millisecond*100, time.Millisecond*10) | ||
} | ||
|
||
func TestCleanUp_Close(t *testing.T) { | ||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||
chainCtx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
counter := new(count.Client) | ||
|
||
client := chain.NewNetworkServiceClient( | ||
begin.NewClient(), | ||
metadata.NewClient(), | ||
cleanup.NewClient(chainCtx), | ||
counter, | ||
) | ||
req := &networkservice.NetworkServiceRequest{ | ||
Connection: &networkservice.Connection{Id: "nsc-1"}, | ||
} | ||
conn, err := client.Request(context.Background(), req) | ||
require.NoError(t, err) | ||
|
||
_, _ = client.Close(context.Background(), conn) | ||
require.Equal(t, 1, counter.Closes()) | ||
cancel() | ||
require.Never(t, func() bool { | ||
return counter.Closes() > 1 | ||
}, time.Millisecond*100, time.Millisecond*10) | ||
} | ||
|
||
func TestCleanUp_Chan(t *testing.T) { | ||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||
chainCtx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
counter := new(count.Client) | ||
|
||
doneCh := make(chan struct{}) | ||
client := chain.NewNetworkServiceClient( | ||
begin.NewClient(), | ||
metadata.NewClient(), | ||
cleanup.NewClient(chainCtx, cleanup.WithDoneChan(doneCh)), | ||
counter, | ||
) | ||
|
||
requestsNumber := 500 | ||
for i := 0; i < requestsNumber; i++ { | ||
req := &networkservice.NetworkServiceRequest{ | ||
Connection: &networkservice.Connection{Id: fmt.Sprintf("nsc-%v", i)}, | ||
} | ||
_, err := client.Request(context.Background(), req) | ||
require.NoError(t, err) | ||
} | ||
|
||
cancel() | ||
<-doneCh | ||
|
||
require.Equal(t, counter.Closes(), requestsNumber) | ||
} |
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,124 @@ | ||
// Copyright (c) 2022 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 cleanup | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
|
||
"github.com/edwarnicke/serialize" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/begin" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/clientconn" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
) | ||
|
||
type cleanupClient struct { | ||
chainCtx context.Context | ||
|
||
ccClose bool | ||
doneCh chan struct{} | ||
activeConns int32 | ||
executor serialize.Executor | ||
} | ||
|
||
// NewClient - returns a cleanup client chain element | ||
func NewClient(ctx context.Context, opts ...Option) networkservice.NetworkServiceClient { | ||
o := &options{} | ||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
c := &cleanupClient{ | ||
chainCtx: ctx, | ||
ccClose: o.ccClose, | ||
doneCh: o.doneCh, | ||
} | ||
go func() { | ||
<-c.chainCtx.Done() | ||
if atomic.LoadInt32(&c.activeConns) == 0 && c.doneCh != nil { | ||
c.executor.AsyncExec(func() { | ||
select { | ||
case <-c.doneCh: | ||
default: | ||
close(c.doneCh) | ||
} | ||
}) | ||
} | ||
}() | ||
return c | ||
} | ||
|
||
func (c *cleanupClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { | ||
conn, err := next.Client(ctx).Request(ctx, request, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Update active connections counter. Needed for a cleanup done notification. | ||
atomic.AddInt32(&c.activeConns, 1) | ||
if cancel, ok := loadAndDeleteCancel(ctx); ok { | ||
cancel() | ||
} | ||
|
||
cancelCtx, cancel := context.WithCancel(context.Background()) | ||
storeCancel(ctx, cancel) | ||
|
||
factory := begin.FromContext(ctx) | ||
go func() { | ||
select { | ||
case <-c.chainCtx.Done(): | ||
// Add to metadata if we want to delete clientconn | ||
if c.ccClose { | ||
storeCC(ctx) | ||
} | ||
|
||
<-factory.Close(begin.CancelContext(cancelCtx)) | ||
atomic.AddInt32(&c.activeConns, -1) | ||
|
||
if atomic.LoadInt32(&c.activeConns) == 0 && c.doneCh != nil { | ||
c.executor.AsyncExec(func() { | ||
select { | ||
case <-c.doneCh: | ||
default: | ||
close(c.doneCh) | ||
} | ||
}) | ||
} | ||
case <-cancelCtx.Done(): | ||
atomic.AddInt32(&c.activeConns, -1) | ||
} | ||
}() | ||
return conn, err | ||
} | ||
|
||
func (c *cleanupClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
if cancel, ok := loadAndDeleteCancel(ctx); ok { | ||
if _, ok := loadAndDeleteCC(ctx); ok { | ||
if cc, ok := clientconn.Load(ctx); ok { | ||
if closable, ok := cc.(interface{ Close() error }); ok { | ||
_ = closable.Close() | ||
} | ||
clientconn.Delete(ctx) | ||
} | ||
} | ||
cancel() | ||
} | ||
return next.Client(ctx).Close(ctx, conn, opts...) | ||
} |
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) 2022 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 cleanup provides networkservice.NetworkService chain elements to clean up resources before termination | ||
package cleanup |
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,58 @@ | ||
// Copyright (c) 2022 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 cleanup | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
type keyCancel struct{} | ||
type keyCC struct{} | ||
|
||
// storeCancel sets the context.CancelFunc stored in per Connection.Id metadata. | ||
func storeCancel(ctx context.Context, cancel context.CancelFunc) { | ||
metadata.Map(ctx, true).Store(keyCancel{}, cancel) | ||
} | ||
|
||
// loadAndDeleteCancel deletes the context.CancelFunc stored in per Connection.Id metadata, | ||
// returning the previous value if any. The loaded result reports whether the key was present. | ||
func loadAndDeleteCancel(ctx context.Context) (value context.CancelFunc, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, true).LoadAndDelete(keyCancel{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(context.CancelFunc) | ||
return value, ok | ||
} | ||
|
||
// storeCC sets the flag to delete clientconn in per Connection.Id metadata. | ||
func storeCC(ctx context.Context) { | ||
metadata.Map(ctx, true).Store(keyCC{}, struct{}{}) | ||
} | ||
|
||
// loadAndDeleteCC deletes the flag stored in per Connection.Id metadata, | ||
// returning the previous value if any. The loaded result reports whether the key was present. | ||
func loadAndDeleteCC(ctx context.Context) (value struct{}, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, true).LoadAndDelete(keyCC{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(struct{}) | ||
return value, 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,39 @@ | ||
// Copyright (c) 2022 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 cleanup | ||
|
||
type options struct { | ||
ccClose bool | ||
doneCh chan struct{} | ||
} | ||
|
||
// Option - options for the cleanup chain element | ||
type Option func(*options) | ||
|
||
// WithCCClose - closes clientconn to prevent calling requests/closes on other datapath objects | ||
func WithCCClose() Option { | ||
return func(o *options) { | ||
o.ccClose = true | ||
} | ||
} | ||
|
||
// WithDoneChan - receives a channel to notify the end of cleaning | ||
func WithDoneChan(doneCh chan struct{}) Option { | ||
return func(o *options) { | ||
o.doneCh = doneCh | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to something readable?