2018-08-17 18:40:15 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package psdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2018-08-20 20:47:24 +01:00
|
|
|
"flag"
|
2018-08-17 18:40:15 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3" // register sqlite to sql
|
|
|
|
|
2018-08-17 18:40:15 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
pstore "storj.io/storj/pkg/piecestore"
|
|
|
|
"storj.io/storj/pkg/utils"
|
2018-08-17 18:40:15 +01:00
|
|
|
pb "storj.io/storj/protos/piecestore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-08-20 20:47:24 +01:00
|
|
|
mon = monkit.Package()
|
2018-09-08 16:34:55 +01:00
|
|
|
defaultCheckInterval = flag.Duration("piecestore.ttl.check_interval", time.Hour, "number of seconds to sleep between ttl checks")
|
2018-08-17 18:40:15 +01:00
|
|
|
)
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// DB is a piece store database
|
|
|
|
type DB struct {
|
|
|
|
dataPath string
|
|
|
|
mu sync.Mutex
|
|
|
|
DB *sql.DB // TODO: hide
|
|
|
|
check *time.Ticker
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// Open opens DB at DBPath
|
|
|
|
func Open(ctx context.Context, DataPath, DBPath string) (db *DB, err error) {
|
2018-08-17 18:40:15 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = os.MkdirAll(filepath.Dir(DBPath), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
sqlite, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc&mutex=full", DBPath))
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// try to enable write-ahead-logging
|
|
|
|
_, _ = sqlite.Exec(`PRAGMA journal_mode = WAL`)
|
|
|
|
|
2018-08-17 18:40:15 +01:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2018-09-08 16:34:55 +01:00
|
|
|
_ = sqlite.Close()
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
tx, err := sqlite.Begin()
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
_, err = tx.Exec("CREATE TABLE IF NOT EXISTS `ttl` (`id` BLOB UNIQUE, `created` INT(10), `expires` INT(10));")
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec("CREATE TABLE IF NOT EXISTS `bandwidth_agreements` (`agreement` BLOB, `signature` BLOB);")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
db = &DB{
|
|
|
|
DB: sqlite,
|
|
|
|
dataPath: DataPath,
|
|
|
|
check: time.NewTicker(*defaultCheckInterval),
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
go db.garbageCollect(ctx)
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
return db, nil
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the database
|
2018-09-08 16:34:55 +01:00
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.DB.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) locked() func() {
|
|
|
|
db.mu.Lock()
|
|
|
|
return db.mu.Unlock
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteExpired checks for expired TTLs in the DB and removes data from both the DB and the FS
|
2018-09-08 16:34:55 +01:00
|
|
|
func (db *DB) DeleteExpired(ctx context.Context) (err error) {
|
2018-08-17 18:40:15 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
var expired []string
|
|
|
|
err = func() error {
|
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
tx, err := db.DB.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
now := time.Now().Unix()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
rows, err := tx.Query("SELECT id FROM ttl WHERE 0 < expires AND ? < expires", now)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
for rows.Next() {
|
|
|
|
var id string
|
|
|
|
if err := rows.Scan(&id); err != nil {
|
2018-08-17 18:40:15 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
expired = append(expired, id)
|
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
_, err = tx.Exec(`DELETE FROM ttl WHERE 0 < expires AND ? < expires`, now)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
return tx.Commit()
|
|
|
|
}()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
var errs []error
|
|
|
|
for _, id := range expired {
|
|
|
|
err := pstore.Delete(id, db.dataPath)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return utils.CombineErrors(errs...)
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// garbageCollect will periodically run DeleteExpired
|
|
|
|
func (db *DB) garbageCollect(ctx context.Context) {
|
|
|
|
for range db.check.C {
|
|
|
|
err := db.DeleteExpired(ctx)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("failed checking entries: %+v", err)
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// WriteBandwidthAllocToDB -- Insert bandwidth agreement into DB
|
|
|
|
func (db *DB) WriteBandwidthAllocToDB(ba *pb.RenterBandwidthAllocation) error {
|
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
_, err := db.DB.Exec(`INSERT INTO bandwidth_agreements (agreement, signature) VALUES (?, ?)`, ba.GetData(), ba.GetSignature())
|
|
|
|
return err
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// GetBandwidthAllocationBySignature finds allocation info by signature
|
|
|
|
func (db *DB) GetBandwidthAllocationBySignature(signature []byte) ([][]byte, error) {
|
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
rows, err := db.DB.Query(`SELECT agreement FROM bandwidth_agreements WHERE signature = ?`, signature)
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
2018-09-08 16:34:55 +01:00
|
|
|
return nil, err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
defer func() {
|
|
|
|
_ = rows.Close()
|
|
|
|
}()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
agreements := [][]byte{}
|
|
|
|
for rows.Next() {
|
|
|
|
var agreement []byte
|
|
|
|
err := rows.Scan(&agreement)
|
|
|
|
if err != nil {
|
|
|
|
return agreements, err
|
|
|
|
}
|
|
|
|
agreements = append(agreements, agreement)
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
return agreements, nil
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// AddTTLToDB adds TTL into database by id
|
|
|
|
func (db *DB) AddTTLToDB(id string, expiration int64) error {
|
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
created := time.Now().Unix()
|
|
|
|
_, err := db.DB.Exec("INSERT or REPLACE INTO ttl (id, created, expires) VALUES (?, ?, ?)", id, created, expiration)
|
|
|
|
return err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// GetTTLByID finds the TTL in the database by id and return it
|
|
|
|
func (db *DB) GetTTLByID(id string) (expiration int64, err error) {
|
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
err = db.DB.QueryRow(`SELECT expires FROM ttl WHERE id=?`, id).Scan(&expiration)
|
|
|
|
return expiration, err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// DeleteTTLByID finds the TTL in the database by id and delete it
|
|
|
|
func (db *DB) DeleteTTLByID(id string) error {
|
|
|
|
defer db.locked()()
|
|
|
|
|
|
|
|
_, err := db.DB.Exec(`DELETE FROM ttl WHERE id=?`, id)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
return err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|