-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
31358: workload: add pgx-based SQLRunner, use it in KV r=RaduBerinde a=RaduBerinde SQLRunner is a common facility for issuing SQL queries through `pgx`. It supports multiple issuing methods, specified by the `--method` flag: - prepare: each statement is prepared once per session and then reused. - noprepare: each statement is executed directly (which internally is equivalent to preparing and executing each statement separately). - simple: parameters are formatted as text and replaced in the query before it is issued. KV is switched to use the new runner. The default `prepare` method should be similar to the old code (except that we go through `pgx`). Benchmark numbers (ops/s for kv80, single node GCE worker): - before: 30990 (541 stddev) - prepare (default): 31478 (331 stddev) - noprepare: 25506 (530 stddev) - simple: 29187 (599 stddev) Release note: None Co-authored-by: Radu Berinde <[email protected]>
- Loading branch information
Showing
4 changed files
with
352 additions
and
30 deletions.
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
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
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,110 @@ | ||
// Copyright 2018 The Cockroach 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. See the AUTHORS file | ||
// for names of contributors. | ||
|
||
package workload | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
|
||
"github.com/jackc/pgx" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// MultiConnPool maintains a set of pgx ConnPools (to different servers). | ||
type MultiConnPool struct { | ||
Pools []*pgx.ConnPool | ||
// Atomic counter used by Get(). | ||
counter uint32 | ||
} | ||
|
||
// NewMultiConnPool creates a new MultiConnPool (with one pool per url). | ||
// The pools have approximately the same number of max connections, adding up to | ||
// maxTotalConnections. | ||
func NewMultiConnPool(maxTotalConnections int, urls ...string) (*MultiConnPool, error) { | ||
m := &MultiConnPool{ | ||
Pools: make([]*pgx.ConnPool, len(urls)), | ||
} | ||
for i := range urls { | ||
cfg, err := pgx.ParseConnectionString(urls[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Use the average number of remaining connections (this handles | ||
// rounding). | ||
numConn := maxTotalConnections / (len(urls) - i) | ||
maxTotalConnections -= numConn | ||
p, err := pgx.NewConnPool(pgx.ConnPoolConfig{ | ||
ConnConfig: cfg, | ||
MaxConnections: numConn, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// "Warm up" the pool so we don't have to establish connections later (which | ||
// would affect the observed latencies of the first requests). We do this by | ||
// acquiring all connections (in parallel), then releasing them back to the | ||
// pool. | ||
conns := make([]*pgx.Conn, numConn) | ||
var g errgroup.Group | ||
for i := range conns { | ||
i := i | ||
g.Go(func() error { | ||
conns[i], err = p.Acquire() | ||
return err | ||
}) | ||
} | ||
if err := g.Wait(); err != nil { | ||
return nil, err | ||
} | ||
for _, c := range conns { | ||
p.Release(c) | ||
} | ||
|
||
m.Pools[i] = p | ||
} | ||
return m, nil | ||
} | ||
|
||
// Get returns one of the pools, in round-robin manner. | ||
func (m *MultiConnPool) Get() *pgx.ConnPool { | ||
i := atomic.AddUint32(&m.counter, 1) - 1 | ||
return m.Pools[i%uint32(len(m.Pools))] | ||
} | ||
|
||
// PrepareEx prepares the given statement on all the pools. | ||
func (m *MultiConnPool) PrepareEx( | ||
ctx context.Context, name, sql string, opts *pgx.PrepareExOptions, | ||
) (*pgx.PreparedStatement, error) { | ||
var ps *pgx.PreparedStatement | ||
for _, p := range m.Pools { | ||
var err error | ||
ps, err = p.PrepareEx(ctx, name, sql, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
// It doesn't matter which PreparedStatement we return, they should be the | ||
// same. | ||
return ps, nil | ||
} | ||
|
||
// Close closes all the pools. | ||
func (m *MultiConnPool) Close() { | ||
for _, p := range m.Pools { | ||
p.Close() | ||
} | ||
} |
Oops, something went wrong.