2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
2019-07-19 18:40:27 +01:00
|
|
|
"context"
|
2019-03-18 10:55:06 +00:00
|
|
|
"database/sql"
|
2019-07-19 18:40:27 +01:00
|
|
|
"database/sql/driver"
|
2019-07-02 22:23:02 +01:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2019-03-18 10:55:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-07-09 01:33:50 +01:00
|
|
|
"time"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2019-08-08 02:47:30 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-05-21 15:30:06 +01:00
|
|
|
"storj.io/storj/internal/dbutil"
|
2019-07-19 18:40:27 +01:00
|
|
|
"storj.io/storj/internal/dbutil/utccheck"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
|
|
|
)
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
// ErrInfo is the default error class for InfoDB
|
2019-03-18 10:55:06 +00:00
|
|
|
var ErrInfo = errs.Class("infodb")
|
|
|
|
|
2019-07-19 18:40:27 +01:00
|
|
|
// SQLDB defines interface that matches *sql.DB
|
|
|
|
// this is such that we can use utccheck.DB for the backend
|
|
|
|
//
|
|
|
|
// TODO: wrap the connector instead of *sql.DB
|
|
|
|
type SQLDB interface {
|
|
|
|
Begin() (*sql.Tx, error)
|
|
|
|
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
|
|
|
|
Close() error
|
|
|
|
Conn(ctx context.Context) (*sql.Conn, error)
|
|
|
|
Driver() driver.Driver
|
|
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
|
|
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
|
|
|
Ping() error
|
|
|
|
PingContext(ctx context.Context) error
|
|
|
|
Prepare(query string) (*sql.Stmt, error)
|
|
|
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
|
|
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
|
|
|
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
|
|
|
QueryRow(query string, args ...interface{}) *sql.Row
|
|
|
|
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
|
|
|
|
SetConnMaxLifetime(d time.Duration)
|
|
|
|
SetMaxIdleConns(n int)
|
|
|
|
SetMaxOpenConns(n int)
|
|
|
|
}
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
// InfoDB implements information database for piecestore.
|
|
|
|
type InfoDB struct {
|
2019-08-08 02:47:30 +01:00
|
|
|
db SQLDB
|
|
|
|
bandwidthdb bandwidthdb
|
|
|
|
v0PieceInfo v0PieceInfo
|
|
|
|
pieceExpirationDB pieceExpirationDB
|
|
|
|
location string
|
2019-08-12 22:43:05 +01:00
|
|
|
pieceSpaceUsedDB pieceSpaceUsedDB
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
// newInfo creates or opens InfoDB at the specified path.
|
|
|
|
func newInfo(path string) (*InfoDB, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:23:02 +01:00
|
|
|
db, err := sql.Open("sqlite3", "file:"+path+"?_journal=WAL&_busy_timeout=10000")
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-06-04 22:30:21 +01:00
|
|
|
dbutil.Configure(db, mon)
|
2019-05-21 15:30:06 +01:00
|
|
|
|
2019-07-19 18:40:27 +01:00
|
|
|
infoDb := &InfoDB{db: db}
|
2019-08-08 02:47:30 +01:00
|
|
|
infoDb.v0PieceInfo = v0PieceInfo{InfoDB: infoDb}
|
2019-07-29 15:07:52 +01:00
|
|
|
infoDb.bandwidthdb = bandwidthdb{InfoDB: infoDb}
|
2019-08-08 02:47:30 +01:00
|
|
|
infoDb.pieceExpirationDB = pieceExpirationDB{InfoDB: infoDb}
|
2019-07-16 17:31:29 +01:00
|
|
|
infoDb.location = path
|
2019-08-12 22:43:05 +01:00
|
|
|
infoDb.pieceSpaceUsedDB = pieceSpaceUsedDB{InfoDB: infoDb}
|
2019-07-09 01:33:50 +01:00
|
|
|
|
|
|
|
return infoDb, nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 18:40:27 +01:00
|
|
|
// NewInfoTest creates a new inmemory InfoDB.
|
|
|
|
func NewInfoTest() (*InfoDB, error) {
|
2019-07-02 22:23:02 +01:00
|
|
|
// create memory DB with a shared cache and a unique name to avoid collisions
|
|
|
|
db, err := sql.Open("sqlite3", fmt.Sprintf("file:memdb%d?mode=memory&cache=shared", rand.Int63()))
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:23:02 +01:00
|
|
|
// Set max idle and max open to 1 to control concurrent access to the memory DB
|
|
|
|
// Setting max open higher than 1 results in table locked errors
|
|
|
|
db.SetMaxIdleConns(1)
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
db.SetConnMaxLifetime(-1)
|
|
|
|
|
|
|
|
mon.Chain("db_stats", monkit.StatSourceFunc(
|
|
|
|
func(cb func(name string, val float64)) {
|
|
|
|
monkit.StatSourceFromStruct(db.Stats()).Stats(cb)
|
|
|
|
}))
|
2019-05-21 15:30:06 +01:00
|
|
|
|
2019-07-19 18:40:27 +01:00
|
|
|
infoDb := &InfoDB{db: utccheck.New(db)}
|
2019-08-08 02:47:30 +01:00
|
|
|
infoDb.v0PieceInfo = v0PieceInfo{InfoDB: infoDb}
|
2019-07-29 15:07:52 +01:00
|
|
|
infoDb.bandwidthdb = bandwidthdb{InfoDB: infoDb}
|
2019-08-08 02:47:30 +01:00
|
|
|
infoDb.pieceExpirationDB = pieceExpirationDB{InfoDB: infoDb}
|
2019-08-12 22:43:05 +01:00
|
|
|
infoDb.pieceSpaceUsedDB = pieceSpaceUsedDB{InfoDB: infoDb}
|
2019-07-09 01:33:50 +01:00
|
|
|
|
|
|
|
return infoDb, nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes any resources.
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) Close() error {
|
2019-03-18 10:55:06 +00:00
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTables creates any necessary tables.
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) CreateTables(log *zap.Logger) error {
|
2019-03-18 10:55:06 +00:00
|
|
|
migration := db.Migration()
|
|
|
|
return migration.Run(log.Named("migration"), db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawDB returns access to the raw database, only for migration tests.
|
2019-07-19 18:40:27 +01:00
|
|
|
func (db *InfoDB) RawDB() SQLDB { return db.db }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Begin begins transaction
|
2019-07-19 18:40:27 +01:00
|
|
|
func (db *InfoDB) Begin() (*sql.Tx, error) { return db.db.Begin() }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Rebind rebind parameters
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) Rebind(s string) string { return s }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Schema returns schema
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) Schema() string { return "" }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Migration returns table migrations.
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) Migration() *migrate.Migration {
|
2019-03-18 10:55:06 +00:00
|
|
|
return &migrate.Migration{
|
|
|
|
Table: "versions",
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
|
|
|
Description: "Initial setup",
|
|
|
|
Version: 0,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
// table for keeping serials that need to be verified against
|
|
|
|
`CREATE TABLE used_serial (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
serial_number BLOB NOT NULL,
|
|
|
|
expiration TIMESTAMP NOT NULL
|
|
|
|
)`,
|
|
|
|
// primary key on satellite id and serial number
|
|
|
|
`CREATE UNIQUE INDEX pk_used_serial ON used_serial(satellite_id, serial_number)`,
|
|
|
|
// expiration index to allow fast deletion
|
|
|
|
`CREATE INDEX idx_used_serial ON used_serial(expiration)`,
|
|
|
|
|
|
|
|
// certificate table for storing uplink/satellite certificates
|
|
|
|
`CREATE TABLE certificate (
|
|
|
|
cert_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
|
|
node_id BLOB NOT NULL, -- same NodeID can have multiple valid leaf certificates
|
|
|
|
peer_identity BLOB UNIQUE NOT NULL -- PEM encoded
|
|
|
|
)`,
|
|
|
|
|
|
|
|
// table for storing piece meta info
|
|
|
|
`CREATE TABLE pieceinfo (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
piece_id BLOB NOT NULL,
|
|
|
|
piece_size BIGINT NOT NULL,
|
2019-03-20 21:12:00 +00:00
|
|
|
piece_expiration TIMESTAMP, -- date when it can be deleted
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
uplink_piece_hash BLOB NOT NULL, -- serialized pb.PieceHash signed by uplink
|
|
|
|
uplink_cert_id INTEGER NOT NULL,
|
|
|
|
|
|
|
|
FOREIGN KEY(uplink_cert_id) REFERENCES certificate(cert_id)
|
|
|
|
)`,
|
|
|
|
// primary key by satellite id and piece id
|
|
|
|
`CREATE UNIQUE INDEX pk_pieceinfo ON pieceinfo(satellite_id, piece_id)`,
|
|
|
|
|
|
|
|
// table for storing bandwidth usage
|
|
|
|
`CREATE TABLE bandwidth_usage (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
action INTEGER NOT NULL,
|
|
|
|
amount BIGINT NOT NULL,
|
|
|
|
created_at TIMESTAMP NOT NULL
|
|
|
|
)`,
|
|
|
|
`CREATE INDEX idx_bandwidth_usage_satellite ON bandwidth_usage(satellite_id)`,
|
|
|
|
`CREATE INDEX idx_bandwidth_usage_created ON bandwidth_usage(created_at)`,
|
|
|
|
|
|
|
|
// table for storing all unsent orders
|
|
|
|
`CREATE TABLE unsent_order (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
serial_number BLOB NOT NULL,
|
|
|
|
|
|
|
|
order_limit_serialized BLOB NOT NULL, -- serialized pb.OrderLimit
|
|
|
|
order_serialized BLOB NOT NULL, -- serialized pb.Order
|
|
|
|
order_limit_expiration TIMESTAMP NOT NULL, -- when is the deadline for sending it
|
|
|
|
|
|
|
|
uplink_cert_id INTEGER NOT NULL,
|
|
|
|
|
|
|
|
FOREIGN KEY(uplink_cert_id) REFERENCES certificate(cert_id)
|
|
|
|
)`,
|
|
|
|
`CREATE UNIQUE INDEX idx_orders ON unsent_order(satellite_id, serial_number)`,
|
|
|
|
|
|
|
|
// table for storing all sent orders
|
|
|
|
`CREATE TABLE order_archive (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
serial_number BLOB NOT NULL,
|
2019-06-04 22:30:21 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
order_limit_serialized BLOB NOT NULL, -- serialized pb.OrderLimit
|
|
|
|
order_serialized BLOB NOT NULL, -- serialized pb.Order
|
2019-06-04 22:30:21 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
uplink_cert_id INTEGER NOT NULL,
|
2019-06-04 22:30:21 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
status INTEGER NOT NULL, -- accepted, rejected, confirmed
|
|
|
|
archived_at TIMESTAMP NOT NULL, -- when was it rejected
|
2019-06-04 22:30:21 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
FOREIGN KEY(uplink_cert_id) REFERENCES certificate(cert_id)
|
|
|
|
)`,
|
|
|
|
`CREATE INDEX idx_order_archive_satellite ON order_archive(satellite_id)`,
|
|
|
|
`CREATE INDEX idx_order_archive_status ON order_archive(status)`,
|
|
|
|
},
|
|
|
|
},
|
2019-05-07 21:05:50 +01:00
|
|
|
{
|
|
|
|
Description: "Network Wipe #2",
|
|
|
|
Version: 1,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`UPDATE pieceinfo SET piece_expiration = '2019-05-09 00:00:00.000000+00:00'`,
|
|
|
|
},
|
|
|
|
},
|
2019-05-08 12:11:59 +01:00
|
|
|
{
|
|
|
|
Description: "Add tracking of deletion failures.",
|
|
|
|
Version: 2,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`ALTER TABLE pieceinfo ADD COLUMN deletion_failed_at TIMESTAMP`,
|
|
|
|
},
|
|
|
|
},
|
2019-06-07 21:20:34 +01:00
|
|
|
{
|
|
|
|
Description: "Add vouchersDB for storing and retrieving vouchers.",
|
|
|
|
Version: 3,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE TABLE vouchers (
|
|
|
|
satellite_id BLOB PRIMARY KEY NOT NULL,
|
|
|
|
voucher_serialized BLOB NOT NULL,
|
|
|
|
expiration TIMESTAMP NOT NULL
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
2019-06-18 00:27:14 +01:00
|
|
|
{
|
|
|
|
Description: "Add index on pieceinfo expireation",
|
|
|
|
Version: 4,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE INDEX idx_pieceinfo_expiration ON pieceinfo(piece_expiration)`,
|
|
|
|
`CREATE INDEX idx_pieceinfo_deletion_failed ON pieceinfo(deletion_failed_at)`,
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 12:10:56 +01:00
|
|
|
{
|
|
|
|
Description: "Partial Network Wipe - Tardigrade Satellites",
|
|
|
|
Version: 5,
|
|
|
|
Action: migrate.SQL{
|
2019-07-10 20:16:13 +01:00
|
|
|
`UPDATE pieceinfo SET piece_expiration = '2019-06-25 00:00:00.000000+00:00' WHERE satellite_id
|
|
|
|
IN (x'84A74C2CD43C5BA76535E1F42F5DF7C287ED68D33522782F4AFABFDB40000000',
|
|
|
|
x'A28B4F04E10BAE85D67F4C6CB82BF8D4C0F0F47A8EA72627524DEB6EC0000000',
|
2019-06-25 12:10:56 +01:00
|
|
|
x'AF2C42003EFC826AB4361F73F9D890942146FE0EBE806786F8E7190800000000'
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
2019-07-08 08:22:36 +01:00
|
|
|
{
|
|
|
|
Description: "Add creation date.",
|
|
|
|
Version: 6,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`ALTER TABLE pieceinfo ADD COLUMN piece_creation TIMESTAMP NOT NULL DEFAULT 'epoch'`,
|
|
|
|
},
|
|
|
|
},
|
2019-07-09 22:33:45 +01:00
|
|
|
{
|
|
|
|
Description: "Drop certificate table.",
|
|
|
|
Version: 7,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`DROP TABLE certificate`,
|
|
|
|
`CREATE TABLE certificate (cert_id INTEGER)`,
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 20:16:13 +01:00
|
|
|
{
|
|
|
|
Description: "Drop old used serials and remove pieceinfo_deletion_failed index.",
|
|
|
|
Version: 8,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`DELETE FROM used_serial`,
|
|
|
|
`DROP INDEX idx_pieceinfo_deletion_failed`,
|
|
|
|
},
|
|
|
|
},
|
2019-07-11 21:51:40 +01:00
|
|
|
{
|
|
|
|
Description: "Add order limit table.",
|
|
|
|
Version: 9,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`ALTER TABLE pieceinfo ADD COLUMN order_limit BLOB NOT NULL DEFAULT X''`,
|
|
|
|
},
|
|
|
|
},
|
2019-07-12 20:29:09 +01:00
|
|
|
{
|
|
|
|
Description: "Optimize index usage.",
|
|
|
|
Version: 10,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`DROP INDEX idx_pieceinfo_expiration`,
|
|
|
|
`DROP INDEX idx_order_archive_satellite`,
|
|
|
|
`DROP INDEX idx_order_archive_status`,
|
2019-07-15 18:38:08 +01:00
|
|
|
`CREATE INDEX idx_pieceinfo_expiration ON pieceinfo(piece_expiration) WHERE piece_expiration IS NOT NULL`,
|
2019-07-12 20:29:09 +01:00
|
|
|
},
|
|
|
|
},
|
2019-07-16 15:58:58 +01:00
|
|
|
{
|
|
|
|
Description: "Create bandwidth_usage_rollup table.",
|
|
|
|
Version: 11,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE TABLE bandwidth_usage_rollups (
|
|
|
|
interval_start TIMESTAMP NOT NULL,
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
action INTEGER NOT NULL,
|
|
|
|
amount BIGINT NOT NULL,
|
|
|
|
PRIMARY KEY ( interval_start, satellite_id, action )
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
2019-07-16 17:31:29 +01:00
|
|
|
{
|
|
|
|
Description: "Clear Tables from Alpha data",
|
|
|
|
Version: 12,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`DROP TABLE pieceinfo`,
|
|
|
|
`DROP TABLE used_serial`,
|
|
|
|
`DROP TABLE order_archive`,
|
|
|
|
`CREATE TABLE pieceinfo_ (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
piece_id BLOB NOT NULL,
|
|
|
|
piece_size BIGINT NOT NULL,
|
|
|
|
piece_expiration TIMESTAMP,
|
|
|
|
|
|
|
|
order_limit BLOB NOT NULL,
|
|
|
|
uplink_piece_hash BLOB NOT NULL,
|
|
|
|
uplink_cert_id INTEGER NOT NULL,
|
|
|
|
|
|
|
|
deletion_failed_at TIMESTAMP,
|
|
|
|
piece_creation TIMESTAMP NOT NULL,
|
|
|
|
|
|
|
|
FOREIGN KEY(uplink_cert_id) REFERENCES certificate(cert_id)
|
|
|
|
)`,
|
|
|
|
`CREATE UNIQUE INDEX pk_pieceinfo_ ON pieceinfo_(satellite_id, piece_id)`,
|
|
|
|
`CREATE INDEX idx_pieceinfo__expiration ON pieceinfo_(piece_expiration) WHERE piece_expiration IS NOT NULL`,
|
|
|
|
`CREATE TABLE used_serial_ (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
serial_number BLOB NOT NULL,
|
|
|
|
expiration TIMESTAMP NOT NULL
|
|
|
|
)`,
|
|
|
|
`CREATE UNIQUE INDEX pk_used_serial_ ON used_serial_(satellite_id, serial_number)`,
|
|
|
|
`CREATE INDEX idx_used_serial_ ON used_serial_(expiration)`,
|
|
|
|
`CREATE TABLE order_archive_ (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
serial_number BLOB NOT NULL,
|
|
|
|
|
|
|
|
order_limit_serialized BLOB NOT NULL,
|
|
|
|
order_serialized BLOB NOT NULL,
|
|
|
|
|
|
|
|
uplink_cert_id INTEGER NOT NULL,
|
|
|
|
|
|
|
|
status INTEGER NOT NULL,
|
|
|
|
archived_at TIMESTAMP NOT NULL,
|
|
|
|
|
|
|
|
FOREIGN KEY(uplink_cert_id) REFERENCES certificate(cert_id)
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Free Storagenodes from trash data",
|
|
|
|
Version: 13,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, mgdb migrate.DB, tx *sql.Tx) error {
|
|
|
|
// When using inmemory DB, skip deletion process
|
|
|
|
if db.location == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.RemoveAll(filepath.Join(filepath.Dir(db.location), "blob/ukfu6bhbboxilvt7jrwlqk7y2tapb5d2r2tsmj2sjxvw5qaaaaaa")) // us-central1
|
|
|
|
if err != nil {
|
|
|
|
log.Sugar().Debug(err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(filepath.Join(filepath.Dir(db.location), "blob/v4weeab67sbgvnbwd5z7tweqsqqun7qox2agpbxy44mqqaaaaaaa")) // europe-west1
|
|
|
|
if err != nil {
|
|
|
|
log.Sugar().Debug(err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(filepath.Join(filepath.Dir(db.location), "blob/qstuylguhrn2ozjv4h2c6xpxykd622gtgurhql2k7k75wqaaaaaa")) // asia-east1
|
|
|
|
if err != nil {
|
|
|
|
log.Sugar().Debug(err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(filepath.Join(filepath.Dir(db.location), "blob/abforhuxbzyd35blusvrifvdwmfx4hmocsva4vmpp3rgqaaaaaaa")) // "tothemoon (stefan)"
|
|
|
|
if err != nil {
|
|
|
|
log.Sugar().Debug(err)
|
|
|
|
}
|
|
|
|
// To prevent the node from starting up, we just log errors and return nil
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
},
|
2019-07-17 14:00:37 +01:00
|
|
|
{
|
|
|
|
Description: "Free Storagenodes from orphaned tmp data",
|
|
|
|
Version: 14,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, mgdb migrate.DB, tx *sql.Tx) error {
|
|
|
|
// When using inmemory DB, skip deletion process
|
|
|
|
if db.location == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.RemoveAll(filepath.Join(filepath.Dir(db.location), "tmp"))
|
|
|
|
if err != nil {
|
|
|
|
log.Sugar().Debug(err)
|
|
|
|
}
|
|
|
|
// To prevent the node from starting up, we just log errors and return nil
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
Description: "Start piece_expirations table, deprecate pieceinfo table",
|
|
|
|
Version: 15,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
// new table to hold expiration data (and only expirations. no other pieceinfo)
|
|
|
|
`CREATE TABLE piece_expirations (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
piece_id BLOB NOT NULL,
|
|
|
|
piece_expiration TIMESTAMP NOT NULL, -- date when it can be deleted
|
|
|
|
deletion_failed_at TIMESTAMP,
|
|
|
|
PRIMARY KEY (satellite_id, piece_id)
|
|
|
|
)`,
|
|
|
|
`CREATE INDEX idx_piece_expirations_piece_expiration ON piece_expirations(piece_expiration)`,
|
|
|
|
`CREATE INDEX idx_piece_expirations_deletion_failed_at ON piece_expirations(deletion_failed_at)`,
|
|
|
|
},
|
|
|
|
},
|
2019-08-08 14:47:04 +01:00
|
|
|
{
|
|
|
|
Description: "Add reputation and storage usage cache tables",
|
|
|
|
Version: 16,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE TABLE reputation (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
uptime_success_count INTEGER NOT NULL,
|
|
|
|
uptime_total_count INTEGER NOT NULL,
|
|
|
|
uptime_reputation_alpha REAL NOT NULL,
|
|
|
|
uptime_reputation_beta REAL NOT NULL,
|
|
|
|
uptime_reputation_score REAL NOT NULL,
|
|
|
|
audit_success_count INTEGER NOT NULL,
|
|
|
|
audit_total_count INTEGER NOT NULL,
|
|
|
|
audit_reputation_alpha REAL NOT NULL,
|
|
|
|
audit_reputation_beta REAL NOT NULL,
|
|
|
|
audit_reputation_score REAL NOT NULL,
|
|
|
|
updated_at TIMESTAMP NOT NULL,
|
|
|
|
PRIMARY KEY (satellite_id)
|
|
|
|
)`,
|
|
|
|
`CREATE TABLE storage_usage (
|
|
|
|
satellite_id BLOB NOT NULL,
|
|
|
|
at_rest_total REAL NOT NUll,
|
|
|
|
timestamp TIMESTAMP NOT NULL,
|
|
|
|
PRIMARY KEY (satellite_id, timestamp)
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
2019-08-12 22:43:05 +01:00
|
|
|
{
|
|
|
|
Description: "Create piece_space_used table",
|
|
|
|
Version: 17,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
// new table to hold the most recent totals from the piece space used cache
|
|
|
|
`CREATE TABLE piece_space_used (
|
|
|
|
total INTEGER NOT NULL,
|
|
|
|
satellite_id BLOB
|
|
|
|
)`,
|
|
|
|
`CREATE UNIQUE INDEX idx_piece_space_used_satellite_id ON piece_space_used(satellite_id)`,
|
|
|
|
},
|
|
|
|
},
|
2019-03-18 10:55:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 14:47:04 +01:00
|
|
|
|
|
|
|
// withTx is a helper method which executes callback in transaction scope
|
|
|
|
func (db *InfoDB) withTx(ctx context.Context, cb func(tx *sql.Tx) error) error {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = errs.Combine(err, tx.Rollback())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cb(tx)
|
|
|
|
}
|