2020-12-18 09:27:19 +00:00
|
|
|
package audit
|
|
|
|
|
2020-12-18 12:52:27 +00:00
|
|
|
import (
|
2020-12-21 08:40:30 +00:00
|
|
|
"context"
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
2021-06-23 12:27:00 +00:00
|
|
|
"encoding/hex"
|
2020-12-21 08:40:30 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2022-06-08 23:18:26 +00:00
|
|
|
netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2022-07-01 18:16:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/storagegroup"
|
2022-01-31 13:34:01 +00:00
|
|
|
cntClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
2020-12-22 00:28:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2022-06-08 23:18:26 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
2020-12-23 15:01:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/rand"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-19 19:40:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-12-18 12:52:27 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
func (ap *Processor) processStartAudit(epoch uint64) {
|
2020-12-21 08:40:30 +00:00
|
|
|
log := ap.log.With(zap.Uint64("epoch", epoch))
|
2020-12-18 12:52:27 +00:00
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
ap.prevAuditCanceler()
|
2020-12-22 12:34:38 +00:00
|
|
|
|
|
|
|
skipped := ap.taskManager.Reset()
|
|
|
|
if skipped > 0 {
|
|
|
|
ap.log.Info("some tasks from previous epoch are skipped",
|
|
|
|
zap.Int("amount", skipped),
|
|
|
|
)
|
|
|
|
}
|
2020-12-22 00:28:42 +00:00
|
|
|
|
2020-12-18 12:52:27 +00:00
|
|
|
containers, err := ap.selectContainersToAudit(epoch)
|
|
|
|
if err != nil {
|
2020-12-21 08:40:30 +00:00
|
|
|
log.Error("container selection failure", zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("select containers for audit", zap.Int("amount", len(containers)))
|
|
|
|
|
|
|
|
nm, err := ap.netmapClient.GetNetMap(0)
|
|
|
|
if err != nil {
|
|
|
|
ap.log.Error("can't fetch network map",
|
2020-12-18 12:52:27 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-24 14:40:29 +00:00
|
|
|
var auditCtx context.Context
|
|
|
|
auditCtx, ap.prevAuditCanceler = context.WithCancel(context.Background())
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
pivot := make([]byte, sha256.Size)
|
|
|
|
|
2020-12-21 08:40:30 +00:00
|
|
|
for i := range containers {
|
2022-01-31 13:34:01 +00:00
|
|
|
cnr, err := cntClient.Get(ap.containerClient, containers[i]) // get container structure
|
2020-12-21 08:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("can't get container info, ignore",
|
|
|
|
zap.Stringer("cid", containers[i]),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
containers[i].Encode(pivot)
|
2021-01-26 16:57:03 +00:00
|
|
|
|
2020-12-21 08:40:30 +00:00
|
|
|
// find all container nodes for current epoch
|
2022-06-28 07:01:05 +00:00
|
|
|
nodes, err := nm.ContainerNodes(cnr.Value.PlacementPolicy(), pivot)
|
2020-12-21 08:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Info("can't build placement for container, ignore",
|
|
|
|
zap.Stringer("cid", containers[i]),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
n := placement.FlattenNodes(nodes)
|
2020-12-23 15:01:03 +00:00
|
|
|
|
|
|
|
// shuffle nodes to ask a random one
|
2022-01-11 14:07:59 +00:00
|
|
|
rand.Shuffle(len(n), func(i, j int) {
|
2020-12-21 08:40:30 +00:00
|
|
|
n[i], n[j] = n[j], n[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
// search storage groups
|
2022-05-19 19:40:22 +00:00
|
|
|
storageGroupsIDs := ap.findStorageGroups(containers[i], n)
|
2020-12-21 08:40:30 +00:00
|
|
|
log.Info("select storage groups for audit",
|
2022-05-19 19:40:22 +00:00
|
|
|
zap.Stringer("cid", containers[i]),
|
|
|
|
zap.Int("amount", len(storageGroupsIDs)))
|
|
|
|
|
|
|
|
// filter expired storage groups
|
|
|
|
storageGroups := ap.filterExpiredSG(containers[i], storageGroupsIDs, nodes, *nm)
|
|
|
|
log.Info("filter expired storage groups for audit",
|
2020-12-21 08:40:30 +00:00
|
|
|
zap.Stringer("cid", containers[i]),
|
|
|
|
zap.Int("amount", len(storageGroups)))
|
|
|
|
|
2022-05-19 19:40:22 +00:00
|
|
|
// skip audit for containers without
|
|
|
|
// non-expired storage groups
|
|
|
|
if len(storageGroupsIDs) == 0 {
|
2021-07-06 18:12:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
auditTask := new(audit.Task).
|
|
|
|
WithReporter(&epochAuditReporter{
|
|
|
|
epoch: epoch,
|
|
|
|
rep: ap.reporter,
|
|
|
|
}).
|
|
|
|
WithAuditContext(auditCtx).
|
|
|
|
WithContainerID(containers[i]).
|
|
|
|
WithStorageGroupList(storageGroups).
|
2022-06-22 10:55:31 +00:00
|
|
|
WithContainerStructure(cnr.Value).
|
2020-12-23 09:51:37 +00:00
|
|
|
WithContainerNodes(nodes).
|
|
|
|
WithNetworkMap(nm)
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
if err := ap.taskManager.PushTask(auditTask); err != nil {
|
|
|
|
ap.log.Error("could not push audit task",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2020-12-21 08:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (ap *Processor) findStorageGroups(cnr cid.ID, shuffled netmapcore.Nodes) []oid.ID {
|
2022-05-31 17:00:41 +00:00
|
|
|
var sg []oid.ID
|
2020-12-21 08:40:30 +00:00
|
|
|
|
|
|
|
ln := len(shuffled)
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
var (
|
|
|
|
info clientcore.NodeInfo
|
2022-07-01 18:16:28 +00:00
|
|
|
prm storagegroup.SearchSGPrm
|
2021-10-27 12:12:05 +00:00
|
|
|
)
|
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
prm.Container = cnr
|
2021-09-28 05:32:30 +00:00
|
|
|
|
2020-12-21 08:40:30 +00:00
|
|
|
for i := range shuffled { // consider iterating over some part of container
|
|
|
|
log := ap.log.With(
|
2022-05-31 17:00:41 +00:00
|
|
|
zap.Stringer("cid", cnr),
|
2021-06-23 12:27:00 +00:00
|
|
|
zap.String("key", hex.EncodeToString(shuffled[0].PublicKey())),
|
2020-12-21 08:40:30 +00:00
|
|
|
zap.Int("try", i),
|
|
|
|
zap.Int("total_tries", ln),
|
|
|
|
)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, netmapcore.Node(shuffled[i]))
|
2020-12-21 08:40:30 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
log.Warn("parse client node info", zap.String("error", err.Error()))
|
2020-12-23 08:45:36 +00:00
|
|
|
|
|
|
|
continue
|
2020-12-21 08:40:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), ap.searchTimeout)
|
2020-12-21 08:40:30 +00:00
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
prm.Context = ctx
|
|
|
|
prm.NodeInfo = info
|
2020-12-21 08:40:30 +00:00
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
var dst storagegroup.SearchSGDst
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
err = ap.sgSrc.ListSG(&dst, prm)
|
2020-12-21 08:40:30 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("error in storage group search", zap.String("error", err.Error()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
sg = append(sg, dst.Objects...)
|
2020-12-21 08:40:30 +00:00
|
|
|
|
|
|
|
break // we found storage groups, so break loop
|
|
|
|
}
|
|
|
|
|
|
|
|
return sg
|
|
|
|
}
|
2022-05-19 19:40:22 +00:00
|
|
|
|
|
|
|
func (ap *Processor) filterExpiredSG(cid cid.ID, sgIDs []oid.ID,
|
2022-07-01 18:45:18 +00:00
|
|
|
cnr [][]netmap.NodeInfo, nm netmap.NetMap) []storagegroup.StorageGroup {
|
|
|
|
sgs := make([]storagegroup.StorageGroup, 0, len(sgIDs))
|
|
|
|
var coreSG storagegroup.StorageGroup
|
2022-05-19 19:40:22 +00:00
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
var getSGPrm storagegroup.GetSGPrm
|
2022-05-19 19:40:22 +00:00
|
|
|
getSGPrm.CID = cid
|
|
|
|
getSGPrm.Container = cnr
|
|
|
|
getSGPrm.NetMap = nm
|
|
|
|
|
|
|
|
for _, sgID := range sgIDs {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), ap.searchTimeout)
|
|
|
|
|
|
|
|
getSGPrm.OID = sgID
|
|
|
|
getSGPrm.Context = ctx
|
|
|
|
|
|
|
|
sg, err := ap.sgSrc.GetSG(getSGPrm)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ap.log.Error(
|
|
|
|
"could not get storage group object for audit, skipping",
|
|
|
|
zap.Stringer("cid", cid),
|
|
|
|
zap.Stringer("oid", sgID),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter expired epochs
|
2022-09-05 05:48:42 +00:00
|
|
|
if sg.ExpirationEpoch() >= ap.epochSrc.EpochCounter() {
|
2022-07-01 18:45:18 +00:00
|
|
|
coreSG.SetID(sgID)
|
|
|
|
coreSG.SetStorageGroup(*sg)
|
|
|
|
|
|
|
|
sgs = append(sgs, coreSG)
|
2022-05-19 19:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sgs
|
|
|
|
}
|