2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-05 09:35:50 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-02-14 21:55:21 +00:00
|
|
|
"go.uber.org/zap"
|
2018-12-07 14:46:42 +00:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/dbutil"
|
2019-12-11 19:04:09 +00:00
|
|
|
"storj.io/storj/private/dbutil/cockroachutil"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2019-06-19 13:02:37 +01:00
|
|
|
"storj.io/storj/satellite/attribution"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/audit"
|
2019-01-16 20:23:28 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-09-25 18:12:44 +01:00
|
|
|
"storj.io/storj/satellite/gracefulexit"
|
2019-03-27 10:24:35 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2019-10-10 18:12:23 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/repair/irreparable"
|
|
|
|
"storj.io/storj/satellite/repair/queue"
|
2019-06-24 21:51:54 +01:00
|
|
|
"storj.io/storj/satellite/rewards"
|
2018-12-05 09:35:50 +00:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
var (
|
|
|
|
// Error is the default satellitedb errs class
|
|
|
|
Error = errs.Class("satellitedb")
|
|
|
|
)
|
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
// satelliteDB combines access to different database tables with a record
|
|
|
|
// of the db driver, db implementation, and db source URL.
|
|
|
|
type satelliteDB struct {
|
|
|
|
*dbx.DB
|
2019-12-02 21:05:21 +00:00
|
|
|
log *zap.Logger
|
|
|
|
driver string
|
|
|
|
implementation dbutil.Implementation
|
|
|
|
source string
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
var _ dbx.DBMethods = &satelliteDB{}
|
|
|
|
|
2019-10-18 20:03:10 +01:00
|
|
|
// New creates instance of database supports postgres
|
2019-02-14 21:55:21 +00:00
|
|
|
func New(log *zap.Logger, databaseURL string) (satellite.DB, error) {
|
2019-12-02 21:05:21 +00:00
|
|
|
driver, source, implementation, err := dbutil.SplitConnStr(databaseURL)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-11 19:04:09 +00:00
|
|
|
if implementation != dbutil.Postgres && implementation != dbutil.Cockroach {
|
2019-10-18 20:03:10 +01:00
|
|
|
return nil, Error.New("unsupported driver %q", driver)
|
2019-03-12 13:29:13 +00:00
|
|
|
}
|
2019-10-18 20:03:10 +01:00
|
|
|
|
|
|
|
source = pgutil.CheckApplicationName(source)
|
2019-12-11 19:04:09 +00:00
|
|
|
source = cockroachutil.TranslateName(source)
|
2019-10-18 20:03:10 +01:00
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
dbxDB, err := dbx.Open("postgres", source)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
2019-12-14 02:29:54 +00:00
|
|
|
return nil, Error.New("failed opening database via DBX at %q: %v",
|
|
|
|
source, err)
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
2019-05-14 16:13:18 +01:00
|
|
|
log.Debug("Connected to:", zap.String("db source", source))
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
dbutil.Configure(dbxDB.DB, mon)
|
2019-05-21 15:30:06 +01:00
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
core := &satelliteDB{
|
|
|
|
DB: dbxDB,
|
|
|
|
log: log,
|
|
|
|
driver: driver,
|
|
|
|
source: source,
|
|
|
|
implementation: implementation,
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
return core, nil
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 21:55:21 +00:00
|
|
|
// TestDBAccess for raw database access,
|
|
|
|
// should not be used outside of migration tests.
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) TestDBAccess() *dbx.DB { return db.DB }
|
2019-02-14 21:55:21 +00:00
|
|
|
|
2019-08-26 17:49:42 +01:00
|
|
|
// PeerIdentities returns a storage for peer identities
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) PeerIdentities() overlay.PeerIdentities {
|
|
|
|
return &peerIdentities{db: db}
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:02:37 +01:00
|
|
|
// Attribution is a getter for value attribution repository
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) Attribution() attribution.DB {
|
|
|
|
return &attributionDB{db: db}
|
2019-06-18 14:06:33 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 20:14:16 +00:00
|
|
|
// OverlayCache is a getter for overlay cache repository
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) OverlayCache() overlay.DB {
|
|
|
|
return &overlaycache{db: db}
|
2018-12-17 20:14:16 +00:00
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
// RepairQueue is a getter for RepairQueue repository
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) RepairQueue() queue.RepairQueue {
|
|
|
|
return &repairQueue{db: db}
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
// StoragenodeAccounting returns database for tracking storagenode usage
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) StoragenodeAccounting() accounting.StoragenodeAccounting {
|
|
|
|
return &StoragenodeAccounting{db: db}
|
2019-05-10 20:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectAccounting returns database for tracking project data use
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) ProjectAccounting() accounting.ProjectAccounting {
|
|
|
|
return &ProjectAccounting{db: db}
|
2018-12-14 14:27:21 +00:00
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
// Irreparable returns database for storing segments that failed repair
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) Irreparable() irreparable.DB {
|
|
|
|
return &irreparableDB{db: db}
|
2018-12-10 19:08:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 20:23:28 +00:00
|
|
|
// Console returns database for storing users, projects and api keys
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) Console() console.DB {
|
2019-01-16 20:23:28 +00:00
|
|
|
return &ConsoleDB{
|
2019-12-14 02:29:54 +00:00
|
|
|
db: db,
|
|
|
|
methods: db,
|
2019-01-16 20:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
// Rewards returns database for storing offers
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) Rewards() rewards.DB {
|
|
|
|
return &offersDB{db: db}
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 10:24:35 +00:00
|
|
|
// Orders returns database for storing orders
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) Orders() orders.DB {
|
|
|
|
return &ordersDB{db: db}
|
2019-03-27 10:24:35 +00:00
|
|
|
}
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// Containment returns database for storing pending audit info
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) Containment() audit.Containment {
|
|
|
|
return &containment{db: db}
|
2019-05-22 15:50:22 +01:00
|
|
|
}
|
2019-09-25 18:12:44 +01:00
|
|
|
|
|
|
|
// GracefulExit returns database for graceful exit
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) GracefulExit() gracefulexit.DB {
|
|
|
|
return &gracefulexitDB{db: db}
|
2019-09-25 18:12:44 +01:00
|
|
|
}
|
2019-10-10 18:12:23 +01:00
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
// StripeCoinPayments returns database for stripecoinpayments.
|
2019-12-14 02:29:54 +00:00
|
|
|
func (db *satelliteDB) StripeCoinPayments() stripecoinpayments.DB {
|
|
|
|
return &stripeCoinPaymentsDB{db: db}
|
2019-10-17 15:04:50 +01:00
|
|
|
}
|