2020-04-09 16:19:44 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/pgutil"
|
2020-04-09 16:19:44 +01:00
|
|
|
"storj.io/private/version"
|
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (cache *overlaycache) SelectStorageNodes(ctx context.Context, totalNeededNodes, newNodeCount int, criteria *overlay.NodeCriteria) (nodes []*overlay.SelectedNode, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
if totalNeededNodes == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if newNodeCount > totalNeededNodes {
|
|
|
|
return nil, Error.New("requested new node count can't exceed requested total node count")
|
|
|
|
}
|
|
|
|
|
|
|
|
needNewNodes := newNodeCount
|
|
|
|
needReputableNodes := totalNeededNodes - needNewNodes
|
|
|
|
|
|
|
|
receivedNewNodes := 0
|
|
|
|
receivedNodeNetworks := make(map[string]struct{})
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
excludedIDs := append([]storj.NodeID{}, criteria.ExcludedIDs...)
|
|
|
|
excludedNetworks := append([]string{}, criteria.ExcludedNetworks...)
|
2020-04-09 16:19:44 +01:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
reputableNodes, newNodes, err := cache.selectStorageNodesOnce(ctx, needReputableNodes, needNewNodes, criteria, excludedIDs, excludedNetworks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range newNodes {
|
|
|
|
// checking for last net collision among reputable and new nodes since we can't check within the query
|
|
|
|
if _, ok := receivedNodeNetworks[node.LastNet]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
excludedIDs = append(excludedIDs, node.ID)
|
|
|
|
excludedNetworks = append(excludedNetworks, node.LastNet)
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
nodes = append(nodes, node)
|
|
|
|
needNewNodes--
|
|
|
|
receivedNewNodes++
|
|
|
|
|
|
|
|
if criteria.DistinctIP {
|
|
|
|
receivedNodeNetworks[node.LastNet] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, node := range reputableNodes {
|
|
|
|
if _, ok := receivedNodeNetworks[node.LastNet]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
excludedIDs = append(excludedIDs, node.ID)
|
|
|
|
excludedNetworks = append(excludedNetworks, node.LastNet)
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
nodes = append(nodes, node)
|
|
|
|
needReputableNodes--
|
|
|
|
|
|
|
|
if criteria.DistinctIP {
|
|
|
|
receivedNodeNetworks[node.LastNet] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
// when we did not find new nodes, then return all as reputable
|
2020-04-09 16:19:44 +01:00
|
|
|
if needNewNodes > 0 && receivedNewNodes == 0 {
|
|
|
|
needReputableNodes += needNewNodes
|
|
|
|
needNewNodes = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if needReputableNodes <= 0 && needNewNodes <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2020-12-22 19:07:07 +00:00
|
|
|
func (cache *overlaycache) selectStorageNodesOnce(ctx context.Context, reputableNodeCount, newNodeCount int, criteria *overlay.NodeCriteria, excludedIDs []storj.NodeID, excludedNetworks []string) (reputableNodes, newNodes []*overlay.SelectedNode, err error) {
|
2020-04-09 16:19:44 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
newNodesCondition, err := nodeSelectionCondition(ctx, criteria, excludedIDs, excludedNetworks, true)
|
2020-04-09 16:19:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
reputableNodesCondition, err := nodeSelectionCondition(ctx, criteria, excludedIDs, excludedNetworks, false)
|
2020-04-09 16:19:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
var reputableNodeQuery, newNodeQuery partialQuery
|
2020-04-09 16:19:44 +01:00
|
|
|
|
2021-05-11 09:49:26 +01:00
|
|
|
asOf := cache.db.impl.AsOfSystemInterval(criteria.AsOfSystemInterval)
|
2020-12-22 19:07:07 +00:00
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
// Note: the true/false at the end of each selection string indicates if the selection is for new nodes or not.
|
|
|
|
// Later, the flag allows us to distinguish if a node is new when scanning the db rows.
|
|
|
|
if !criteria.DistinctIP {
|
|
|
|
reputableNodeQuery = partialQuery{
|
2020-12-22 19:07:07 +00:00
|
|
|
selection: `SELECT last_net, id, address, last_ip_port, false FROM nodes ` + asOf,
|
|
|
|
condition: reputableNodesCondition,
|
|
|
|
limit: reputableNodeCount,
|
|
|
|
aostClause: asOf,
|
2020-04-09 16:54:12 +01:00
|
|
|
}
|
|
|
|
newNodeQuery = partialQuery{
|
2020-12-22 19:07:07 +00:00
|
|
|
selection: `SELECT last_net, id, address, last_ip_port, true FROM nodes ` + asOf,
|
|
|
|
condition: newNodesCondition,
|
|
|
|
limit: newNodeCount,
|
|
|
|
aostClause: asOf,
|
2020-04-09 16:54:12 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reputableNodeQuery = partialQuery{
|
2020-12-22 19:07:07 +00:00
|
|
|
selection: `SELECT DISTINCT ON (last_net) last_net, id, address, last_ip_port, false FROM nodes ` + asOf,
|
|
|
|
condition: reputableNodesCondition,
|
|
|
|
distinct: true,
|
|
|
|
limit: reputableNodeCount,
|
|
|
|
orderBy: "last_net",
|
|
|
|
aostClause: asOf,
|
2020-04-09 16:54:12 +01:00
|
|
|
}
|
|
|
|
newNodeQuery = partialQuery{
|
2020-12-22 19:07:07 +00:00
|
|
|
selection: `SELECT DISTINCT ON (last_net) last_net, id, address, last_ip_port, true FROM nodes ` + asOf,
|
|
|
|
condition: newNodesCondition,
|
|
|
|
distinct: true,
|
|
|
|
limit: newNodeCount,
|
|
|
|
orderBy: "last_net",
|
|
|
|
aostClause: asOf,
|
2020-04-09 16:54:12 +01:00
|
|
|
}
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
query := unionAll(newNodeQuery, reputableNodeQuery)
|
2020-04-09 16:19:44 +01:00
|
|
|
|
|
|
|
rows, err := cache.db.Query(ctx, cache.db.Rebind(query.query), query.args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var node overlay.SelectedNode
|
|
|
|
node.Address = &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC}
|
|
|
|
var lastIPPort sql.NullString
|
|
|
|
var isNew bool
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
err = rows.Scan(&node.LastNet, &node.ID, &node.Address.Address, &node.LastIPPort, &isNew)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
if lastIPPort.Valid {
|
|
|
|
node.LastIPPort = lastIPPort.String
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
if isNew {
|
|
|
|
newNodes = append(newNodes, &node)
|
|
|
|
} else {
|
|
|
|
reputableNodes = append(reputableNodes, &node)
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
if len(newNodes) >= newNodeCount && len(reputableNodes) >= reputableNodeCount {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reputableNodes, newNodes, Error.Wrap(rows.Err())
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
// nodeSelectionCondition creates a condition with arguments that corresponds to the arguments.
|
2020-12-22 19:07:07 +00:00
|
|
|
func nodeSelectionCondition(ctx context.Context, criteria *overlay.NodeCriteria, excludedIDs []storj.NodeID, excludedNetworks []string, isNewNodeQuery bool) (condition, error) {
|
2020-04-09 16:19:44 +01:00
|
|
|
var conds conditions
|
2020-04-09 16:54:12 +01:00
|
|
|
conds.add(`disqualified IS NULL`)
|
2020-06-10 17:11:25 +01:00
|
|
|
conds.add(`unknown_audit_suspended IS NULL`)
|
2021-03-18 19:55:06 +00:00
|
|
|
conds.add(`offline_suspended IS NULL`)
|
2020-04-09 16:54:12 +01:00
|
|
|
conds.add(`exit_initiated_at IS NULL`)
|
|
|
|
|
|
|
|
conds.add(`type = ?`, int(pb.NodeType_STORAGE))
|
|
|
|
conds.add(`free_disk >= ?`, criteria.FreeDisk)
|
2020-07-08 15:28:49 +01:00
|
|
|
conds.add(`last_contact_success > ?`, time.Now().UTC().Add(-criteria.OnlineWindow))
|
2020-04-09 16:54:12 +01:00
|
|
|
|
2020-04-09 16:19:44 +01:00
|
|
|
if isNewNodeQuery {
|
|
|
|
conds.add(
|
2020-07-08 15:28:49 +01:00
|
|
|
`vetted_at IS NULL`,
|
2020-04-09 16:19:44 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
conds.add(
|
2020-07-08 15:28:49 +01:00
|
|
|
`vetted_at is NOT NULL`,
|
2020-04-09 16:19:44 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if criteria.MinimumVersion != "" {
|
|
|
|
v, err := version.NewSemVer(criteria.MinimumVersion)
|
|
|
|
if err != nil {
|
|
|
|
return condition{}, Error.New("invalid node selection criteria version: %v", err)
|
|
|
|
}
|
|
|
|
conds.add(
|
2020-04-09 16:54:12 +01:00
|
|
|
`(major > ? OR (major = ? AND (minor > ? OR (minor = ? AND patch >= ?)))) AND release`,
|
2020-04-09 16:19:44 +01:00
|
|
|
v.Major, v.Major, v.Minor, v.Minor, v.Patch,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(excludedIDs) > 0 {
|
2020-04-09 18:32:31 +01:00
|
|
|
conds.add(
|
|
|
|
`not (id = any(?::bytea[]))`,
|
2020-06-28 04:56:29 +01:00
|
|
|
pgutil.NodeIDArray(excludedIDs),
|
2020-04-09 16:54:12 +01:00
|
|
|
)
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
if criteria.DistinctIP {
|
|
|
|
if len(excludedNetworks) > 0 {
|
2020-04-09 18:32:31 +01:00
|
|
|
conds.add(
|
|
|
|
`not (last_net = any(?::text[]))`,
|
2020-09-29 10:53:15 +01:00
|
|
|
pgutil.TextArray(excludedNetworks),
|
2020-04-09 16:54:12 +01:00
|
|
|
)
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
conds.add(`last_net <> ''`)
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
return conds.combine(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// partialQuery corresponds to a query.
|
2020-04-09 16:54:12 +01:00
|
|
|
//
|
2020-12-05 16:01:42 +00:00
|
|
|
// distinct=false
|
2020-04-09 16:54:12 +01:00
|
|
|
//
|
2020-12-05 16:01:42 +00:00
|
|
|
// $selection WHERE $condition ORDER BY $orderBy, RANDOM() LIMIT $limit
|
2020-04-09 16:54:12 +01:00
|
|
|
//
|
2020-12-05 16:01:42 +00:00
|
|
|
// distinct=true
|
2020-04-09 16:54:12 +01:00
|
|
|
//
|
2020-12-05 16:01:42 +00:00
|
|
|
// SELECT * FROM ($selection WHERE $condition ORDER BY $orderBy, RANDOM()) filtered ORDER BY RANDOM() LIMIT $limit
|
2020-04-09 16:54:12 +01:00
|
|
|
//
|
|
|
|
type partialQuery struct {
|
2020-12-22 19:07:07 +00:00
|
|
|
selection string
|
|
|
|
condition condition
|
|
|
|
distinct bool
|
|
|
|
orderBy string
|
|
|
|
limit int
|
|
|
|
aostClause string
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
// isEmpty returns whether the result for the query is definitely empty.
|
|
|
|
func (partial partialQuery) isEmpty() bool {
|
|
|
|
return partial.limit == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// asQuery combines partialQuery parameters into a single select query.
|
|
|
|
func (partial partialQuery) asQuery() query {
|
2020-04-09 16:19:44 +01:00
|
|
|
var q strings.Builder
|
|
|
|
var args []interface{}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
if partial.distinct {
|
|
|
|
// For distinct queries we need to redo randomized ordering.
|
|
|
|
fmt.Fprintf(&q, "SELECT * FROM (")
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
fmt.Fprint(&q, partial.selection, " WHERE ", partial.condition.query)
|
2020-04-09 16:19:44 +01:00
|
|
|
args = append(args, partial.condition.args...)
|
|
|
|
|
|
|
|
if partial.orderBy != "" {
|
|
|
|
fmt.Fprintf(&q, " ORDER BY %s, RANDOM() ", partial.orderBy)
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(&q, " ORDER BY RANDOM() ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !partial.distinct {
|
2020-04-09 16:54:12 +01:00
|
|
|
fmt.Fprint(&q, " LIMIT ? ")
|
|
|
|
args = append(args, partial.limit)
|
|
|
|
} else {
|
2020-12-22 19:07:07 +00:00
|
|
|
fmt.Fprint(&q, ") filtered "+partial.aostClause+" ORDER BY RANDOM() LIMIT ?")
|
2020-04-09 16:19:44 +01:00
|
|
|
args = append(args, partial.limit)
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
return query{query: q.String(), args: args}
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
// unionAll combines multiple partial queries into a single query.
|
|
|
|
func unionAll(partials ...partialQuery) query {
|
|
|
|
var queries []string
|
|
|
|
var args []interface{}
|
|
|
|
for _, partial := range partials {
|
|
|
|
if partial.isEmpty() {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-09 16:19:44 +01:00
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
q := partial.asQuery()
|
|
|
|
queries = append(queries, q.query)
|
|
|
|
args = append(args, q.args...)
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
if len(queries) == 0 {
|
|
|
|
return query{}
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
if len(queries) == 1 {
|
|
|
|
return query{query: queries[0], args: args}
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return query{
|
2020-04-09 16:54:12 +01:00
|
|
|
query: "(" + strings.Join(queries, ") UNION ALL (") + ")",
|
2020-04-09 16:19:44 +01:00
|
|
|
args: args,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type condition struct {
|
|
|
|
query string
|
|
|
|
args []interface{}
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
type conditions []condition
|
2020-04-09 16:19:44 +01:00
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
func (conds *conditions) add(q string, args ...interface{}) {
|
|
|
|
*conds = append(*conds, condition{query: q, args: args})
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:54:12 +01:00
|
|
|
func (conds conditions) combine() condition {
|
2020-04-09 16:19:44 +01:00
|
|
|
var qs []string
|
|
|
|
var args []interface{}
|
2020-04-09 16:54:12 +01:00
|
|
|
for _, c := range conds {
|
2020-04-09 16:19:44 +01:00
|
|
|
qs = append(qs, c.query)
|
|
|
|
args = append(args, c.args...)
|
|
|
|
}
|
2020-04-09 16:54:12 +01:00
|
|
|
return condition{query: " " + strings.Join(qs, " AND ") + " ", args: args}
|
2020-04-09 16:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type query struct {
|
|
|
|
query string
|
|
|
|
args []interface{}
|
|
|
|
}
|