2019-08-08 14:47:04 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-06 13:01:03 +01:00
|
|
|
"database/sql"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-12-30 19:47:14 +00:00
|
|
|
"storj.io/common/pb"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2019-08-08 14:47:04 +01:00
|
|
|
"storj.io/storj/storagenode/reputation"
|
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrReputation represents errors from the reputation database.
|
|
|
|
var ErrReputation = errs.Class("reputation error")
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
// ReputationDBName represents the database name.
|
|
|
|
const ReputationDBName = "reputation"
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// reputation works with node reputation DB.
|
2019-08-08 14:47:04 +01:00
|
|
|
type reputationDB struct {
|
2019-11-13 16:49:22 +00:00
|
|
|
dbContainerImpl
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 13:01:03 +01:00
|
|
|
// Store inserts or updates reputation stats into the db.
|
2019-08-08 14:47:04 +01:00
|
|
|
func (db *reputationDB) Store(ctx context.Context, stats reputation.Stats) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-28 21:54:12 +01:00
|
|
|
query := `INSERT OR REPLACE INTO reputation (
|
2019-11-13 16:49:22 +00:00
|
|
|
satellite_id,
|
2019-08-28 21:54:12 +01:00
|
|
|
uptime_success_count,
|
|
|
|
uptime_total_count,
|
|
|
|
uptime_reputation_alpha,
|
|
|
|
uptime_reputation_beta,
|
|
|
|
uptime_reputation_score,
|
|
|
|
audit_success_count,
|
|
|
|
audit_total_count,
|
|
|
|
audit_reputation_alpha,
|
|
|
|
audit_reputation_beta,
|
|
|
|
audit_reputation_score,
|
2020-05-03 17:30:54 +01:00
|
|
|
audit_unknown_reputation_alpha,
|
|
|
|
audit_unknown_reputation_beta,
|
2020-05-18 11:01:34 +01:00
|
|
|
audit_unknown_reputation_score,
|
2020-09-01 12:20:12 +01:00
|
|
|
online_score,
|
2020-12-30 19:47:14 +00:00
|
|
|
audit_history,
|
2020-09-01 12:20:12 +01:00
|
|
|
disqualified_at,
|
|
|
|
suspended_at,
|
|
|
|
offline_suspended_at,
|
2020-09-02 16:37:54 +01:00
|
|
|
offline_under_review_at,
|
2020-04-16 16:40:28 +01:00
|
|
|
updated_at,
|
|
|
|
joined_at
|
2020-12-30 19:47:14 +00:00
|
|
|
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
|
2019-08-28 21:54:12 +01:00
|
|
|
|
|
|
|
// ensure we insert utc
|
2020-09-01 12:20:12 +01:00
|
|
|
if stats.DisqualifiedAt != nil {
|
|
|
|
utc := stats.DisqualifiedAt.UTC()
|
|
|
|
stats.DisqualifiedAt = &utc
|
2019-08-28 21:54:12 +01:00
|
|
|
}
|
2020-09-01 12:20:12 +01:00
|
|
|
if stats.SuspendedAt != nil {
|
|
|
|
utc := stats.SuspendedAt.UTC()
|
|
|
|
stats.SuspendedAt = &utc
|
|
|
|
}
|
|
|
|
if stats.OfflineSuspendedAt != nil {
|
|
|
|
utc := stats.OfflineSuspendedAt.UTC()
|
|
|
|
stats.OfflineSuspendedAt = &utc
|
2020-03-27 18:50:57 +00:00
|
|
|
}
|
2020-09-02 16:37:54 +01:00
|
|
|
if stats.OfflineUnderReviewAt != nil {
|
|
|
|
utc := stats.OfflineUnderReviewAt.UTC()
|
|
|
|
stats.OfflineUnderReviewAt = &utc
|
|
|
|
}
|
2019-08-28 21:54:12 +01:00
|
|
|
|
2020-12-30 19:47:14 +00:00
|
|
|
var auditHistoryBytes []byte
|
|
|
|
if stats.AuditHistory != nil {
|
|
|
|
auditHistoryBytes, err = pb.Marshal(stats.AuditHistory)
|
|
|
|
if err != nil {
|
|
|
|
return ErrReputation.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:54:12 +01:00
|
|
|
_, err = db.ExecContext(ctx, query,
|
2019-08-08 14:47:04 +01:00
|
|
|
stats.SatelliteID,
|
|
|
|
stats.Uptime.SuccessCount,
|
|
|
|
stats.Uptime.TotalCount,
|
|
|
|
stats.Uptime.Alpha,
|
|
|
|
stats.Uptime.Beta,
|
|
|
|
stats.Uptime.Score,
|
|
|
|
stats.Audit.SuccessCount,
|
|
|
|
stats.Audit.TotalCount,
|
|
|
|
stats.Audit.Alpha,
|
|
|
|
stats.Audit.Beta,
|
|
|
|
stats.Audit.Score,
|
2020-05-03 17:30:54 +01:00
|
|
|
stats.Audit.UnknownAlpha,
|
|
|
|
stats.Audit.UnknownBeta,
|
2020-05-18 11:01:34 +01:00
|
|
|
stats.Audit.UnknownScore,
|
2020-09-01 12:20:12 +01:00
|
|
|
stats.OnlineScore,
|
2020-12-30 19:47:14 +00:00
|
|
|
auditHistoryBytes,
|
2020-09-01 12:20:12 +01:00
|
|
|
stats.DisqualifiedAt,
|
|
|
|
stats.SuspendedAt,
|
|
|
|
stats.OfflineSuspendedAt,
|
2020-09-02 16:37:54 +01:00
|
|
|
stats.OfflineUnderReviewAt,
|
2019-08-08 14:47:04 +01:00
|
|
|
stats.UpdatedAt.UTC(),
|
2020-04-16 16:40:28 +01:00
|
|
|
stats.JoinedAt.UTC(),
|
2019-08-08 14:47:04 +01:00
|
|
|
)
|
|
|
|
|
2019-08-28 21:54:12 +01:00
|
|
|
return ErrReputation.Wrap(err)
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 13:01:03 +01:00
|
|
|
// Get retrieves stats for specific satellite.
|
2019-08-08 14:47:04 +01:00
|
|
|
func (db *reputationDB) Get(ctx context.Context, satelliteID storj.NodeID) (_ *reputation.Stats, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-09-06 13:01:03 +01:00
|
|
|
stats := reputation.Stats{
|
|
|
|
SatelliteID: satelliteID,
|
|
|
|
}
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
row := db.QueryRowContext(ctx,
|
2019-09-06 13:01:03 +01:00
|
|
|
`SELECT uptime_success_count,
|
2019-08-28 21:54:12 +01:00
|
|
|
uptime_total_count,
|
|
|
|
uptime_reputation_alpha,
|
|
|
|
uptime_reputation_beta,
|
|
|
|
uptime_reputation_score,
|
|
|
|
audit_success_count,
|
|
|
|
audit_total_count,
|
|
|
|
audit_reputation_alpha,
|
|
|
|
audit_reputation_beta,
|
|
|
|
audit_reputation_score,
|
2020-05-03 17:30:54 +01:00
|
|
|
audit_unknown_reputation_alpha,
|
|
|
|
audit_unknown_reputation_beta,
|
2020-05-18 11:01:34 +01:00
|
|
|
audit_unknown_reputation_score,
|
2020-09-01 12:20:12 +01:00
|
|
|
online_score,
|
2020-12-30 19:47:14 +00:00
|
|
|
audit_history,
|
2020-09-01 12:20:12 +01:00
|
|
|
disqualified_at,
|
|
|
|
suspended_at,
|
|
|
|
offline_suspended_at,
|
2020-09-02 16:37:54 +01:00
|
|
|
offline_under_review_at,
|
2020-04-16 16:40:28 +01:00
|
|
|
updated_at,
|
|
|
|
joined_at
|
2019-08-28 21:54:12 +01:00
|
|
|
FROM reputation WHERE satellite_id = ?`,
|
2019-08-08 14:47:04 +01:00
|
|
|
satelliteID,
|
|
|
|
)
|
|
|
|
|
2020-12-30 19:47:14 +00:00
|
|
|
var auditHistoryBytes []byte
|
2019-09-06 13:01:03 +01:00
|
|
|
err = row.Scan(
|
2019-08-08 14:47:04 +01:00
|
|
|
&stats.Uptime.SuccessCount,
|
|
|
|
&stats.Uptime.TotalCount,
|
|
|
|
&stats.Uptime.Alpha,
|
|
|
|
&stats.Uptime.Beta,
|
|
|
|
&stats.Uptime.Score,
|
|
|
|
&stats.Audit.SuccessCount,
|
|
|
|
&stats.Audit.TotalCount,
|
|
|
|
&stats.Audit.Alpha,
|
|
|
|
&stats.Audit.Beta,
|
|
|
|
&stats.Audit.Score,
|
2020-05-03 17:30:54 +01:00
|
|
|
&stats.Audit.UnknownAlpha,
|
|
|
|
&stats.Audit.UnknownBeta,
|
2020-05-18 11:01:34 +01:00
|
|
|
&stats.Audit.UnknownScore,
|
2020-09-01 12:20:12 +01:00
|
|
|
&stats.OnlineScore,
|
2020-12-30 19:47:14 +00:00
|
|
|
&auditHistoryBytes,
|
2020-09-01 12:20:12 +01:00
|
|
|
&stats.DisqualifiedAt,
|
|
|
|
&stats.SuspendedAt,
|
|
|
|
&stats.OfflineSuspendedAt,
|
2020-09-02 16:37:54 +01:00
|
|
|
&stats.OfflineUnderReviewAt,
|
2019-08-08 14:47:04 +01:00
|
|
|
&stats.UpdatedAt,
|
2020-04-16 16:40:28 +01:00
|
|
|
&stats.JoinedAt,
|
2019-08-08 14:47:04 +01:00
|
|
|
)
|
2019-08-28 21:54:12 +01:00
|
|
|
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-09-06 13:01:03 +01:00
|
|
|
err = nil
|
2020-12-30 19:47:14 +00:00
|
|
|
return &stats, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return &stats, ErrReputation.Wrap(err)
|
2019-09-06 13:01:03 +01:00
|
|
|
}
|
|
|
|
|
2020-12-30 19:47:14 +00:00
|
|
|
if auditHistoryBytes != nil {
|
|
|
|
stats.AuditHistory = &pb.AuditHistory{}
|
|
|
|
err = pb.Unmarshal(auditHistoryBytes, stats.AuditHistory)
|
|
|
|
}
|
2019-08-28 21:54:12 +01:00
|
|
|
return &stats, ErrReputation.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-09-06 13:01:03 +01:00
|
|
|
// All retrieves all stats from DB.
|
2019-08-28 21:54:12 +01:00
|
|
|
func (db *reputationDB) All(ctx context.Context) (_ []reputation.Stats, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-13 16:49:22 +00:00
|
|
|
query := `SELECT satellite_id,
|
2019-08-28 21:54:12 +01:00
|
|
|
uptime_success_count,
|
|
|
|
uptime_total_count,
|
|
|
|
uptime_reputation_alpha,
|
|
|
|
uptime_reputation_beta,
|
|
|
|
uptime_reputation_score,
|
|
|
|
audit_success_count,
|
|
|
|
audit_total_count,
|
|
|
|
audit_reputation_alpha,
|
|
|
|
audit_reputation_beta,
|
|
|
|
audit_reputation_score,
|
2020-05-03 17:30:54 +01:00
|
|
|
audit_unknown_reputation_alpha,
|
|
|
|
audit_unknown_reputation_beta,
|
2020-05-18 11:01:34 +01:00
|
|
|
audit_unknown_reputation_score,
|
2020-09-01 12:20:12 +01:00
|
|
|
online_score,
|
|
|
|
disqualified_at,
|
|
|
|
suspended_at,
|
|
|
|
offline_suspended_at,
|
2020-09-02 16:37:54 +01:00
|
|
|
offline_under_review_at,
|
2020-04-16 16:40:28 +01:00
|
|
|
updated_at,
|
|
|
|
joined_at
|
2019-08-28 21:54:12 +01:00
|
|
|
FROM reputation`
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query)
|
2019-08-08 14:47:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:54:12 +01:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
var statsList []reputation.Stats
|
|
|
|
for rows.Next() {
|
|
|
|
var stats reputation.Stats
|
|
|
|
|
|
|
|
err := rows.Scan(&stats.SatelliteID,
|
|
|
|
&stats.Uptime.SuccessCount,
|
|
|
|
&stats.Uptime.TotalCount,
|
|
|
|
&stats.Uptime.Alpha,
|
|
|
|
&stats.Uptime.Beta,
|
|
|
|
&stats.Uptime.Score,
|
|
|
|
&stats.Audit.SuccessCount,
|
|
|
|
&stats.Audit.TotalCount,
|
|
|
|
&stats.Audit.Alpha,
|
|
|
|
&stats.Audit.Beta,
|
|
|
|
&stats.Audit.Score,
|
2020-05-03 17:30:54 +01:00
|
|
|
&stats.Audit.UnknownAlpha,
|
|
|
|
&stats.Audit.UnknownBeta,
|
2020-05-18 11:01:34 +01:00
|
|
|
&stats.Audit.UnknownScore,
|
2020-09-01 12:20:12 +01:00
|
|
|
&stats.OnlineScore,
|
|
|
|
&stats.DisqualifiedAt,
|
|
|
|
&stats.SuspendedAt,
|
|
|
|
&stats.OfflineSuspendedAt,
|
2020-09-02 16:37:54 +01:00
|
|
|
&stats.OfflineUnderReviewAt,
|
2019-08-28 21:54:12 +01:00
|
|
|
&stats.UpdatedAt,
|
2020-04-16 16:40:28 +01:00
|
|
|
&stats.JoinedAt,
|
2019-08-28 21:54:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrReputation.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statsList = append(statsList, stats)
|
|
|
|
}
|
|
|
|
|
2020-01-16 14:36:50 +00:00
|
|
|
return statsList, rows.Err()
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|