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

Rework adapters #543

Merged
merged 4 commits into from
Oct 28, 2020
Merged

Rework adapters #543

merged 4 commits into from
Oct 28, 2020

Conversation

xzfc
Copy link
Contributor

@xzfc xzfc commented Oct 19, 2020

This PR fixes several problems in adapters:

  1. In nested adapters case, the old adapter may run some requests (a) in the wrong order, or (b) repeat them twice or more. See Rework adapters to prevent calling chain elements twice #535 (comment)

  2. Nesting order. Adapters are now nest requests in the same way as chain and next do. See Rework adapters to prevent calling chain elements twice #535 (comment)

  3. Adapters are now thread-safe.

Tests contain two checks that look similar but serve different purposes:

  1. Context passing: check that the adapter passes the context correctly.
  2. Execution order: check that each chain element is called exactly once (or never called if some chain element intentionally doesn't call the next chain element). Also, it checks the nesting order.

Closes #535.

@@ -224,7 +224,7 @@ func (f *updateTokenServerSuite) TestChain() {

got, err := server.Request(context.Background(), request)
require.Equal(t, 3, len(got.Path.PathSegments))
require.Equal(t, 2, int(got.Path.Index))
require.Equal(t, 0, int(got.Path.Index))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context:

elements := []networkservice.NetworkServiceServer{
adapters.NewClientToServer(next.NewNetworkServiceClient(updatepath.NewClient("nsc-1"), updatetoken.NewClient(TokenGenerator))),
updatepath.NewServer("local-nsm-1"),
updatetoken.NewServer(TokenGenerator),
adapters.NewClientToServer(next.NewNetworkServiceClient(updatepath.NewClient("local-nsm-1"), updatetoken.NewClient(TokenGenerator))),
updatepath.NewServer("remote-nsm-1"),
updatetoken.NewServer(TokenGenerator),
adapters.NewClientToServer(next.NewNetworkServiceClient(updatepath.NewClient("remote-nsm-1"), updatetoken.NewClient(TokenGenerator)))}
server := next.NewNetworkServiceServer(elements...)
got, err := server.Request(context.Background(), request)
require.Equal(t, 3, len(got.Path.PathSegments))
require.Equal(t, 0, int(got.Path.Index))

Path.Index is now restored by updatetoken on line 215.
This is changed because of the restored nesting order.

pkg/networkservice/core/adapters/networkservice.go Outdated Show resolved Hide resolved
return next.Client(ctx).Close(ctx, in, opts...)
}

func appendContext(ctxPtr *context.Context, prefix, str string) func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think strings.Builder could reduce strings copying for these tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests run in 0.038s on CI, so I think it doesn't worth optimizing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see these test run in 1s on windows

github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters	1.044s

It is not super critical but It could be faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On windows, every test run in at least one second, even such simple as clienturl. It looks like every test on windows delayed exactly by 1 second (or time measured incorrectly).

Copy link
Member

@denis-tingaikin denis-tingaikin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think we should apply the same patch for registry chains https://github.com/networkservicemesh/sdk/tree/master/pkg/registry/core/adapters.

@xzfc
Copy link
Contributor Author

xzfc commented Oct 20, 2020

Ready to merge.

Applying the same patch for registry chains (#543 (review)) isn't straightforward because we have to deal with gRPC streams in the Find method, so it's better to be done in a separate PR.

@xzfc
Copy link
Contributor Author

xzfc commented Oct 20, 2020

Update: slightly reworked, now adapters use next.NewNetworkServiceClient instead of context.WithValue to pass the client. (and same for server) The basic logic remains the same.

Reason: make adapters resistant to context clearing. I.e. the following code works fine now:

next.Client(ctx).Request(context.Background() /* ← note this */, request, args...)

@edwarnicke
Copy link
Member

Update: slightly reworked, now adapters use next.NewNetworkServiceClient instead of context.WithValue to pass the client. (and same for server) The basic logic remains the same.

Reason: make adapters resistant to context clearing. I.e. the following code works fine now:

next.Client(ctx).Request(context.Background() /* ← note this */, request, args...)

Oh... that's slick :)

Copy link
Member

@denis-tingaikin denis-tingaikin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I like these changes.
I think also we can add a few simple benchmark tests to track the performance of adapters.

pkg/registry/core/adapters/nse_next_server.go Outdated Show resolved Hide resolved
pkg/registry/core/adapters/ns_registry.go Outdated Show resolved Hide resolved
pkg/registry/core/adapters/ns_registry.go Outdated Show resolved Hide resolved
@xzfc
Copy link
Contributor Author

xzfc commented Oct 26, 2020

Benchmarks on my machine, comparing the old and new implementations.

pkg/networkservice/core/adapters Old New1 New2
BenchmarkServerToClient-8 686 596 598
BenchmarkClientToServer-8 700 589 587
BenchmarkDeepNested-8 96000 12669 12704
pkg/registry/core/adapters Old New2
BenchmarkNSServerFind-8 1679 1705
BenchmarkNSClientFind-8 2725 2669
BenchmarkNSEServerFind-8 1684 1747
BenchmarkNSEClientFind-8 2677 2694

Times in ns/op.
Old is f7fb405 (from master).
New1 is the first context-based adapters (previous version of this PR).
New2 is the next-based adapters (the current version of this PR).
Only the Find method benchmarked in registry adapters, as the rest should be the same as in the core benchmarks.

@denis-tingaikin
Copy link
Member

@xzfc could you add the current benchmark result into README.md of pkg?

Copy link
Member

@denis-tingaikin denis-tingaikin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -0,0 +1,9 @@
This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xzfc Could you add the same README.md file for registry adapters?

@@ -0,0 +1,9 @@
This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.
## Intro
This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.

Signed-off-by: Albert Safin <albert.safin@xored.com>
Signed-off-by: Albert Safin <albert.safin@xored.com>
Signed-off-by: Albert Safin <albert.safin@xored.com>
Signed-off-by: Albert Safin <albert.safin@xored.com>
@denis-tingaikin denis-tingaikin merged commit dfb11c9 into networkservicemesh:master Oct 28, 2020
nsmbot pushed a commit to networkservicemesh/sdk-kernel that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-registry-memory that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-registry-proxy-dns that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nsmgr-proxy that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nsmgr that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-icmp-responder that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/sdk-vppagent that referenced this pull request Oct 28, 2020
…k@master networkservicemesh/sdk#543

networkservicemesh/sdk PR link: networkservicemesh/sdk#543

networkservicemesh/sdk commit message:
commit dfb11c96a95752aee842105d70c7f4dbe0dc7c60
Author: xzfc <xzfc@users.noreply.github.com>
Date:   Wed Oct 28 06:31:10 2020 +0000

    Rework adapters (#543)

    * NS adapters: rework, update tests, add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * NS adapters: use next instead of context

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: rework

    Signed-off-by: Albert Safin <albert.safin@xored.com>

    * Registry adapters: add benchmarks and readme

    Signed-off-by: Albert Safin <albert.safin@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
illbegood pushed a commit to illbegood/sdk that referenced this pull request Dec 20, 2020
* NS adapters: rework, update tests, add benchmarks and readme

Signed-off-by: Albert Safin <albert.safin@xored.com>

* NS adapters: use next instead of context

Signed-off-by: Albert Safin <albert.safin@xored.com>

* Registry adapters: rework

Signed-off-by: Albert Safin <albert.safin@xored.com>

* Registry adapters: add benchmarks and readme

Signed-off-by: Albert Safin <albert.safin@xored.com>
Signed-off-by: Sergey Ershov <sergey.ershov@xored.com>
illbegood pushed a commit to illbegood/sdk that referenced this pull request Dec 23, 2020
* NS adapters: rework, update tests, add benchmarks and readme

Signed-off-by: Albert Safin <albert.safin@xored.com>

* NS adapters: use next instead of context

Signed-off-by: Albert Safin <albert.safin@xored.com>

* Registry adapters: rework

Signed-off-by: Albert Safin <albert.safin@xored.com>

* Registry adapters: add benchmarks and readme

Signed-off-by: Albert Safin <albert.safin@xored.com>
Signed-off-by: Sergey Ershov <sergey.ershov@xored.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework adapters to prevent calling chain elements twice
3 participants