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"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"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-09-22 15:51:34 +01:00
|
|
|
"storj.io/storj/private/dbutil"
|
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
Error = errs.Class("multinodedb internal error")
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
driver string
|
|
|
|
implementation dbutil.Implementation
|
|
|
|
source string
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-22 15:51:34 +01:00
|
|
|
driver, source, implementation, err := dbutil.SplitConnStr(databaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: do we need cockroach implementation?
|
|
|
|
if implementation != dbutil.Postgres && implementation != dbutil.Cockroach {
|
|
|
|
return nil, Error.New("unsupported driver %q", driver)
|
|
|
|
}
|
|
|
|
|
|
|
|
source = pgutil.CheckApplicationName(source)
|
|
|
|
|
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
|
|
|
|
|
|
|
core := &multinodeDB{
|
2020-10-14 15:26:50 +01:00
|
|
|
DB: dbxDB,
|
2020-09-22 15:51:34 +01:00
|
|
|
|
|
|
|
log: log,
|
|
|
|
driver: driver,
|
|
|
|
implementation: implementation,
|
|
|
|
source: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
return core, nil
|
|
|
|
}
|
2020-10-02 17:21:58 +01:00
|
|
|
|
|
|
|
// Nodes returns nodes database.
|
|
|
|
func (db *multinodeDB) Nodes() console.Nodes {
|
|
|
|
return &nodes{
|
|
|
|
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
|
|
|
|
}
|