* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
188 lines
5.0 KiB
Go
188 lines
5.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/observability/event"
|
|
"github.com/hashicorp/go-dbw"
|
|
_ "github.com/jackc/pgx/v5"
|
|
"gorm.io/driver/postgres"
|
|
)
|
|
|
|
func init() {
|
|
dbw.InitNonCreatableFields([]string{"CreateTime", "UpdateTime"})
|
|
dbw.InitNonUpdatableFields([]string{"PublicId", "CreateTime", "UpdateTime"})
|
|
}
|
|
|
|
type DbType int
|
|
|
|
const (
|
|
UnknownDB DbType = 0
|
|
Postgres DbType = 1
|
|
)
|
|
|
|
func (db DbType) String() string {
|
|
return [...]string{
|
|
"unknown",
|
|
"postgres",
|
|
}[db]
|
|
}
|
|
|
|
func StringToDbType(dialect string) (DbType, error) {
|
|
switch dialect {
|
|
case "postgres":
|
|
return Postgres, nil
|
|
default:
|
|
return UnknownDB, fmt.Errorf("%s is an unknown dialect", dialect)
|
|
}
|
|
}
|
|
|
|
// DB is a wrapper around the ORM
|
|
type DB struct {
|
|
wrapped *atomic.Pointer[dbw.DB]
|
|
}
|
|
|
|
type closeDbFn func(context.Context)
|
|
|
|
var CloseSwappedDbDuration = 5 * time.Minute
|
|
|
|
// Swap replaces *DB's underlying database object with the one from `newDB`.
|
|
// It returns a function that calls Close() on the outgoing database object.
|
|
// Note: Swap does not verify the incoming *DB object is correctly set-up.
|
|
func (db *DB) Swap(ctx context.Context, newDB *DB) (closeDbFn, error) {
|
|
const op = "db.(DB).Swap"
|
|
if newDB == nil || newDB.wrapped == nil || newDB.wrapped.Load() == nil {
|
|
return nil, fmt.Errorf("no new db object present")
|
|
}
|
|
if db == nil || db.wrapped == nil || db.wrapped.Load() == nil {
|
|
// TBD: We could be helpful here and set the new DB instead of err?
|
|
return nil, fmt.Errorf("no current db is present to swap, aborting")
|
|
}
|
|
|
|
// Grab the old db to allow for cleanup after swap.
|
|
oldDbw := db.wrapped.Swap(newDB.wrapped.Load())
|
|
closeOldDbFn := func(ctx context.Context) {
|
|
go func() {
|
|
maxTime := time.Now().Add(CloseSwappedDbDuration)
|
|
t := time.NewTicker(time.Second)
|
|
var done bool
|
|
for {
|
|
select {
|
|
case <-t.C:
|
|
if time.Now().After(maxTime) || done {
|
|
t.Stop()
|
|
if err := oldDbw.Close(ctx); err != nil {
|
|
event.WriteError(ctx, op, errors.Wrap(ctx, err, op))
|
|
}
|
|
return
|
|
}
|
|
sqlDb, err := oldDbw.SqlDB(ctx)
|
|
if err != nil {
|
|
event.WriteError(ctx, op, fmt.Errorf("unable to load old sqldb to check stats"))
|
|
continue
|
|
}
|
|
stats := sqlDb.Stats()
|
|
if stats.InUse == 0 {
|
|
done = true
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
t.Stop()
|
|
event.WriteError(ctx, op, fmt.Errorf("context canceled before old database connection was closed, aborting"))
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
return closeOldDbFn, nil
|
|
}
|
|
|
|
// Debug will enable/disable debug info for the connection
|
|
func (db *DB) Debug(on bool) {
|
|
db.wrapped.Load().Debug(on)
|
|
}
|
|
|
|
// SqlDB returns the underlying sql.DB
|
|
//
|
|
// Note: This func is not named DB(), because our choice of ORM (gorm) which is
|
|
// embedded for various reasons, already exports a DB() func and it's base type
|
|
// is also an export DB type
|
|
func (d *DB) SqlDB(ctx context.Context) (*sql.DB, error) {
|
|
const op = "db.(DB).SqlDB"
|
|
if d.wrapped == nil {
|
|
return nil, errors.New(ctx, errors.Internal, op, "missing underlying database")
|
|
}
|
|
return d.wrapped.Load().SqlDB(ctx)
|
|
}
|
|
|
|
// Close the underlying sql.DB
|
|
func (d *DB) Close(ctx context.Context) error {
|
|
const op = "db.(DB).Close"
|
|
if d.wrapped == nil {
|
|
return errors.New(ctx, errors.Internal, op, "missing underlying database")
|
|
}
|
|
return d.wrapped.Load().Close(ctx)
|
|
}
|
|
|
|
// Open a database connection which is long-lived. The options of
|
|
// WithGormFormatter and WithMaxOpenConnections are supported.
|
|
//
|
|
// Note: Consider if you need to call Close() on the returned DB. Typically the
|
|
// answer is no, but there are occasions when it's necessary. See the sql.DB
|
|
// docs for more information.
|
|
func Open(ctx context.Context, dbType DbType, connectionUrl string, opt ...Option) (*DB, error) {
|
|
const op = "db.Open"
|
|
var dialect dbw.Dialector
|
|
switch dbType {
|
|
case Postgres:
|
|
dialect = postgres.New(postgres.Config{
|
|
DSN: connectionUrl,
|
|
},
|
|
)
|
|
default:
|
|
return nil, fmt.Errorf("unable to open %s database type", dbType)
|
|
}
|
|
opts := GetOpts(opt...)
|
|
var wrappedOpts []dbw.Option
|
|
if opts.withGormFormatter != nil {
|
|
wrappedOpts = append(wrappedOpts, dbw.WithLogger(opts.withGormFormatter))
|
|
}
|
|
if opts.withMaxOpenConnections > 0 {
|
|
if opts.withMaxOpenConnections < 5 {
|
|
return nil, fmt.Errorf("max_open_connections cannot be below 5")
|
|
}
|
|
wrappedOpts = append(wrappedOpts, dbw.WithMaxOpenConnections(opts.withMaxOpenConnections))
|
|
}
|
|
|
|
wrapped, err := dbw.OpenWith(dialect, wrappedOpts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
sdb, err := wrapped.SqlDB(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if opts.withMaxIdleConnections != nil {
|
|
sdb.SetMaxIdleConns(*opts.withMaxIdleConnections)
|
|
}
|
|
|
|
if opts.withConnMaxIdleTimeDuration != nil {
|
|
sdb.SetConnMaxIdleTime(*opts.withConnMaxIdleTimeDuration)
|
|
}
|
|
|
|
ret := &DB{wrapped: new(atomic.Pointer[dbw.DB])}
|
|
ret.wrapped.Store(wrapped)
|
|
return ret, nil
|
|
}
|