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"
|
2021-04-28 02:30:01 +01:00
|
|
|
"storj.io/private/tagsql"
|
2020-09-22 15:51:34 +01:00
|
|
|
"storj.io/storj/multinode"
|
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"
|
2021-04-28 02:30:01 +01:00
|
|
|
"storj.io/storj/private/migrate"
|
2020-09-22 15:51:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ensures that multinodeDB implements multinode.DB.
|
2021-04-28 02:30:01 +01:00
|
|
|
_ multinode.DB = (*DB)(nil)
|
2020-09-22 15:51:34 +01:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
// DB combines access to different database tables with a record
|
2020-09-22 15:51:34 +01:00
|
|
|
// of the db driver, db implementation, and db source URL.
|
|
|
|
// Implementation of multinode.DB interface.
|
|
|
|
//
|
|
|
|
// architecture: Master Database
|
2021-04-28 02:30:01 +01:00
|
|
|
type DB struct {
|
2020-09-30 17:38:28 +01:00
|
|
|
*dbx.DB
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
log *zap.Logger
|
|
|
|
driver string
|
|
|
|
source string
|
|
|
|
implementation dbutil.Implementation
|
|
|
|
migrationDB tagsql.DB
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
|
|
|
|
2020-10-14 15:26:50 +01:00
|
|
|
// Open creates instance of database supports postgres.
|
2021-04-28 02:30:01 +01:00
|
|
|
func Open(ctx context.Context, log *zap.Logger, databaseURL string) (*DB, error) {
|
|
|
|
driver, source, implementation, err := dbutil.SplitConnStr(databaseURL)
|
2020-09-22 15:51:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
switch implementation {
|
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)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
core := &DB{
|
2020-10-14 15:26:50 +01:00
|
|
|
DB: dbxDB,
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
log: log,
|
|
|
|
driver: driver,
|
|
|
|
implementation: implementation,
|
|
|
|
source: source,
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
core.migrationDB = core
|
|
|
|
|
2020-09-22 15:51:34 +01:00
|
|
|
return core, nil
|
|
|
|
}
|
2020-10-02 17:21:58 +01:00
|
|
|
|
|
|
|
// Nodes returns nodes database.
|
2021-04-28 02:30:01 +01:00
|
|
|
func (db *DB) Nodes() nodes.DB {
|
2020-12-09 16:34:37 +00:00
|
|
|
return &nodesdb{
|
2020-10-02 17:21:58 +01:00
|
|
|
methods: db,
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 15:26:50 +01:00
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
// MigrateToLatest migrates db to the latest version.
|
|
|
|
func (db DB) MigrateToLatest(ctx context.Context) error {
|
|
|
|
var migration *migrate.Migration
|
2020-10-14 15:26:50 +01:00
|
|
|
|
2021-04-28 02:30:01 +01:00
|
|
|
switch db.implementation {
|
|
|
|
case dbutil.SQLite3:
|
|
|
|
migration = db.SQLite3Migration()
|
|
|
|
case dbutil.Postgres:
|
|
|
|
migration = db.PostgresMigration()
|
|
|
|
default:
|
|
|
|
return migrate.Create(ctx, "database", db.DB)
|
|
|
|
}
|
|
|
|
return migration.Run(ctx, db.log)
|
2020-10-14 15:26:50 +01:00
|
|
|
}
|
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"
|
|
|
|
}
|