2020-09-22 15:51:34 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-10-14 15:26:50 +01:00
|
|
|
package multinodedb
|
2020-09-22 15:51:34 +01:00
|
|
|
|
|
|
|
import (
|
2020-10-14 15:26:50 +01:00
|
|
|
"context"
|
2021-02-04 14:38:45 +00:00
|
|
|
"strings"
|
2020-10-14 15:26:50 +01:00
|
|
|
|
2020-10-02 17:21:58 +01:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2020-09-22 15:51:34 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil"
|
|
|
|
"storj.io/private/dbutil/pgutil"
|
2020-09-22 15:51:34 +01:00
|
|
|
"storj.io/storj/multinode"
|
2020-10-02 17:21:58 +01:00
|
|
|
"storj.io/storj/multinode/console"
|
2020-10-14 15:26:50 +01:00
|
|
|
"storj.io/storj/multinode/multinodedb/dbx"
|
2020-12-09 16:34:37 +00:00
|
|
|
"storj.io/storj/multinode/nodes"
|
2020-09-22 15:51:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ensures that multinodeDB implements multinode.DB.
|
|
|
|
_ multinode.DB = (*multinodeDB)(nil)
|
|
|
|
|
2020-10-02 17:21:58 +01:00
|
|
|
mon = monkit.Package()
|
|
|
|
|
2020-09-22 15:51:34 +01:00
|
|
|
// Error is the default multinodedb errs class.
|
2021-04-28 09:06:17 +01:00
|
|
|
Error = errs.Class("multinodedb")
|
2020-09-22 15:51:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// multinodeDB combines access to different database tables with a record
|
|
|
|
// of the db driver, db implementation, and db source URL.
|
|
|
|
// Implementation of multinode.DB interface.
|
|
|
|
//
|
|
|
|
// architecture: Master Database
|
|
|
|
type multinodeDB struct {
|
2020-09-30 17:38:28 +01:00
|
|
|
*dbx.DB
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2021-05-11 09:49:26 +01:00
|
|
|
log *zap.Logger
|
|
|
|
driver string
|
|
|
|
impl dbutil.Implementation
|
|
|
|
source string
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
|
|
|
|
2020-10-14 15:26:50 +01:00
|
|
|
// Open creates instance of database supports postgres.
|
|
|
|
func Open(ctx context.Context, log *zap.Logger, databaseURL string) (multinode.DB, error) {
|
2021-05-11 09:49:26 +01:00
|
|
|
driver, source, impl, err := dbutil.SplitConnStr(databaseURL)
|
2020-09-22 15:51:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-11 09:49:26 +01:00
|
|
|
switch impl {
|
2021-02-04 14:38:45 +00:00
|
|
|
case dbutil.SQLite3:
|
|
|
|
source = sqlite3SetDefaultOptions(source)
|
|
|
|
case dbutil.Postgres:
|
|
|
|
source, err = pgutil.CheckApplicationName(source, "multinode")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, Error.New("unsupported driver %q", driver)
|
2020-12-04 10:24:39 +00:00
|
|
|
}
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2020-10-14 15:26:50 +01:00
|
|
|
dbxDB, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.New("failed opening database via DBX at %q: %v",
|
|
|
|
source, err)
|
|
|
|
}
|
2021-02-04 14:38:45 +00:00
|
|
|
|
2020-10-14 15:26:50 +01:00
|
|
|
log.Debug("Connected to:", zap.String("db source", source))
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2020-10-14 15:26:50 +01:00
|
|
|
dbutil.Configure(ctx, dbxDB.DB, "multinodedb", mon)
|
2020-09-22 15:51:34 +01:00
|
|
|
|
|
|
|
core := &multinodeDB{
|
2020-10-14 15:26:50 +01:00
|
|
|
DB: dbxDB,
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2021-05-11 09:49:26 +01:00
|
|
|
log: log,
|
|
|
|
driver: driver,
|
|
|
|
impl: impl,
|
|
|
|
source: source,
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return core, nil
|
|
|
|
}
|
2020-10-02 17:21:58 +01:00
|
|
|
|
|
|
|
// Nodes returns nodes database.
|
2020-12-09 16:34:37 +00:00
|
|
|
func (db *multinodeDB) Nodes() nodes.DB {
|
|
|
|
return &nodesdb{
|
2020-10-02 17:21:58 +01:00
|
|
|
methods: db,
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 15:26:50 +01:00
|
|
|
|
|
|
|
// Members returns members database.
|
|
|
|
func (db *multinodeDB) Members() console.Members {
|
|
|
|
return &members{
|
|
|
|
methods: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSchema creates schema.
|
|
|
|
func (db *multinodeDB) CreateSchema(ctx context.Context) error {
|
|
|
|
_, err := db.ExecContext(ctx, db.DB.Schema())
|
|
|
|
return err
|
|
|
|
}
|
2021-02-04 14:38:45 +00:00
|
|
|
|
|
|
|
// sqlite3SetDefaultOptions sets default options for disk-based db with URI filename source string
|
|
|
|
// if no options were set.
|
|
|
|
func sqlite3SetDefaultOptions(source string) string {
|
|
|
|
if !strings.HasPrefix(source, "file:") {
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
// do not set anything for in-memory db
|
|
|
|
if strings.HasPrefix(source, "file::memory:") {
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
if strings.Contains(source, "?") {
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
|
|
|
return source + "?_journal=WAL&_busy_timeout=10000"
|
|
|
|
}
|