2019-12-11 19:04:09 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package cockroachutil
|
|
|
|
|
|
|
|
import (
|
2020-01-10 21:34:54 +00:00
|
|
|
"context"
|
2019-12-11 19:04:09 +00:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2020-05-15 19:31:22 +01:00
|
|
|
"io"
|
2019-12-11 19:04:09 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-06-28 04:56:29 +01:00
|
|
|
"github.com/jackc/pgx/v4/stdlib"
|
2020-01-10 21:34:54 +00:00
|
|
|
"github.com/zeebo/errs"
|
2020-06-28 04:56:29 +01:00
|
|
|
|
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
2019-12-11 19:04:09 +00:00
|
|
|
)
|
|
|
|
|
2020-06-28 04:56:29 +01:00
|
|
|
// Driver is the type for the "cockroach" sql/database driver. It uses
|
|
|
|
// github.com/jackc/pgx/v4/stdlib under the covers because of Cockroach's
|
|
|
|
// PostgreSQL compatibility, but allows differentiation between pg and crdb
|
|
|
|
// connections.
|
2019-12-11 19:04:09 +00:00
|
|
|
type Driver struct {
|
2020-06-28 04:56:29 +01:00
|
|
|
pgxDriver stdlib.Driver
|
2019-12-11 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens a new cockroachDB connection.
|
|
|
|
func (cd *Driver) Open(name string) (driver.Conn, error) {
|
2020-01-03 19:13:57 +00:00
|
|
|
name = translateName(name)
|
2020-06-28 04:56:29 +01:00
|
|
|
conn, err := cd.pgxDriver.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pgxStdlibConn, ok := conn.(*stdlib.Conn)
|
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("Conn from pgx is not a *stdlib.Conn??? T: %T", conn)
|
|
|
|
}
|
|
|
|
return &cockroachConn{pgxStdlibConn}, nil
|
2019-12-11 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenConnector obtains a new db Connector, which sql.DB can use to
|
|
|
|
// obtain each needed connection at the appropriate time.
|
|
|
|
func (cd *Driver) OpenConnector(name string) (driver.Connector, error) {
|
2020-01-03 19:13:57 +00:00
|
|
|
name = translateName(name)
|
2020-06-28 04:56:29 +01:00
|
|
|
pgxConnector, err := cd.pgxDriver.OpenConnector(name)
|
2019-12-11 19:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-28 04:56:29 +01:00
|
|
|
return &cockroachConnector{driver: cd, pgxConnector: pgxConnector}, nil
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cockroachConnector is a thin wrapper around a pq-based connector. This allows
|
|
|
|
// Driver to supply our custom cockroachConn type for connections.
|
|
|
|
type cockroachConnector struct {
|
2020-06-28 04:56:29 +01:00
|
|
|
driver *Driver
|
|
|
|
pgxConnector driver.Connector
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Driver returns the driver being used for this connector.
|
|
|
|
func (c *cockroachConnector) Driver() driver.Driver {
|
2020-06-28 04:56:29 +01:00
|
|
|
return c.driver
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect creates a new connection using the connector.
|
|
|
|
func (c *cockroachConnector) Connect(ctx context.Context) (driver.Conn, error) {
|
2020-06-28 04:56:29 +01:00
|
|
|
pgxConn, err := c.pgxConnector.Connect(ctx)
|
2020-01-10 21:34:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-28 04:56:29 +01:00
|
|
|
pgxStdlibConn, ok := pgxConn.(*stdlib.Conn)
|
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("Conn from pgx is not a *stdlib.Conn??? T: %T", pgxConn)
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
2020-06-28 04:56:29 +01:00
|
|
|
return &cockroachConn{pgxStdlibConn}, nil
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type connAll interface {
|
|
|
|
driver.Conn
|
|
|
|
driver.ConnBeginTx
|
|
|
|
driver.ExecerContext
|
|
|
|
driver.QueryerContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// cockroachConn is a connection to a database. It is not used concurrently by multiple goroutines.
|
|
|
|
type cockroachConn struct {
|
2020-06-28 04:56:29 +01:00
|
|
|
underlying *stdlib.Conn
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that cockroachConn fulfills connAll.
|
|
|
|
var _ connAll = (*cockroachConn)(nil)
|
|
|
|
|
|
|
|
// Close closes the cockroachConn.
|
|
|
|
func (c *cockroachConn) Close() error {
|
|
|
|
return c.underlying.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecContext (when implemented by a driver.Conn) provides ExecContext
|
|
|
|
// functionality to a sql.DB instance. This implementation provides
|
|
|
|
// retry semantics for single statements.
|
|
|
|
func (c *cockroachConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
|
|
|
result, err := c.underlying.ExecContext(ctx, query, args)
|
2020-05-26 16:58:05 +01:00
|
|
|
for err != nil && !c.isInTransaction() && NeedsRetry(err) {
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-01-10 21:34:54 +00:00
|
|
|
result, err = c.underlying.ExecContext(ctx, query, args)
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2020-05-15 19:31:22 +01:00
|
|
|
type cockroachRows struct {
|
|
|
|
rows driver.Rows
|
|
|
|
firstResults []driver.Value
|
|
|
|
eof bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Columns returns the names of the columns.
|
|
|
|
func (rows *cockroachRows) Columns() []string {
|
|
|
|
return rows.rows.Columns()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the rows iterator.
|
|
|
|
func (rows *cockroachRows) Close() error {
|
|
|
|
return rows.rows.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next implements the Next method on driver.Rows.
|
|
|
|
func (rows *cockroachRows) Next(dest []driver.Value) error {
|
|
|
|
if rows.eof {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
if rows.firstResults == nil {
|
|
|
|
return rows.rows.Next(dest)
|
|
|
|
}
|
|
|
|
copy(dest, rows.firstResults)
|
|
|
|
rows.firstResults = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapRows(rows driver.Rows) (crdbRows *cockroachRows, err error) {
|
|
|
|
columns := rows.Columns()
|
|
|
|
dest := make([]driver.Value, len(columns))
|
|
|
|
err = rows.Next(dest)
|
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, io.EOF) {
|
2020-05-15 19:31:22 +01:00
|
|
|
return &cockroachRows{rows: rows, firstResults: nil, eof: true}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cockroachRows{rows: rows, firstResults: dest}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:34:54 +00:00
|
|
|
// QueryContext (when implemented by a driver.Conn) provides QueryContext
|
|
|
|
// functionality to a sql.DB instance. This implementation provides
|
|
|
|
// retry semantics for single statements.
|
2020-05-20 19:48:13 +01:00
|
|
|
func (c *cockroachConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Rows, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-05-15 19:31:22 +01:00
|
|
|
for {
|
|
|
|
result, err := c.underlying.QueryContext(ctx, query, args)
|
|
|
|
if err != nil {
|
2020-05-26 16:58:05 +01:00
|
|
|
if NeedsRetry(err) {
|
2020-05-15 19:31:22 +01:00
|
|
|
if c.isInTransaction() {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-05-15 19:31:22 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrappedResult, err := wrapRows(result)
|
|
|
|
if err != nil {
|
|
|
|
// If this returns an error it's probably the same error
|
|
|
|
// we got from calling Next inside wrapRows.
|
|
|
|
_ = result.Close()
|
2020-05-26 16:58:05 +01:00
|
|
|
if NeedsRetry(err) {
|
2020-05-15 19:31:22 +01:00
|
|
|
if c.isInTransaction() {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-05-15 19:31:22 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wrappedResult, nil
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin starts a new transaction.
|
|
|
|
func (c *cockroachConn) Begin() (driver.Tx, error) {
|
|
|
|
return c.BeginTx(context.Background(), driver.TxOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeginTx begins a new transaction using the specified context and with the specified options.
|
|
|
|
func (c *cockroachConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
|
|
|
return c.underlying.BeginTx(ctx, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare prepares a statement for future execution.
|
|
|
|
func (c *cockroachConn) Prepare(query string) (driver.Stmt, error) {
|
|
|
|
pqStmt, err := c.underlying.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
adapted, ok := pqStmt.(stmtAll)
|
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("Stmt type %T does not provide stmtAll?!", adapted)
|
|
|
|
}
|
|
|
|
return &cockroachStmt{underlyingStmt: adapted, conn: c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type transactionStatus byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
txnStatusIdle transactionStatus = 'I'
|
|
|
|
txnStatusIdleInTransaction transactionStatus = 'T'
|
|
|
|
txnStatusInFailedTransaction transactionStatus = 'E'
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *cockroachConn) txnStatus() transactionStatus {
|
2020-06-28 04:56:29 +01:00
|
|
|
pgConn := c.underlying.Conn().PgConn()
|
|
|
|
return transactionStatus(pgConn.TxStatus())
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cockroachConn) isInTransaction() bool {
|
|
|
|
txnStatus := c.txnStatus()
|
|
|
|
return txnStatus == txnStatusIdleInTransaction || txnStatus == txnStatusInFailedTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
type stmtAll interface {
|
|
|
|
driver.Stmt
|
|
|
|
driver.StmtExecContext
|
|
|
|
driver.StmtQueryContext
|
|
|
|
}
|
|
|
|
|
|
|
|
type cockroachStmt struct {
|
|
|
|
underlyingStmt stmtAll
|
|
|
|
conn *cockroachConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that cockroachStmt satisfies StmtExecContext and StmtQueryContext.
|
|
|
|
var _ stmtAll = (*cockroachStmt)(nil)
|
|
|
|
|
|
|
|
// Close closes a prepared statement.
|
|
|
|
func (stmt *cockroachStmt) Close() error {
|
|
|
|
return stmt.underlyingStmt.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumInput returns the number of placeholder parameters.
|
|
|
|
func (stmt *cockroachStmt) NumInput() int {
|
|
|
|
return stmt.underlyingStmt.NumInput()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes a SQL statement in the background context.
|
|
|
|
func (stmt *cockroachStmt) Exec(args []driver.Value) (driver.Result, error) {
|
|
|
|
// since (driver.Stmt).Exec() is deprecated, we translate our Value args to NamedValue args
|
|
|
|
// and pass in background context to ExecContext instead.
|
|
|
|
namedArgs := make([]driver.NamedValue, len(args))
|
|
|
|
for i, arg := range args {
|
|
|
|
namedArgs[i] = driver.NamedValue{Ordinal: i + 1, Value: arg}
|
|
|
|
}
|
|
|
|
result, err := stmt.underlyingStmt.ExecContext(context.Background(), namedArgs)
|
2020-05-26 16:58:05 +01:00
|
|
|
for err != nil && !stmt.conn.isInTransaction() && NeedsRetry(err) {
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-01-10 21:34:54 +00:00
|
|
|
result, err = stmt.underlyingStmt.ExecContext(context.Background(), namedArgs)
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query executes a query in the background context.
|
|
|
|
func (stmt *cockroachStmt) Query(args []driver.Value) (driver.Rows, error) {
|
|
|
|
// since (driver.Stmt).Query() is deprecated, we translate our Value args to NamedValue args
|
|
|
|
// and pass in background context to QueryContext instead.
|
|
|
|
namedArgs := make([]driver.NamedValue, len(args))
|
|
|
|
for i, arg := range args {
|
|
|
|
namedArgs[i] = driver.NamedValue{Ordinal: i + 1, Value: arg}
|
|
|
|
}
|
2020-05-15 19:31:22 +01:00
|
|
|
return stmt.QueryContext(context.Background(), namedArgs)
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecContext executes SQL statements in the specified context.
|
|
|
|
func (stmt *cockroachStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
|
|
|
result, err := stmt.underlyingStmt.ExecContext(ctx, args)
|
2020-05-26 16:58:05 +01:00
|
|
|
for err != nil && !stmt.conn.isInTransaction() && NeedsRetry(err) {
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-01-10 21:34:54 +00:00
|
|
|
result, err = stmt.underlyingStmt.ExecContext(ctx, args)
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryContext executes a query in the specified context.
|
2020-05-20 19:48:13 +01:00
|
|
|
func (stmt *cockroachStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ driver.Rows, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-05-15 19:31:22 +01:00
|
|
|
for {
|
|
|
|
result, err := stmt.underlyingStmt.QueryContext(ctx, args)
|
|
|
|
if err != nil {
|
2020-05-26 16:58:05 +01:00
|
|
|
if NeedsRetry(err) {
|
2020-05-15 19:31:22 +01:00
|
|
|
if stmt.conn.isInTransaction() {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-05-15 19:31:22 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrappedResult, err := wrapRows(result)
|
|
|
|
if err != nil {
|
|
|
|
// If this returns an error it's probably the same error
|
|
|
|
// we got from calling Next inside wrapRows.
|
|
|
|
_ = result.Close()
|
2020-05-26 16:58:05 +01:00
|
|
|
if NeedsRetry(err) {
|
2020-05-15 19:31:22 +01:00
|
|
|
if stmt.conn.isInTransaction() {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-29 15:45:46 +01:00
|
|
|
mon.Event("needed_retry")
|
2020-05-15 19:31:22 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wrappedResult, nil
|
2020-01-10 21:34:54 +00:00
|
|
|
}
|
2019-12-11 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 19:13:57 +00:00
|
|
|
// translateName changes the scheme name in a `cockroach://` URL to
|
2020-06-28 04:56:29 +01:00
|
|
|
// `postgres://`, as that is what jackc/pgx will expect.
|
2020-01-03 19:13:57 +00:00
|
|
|
func translateName(name string) string {
|
2019-12-11 19:04:09 +00:00
|
|
|
if strings.HasPrefix(name, "cockroach://") {
|
|
|
|
name = "postgres://" + name[12:]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:58:05 +01:00
|
|
|
// NeedsRetry checks if the error code means a retry is needed,
|
|
|
|
// borrowed from code in crdb.
|
|
|
|
func NeedsRetry(err error) bool {
|
2020-06-28 04:56:29 +01:00
|
|
|
code := pgutil.ErrorCode(err)
|
2020-05-29 15:45:46 +01:00
|
|
|
|
|
|
|
// 57P01 occurs when a CRDB node rejoins the cluster but is not ready to accept connections
|
|
|
|
// CRDB support recommended a retry at this point
|
|
|
|
// Support ticket: https://support.cockroachlabs.com/hc/en-us/requests/5510
|
|
|
|
// TODO re-evaluate this if support provides a better solution
|
|
|
|
return code == "40001" || code == "CR000" || code == "57P01"
|
2019-12-11 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 04:56:29 +01:00
|
|
|
var defaultDriver = &Driver{}
|
2019-12-11 19:04:09 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-06-28 04:56:29 +01:00
|
|
|
sql.Register("cockroach", defaultDriver)
|
2019-12-11 19:04:09 +00:00
|
|
|
}
|