Drop the module leader recompute event

The queue event did two things: set the routing-changed flag, which the lifecycle
event of the same change already sets, and deliver the leader-change info log. It
cost an extra raft INSERT per connect/disconnect, two GetForLeaderElection reads per
lifecycle event used for nothing else, a payload kind, a compaction key and a
subscription-service method that only logged.

The leader is now logged where it is published: the address-list emission logs the
leader it just sent, and the routing rebuild logs one summary line with the leaders
it published. Modules without election stay silent, as before.

A future self-election needs a recompute trigger that also re-broadcasts the address
list; that event will be added when the requirement exists, not carried as a no-op.
This commit is contained in:
komissarovyv committed 2026-11-19 12:57:28 +03:00
1 parent a8a475435c
commit 439e6f5ebf
9 files changed
+159 -553

No files matched your search

+24 -61
View File
@@ -12,15 +12,8 @@ import (
"github.com/txix-open/isp-kit/log"
)
type recomputeCall struct {
moduleId string
reason string
previousLeader string
}
type subscriptionSpy struct {
backendsChanged []string
recomputes []recomputeCall
routingCalls int
}
@@ -33,17 +26,6 @@ func (s *subscriptionSpy) NotifyBackendsChanged(ctx context.Context, moduleId st
return nil
}
func (s *subscriptionSpy) NotifyModuleLeaderRecompute(
ctx context.Context,
recompute entity.PayloadModuleLeaderRecompute,
) {
s.recomputes = append(s.recomputes, recomputeCall{
moduleId: recompute.ModuleId,
reason: recompute.Reason,
previousLeader: recompute.PreviousLeader,
})
}
func (s *subscriptionSpy) NotifyRoutingChanged(ctx context.Context) error {
s.routingCalls++
return nil
@@ -63,55 +45,38 @@ func (l *loggerSpy) Info(ctx context.Context, message any, fields ...log.Field)
func (l *loggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
func TestHandleModuleLeaderRecomputeEvent(t *testing.T) {
t.Parallel()
require := require.New(t)
subscription := &subscriptionSpy{}
logger := &loggerSpy{}
handler := event.NewHandler(subscription, logger)
handler.Handle(t.Context(), []entity.Event{
moduleLeaderRecomputeEvent(entity.ModuleLeaderReasonDisconnect, "10.0.0.1:9000"),
})
require.Equal([]recomputeCall{{
moduleId: testModuleId,
reason: entity.ModuleLeaderReasonDisconnect,
previousLeader: "10.0.0.1:9000",
}}, subscription.recomputes)
require.Empty(subscription.backendsChanged, "the recompute event maps the address list on its own")
require.Equal(1, subscription.routingCalls, "the recompute event fans the routing map out with the batch")
require.Empty(logger.errors)
}
func TestHandleBatchKeepsModuleLeaderRecompute(t *testing.T) {
t.Parallel()
require := require.New(t)
subscription := &subscriptionSpy{}
logger := &loggerSpy{}
handler := event.NewHandler(subscription, logger)
ready := entity.NewEvent(entity.EventPayload{
func moduleReadyEvent(id int) entity.Event {
event := entity.NewEvent(entity.EventPayload{
ModuleReady: &entity.PayloadModuleReady{ModuleId: testModuleId},
})
ready.Id = 1
event.Id = id
return event
}
disconnected := entity.NewEvent(entity.EventPayload{
func moduleDisconnectedEvent(id int) entity.Event {
event := entity.NewEvent(entity.EventPayload{
ModuleDisconnected: &entity.PayloadModuleDisconnected{ModuleId: testModuleId},
})
disconnected.Id = 2
event.Id = id
return event
}
recompute := moduleLeaderRecomputeEvent(entity.ModuleLeaderReasonDisconnect, "10.0.0.1:9000")
recompute.Id = 3
func TestHandleLifecycleBatchFansRoutingOnce(t *testing.T) {
t.Parallel()
require := require.New(t)
batch := event.NewCompactor().Compact([]entity.Event{ready, disconnected, recompute})
subscription := &subscriptionSpy{}
logger := &loggerSpy{}
handler := event.NewHandler(subscription, logger)
batch := event.NewCompactor().Compact([]entity.Event{
moduleReadyEvent(1),
moduleDisconnectedEvent(2),
})
handler.Handle(t.Context(), batch)
require.Equal([]string{testModuleId}, subscription.backendsChanged,
"ready and disconnected collapse into the newest lifecycle event")
require.Len(subscription.recomputes, 1, "the leader recompute must survive compaction")
require.Equal(1, subscription.routingCalls, "the routing map is broadcast once per batch")
require.Empty(logger.errors)
}
@@ -126,13 +91,11 @@ func TestHandleUnknownEventDoesNotAbortBatch(t *testing.T) {
unknown := entity.NewEvent(entity.EventPayload{})
handler.Handle(t.Context(), []entity.Event{unknown, moduleLeaderRecomputeEvent(
entity.ModuleLeaderReasonReady,
"",
)})
handler.Handle(t.Context(), []entity.Event{unknown, moduleReadyEvent(1)})
require.Len(logger.errors, 1)
require.Contains(logger.errors[0], "unknown event")
require.Len(subscription.recomputes, 1, "an unknown payload kind must not stop the batch")
require.Equal([]string{testModuleId}, subscription.backendsChanged,
"an unknown payload kind must not stop the batch")
require.Equal(1, subscription.routingCalls)
}