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

[vtadmin] cluster rpc pools #8421

Merged
merged 5 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions go/pools/rpc_pool.go
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)
Copy link
Contributor

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 the ctx (first parameter)'s timeout? 🤔 I think I am missing something since the docs say:

// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).

Copy link
Member

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.

Copy link
Contributor Author

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:

WithDeadline returns a copy of the parent context with the deadline adjusted to be no later than d. If the parent's deadline is already earlier than d, WithDeadline(parent, d) is semantically equivalent to parent.

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 }
83 changes: 83 additions & 0 deletions go/pools/rpc_pool_test.go
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()
}
}
Loading