2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2019-03-21 13:24:26 +00:00
|
|
|
"time"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-03-21 13:24:26 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/orders"
|
|
|
|
)
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
type ordersdb struct{ *InfoDB }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Orders returns database for storing orders
|
|
|
|
func (db *DB) Orders() orders.DB { return db.info.Orders() }
|
|
|
|
|
|
|
|
// Orders returns database for storing orders
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) Orders() orders.DB { return &ordersdb{db} }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Enqueue inserts order to the unsent list
|
2019-06-04 13:31:39 +01:00
|
|
|
func (db *ordersdb) Enqueue(ctx context.Context, info *orders.Info) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
limitSerialized, err := proto.Marshal(info.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
orderSerialized, err := proto.Marshal(info.Order)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.db.Exec(`
|
|
|
|
INSERT INTO unsent_order(
|
|
|
|
satellite_id, serial_number,
|
|
|
|
order_limit_serialized, order_serialized, order_limit_expiration,
|
|
|
|
uplink_cert_id
|
|
|
|
) VALUES (?,?, ?,?,?, ?)
|
2019-07-09 22:54:00 +01:00
|
|
|
`, info.Limit.SatelliteId, info.Limit.SerialNumber, limitSerialized, orderSerialized, info.Limit.OrderExpiration.UTC(), 0)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListUnsent returns orders that haven't been sent yet.
|
|
|
|
func (db *ordersdb) ListUnsent(ctx context.Context, limit int) (_ []*orders.Info, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
rows, err := db.db.Query(`
|
2019-07-09 22:33:45 +01:00
|
|
|
SELECT order_limit_serialized, order_serialized
|
2019-03-18 10:55:06 +00:00
|
|
|
FROM unsent_order
|
|
|
|
LIMIT ?
|
|
|
|
`, limit)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
var infos []*orders.Info
|
|
|
|
for rows.Next() {
|
|
|
|
var limitSerialized []byte
|
|
|
|
var orderSerialized []byte
|
|
|
|
|
2019-07-09 22:33:45 +01:00
|
|
|
err := rows.Scan(&limitSerialized, &orderSerialized)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info orders.Info
|
2019-07-01 16:54:11 +01:00
|
|
|
info.Limit = &pb.OrderLimit{}
|
|
|
|
info.Order = &pb.Order{}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
err = proto.Unmarshal(limitSerialized, info.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(orderSerialized, info.Order)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
infos = append(infos, &info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos, ErrInfo.Wrap(rows.Err())
|
|
|
|
}
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
// ListUnsentBySatellite returns orders that haven't been sent yet grouped by satellite.
|
|
|
|
// Does not return uplink identity.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (db *ordersdb) ListUnsentBySatellite(ctx context.Context) (_ map[storj.NodeID][]*orders.Info, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-21 13:24:26 +00:00
|
|
|
// TODO: add some limiting
|
|
|
|
|
|
|
|
rows, err := db.db.Query(`
|
|
|
|
SELECT order_limit_serialized, order_serialized
|
|
|
|
FROM unsent_order
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
infos := map[storj.NodeID][]*orders.Info{}
|
|
|
|
for rows.Next() {
|
|
|
|
var limitSerialized []byte
|
|
|
|
var orderSerialized []byte
|
|
|
|
|
|
|
|
err := rows.Scan(&limitSerialized, &orderSerialized)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info orders.Info
|
2019-07-01 16:54:11 +01:00
|
|
|
info.Limit = &pb.OrderLimit{}
|
|
|
|
info.Order = &pb.Order{}
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
err = proto.Unmarshal(limitSerialized, info.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(orderSerialized, info.Order)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
infos[info.Limit.SatelliteId] = append(infos[info.Limit.SatelliteId], &info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos, ErrInfo.Wrap(rows.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Archive marks order as being handled.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (db *ordersdb) Archive(ctx context.Context, satellite storj.NodeID, serial storj.SerialNumber, status orders.Status) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
result, err := db.db.Exec(`
|
|
|
|
INSERT INTO order_archive (
|
|
|
|
satellite_id, serial_number,
|
|
|
|
order_limit_serialized, order_serialized,
|
|
|
|
uplink_cert_id,
|
|
|
|
status, archived_at
|
2019-06-04 13:31:39 +01:00
|
|
|
) SELECT
|
2019-03-21 13:24:26 +00:00
|
|
|
satellite_id, serial_number,
|
2019-06-04 13:31:39 +01:00
|
|
|
order_limit_serialized, order_serialized,
|
2019-03-21 13:24:26 +00:00
|
|
|
uplink_cert_id,
|
|
|
|
?, ?
|
|
|
|
FROM unsent_order
|
|
|
|
WHERE satellite_id = ? AND serial_number = ?;
|
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
DELETE FROM unsent_order
|
2019-03-21 13:24:26 +00:00
|
|
|
WHERE satellite_id = ? AND serial_number = ?;
|
|
|
|
`, int(status), time.Now(), satellite, serial, satellite, serial)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
return ErrInfo.New("order was not in unsent list")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListArchived returns orders that have been sent.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (db *ordersdb) ListArchived(ctx context.Context, limit int) (_ []*orders.ArchivedInfo, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
rows, err := db.db.Query(`
|
2019-07-09 22:33:45 +01:00
|
|
|
SELECT order_limit_serialized, order_serialized, status, archived_at
|
2019-03-21 13:24:26 +00:00
|
|
|
FROM order_archive
|
|
|
|
LIMIT ?
|
|
|
|
`, limit)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
var infos []*orders.ArchivedInfo
|
|
|
|
for rows.Next() {
|
|
|
|
var limitSerialized []byte
|
|
|
|
var orderSerialized []byte
|
|
|
|
|
|
|
|
var status int
|
|
|
|
var archivedAt time.Time
|
|
|
|
|
2019-07-09 22:33:45 +01:00
|
|
|
err := rows.Scan(&limitSerialized, &orderSerialized, &status, &archivedAt)
|
2019-03-21 13:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info orders.ArchivedInfo
|
2019-07-01 16:54:11 +01:00
|
|
|
info.Limit = &pb.OrderLimit{}
|
|
|
|
info.Order = &pb.Order{}
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
info.Status = orders.Status(status)
|
|
|
|
info.ArchivedAt = archivedAt
|
|
|
|
|
|
|
|
err = proto.Unmarshal(limitSerialized, info.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(orderSerialized, info.Order)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
infos = append(infos, &info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos, ErrInfo.Wrap(rows.Err())
|
|
|
|
}
|