-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a different connection pool for read vs. write operations
- Loading branch information
Showing
8 changed files
with
249 additions
and
32 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
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 |
---|---|---|
|
@@ -36,10 +36,13 @@ type crateReadResponse struct { | |
} | ||
|
||
type crateEndpoint struct { | ||
pool *pgxpool.Pool | ||
poolConf *pgxpool.Config | ||
readTimeout time.Duration | ||
writeTimeout time.Duration | ||
poolConf *pgxpool.Config | ||
readPoolSize int | ||
writePoolSize int | ||
readTimeout time.Duration | ||
writeTimeout time.Duration | ||
readPool *pgxpool.Pool | ||
writePool *pgxpool.Pool | ||
} | ||
|
||
func newCrateEndpoint(ep *endpointConfig) *crateEndpoint { | ||
|
@@ -64,8 +67,11 @@ func newCrateEndpoint(ep *endpointConfig) *crateEndpoint { | |
// # Example URL | ||
// postgres://jack:[email protected]:5432/mydb?sslmode=verify-ca | ||
connectionString := fmt.Sprintf( | ||
"postgres://%s:%s@%s:%v/%s?connect_timeout=%v&pool_max_conns=%v", | ||
ep.User, ep.Password, ep.Host, ep.Port, ep.Schema, ep.ConnectTimeout, ep.MaxConnections) | ||
"postgres://%s:%s@%s:%v/%s?connect_timeout=%v", | ||
ep.User, ep.Password, ep.Host, ep.Port, ep.Schema, ep.ConnectTimeout) | ||
if ep.MaxConnections != 0 { | ||
connectionString += fmt.Sprintf("&pool_max_conns=%v", ep.MaxConnections) | ||
} | ||
poolConf, err := pgxpool.ParseConfig(connectionString) | ||
if err != nil { | ||
return nil | ||
|
@@ -92,24 +98,25 @@ func newCrateEndpoint(ep *endpointConfig) *crateEndpoint { | |
return err | ||
} | ||
return &crateEndpoint{ | ||
poolConf: poolConf, | ||
readTimeout: time.Duration(ep.ReadTimeout) * time.Second, | ||
writeTimeout: time.Duration(ep.WriteTimeout) * time.Second, | ||
poolConf: poolConf, | ||
readPoolSize: ep.ReadPoolSize, | ||
writePoolSize: ep.WritePoolSize, | ||
readTimeout: time.Duration(ep.ReadTimeout) * time.Second, | ||
writeTimeout: time.Duration(ep.WriteTimeout) * time.Second, | ||
} | ||
} | ||
|
||
func (c *crateEndpoint) endpoint() endpoint.Endpoint { | ||
/** | ||
* Initialize connection pools lazily here instead of in `newCrateEndpoint()`, | ||
* so that the adapter does not crash on startup if the endpoint is unavailable. | ||
**/ | ||
return func(ctx context.Context, request interface{}) (response interface{}, err error) { | ||
// We initialize the connection pool lazily here instead of in newCrateEndpoint() so | ||
// that the adapter does not crash on startup if an endpoint is unavailable. | ||
if c.pool == nil { | ||
pool, err := pgxpool.NewWithConfig(ctx, c.poolConf) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening connection to CrateDB: %v", err) | ||
} | ||
c.pool = pool | ||
} | ||
|
||
// Initialize database connection pools. | ||
err = c.createPools(ctx) | ||
|
||
// Dispatch by request type. | ||
switch r := request.(type) { | ||
case *crateWriteRequest: | ||
return nil, c.write(ctx, r) | ||
|
@@ -121,6 +128,42 @@ func (c *crateEndpoint) endpoint() endpoint.Endpoint { | |
} | ||
} | ||
|
||
func (c *crateEndpoint) createPools(ctx context.Context) (err error) { | ||
/** | ||
* Initialize two connection pools, one for read/write each. | ||
**/ | ||
c.readPool, err = createPoolWithPoolSize(ctx, c.poolConf.Copy(), c.readPoolSize) | ||
if c.readPool == nil { | ||
c.readPool, err = createPoolWithPoolSize(ctx, c.poolConf.Copy(), c.readPoolSize) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if c.writePool == nil { | ||
c.writePool, err = createPoolWithPoolSize(ctx, c.poolConf.Copy(), c.writePoolSize) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func createPool(ctx context.Context, poolConf *pgxpool.Config) (pool *pgxpool.Pool, err error) { | ||
pool, err = pgxpool.NewWithConfig(ctx, poolConf) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening connection to CrateDB: %v", err) | ||
} else { | ||
return pool, nil | ||
} | ||
} | ||
|
||
func createPoolWithPoolSize(ctx context.Context, poolConf *pgxpool.Config, maxConns int) (pool *pgxpool.Pool, err error) { | ||
if maxConns != 0 { | ||
poolConf.MaxConns = int32(maxConns) | ||
} | ||
return createPool(ctx, poolConf) | ||
} | ||
|
||
func (c crateEndpoint) write(ctx context.Context, r *crateWriteRequest) error { | ||
batch := &pgx.Batch{} | ||
for _, a := range r.rows { | ||
|
@@ -179,7 +222,7 @@ func (c crateEndpoint) write(ctx context.Context, r *crateWriteRequest) error { | |
// | ||
ctx, _ = context.WithTimeout(ctx, c.writeTimeout) | ||
|
||
batchResults := c.pool.SendBatch(ctx, batch) | ||
batchResults := c.writePool.SendBatch(ctx, batch) | ||
var qerr error | ||
if qerr != nil { | ||
return fmt.Errorf("error executing write batch: %v", qerr) | ||
|
@@ -196,7 +239,7 @@ func (c crateEndpoint) read(ctx context.Context, r *crateReadRequest) (*crateRea | |
// pgx4 implements query timeouts using context cancellation. | ||
// See `write` function for more details. | ||
ctx, _ = context.WithTimeout(ctx, c.readTimeout) | ||
rows, err := c.pool.Query(ctx, r.stmt, nil) | ||
rows, err := c.readPool.Query(ctx, r.stmt, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("error executing read request query: %v", err) | ||
} | ||
|
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 |
---|---|---|
@@ -1,14 +1,154 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/require" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
var CPU_COUNT = int32(runtime.NumCPU()) | ||
|
||
func TestNewCrateEndpoint(t *testing.T) { | ||
conf := builtinConfig() | ||
endpoint := newCrateEndpoint(&conf.Endpoints[0]) | ||
require.Equal(t, | ||
"postgres://crate:@localhost:5432/?connect_timeout=10", | ||
endpoint.poolConf.ConnString(), | ||
) | ||
require.GreaterOrEqual(t, endpoint.poolConf.MaxConns, CPU_COUNT) | ||
} | ||
|
||
func TestConfigurePoolDefault(t *testing.T) { | ||
/** | ||
* Verify that, by default, the pool size got configured with one connection per core by default. | ||
**/ | ||
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/") | ||
require.Equal(t, "localhost", poolConf.ConnConfig.Host) | ||
require.GreaterOrEqual(t, poolConf.MaxConns, CPU_COUNT) | ||
|
||
ctx := context.Background() | ||
pool, err := createPool(ctx, poolConf) | ||
require.IsType(t, &pgxpool.Pool{}, pool) | ||
require.Equal(t, err, nil) | ||
require.GreaterOrEqual(t, pool.Config().MaxConns, CPU_COUNT) | ||
} | ||
|
||
func TestConfigurePoolWithPoolSizeFromConnectionString(t *testing.T) { | ||
/** | ||
* Verify that the pool size gets obtained from the database connection string. | ||
**/ | ||
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/?pool_max_conns=42") | ||
require.Equal(t, "localhost", poolConf.ConnConfig.Host) | ||
require.Equal(t, int32(42), poolConf.MaxConns) | ||
|
||
ctx := context.Background() | ||
pool, err := createPool(ctx, poolConf) | ||
require.IsType(t, &pgxpool.Pool{}, pool) | ||
require.Equal(t, err, nil) | ||
require.Equal(t, int32(42), pool.Config().MaxConns) | ||
} | ||
|
||
func TestConfigurePoolWithPoolSizeFromSettingsVanilla(t *testing.T) { | ||
/** | ||
* Verify that the pool size can be configured using a configuration setting. | ||
**/ | ||
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/") | ||
require.Equal(t, "localhost", poolConf.ConnConfig.Host) | ||
require.GreaterOrEqual(t, poolConf.MaxConns, CPU_COUNT) | ||
|
||
ctx := context.Background() | ||
pool, err := createPoolWithPoolSize(ctx, poolConf, 42) | ||
require.IsType(t, &pgxpool.Pool{}, pool) | ||
require.Equal(t, err, nil) | ||
require.Equal(t, int32(42), pool.Config().MaxConns) | ||
} | ||
|
||
func TestConfigurePoolWithPoolSizeFromSettingsPrecedence(t *testing.T) { | ||
/** | ||
* Verify that the pool size configuration setting takes precedence over the connection string. | ||
**/ | ||
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/?pool_max_conns=33") | ||
require.Equal(t, "localhost", poolConf.ConnConfig.Host) | ||
require.Equal(t, int32(33), poolConf.MaxConns) | ||
|
||
ctx := context.Background() | ||
pool, err := createPoolWithPoolSize(ctx, poolConf, 42) | ||
require.IsType(t, &pgxpool.Pool{}, pool) | ||
require.Equal(t, err, nil) | ||
require.Equal(t, int32(42), pool.Config().MaxConns) | ||
} | ||
|
||
func TestPoolsDefault(t *testing.T) { | ||
/** | ||
* Verify connection pool sizes when not configured explicitly. | ||
**/ | ||
conf := builtinConfig() | ||
endpoint := newCrateEndpoint(&conf.Endpoints[0]) | ||
ctx := context.Background() | ||
endpoint.createPools(ctx) | ||
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool) | ||
require.Equal(t, | ||
"postgres://crate:@localhost:5432/?connect_timeout=10", | ||
endpoint.poolConf.ConnString(), | ||
) | ||
require.GreaterOrEqual(t, endpoint.readPool.Config().MaxConns, CPU_COUNT) | ||
require.GreaterOrEqual(t, endpoint.writePool.Config().MaxConns, CPU_COUNT) | ||
} | ||
|
||
func TestPoolsWithMaxConnections(t *testing.T) { | ||
/** | ||
* Verify connection pool sizes when configured using `MaxConnections`. | ||
**/ | ||
conf := builtinConfig() | ||
conf.Endpoints[0].MaxConnections = 42 | ||
endpoint := newCrateEndpoint(&conf.Endpoints[0]) | ||
ctx := context.Background() | ||
endpoint.createPools(ctx) | ||
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool) | ||
require.Equal(t, | ||
"postgres://crate:@localhost:5432/?connect_timeout=10&pool_max_conns=42", | ||
endpoint.poolConf.ConnString(), | ||
) | ||
require.Equal(t, int32(42), endpoint.readPool.Config().MaxConns) | ||
require.Equal(t, int32(42), endpoint.writePool.Config().MaxConns) | ||
} | ||
|
||
func TestPoolsWithIndividualPoolSizes(t *testing.T) { | ||
/** | ||
* Verify connection pool sizes when configured using `ReadPoolSize` and `WritePoolSize`. | ||
**/ | ||
conf := builtinConfig() | ||
conf.Endpoints[0].ReadPoolSize = 11 | ||
conf.Endpoints[0].WritePoolSize = 22 | ||
endpoint := newCrateEndpoint(&conf.Endpoints[0]) | ||
ctx := context.Background() | ||
endpoint.createPools(ctx) | ||
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool) | ||
require.Equal(t, | ||
"postgres://crate:@localhost:5432/?connect_timeout=10", | ||
endpoint.poolConf.ConnString(), | ||
) | ||
require.Equal(t, int32(11), endpoint.readPool.Config().MaxConns) | ||
require.Equal(t, int32(22), endpoint.writePool.Config().MaxConns) | ||
} | ||
|
||
func TestPoolsWithMaxConnectionsAndIndividualPoolSizes(t *testing.T) { | ||
/** | ||
* Verify connection pool sizes when configured using `MaxConnections` and `ReadPoolSize`. | ||
**/ | ||
conf := builtinConfig() | ||
conf.Endpoints[0].MaxConnections = 5 | ||
conf.Endpoints[0].ReadPoolSize = 40 | ||
endpoint := newCrateEndpoint(&conf.Endpoints[0]) | ||
ctx := context.Background() | ||
endpoint.createPools(ctx) | ||
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool) | ||
require.Equal(t, | ||
"postgres://crate:@localhost:5432/?connect_timeout=10&pool_max_conns=5", | ||
endpoint.poolConf.ConnString(), | ||
"postgres://crate:@localhost:5432/?connect_timeout=10&pool_max_conns=5") | ||
) | ||
require.Equal(t, int32(40), endpoint.readPool.Config().MaxConns) | ||
require.Equal(t, int32(5), endpoint.writePool.Config().MaxConns) | ||
} |
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
Oops, something went wrong.