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 clientmap.RefcountMap, add tests, add bench tests. #610

Merged
merged 6 commits into from
Dec 9, 2020

Conversation

Bolodya1997
Copy link

Issue

Existing refcount map is incorrect, such test can fail:

var m clientmap.RefcountMap
m.Store("a", null.NewClient())

var loaded bool
go func() {
    _, loaded = m.Load("a") // count += 1 (if exists)
}()
go m.Delete("a") // count -= 1 (delete if 0)

_, exists := m.Load("a")
require.Equal(t, loaded, exists) // if loaded, Delete can't delete, only should decrement the count

Solution

Rework refcount map with per-entry locks, add action tests, add bench tests.

@Bolodya1997 Bolodya1997 mentioned this pull request Dec 1, 2020
@Bolodya1997 Bolodya1997 marked this pull request as draft December 1, 2020 07:55
@Bolodya1997 Bolodya1997 marked this pull request as ready for review December 1, 2020 08:09
@denis-tingaikin
Copy link
Member

denis-tingaikin commented Dec 1, 2020

@Bolodya1997

Existing refcount map is incorrect, such test can fail:

var loaded bool
go func() {
_, loaded = m.Load("a") // count += 1 (if exists)
}()

Is test failing if work with loaded flag synchronized?

@Bolodya1997
Copy link
Author

Is test failing if work with loaded flag synchronized?

Yep, it is not the line to line test. In the real test there was sync.WaitGroup additionally used.

Vladimir Popov added 2 commits December 1, 2020 15:37
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
pkg/tools/clientmap/bench_test.go Show resolved Hide resolved
pkg/tools/clientmap/refcount.go Show resolved Hide resolved
pkg/tools/clientmap/refcount.go Outdated Show resolved Hide resolved
pkg/tools/clientmap/refcount.go Show resolved Hide resolved
pkg/tools/clientmap/refcount.go Outdated Show resolved Hide resolved
pkg/tools/clientmap/refcount.go Outdated Show resolved Hide resolved
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

func BenchmarkMap(b *testing.B) {
var m clientmap.Map
b.SetParallelism(1000)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to use 1000 here?

Copy link
Author

Choose a reason for hiding this comment

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

You are right, there is no need to use such big numbers in here. Our case is:

  • executors count > IDs count (multiple connections to single clientURL)
  • IDs count and executors count should be some prime numbers to be sure that all types of operations are executed on all ID+executor pairs

Probably something like 23 executors and 11 IDs is well enough for us in such case.

var m clientmap.Map
b.SetParallelism(1000)
b.RunParallel(func(pb *testing.PB) {
client := null.NewClient()
Copy link
Member

Choose a reason for hiding this comment

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

This allocation could a little bit affects results.

Copy link
Author

Choose a reason for hiding this comment

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

I have tried with it and without it, it doesn't affect the results.

Copy link
Member

@denis-tingaikin denis-tingaikin Dec 8, 2020

Choose a reason for hiding this comment

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

I guess it can affect tests with cmd go test -v -bench=. -benchmem

Copy link
Author

Choose a reason for hiding this comment

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

Fixed with per-test readonly client.

"github.com/stretchr/testify/assert"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/null"
"github.com/networkservicemesh/sdk/pkg/tools/clientmap"
)

const (
parallelCount = 1000
Copy link
Member

Choose a reason for hiding this comment

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

Why 1000?

Copy link
Author

Choose a reason for hiding this comment

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

My fault it is not actually the parallelCount but the retriesCount.
TestRefcountMap_Actions is not the test fully validating the correctness of the RefcountMap. It should eventually fail if RefcountMap is invalid. So if we start increasing retries count, such eventually is becoming most probably.
1000 is good, because it is not too big to make test performing > 1 sec (10000 is too big for such case) and not too small to keep eventually somewhere far away from us.

@@ -1,3 +1,5 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
Copy link
Member

Choose a reason for hiding this comment

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

I feel this test can be simplified.

Please take a look at tests from sync.Map https://golang.org/src/sync/map_test.go

Copy link
Author

@Bolodya1997 Bolodya1997 Dec 7, 2020

Choose a reason for hiding this comment

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

It is the only way to add some eventually failing on incorrect implementation test I see. There are no such tests actually in the sync.Map tests. Such tests should be very helpful for someone who will try to optimize the RefcountMap implementation.

Vladimir Popov added 2 commits December 7, 2020 18:16
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
@denis-tingaikin denis-tingaikin merged commit 1080307 into networkservicemesh:master Dec 9, 2020
nsmbot pushed a commit to networkservicemesh/sdk-vpp that referenced this pull request Dec 9, 2020
…k@master networkservicemesh/sdk#610

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

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

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

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

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

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

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/sdk-kernel that referenced this pull request Dec 9, 2020
…k@master networkservicemesh/sdk#610

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

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

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

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

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

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

networkservicemesh/sdk PR link: networkservicemesh/sdk#610

networkservicemesh/sdk commit message:
commit 1080307813ee9db83b78bf0a3331b7def61258e4
Author: Vladimir Popov <vladimir.popov@xored.com>
Date:   Wed Dec 9 15:01:32 2020 +0700

    Rework clientmap.RefcountMap, add tests, add bench tests. (#610)

    * Rework clientmap.RefcountMap, add tests, add bench tests.

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Make RefcountMap.LoadAndDelete(), .Delete() return deleted

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Add README, fix copyright issues

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix naming issue§

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

    * Fix bench tests

    Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
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.

2 participants