-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[vtadmin] cluster rpc pools #8421
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
db46646
Add RPCPool implementation
ajm188 d19a2cd
Restructure parseOne to remove the if/else chain
ajm188 5eb069b
Add RPCPoolConfig and read-only rpc pools, plus supporting code
ajm188 c5bf994
Gate all rpcs with their respective pools
ajm188 c22713c
Update tests to ensure that pools are initialized
ajm188 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,91 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
|
||
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 pools | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// RPCPool is a specialized version of the ResourcePool, for bounding concurrent | ||
// access to making RPC calls. | ||
// | ||
// Whether you use this, or a sync2.Semaphore to gate RPCs (or shared access to | ||
// any shared resource) depends on what timeout semantics you need. A sync2.Semaphore | ||
// takes a global timeout, which applies to calls to Acquire(); if you need to | ||
// respect a context's deadline, you can call AcquireContext(context.Context), | ||
// but that ignores the global timeout. Conversely, an RPCPool provides only | ||
// one method of acquisition, Acquire(context.Context), which always uses the | ||
// lower of the pool-global timeout or the context deadline. | ||
type RPCPool struct { | ||
rp *ResourcePool | ||
waitTimeout time.Duration | ||
} | ||
|
||
// NewRPCPool returns an RPCPool with the given size and wait timeout. A zero | ||
// timeout will be ignored on calls to Acquire, and only the context deadline | ||
// will be used. If a logWait function is provided, it will be called whenever | ||
// a call to Acquire has to wait for a resource, but only after a successful | ||
// wait (meaning if we hit a timeout before a resource becomes available, it | ||
// will not be called). | ||
func NewRPCPool(size int, waitTimeout time.Duration, logWait func(time.Time)) *RPCPool { | ||
return &RPCPool{ | ||
rp: NewResourcePool(rpcResourceFactory, size, size, 0, size, logWait), | ||
waitTimeout: waitTimeout, | ||
} | ||
} | ||
|
||
// Acquire acquires one slot in the RPCPool. If a slot is not immediately | ||
// available, it will block until one becomes available or until a timeout is | ||
// reached. The lower of the context deadline and the pool's waitTimeout will | ||
// be used as the timeout, except when the pool's waitTimeout is zero, in which | ||
// case the context deadline will always serve as the overall timeout. | ||
// | ||
// It returns nil on successful acquisition, and an error if a timeout occurred | ||
// before a slot became available. | ||
// | ||
// Note: For every successful call to Acquire, the caller must make a | ||
// corresponding call to Release. | ||
func (pool *RPCPool) Acquire(ctx context.Context) error { | ||
if pool.waitTimeout > 0 { | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, pool.waitTimeout) | ||
defer cancel() | ||
} | ||
|
||
_, err := pool.rp.Get(ctx) | ||
return err | ||
} | ||
|
||
// Release frees a slot in the pool. It must only be called after a successful | ||
// call to Acquire. | ||
func (pool *RPCPool) Release() { pool.rp.Put(rpc) } | ||
|
||
// Close empties the pool, preventing further Acquire calls from succeeding. | ||
// It waits for all slots to be freed via Release. | ||
func (pool *RPCPool) Close() { pool.rp.Close() } | ||
|
||
type _rpc struct{} | ||
|
||
var rpc = &_rpc{} | ||
|
||
// Close implements Resource for _rpc. | ||
func (*_rpc) Close() {} | ||
|
||
// we only ever return the same rpc pointer. it's used as a sentinel and is | ||
// only used internally so using the same one over and over doesn't matter. | ||
func rpcResourceFactory(ctx context.Context) (Resource, error) { return rpc, nil } |
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,83 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
|
||
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 pools | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRPCPool(t *testing.T) { | ||
t.Parallel() | ||
|
||
pool := NewRPCPool(1, time.Millisecond*100, nil) | ||
|
||
err := pool.Acquire(context.Background()) | ||
require.NoError(t, err) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*5) | ||
err = pool.Acquire(ctx) | ||
cancel() | ||
assert.Error(t, err) | ||
|
||
pool.Release() | ||
err = pool.Acquire(context.Background()) | ||
require.NoError(t, err) | ||
|
||
t.Run("context timeout exceeded", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50) | ||
defer cancel() | ||
|
||
err := pool.Acquire(ctx) | ||
assert.Error(t, err) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
default: | ||
assert.Fail(t, "calling context should be done after failed Acquire") | ||
} | ||
}) | ||
|
||
t.Run("pool timeout exceeded", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500) | ||
defer cancel() | ||
|
||
err := pool.Acquire(ctx) | ||
assert.Error(t, err) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
assert.Fail(t, "calling context should not be done after failed Acquire") | ||
default: | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkRPCPoolAllocs(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
NewRPCPool(1000, 0, nil) | ||
// p.Close() | ||
} | ||
} |
Oops, something went wrong.
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.
Does
context.WithTimeout
automatically respect the lesser of the given timeout or thectx
(first parameter)'s timeout? 🤔 I think I am missing something since the docs say: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.
Yes, you can't extend a timeout in a context, only shorten it.
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.
Yeah, it's in the docs for WithDeadline: