2020-12-18 09:27:19 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2020-12-22 00:28:42 +00:00
|
|
|
"context"
|
2020-12-23 15:07:10 +00:00
|
|
|
"time"
|
2020-12-22 00:28:42 +00:00
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-12-18 13:30:13 +00:00
|
|
|
SDKClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-12-21 08:40:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/invoke"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-12-21 08:40:30 +00:00
|
|
|
wrapContainer "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
|
|
|
wrapNetmap "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
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"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Indexer is a callback interface for inner ring global state.
|
|
|
|
Indexer interface {
|
|
|
|
Index() int32
|
2020-12-18 12:52:27 +00:00
|
|
|
InnerRingSize() int32
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 13:30:13 +00:00
|
|
|
// NeoFSClientCache is an interface for cache of neofs RPC clients
|
|
|
|
NeoFSClientCache interface {
|
|
|
|
Get(address string, opts ...SDKClient.Option) (*SDKClient.Client, error)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
// Processor of events related with data audit.
|
|
|
|
Processor struct {
|
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
containerContract util.Uint160
|
|
|
|
auditContract util.Uint160
|
|
|
|
morphClient *client.Client
|
|
|
|
irList Indexer
|
2020-12-18 13:30:13 +00:00
|
|
|
clientCache NeoFSClientCache
|
2020-12-23 15:07:10 +00:00
|
|
|
searchTimeout time.Duration
|
2020-12-21 08:40:30 +00:00
|
|
|
|
|
|
|
containerClient *wrapContainer.Wrapper
|
|
|
|
netmapClient *wrapNetmap.Wrapper
|
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 {
|
|
|
|
Log *zap.Logger
|
2020-12-21 08:40:30 +00:00
|
|
|
NetmapContract util.Uint160
|
2020-12-18 09:27:19 +00:00
|
|
|
ContainerContract util.Uint160
|
|
|
|
AuditContract util.Uint160
|
|
|
|
MorphClient *client.Client
|
|
|
|
IRList Indexer
|
2020-12-18 13:30:13 +00:00
|
|
|
ClientCache NeoFSClientCache
|
2020-12-23 15:07:10 +00:00
|
|
|
RPCSearchTimeout time.Duration
|
2020-12-22 00:28:42 +00:00
|
|
|
TaskManager TaskManager
|
|
|
|
Reporter audit.Reporter
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
type epochAuditReporter struct {
|
|
|
|
epoch uint64
|
|
|
|
|
|
|
|
rep audit.Reporter
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
// AuditProcessor manages audit tasks and fills queue for next epoch. This
|
|
|
|
// process must not be interrupted by new audit epoch, so we limit pool size
|
|
|
|
// for processor to one.
|
|
|
|
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.MorphClient == nil:
|
|
|
|
return nil, errors.New("ir/audit: neo:morph client is not set")
|
|
|
|
case p.IRList == nil:
|
|
|
|
return nil, errors.New("ir/audit: global state is not set")
|
2020-12-18 13:30:13 +00:00
|
|
|
case p.ClientCache == nil:
|
|
|
|
return nil, errors.New("ir/audit: neofs RPC client cache 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")
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir/audit: can't create worker pool")
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:40:30 +00:00
|
|
|
// creating enhanced client for getting network map
|
2021-01-29 07:04:03 +00:00
|
|
|
netmapClient, err := invoke.NewNetmapClient(p.MorphClient, p.NetmapContract)
|
2020-12-21 08:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating enhanced client for getting containers
|
2021-01-29 07:04:03 +00:00
|
|
|
containerClient, err := invoke.NewContainerClient(p.MorphClient, p.ContainerContract)
|
2020-12-21 08:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
return &Processor{
|
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
|
|
|
containerContract: p.ContainerContract,
|
|
|
|
auditContract: p.AuditContract,
|
|
|
|
morphClient: p.MorphClient,
|
|
|
|
irList: p.IRList,
|
2020-12-18 13:30:13 +00:00
|
|
|
clientCache: p.ClientCache,
|
2020-12-23 15:07:10 +00:00
|
|
|
searchTimeout: p.RPCSearchTimeout,
|
2020-12-21 08:40:30 +00:00
|
|
|
containerClient: containerClient,
|
|
|
|
netmapClient: 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerParsers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerParsers() []event.ParserInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerHandlers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerHandlers() []event.HandlerInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
|
|
|
func (ap *Processor) TimersHandlers() []event.HandlerInfo {
|
|
|
|
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()
|
|
|
|
res.SetAuditEpoch(r.epoch)
|
|
|
|
|
|
|
|
return r.rep.WriteReport(rep)
|
|
|
|
}
|