2020-12-18 09:27:19 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2020-12-22 00:28:42 +00:00
|
|
|
"context"
|
2021-03-13 15:22:21 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-23 15:07:10 +00:00
|
|
|
"time"
|
2020-12-22 00:28:42 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2022-01-31 13:34:01 +00:00
|
|
|
cntClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
2022-01-31 11:58:55 +00:00
|
|
|
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2020-12-22 00:28:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-01-26 12:11:13 +00:00
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Indexer is a callback interface for inner ring global state.
|
|
|
|
Indexer interface {
|
2021-03-23 14:14:28 +00:00
|
|
|
InnerRingIndex() int
|
2021-02-21 06:28:53 +00:00
|
|
|
InnerRingSize() int
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
TaskManager interface {
|
|
|
|
PushTask(*audit.Task) error
|
2020-12-22 12:34:38 +00:00
|
|
|
|
|
|
|
// Must skip all tasks planned for execution and
|
|
|
|
// return their number.
|
|
|
|
Reset() int
|
2020-12-22 00:28:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Processor of events related to data audit.
|
2020-12-18 09:27:19 +00:00
|
|
|
Processor struct {
|
2021-05-31 11:50:11 +00:00
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
irList Indexer
|
2021-10-27 12:12:05 +00:00
|
|
|
sgSrc SGSource
|
2021-05-31 11:50:11 +00:00
|
|
|
searchTimeout time.Duration
|
2020-12-21 08:40:30 +00:00
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
containerClient *cntClient.Client
|
2022-01-31 11:58:55 +00:00
|
|
|
netmapClient *nmClient.Client
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
taskManager TaskManager
|
|
|
|
reporter audit.Reporter
|
|
|
|
prevAuditCanceler context.CancelFunc
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2021-05-31 11:50:11 +00:00
|
|
|
Log *zap.Logger
|
2022-01-31 11:58:55 +00:00
|
|
|
NetmapClient *nmClient.Client
|
2022-01-31 13:34:01 +00:00
|
|
|
ContainerClient *cntClient.Client
|
2021-05-31 11:50:11 +00:00
|
|
|
IRList Indexer
|
2021-10-27 12:12:05 +00:00
|
|
|
SGSource SGSource
|
2021-05-31 11:50:11 +00:00
|
|
|
RPCSearchTimeout time.Duration
|
|
|
|
TaskManager TaskManager
|
|
|
|
Reporter audit.Reporter
|
|
|
|
Key *ecdsa.PrivateKey
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
// SearchSGPrm groups the parameters which are formed by Processor to search the storage group objects.
|
|
|
|
type SearchSGPrm struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
id *cid.ID
|
|
|
|
|
|
|
|
info client.NodeInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns context to use for network communication.
|
|
|
|
func (x SearchSGPrm) Context() context.Context {
|
|
|
|
return x.ctx
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// CID returns the identifier of the container to search SG in.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x SearchSGPrm) CID() *cid.ID {
|
|
|
|
return x.id
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// NodeInfo returns information about a storage node to communicate with.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x SearchSGPrm) NodeInfo() client.NodeInfo {
|
|
|
|
return x.info
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SearchSGDst groups the target values which Processor expects from SG searching to process.
|
2021-10-27 12:12:05 +00:00
|
|
|
type SearchSGDst struct {
|
2022-03-28 13:15:15 +00:00
|
|
|
ids []oidSDK.ID
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WriteIDList writes a list of identifiers of storage group objects stored in the container.
|
2022-03-28 13:15:15 +00:00
|
|
|
func (x *SearchSGDst) WriteIDList(ids []oidSDK.ID) {
|
2021-10-27 12:12:05 +00:00
|
|
|
x.ids = ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// SGSource is a storage group information source interface.
|
|
|
|
type SGSource interface {
|
|
|
|
// Lists storage group objects in the container. Formed list must be written to destination.
|
|
|
|
//
|
|
|
|
// Must return any error encountered which did not allow to form the list.
|
|
|
|
ListSG(*SearchSGDst, SearchSGPrm) error
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
type epochAuditReporter struct {
|
|
|
|
epoch uint64
|
|
|
|
|
|
|
|
rep audit.Reporter
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// ProcessorPoolSize limits pool size for audit Processor. Processor manages
|
2022-04-21 11:28:05 +00:00
|
|
|
// audit tasks and fills queue for the next epoch. This process must not be interrupted
|
|
|
|
// by a new audit epoch, so we limit the pool size for the processor to one.
|
2020-12-18 09:27:19 +00:00
|
|
|
const ProcessorPoolSize = 1
|
|
|
|
|
|
|
|
// New creates audit processor instance.
|
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/audit: logger is not set")
|
|
|
|
case p.IRList == nil:
|
|
|
|
return nil, errors.New("ir/audit: global state is not set")
|
2021-10-27 12:12:05 +00:00
|
|
|
case p.SGSource == nil:
|
|
|
|
return nil, errors.New("ir/audit: SG source is not set")
|
2020-12-23 15:07:10 +00:00
|
|
|
case p.TaskManager == nil:
|
|
|
|
return nil, errors.New("ir/audit: audit task manager is not set")
|
|
|
|
case p.Reporter == nil:
|
|
|
|
return nil, errors.New("ir/audit: audit result reporter is not set")
|
2021-03-13 15:22:21 +00:00
|
|
|
case p.Key == nil:
|
|
|
|
return nil, errors.New("ir/audit: signing key is not set")
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir/audit: can't create worker pool: %w", err)
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Processor{
|
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
2021-05-31 11:50:11 +00:00
|
|
|
containerClient: p.ContainerClient,
|
2020-12-18 09:27:19 +00:00
|
|
|
irList: p.IRList,
|
2021-10-27 12:12:05 +00:00
|
|
|
sgSrc: p.SGSource,
|
2020-12-23 15:07:10 +00:00
|
|
|
searchTimeout: p.RPCSearchTimeout,
|
2021-05-31 11:50:11 +00:00
|
|
|
netmapClient: p.NetmapClient,
|
2020-12-22 00:28:42 +00:00
|
|
|
taskManager: p.TaskManager,
|
|
|
|
reporter: p.Reporter,
|
|
|
|
prevAuditCanceler: func() {},
|
2020-12-18 09:27:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
2020-12-18 09:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
2020-12-18 09:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
2021-08-12 15:24:17 +00:00
|
|
|
func (ap *Processor) TimersHandlers() []event.NotificationHandlerInfo {
|
2020-12-18 09:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartAuditHandler for the internal event producer.
|
|
|
|
func (ap *Processor) StartAuditHandler() event.Handler {
|
|
|
|
return ap.handleNewAuditRound
|
|
|
|
}
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
func (r *epochAuditReporter) WriteReport(rep *audit.Report) error {
|
|
|
|
res := rep.Result()
|
2022-05-11 10:26:22 +00:00
|
|
|
res.ForEpoch(r.epoch)
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
return r.rep.WriteReport(rep)
|
|
|
|
}
|