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

grpc filter use object pool #977

Merged
merged 13 commits into from
May 22, 2023

Conversation

sodaRyCN
Copy link
Contributor

@sodaRyCN sodaRyCN commented Apr 4, 2023

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Apr 4, 2023

Codecov Report

Patch coverage: 47.82% and project coverage change: +0.28 🎉

Comparison is base (f870f90) 73.05% compared to head (3af8469) 73.33%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #977      +/-   ##
==========================================
+ Coverage   73.05%   73.33%   +0.28%     
==========================================
  Files         134      136       +2     
  Lines       14866    15184     +318     
==========================================
+ Hits        10860    11135     +275     
- Misses       3380     3416      +36     
- Partials      626      633       +7     
Impacted Files Coverage Δ
pkg/object/grpcserver/mux.go 50.46% <0.00%> (+1.07%) ⬆️
pkg/filters/proxies/grpcproxy/pool.go 14.41% <40.47%> (+3.56%) ⬆️
pkg/util/objectpool/objectpool.go 26.66% <47.36%> (-3.53%) ⬇️
pkg/filters/proxies/grpcproxy/proxy.go 49.10% <59.18%> (+0.49%) ⬆️

... and 6 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@localvar localvar left a comment

Choose a reason for hiding this comment

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

hi @sodaRyCN , please checkout my comments, basically, I think this PR brings too much couplings to the code.

pkg/util/objectpool/objectpool.go Outdated Show resolved Hide resolved
pkg/util/objectpool/objectpool.go Outdated Show resolved Hide resolved
Comment on lines 190 to 197
if value, ok = m.pools.Load(key); !ok {
m.lock.Lock()
defer m.lock.Unlock()
if value, _ = m.pools.Load(key); !ok {
value = NewWithSpec(m.spec, ctx)
defer m.pools.Store(key, value)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use sync.Map.LoadOrStore to refactor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, but func LoadOrStore means that a pool will be created every time, and when the initNum of the pool's spec is non-zero, it will create some unused connections at the same time, then these connections need to be closed if it is load instead of store.
'LoadOrStore' cannot achieve an effect like this
func LoadOrStore(key interface{}, value func () interface{}) {
if key exists {
return old;
} else {
newValue = value()
store newValue
.....
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can do it like this, though there's still possibility that an unnecessary object being created, but it should be fine now.

Suggested change
if value, ok = m.pools.Load(key); !ok {
m.lock.Lock()
defer m.lock.Unlock()
if value, _ = m.pools.Load(key); !ok {
value = NewWithSpec(m.spec, ctx)
defer m.pools.Store(key, value)
}
}
if value, ok = m.pools.Load(key); !ok {
value = NewWithSpec(m.spec, ctx)
value, _ = m.pools.LoadOrStore(key, value)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the case of concurrency, more than one Pool woule be created by L191, and the initnum of these pools may be 100、200, etc., and so many PoolObject instances that will not be used will be created.
L192, the variable value is reassigned, It is not good for the PoolObjects that need to explicitly call destroy or pool.Close, such as the underlying connection, variables will be GC, but the connection still exists.
Based on the above reasons, I have a tried like the follow before deciding to use sync.Mutex, but it have a problem:newValue.Close will always be called.

if value, ok = m.pools.Load(key); !ok {
	       newValue = NewWithSpec(m.spec, ctx)
	       if value, ok = m.pools.LoadOrStore(key, newValue) ; ok{
                  newValue.Close()
               }
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

I checked the code, this has not been updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I said, using sync.Map seems inappropriate in this case.
On the one hand it creates some unused object and needs to call Destroy, on the other hand, as shown in the code example, LoadOrStore(key, value any) (actual any, loaded bool), loaded always returns true, and newValue.Close always be called. Of course, we could solve this bug like the following code (use object equals instead of loaded):

if value, ok = m.pools.Load(key); !ok {
	       newValue = NewWithSpec(m.spec, ctx)
	       if value, _= m.pools.LoadOrStore(key, newValue) ; value != newValue  {
                  newValue.Close()
               }
}

For the reasons, I think it would be better to use sync.Mutex instead of LoadOrStore.

pkg/filters/proxies/grpcproxy/proxy.go Outdated Show resolved Hide resolved
pkg/filters/proxies/grpcproxy/proxy.go Outdated Show resolved Hide resolved
pkg/util/objectpool/objectpool.go Outdated Show resolved Hide resolved
@sodaRyCN sodaRyCN closed this Apr 22, 2023
@sodaRyCN sodaRyCN reopened this Apr 23, 2023
@sodaRyCN sodaRyCN closed this Apr 23, 2023
@sodaRyCN sodaRyCN reopened this Apr 23, 2023
@localvar localvar added this pull request to the merge queue May 22, 2023
Merged via the queue into easegress-io:main with commit bd4cc3a May 22, 2023
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.

None yet

4 participants