2022-02-17 12:42:07 +00:00
|
|
|
package notificator
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-02-17 12:42:07 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-02-17 12:42:07 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Prm groups Notificator constructor's
|
|
|
|
// parameters. All are required.
|
|
|
|
type Prm struct {
|
|
|
|
writer NotificationWriter
|
|
|
|
notificationSource NotificationSource
|
2022-09-28 07:41:01 +00:00
|
|
|
logger *logger.Logger
|
2022-02-17 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger sets a logger.
|
2022-09-28 07:41:01 +00:00
|
|
|
func (prm *Prm) SetLogger(v *logger.Logger) *Prm {
|
2022-02-17 12:42:07 +00:00
|
|
|
prm.logger = v
|
|
|
|
return prm
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriter sets notification writer.
|
|
|
|
func (prm *Prm) SetWriter(v NotificationWriter) *Prm {
|
|
|
|
prm.writer = v
|
|
|
|
return prm
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNotificationSource sets notification source.
|
|
|
|
func (prm *Prm) SetNotificationSource(v NotificationSource) *Prm {
|
|
|
|
prm.notificationSource = v
|
|
|
|
return prm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notificator is a notification producer that handles
|
|
|
|
// objects with defined notification epoch.
|
|
|
|
//
|
|
|
|
// Working client must be created via constructor New.
|
|
|
|
// Using the Client that has been created with new(Client)
|
|
|
|
// expression (or just declaring a Client variable) is unsafe
|
|
|
|
// and can lead to panic.
|
|
|
|
type Notificator struct {
|
|
|
|
w NotificationWriter
|
|
|
|
ns NotificationSource
|
2022-09-28 07:41:01 +00:00
|
|
|
l *logger.Logger
|
2022-02-17 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates, initializes and returns the Notificator instance.
|
|
|
|
//
|
|
|
|
// Panics if any field of the passed Prm structure is not set/set
|
|
|
|
// to nil.
|
|
|
|
func New(prm *Prm) *Notificator {
|
2023-02-21 11:42:45 +00:00
|
|
|
panicOnNil := func(v any, name string) {
|
2022-02-17 12:42:07 +00:00
|
|
|
if v == nil {
|
|
|
|
panic(fmt.Sprintf("Notificator constructor: %s is nil\n", name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panicOnNil(prm.writer, "NotificationWriter")
|
|
|
|
panicOnNil(prm.notificationSource, "NotificationSource")
|
|
|
|
panicOnNil(prm.logger, "Logger")
|
|
|
|
|
|
|
|
return &Notificator{
|
|
|
|
w: prm.writer,
|
|
|
|
ns: prm.notificationSource,
|
|
|
|
l: prm.logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessEpoch looks for all objects with defined epoch in the storage
|
|
|
|
// and passes their addresses to the NotificationWriter.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (n *Notificator) ProcessEpoch(ctx context.Context, epoch uint64) {
|
2022-02-17 12:42:07 +00:00
|
|
|
logger := n.l.With(zap.Uint64("epoch", epoch))
|
2023-04-12 14:35:10 +00:00
|
|
|
logger.Debug(logs.NotificatorNotificatorStartProcessingObjectNotifications)
|
2022-02-17 12:42:07 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
n.ns.Iterate(ctx, epoch, func(topic string, addr oid.Address) {
|
2023-04-12 14:35:10 +00:00
|
|
|
n.l.Debug(logs.NotificatorNotificatorProcessingObjectNotification,
|
2022-02-17 12:42:07 +00:00
|
|
|
zap.String("topic", topic),
|
|
|
|
zap.Stringer("address", addr),
|
|
|
|
)
|
|
|
|
|
|
|
|
n.w.Notify(topic, addr)
|
|
|
|
})
|
|
|
|
}
|