mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## 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?
226 lines
6.6 KiB
Go
226 lines
6.6 KiB
Go
// The MIT License
|
|
//
|
|
// 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 postgresql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"go.temporal.io/api/serviceerror"
|
|
|
|
"go.temporal.io/server/common/persistence/sql/sqlplugin"
|
|
)
|
|
|
|
const (
|
|
createNamespaceQuery = `INSERT INTO
|
|
namespaces (partition_id, id, name, is_global, data, data_encoding, notification_version)
|
|
VALUES($1, $2, $3, $4, $5, $6, $7)`
|
|
|
|
updateNamespaceQuery = `UPDATE namespaces
|
|
SET name = $1, data = $2, data_encoding = $3, is_global = $4, notification_version = $5
|
|
WHERE partition_id=54321 AND id = $6`
|
|
|
|
getNamespacePart = `SELECT id, name, is_global, data, data_encoding, notification_version FROM namespaces`
|
|
|
|
getNamespaceByIDQuery = getNamespacePart + ` WHERE partition_id=$1 AND id = $2`
|
|
getNamespaceByNameQuery = getNamespacePart + ` WHERE partition_id=$1 AND name = $2`
|
|
|
|
listNamespacesQuery = getNamespacePart + ` WHERE partition_id=$1 ORDER BY id LIMIT $2`
|
|
listNamespacesRangeQuery = getNamespacePart + ` WHERE partition_id=$1 AND id > $2 ORDER BY id LIMIT $3`
|
|
|
|
deleteNamespaceByIDQuery = `DELETE FROM namespaces WHERE partition_id=$1 AND id = $2`
|
|
deleteNamespaceByNameQuery = `DELETE FROM namespaces WHERE partition_id=$1 AND name = $2`
|
|
|
|
getNamespaceMetadataQuery = `SELECT notification_version FROM namespace_metadata WHERE partition_id=$1`
|
|
lockNamespaceMetadataQuery = `SELECT notification_version FROM namespace_metadata WHERE partition_id=$1 FOR UPDATE`
|
|
updateNamespaceMetadataQuery = `UPDATE namespace_metadata SET notification_version = $1 WHERE notification_version = $2 AND partition_id=$3`
|
|
)
|
|
|
|
const (
|
|
partitionID = 54321
|
|
)
|
|
|
|
var errMissingArgs = errors.New("missing one or more args for API")
|
|
|
|
// InsertIntoNamespace inserts a single row into namespaces table
|
|
func (pdb *db) InsertIntoNamespace(
|
|
ctx context.Context,
|
|
row *sqlplugin.NamespaceRow,
|
|
) (sql.Result, error) {
|
|
return pdb.ExecContext(ctx, createNamespaceQuery, partitionID, row.ID, row.Name, row.IsGlobal, row.Data, row.DataEncoding, row.NotificationVersion)
|
|
}
|
|
|
|
// UpdateNamespace updates a single row in namespaces table
|
|
func (pdb *db) UpdateNamespace(
|
|
ctx context.Context,
|
|
row *sqlplugin.NamespaceRow,
|
|
) (sql.Result, error) {
|
|
return pdb.ExecContext(ctx, updateNamespaceQuery, row.Name, row.Data, row.DataEncoding, row.IsGlobal, row.NotificationVersion, row.ID)
|
|
}
|
|
|
|
// SelectFromNamespace reads one or more rows from namespaces table
|
|
func (pdb *db) SelectFromNamespace(
|
|
ctx context.Context,
|
|
filter sqlplugin.NamespaceFilter,
|
|
) ([]sqlplugin.NamespaceRow, error) {
|
|
var res []sqlplugin.NamespaceRow
|
|
var err error
|
|
switch {
|
|
case filter.ID != nil || filter.Name != nil:
|
|
if filter.ID != nil && filter.Name != nil {
|
|
return nil, serviceerror.NewInternal("only ID or name filter can be specified for selection")
|
|
}
|
|
res, err = pdb.selectFromNamespace(ctx, filter)
|
|
case filter.PageSize != nil && *filter.PageSize > 0:
|
|
res, err = pdb.selectAllFromNamespace(ctx, filter)
|
|
default:
|
|
return nil, errMissingArgs
|
|
}
|
|
|
|
return res, err
|
|
}
|
|
|
|
func (pdb *db) selectFromNamespace(
|
|
ctx context.Context,
|
|
filter sqlplugin.NamespaceFilter,
|
|
) ([]sqlplugin.NamespaceRow, error) {
|
|
var err error
|
|
var row sqlplugin.NamespaceRow
|
|
switch {
|
|
case filter.ID != nil:
|
|
err = pdb.GetContext(ctx,
|
|
&row,
|
|
getNamespaceByIDQuery,
|
|
partitionID,
|
|
*filter.ID,
|
|
)
|
|
case filter.Name != nil:
|
|
err = pdb.GetContext(ctx,
|
|
&row,
|
|
getNamespaceByNameQuery,
|
|
partitionID,
|
|
*filter.Name,
|
|
)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []sqlplugin.NamespaceRow{row}, nil
|
|
}
|
|
|
|
func (pdb *db) selectAllFromNamespace(
|
|
ctx context.Context,
|
|
filter sqlplugin.NamespaceFilter,
|
|
) ([]sqlplugin.NamespaceRow, error) {
|
|
var err error
|
|
var rows []sqlplugin.NamespaceRow
|
|
switch {
|
|
case filter.GreaterThanID != nil:
|
|
err = pdb.SelectContext(ctx,
|
|
&rows,
|
|
listNamespacesRangeQuery,
|
|
partitionID,
|
|
*filter.GreaterThanID,
|
|
*filter.PageSize,
|
|
)
|
|
default:
|
|
err = pdb.SelectContext(ctx,
|
|
&rows,
|
|
listNamespacesQuery,
|
|
partitionID,
|
|
filter.PageSize,
|
|
)
|
|
}
|
|
return rows, err
|
|
}
|
|
|
|
// DeleteFromNamespace deletes a single row in namespaces table
|
|
func (pdb *db) DeleteFromNamespace(
|
|
ctx context.Context,
|
|
filter sqlplugin.NamespaceFilter,
|
|
) (sql.Result, error) {
|
|
var err error
|
|
var result sql.Result
|
|
switch {
|
|
case filter.ID != nil:
|
|
result, err = pdb.ExecContext(ctx,
|
|
deleteNamespaceByIDQuery,
|
|
partitionID,
|
|
filter.ID,
|
|
)
|
|
default:
|
|
result, err = pdb.ExecContext(ctx,
|
|
deleteNamespaceByNameQuery,
|
|
partitionID,
|
|
filter.Name,
|
|
)
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
// LockNamespaceMetadata acquires a write lock on a single row in namespace_metadata table
|
|
func (pdb *db) LockNamespaceMetadata(
|
|
ctx context.Context,
|
|
) (*sqlplugin.NamespaceMetadataRow, error) {
|
|
var row sqlplugin.NamespaceMetadataRow
|
|
|
|
err := pdb.GetContext(ctx,
|
|
&row.NotificationVersion,
|
|
lockNamespaceMetadataQuery,
|
|
partitionID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &row, nil
|
|
}
|
|
|
|
// SelectFromNamespaceMetadata reads a single row in namespace_metadata table
|
|
func (pdb *db) SelectFromNamespaceMetadata(
|
|
ctx context.Context,
|
|
) (*sqlplugin.NamespaceMetadataRow, error) {
|
|
var row sqlplugin.NamespaceMetadataRow
|
|
err := pdb.GetContext(ctx,
|
|
&row.NotificationVersion,
|
|
getNamespaceMetadataQuery,
|
|
partitionID,
|
|
)
|
|
return &row, err
|
|
}
|
|
|
|
// UpdateNamespaceMetadata updates a single row in namespace_metadata table
|
|
func (pdb *db) UpdateNamespaceMetadata(
|
|
ctx context.Context,
|
|
row *sqlplugin.NamespaceMetadataRow,
|
|
) (sql.Result, error) {
|
|
return pdb.ExecContext(ctx,
|
|
updateNamespaceMetadataQuery,
|
|
row.NotificationVersion+1,
|
|
row.NotificationVersion,
|
|
partitionID,
|
|
)
|
|
}
|