Files
isp-config-service/service/event/handler.go
T
komissarovyv 45de1d1b12 Log module leader changes on the recompute event
The event handler maps the leader recompute onto the routing map rebuild with
the ready and disconnected events of the batch, so a leader change reaches the
routing map of every config service node.

The address list is not re-broadcast from it: the ready and disconnected event
of the same change already broadcasts it, and an extra snapshot per connect
broke the acceptance test on the exact number of host lists a module receives.

An info is written when the leader address in the payload differs from the
address before the change, naming the module, both addresses and the reason.
An event that changed nothing is silent, so the log stays readable.
2026-11-18 21:29:29 +03:00

80 lines
2.3 KiB
Go

package event
import (
"context"
"github.com/txix-open/isp-kit/errors"
"github.com/txix-open/isp-kit/log"
"isp-config-service/entity"
)
type SubscriptionService interface {
NotifyConfigChanged(ctx context.Context, moduleId string) error
NotifyBackendsChanged(ctx context.Context, moduleId string) error
NotifyModuleLeaderRecompute(ctx context.Context, recompute entity.PayloadModuleLeaderRecompute)
NotifyRoutingChanged(ctx context.Context) error
}
type Handler struct {
subscriptionService SubscriptionService
logger log.Logger
}
func NewHandler(
subscriptionService SubscriptionService,
logger log.Logger,
) Handler {
return Handler{
subscriptionService: subscriptionService,
logger: logger,
}
}
func (h Handler) Handle(ctx context.Context, events []entity.Event) {
routingChanged := false
for _, event := range events {
payload := event.Payload.Value
routingChanged = routingChanged ||
payload.ModuleReady != nil ||
payload.ModuleDisconnected != nil ||
payload.ModuleLeaderRecompute != nil
err := h.handleEvent(ctx, event)
if err != nil {
h.logger.Error(ctx, errors.WithMessagef(err, "handle event: %s", event.Key()))
}
}
if routingChanged {
err := h.subscriptionService.NotifyRoutingChanged(ctx)
if err != nil {
h.logger.Error(ctx, errors.WithMessage(err, "handle routing changed"))
}
}
}
func (h Handler) handleEvent(ctx context.Context, event entity.Event) error {
payload := event.Payload.Value
switch {
case payload.ConfigUpdated != nil:
err := h.subscriptionService.NotifyConfigChanged(ctx, payload.ConfigUpdated.ModuleId)
if err != nil {
return errors.WithMessage(err, "handle config changed event")
}
case payload.ModuleReady != nil:
err := h.subscriptionService.NotifyBackendsChanged(ctx, payload.ModuleReady.ModuleId)
if err != nil {
return errors.WithMessage(err, "handle module ready event")
}
case payload.ModuleDisconnected != nil:
err := h.subscriptionService.NotifyBackendsChanged(ctx, payload.ModuleDisconnected.ModuleId)
if err != nil {
return errors.WithMessage(err, "handle module disconnected event")
}
case payload.ModuleLeaderRecompute != nil:
h.subscriptionService.NotifyModuleLeaderRecompute(ctx, *payload.ModuleLeaderRecompute)
default:
return errors.Errorf("unknown event: %v", payload)
}
return nil
}