Files
tasks/common/persistence/sql/sqlplugin/sqlite/plugin.go
T
Tim Deeb-SwihartandGitHub be7b5514a5 Reconnect to SQL databases when connections fail (#5926)
## What changed?

Both our PostgreSQL and MySQL database backends will now automatically
reconnect to the database when certain errors occur: all errors chosen
have been experienced when testing this behavior through an AWS Aurora
RDS failover of either MySQL or PostgreSQL.

For both backends we will reconnect when we see:
- `ECONNRESET`
- `ECONNABORTED`
- `ECONNREFUSED`
- `io.EOF`
- `io.ErrUnexpectedEOF`
- `database/sql/driver.ErrBadConn`

for postgres we will also reconnect on the following SQLStates:
- `25006` read-only transaction
- `57P03` cannot connect now
- `0A000` feature not supported, but ONLY when the message is `cannot
set transaction read-write mode during recovery`

for mysql we will also reconnect when we see the following error codes:
- `1040` too many connections 
- `1792` read-only transaction (SQLstate `25006`)
- `1836` running in read-only mode

This logic is easily extensible should we discover more failure modes
over time

## Why?

We've had multiple community reports of Temporal problems during RDS
failover. One part of this is the fact that we wouldn't necessarily
reconnect; we were at the whims of our chosen SQL abstraction's
connection pooling logic.

## How did you test it?

I manually tested this functionality in the presence of repeated RDS
failovers: 

- [x] postgres12 plugin with pq driver
- [x] postgres12 plugin with pgx driver
- [x] mysql plugin

Automated testing will be added to our regular testing pipelines once
our infrastructure friends have added the support I need (it's in
progress)

## Potential risks

We're concerned there's a correctness issue in our PostgreSQL backend
that's related to our behavior during an RDS failover. If we merge this
before I figure out what's going on, we could hide the issue and make it
harder to reproduce.

## Documentation
N/A

## Is hotfix candidate?
Yes?
2024-05-17 16:59:15 -07:00

224 lines
6.1 KiB
Go

// The MIT License
//
// Copyright (c) 2021 Datadog, Inc.
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package sqlite
import (
"fmt"
"net/url"
"sort"
"strings"
"github.com/iancoleman/strcase"
"github.com/jmoiron/sqlx"
"golang.org/x/exp/maps"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/persistence/sql"
"go.temporal.io/server/common/persistence/sql/sqlplugin"
"go.temporal.io/server/common/resolver"
sqliteschema "go.temporal.io/server/schema/sqlite"
)
const (
// PluginName is the name of the plugin
PluginName = "sqlite"
)
// List of non-pragma parameters
// Taken from https://www.sqlite.org/uri.html
var queryParameters = map[string]struct{}{
"cache": {},
"immutable": {},
"mode": {},
"modeof": {},
"nolock": {},
"psow": {},
"setup": {},
"vfs": {},
}
type plugin struct {
connPool *connPool
}
var sqlitePlugin = &plugin{}
func init() {
sqlitePlugin.connPool = newConnPool()
sql.RegisterPlugin(PluginName, sqlitePlugin)
}
// CreateDB initialize the db object
func (p *plugin) CreateDB(
dbKind sqlplugin.DbKind,
cfg *config.SQL,
r resolver.ServiceResolver,
_ log.Logger,
_ metrics.Handler,
) (sqlplugin.DB, error) {
conn, err := p.connPool.Allocate(cfg, r, p.createDBConnection)
if err != nil {
return nil, err
}
db := newDB(dbKind, cfg.DatabaseName, conn, nil)
db.OnClose(func() { p.connPool.Close(cfg) }) // remove reference
return db, nil
}
// CreateAdminDB initialize the db object
func (p *plugin) CreateAdminDB(
dbKind sqlplugin.DbKind,
cfg *config.SQL,
r resolver.ServiceResolver,
_ log.Logger,
_ metrics.Handler,
) (sqlplugin.AdminDB, error) {
conn, err := p.connPool.Allocate(cfg, r, p.createDBConnection)
if err != nil {
return nil, err
}
db := newDB(dbKind, cfg.DatabaseName, conn, nil)
db.OnClose(func() { p.connPool.Close(cfg) }) // remove reference
return db, nil
}
// createDBConnection creates a returns a reference to a logical connection to the
// underlying SQL database. The returned object is tied to a single
// SQL database and the object can be used to perform CRUD operations on
// the tables in the database.
func (p *plugin) createDBConnection(
cfg *config.SQL,
_ resolver.ServiceResolver,
) (*sqlx.DB, error) {
dsn, err := buildDSN(cfg)
if err != nil {
return nil, fmt.Errorf("error building DSN: %w", err)
}
db, err := sqlx.Connect(goSqlDriverName, dsn)
if err != nil {
return nil, err
}
// The following options are set based on advice from https://github.com/mattn/go-sqlite3#faq
//
// Dealing with the error `database is locked`
// > ... set the database connections of the SQL package to 1.
db.SetMaxOpenConns(1)
// Settings for in-memory database (should be fine for file mode as well)
// > Note that if the last database connection in the pool closes, the in-memory database is deleted.
// > Make sure the max idle connection limit is > 0, and the connection lifetime is infinite.
db.SetMaxIdleConns(1)
db.SetConnMaxIdleTime(0)
// Maps struct names in CamelCase to snake without need for db struct tags.
db.MapperFunc(strcase.ToSnake)
switch {
case cfg.ConnectAttributes["mode"] == "memory":
// creates temporary DB overlay in order to configure database and schemas
if err := p.setupSQLiteDatabase(cfg, db); err != nil {
_ = db.Close()
return nil, err
}
case cfg.ConnectAttributes["setup"] == "true": // file mode, optional setting to setup the schema
if err := p.setupSQLiteDatabase(cfg, db); err != nil && !isTableExistsError(err) { // benign error indicating tables already exist
_ = db.Close()
return nil, err
}
}
return db, nil
}
func (p *plugin) setupSQLiteDatabase(cfg *config.SQL, conn *sqlx.DB) error {
db := newDB(sqlplugin.DbKindUnknown, cfg.DatabaseName, conn, nil)
defer func() { _ = db.Close() }()
err := db.CreateDatabase(cfg.DatabaseName)
if err != nil {
return err
}
// init tables
return sqliteschema.SetupSchemaOnDB(db)
}
func buildDSN(cfg *config.SQL) (string, error) {
if cfg.ConnectAttributes == nil {
cfg.ConnectAttributes = make(map[string]string)
}
vals, err := buildDSNAttr(cfg)
if err != nil {
return "", err
}
dsn := fmt.Sprintf(
"file:%s?%v",
cfg.DatabaseName,
vals.Encode(),
)
return dsn, nil
}
func buildDSNAttr(cfg *config.SQL) (url.Values, error) {
parameters := url.Values{}
// sort ConnectAttributes to get a deterministic order
keys := maps.Keys(cfg.ConnectAttributes)
sort.Strings(keys)
for _, k := range keys {
key := strings.TrimSpace(k)
value := strings.TrimSpace(cfg.ConnectAttributes[k])
if parameters.Get(key) != "" {
return nil, fmt.Errorf("duplicate connection attr: %v:%v, %v:%v",
key,
parameters.Get(key),
key, value,
)
}
if _, isValidQueryParameter := queryParameters[key]; isValidQueryParameter {
parameters.Set(key, value)
continue
}
// assume pragma
parameters.Add("_pragma", fmt.Sprintf("%s=%s", key, value))
}
// set time format
parameters.Add("_time_format", "sqlite")
return parameters, nil
}