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:
1 parent
a8a475435c
commit
439e6f5ebf
9 files changed
+159
-553
No files matched your search
@@ -3,6 +3,7 @@ package subscription
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/txix-open/etp/v4"
|
||||
|
||||
@@ -168,18 +169,6 @@ func (s Service) NotifyRoutingChanged(ctx context.Context) error {
|
||||
return s.notifyRoutingChanged(ctx, cluster.ConfigSendRoutesChanged, conns)
|
||||
}
|
||||
|
||||
func (s Service) NotifyModuleLeaderRecompute(ctx context.Context, recompute entity.PayloadModuleLeaderRecompute) {
|
||||
if recompute.Leader == recompute.PreviousLeader {
|
||||
return
|
||||
}
|
||||
|
||||
s.logger.Info(ctx, "module leader changed",
|
||||
log.String("moduleName", recompute.ModuleName),
|
||||
log.String("previousLeaderAddress", recompute.PreviousLeader),
|
||||
log.String("newLeaderAddress", recompute.Leader),
|
||||
log.String("reason", recompute.Reason))
|
||||
}
|
||||
|
||||
func (s Service) notifyBackendsChanged(
|
||||
ctx context.Context,
|
||||
moduleId string,
|
||||
@@ -225,7 +214,7 @@ func (s Service) emitLeaderAddress(
|
||||
leaderAddress = election.Leader.Address
|
||||
}
|
||||
|
||||
s.logger.Debug(ctx, "module leader election narrows the address list",
|
||||
s.logger.Info(ctx, "module address list narrowed to the leader",
|
||||
log.String("moduleName", moduleName),
|
||||
log.String("leaderAddress", leaderAddress))
|
||||
|
||||
@@ -242,6 +231,7 @@ func (s Service) notifyRoutingChanged(ctx context.Context, event string, conns [
|
||||
}
|
||||
|
||||
elections := leader.DeriveByModule(backends)
|
||||
moduleLeaders := make([]string, 0, len(elections))
|
||||
|
||||
routingConfig := cluster.RoutingConfig{}
|
||||
for _, backend := range backends {
|
||||
@@ -256,9 +246,7 @@ func (s Service) notifyRoutingChanged(ctx context.Context, event string, conns [
|
||||
}
|
||||
|
||||
if election.HasElection {
|
||||
s.logger.Debug(ctx, "module leader election narrows the routing map",
|
||||
log.String("moduleName", backend.ModuleName),
|
||||
log.String("leaderAddress", backend.Address))
|
||||
moduleLeaders = append(moduleLeaders, backend.ModuleName+"="+backend.Address)
|
||||
}
|
||||
|
||||
declaration := cluster.BackendDeclaration{
|
||||
@@ -276,6 +264,12 @@ func (s Service) notifyRoutingChanged(ctx context.Context, event string, conns [
|
||||
}
|
||||
routingConfig = append(routingConfig, declaration)
|
||||
}
|
||||
|
||||
if len(moduleLeaders) > 0 {
|
||||
s.logger.Info(ctx, "routing map publishes module leaders",
|
||||
log.String("moduleLeaders", strings.Join(moduleLeaders, ",")))
|
||||
}
|
||||
|
||||
data, err := json.Marshal(routingConfig)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "marshal routing config")
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/txix-open/etp/v4"
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,12 @@ const (
|
||||
testModuleId = "test-module-id"
|
||||
testModuleName = "test-module"
|
||||
|
||||
leaderConnId = "conn-1"
|
||||
followerConnId = "conn-2"
|
||||
|
||||
leaderAddress = "10.0.0.1:9000"
|
||||
successorAddress = "10.0.0.2:9000"
|
||||
plainAddress = "10.0.0.3:9000"
|
||||
)
|
||||
|
||||
type storedLog struct {
|
||||
@@ -49,27 +54,29 @@ func (l *loggerSpy) Debug(ctx context.Context, message any, fields ...log.Field)
|
||||
type moduleRepoSpy struct{}
|
||||
|
||||
func (r *moduleRepoSpy) GetByNames(ctx context.Context, names []string) ([]entity.Module, error) {
|
||||
return nil, nil
|
||||
return []entity.Module{{Id: testModuleId, Name: testModuleName}}, nil
|
||||
}
|
||||
|
||||
func (r *moduleRepoSpy) GetById(ctx context.Context, id string) (*entity.Module, error) {
|
||||
return &entity.Module{Id: testModuleId, Name: testModuleName}, nil
|
||||
}
|
||||
|
||||
type backendRepoSpy struct{}
|
||||
type backendRepoSpy struct {
|
||||
forLeaderElection []entity.Backend
|
||||
all []entity.Backend
|
||||
}
|
||||
|
||||
func (r *backendRepoSpy) GetForLeaderElection(ctx context.Context, moduleId string) ([]entity.Backend, error) {
|
||||
return nil, nil
|
||||
return r.forLeaderElection, nil
|
||||
}
|
||||
|
||||
func (r *backendRepoSpy) All(ctx context.Context) ([]entity.Backend, error) {
|
||||
return nil, nil
|
||||
return r.all, nil
|
||||
}
|
||||
|
||||
type configRepoSpy struct{}
|
||||
|
||||
func (r *configRepoSpy) GetActive(ctx context.Context, moduleId string) (*entity.Config, error) {
|
||||
// The leader recompute path never reads a configuration.
|
||||
return &entity.Config{ModuleId: moduleId}, nil
|
||||
}
|
||||
|
||||
@@ -83,121 +90,98 @@ type emitterSpy struct{}
|
||||
|
||||
func (e *emitterSpy) Emit(ctx context.Context, conn *etp.Conn, event string, data []byte) {}
|
||||
|
||||
func TestNotifyModuleLeaderRecomputeLogsLeaderChange(t *testing.T) {
|
||||
// The on-call engineer answers "who is accepting sessions of this module" from the log of the node that
|
||||
// emitted the address list: the same derivation the subscriber received, logged where it is published.
|
||||
func TestNotifyBackendsChangedLogsTheLeaderItPublishes(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
tests := []struct {
|
||||
recompute entity.PayloadModuleLeaderRecompute
|
||||
expected *storedLog
|
||||
name string
|
||||
}{
|
||||
{
|
||||
name: "the leader moved to the successor",
|
||||
recompute: entity.PayloadModuleLeaderRecompute{
|
||||
ModuleId: testModuleId,
|
||||
ModuleName: testModuleName,
|
||||
Reason: entity.ModuleLeaderReasonDisconnect,
|
||||
PreviousLeader: leaderAddress,
|
||||
Leader: successorAddress,
|
||||
},
|
||||
expected: &storedLog{
|
||||
message: "module leader changed",
|
||||
fields: map[string]string{
|
||||
"moduleName": testModuleName,
|
||||
"previousLeaderAddress": leaderAddress,
|
||||
"newLeaderAddress": successorAddress,
|
||||
"reason": entity.ModuleLeaderReasonDisconnect,
|
||||
},
|
||||
},
|
||||
logger := &loggerSpy{}
|
||||
rooms := etp.NewServer().Rooms()
|
||||
rooms.Join(&etp.Conn{}, subscription.BackendsChangingRoom(testModuleName))
|
||||
service := newService(logger, rooms, &backendRepoSpy{
|
||||
forLeaderElection: []entity.Backend{
|
||||
electedInstance(leaderConnId, leaderAddress),
|
||||
electedInstance(followerConnId, successorAddress),
|
||||
},
|
||||
{
|
||||
name: "the last leader is gone",
|
||||
recompute: entity.PayloadModuleLeaderRecompute{
|
||||
ModuleId: testModuleId,
|
||||
ModuleName: testModuleName,
|
||||
Reason: entity.ModuleLeaderReasonDisconnect,
|
||||
PreviousLeader: leaderAddress,
|
||||
Leader: "",
|
||||
},
|
||||
expected: &storedLog{
|
||||
message: "module leader changed",
|
||||
fields: map[string]string{
|
||||
"moduleName": testModuleName,
|
||||
"previousLeaderAddress": leaderAddress,
|
||||
"newLeaderAddress": "",
|
||||
"reason": entity.ModuleLeaderReasonDisconnect,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
err := service.NotifyBackendsChanged(t.Context(), testModuleId)
|
||||
|
||||
require.NoError(err)
|
||||
require.Len(logger.infos, 1)
|
||||
require.Equal("module address list narrowed to the leader", logger.infos[0].message)
|
||||
require.Equal(testModuleName, logger.infos[0].fields["moduleName"])
|
||||
require.Equal(leaderAddress, logger.infos[0].fields["leaderAddress"],
|
||||
"the logged leader is the oldest elected instance, the one the subscriber received")
|
||||
}
|
||||
|
||||
func TestNotifyRoutingChangedLogsPublishedLeaders(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
logger := &loggerSpy{}
|
||||
rooms := etp.NewServer().Rooms()
|
||||
rooms.Join(&etp.Conn{}, subscription.RoutingChangingRoom())
|
||||
service := newService(logger, rooms, &backendRepoSpy{
|
||||
all: []entity.Backend{
|
||||
electedInstance(leaderConnId, leaderAddress),
|
||||
electedInstance(followerConnId, successorAddress),
|
||||
plainInstance("conn-3", plainAddress),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
err := service.NotifyRoutingChanged(t.Context())
|
||||
|
||||
logger := &loggerSpy{}
|
||||
service := newService(logger)
|
||||
require.NoError(err)
|
||||
require.Len(logger.infos, 1, "one summary line per routing rebuild")
|
||||
require.Equal("routing map publishes module leaders", logger.infos[0].message)
|
||||
require.Equal(testModuleName+"="+leaderAddress, logger.infos[0].fields["moduleLeaders"],
|
||||
"only the elected module and only its leader are published, the plain module is not listed")
|
||||
}
|
||||
|
||||
service.NotifyModuleLeaderRecompute(t.Context(), tt.recompute)
|
||||
func TestNotifyRoutingChangedWithoutElectionLogsNothing(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
require.Len(logger.infos, 1)
|
||||
require.Equal(tt.expected.message, logger.infos[0].message)
|
||||
require.Equal(tt.expected.fields, logger.infos[0].fields)
|
||||
})
|
||||
logger := &loggerSpy{}
|
||||
rooms := etp.NewServer().Rooms()
|
||||
rooms.Join(&etp.Conn{}, subscription.RoutingChangingRoom())
|
||||
service := newService(logger, rooms, &backendRepoSpy{
|
||||
all: []entity.Backend{plainInstance("conn-3", plainAddress)},
|
||||
})
|
||||
|
||||
err := service.NotifyRoutingChanged(t.Context())
|
||||
|
||||
require.NoError(err)
|
||||
require.Empty(logger.infos, "modules without election keep the previous behaviour, including the log")
|
||||
}
|
||||
|
||||
func electedInstance(connId string, address string) entity.Backend {
|
||||
return entity.Backend{
|
||||
WsConnectionId: connId,
|
||||
ModuleId: testModuleId,
|
||||
ModuleName: testModuleName,
|
||||
Address: address,
|
||||
ModuleLeaderMode: cluster.ModuleLeaderModeRequestElection,
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing the module leader recompute describes as a leader change means no log: the queue carries an event
|
||||
// for every connect and disconnect, and most of them leave the leader where it was.
|
||||
func TestNotifyModuleLeaderRecomputeSkipsUnchangedLeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
recompute entity.PayloadModuleLeaderRecompute
|
||||
name string
|
||||
}{
|
||||
{
|
||||
name: "the leader is the same instance",
|
||||
recompute: entity.PayloadModuleLeaderRecompute{
|
||||
ModuleId: testModuleId,
|
||||
ModuleName: testModuleName,
|
||||
Reason: entity.ModuleLeaderReasonReady,
|
||||
PreviousLeader: leaderAddress,
|
||||
Leader: leaderAddress,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "the module has no leader at all",
|
||||
recompute: entity.PayloadModuleLeaderRecompute{
|
||||
ModuleId: testModuleId,
|
||||
ModuleName: testModuleName,
|
||||
Reason: entity.ModuleLeaderReasonReady,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
logger := &loggerSpy{}
|
||||
service := newService(logger)
|
||||
|
||||
service.NotifyModuleLeaderRecompute(t.Context(), tt.recompute)
|
||||
|
||||
require.Empty(logger.infos, "an event assembled without a leader change must not log")
|
||||
})
|
||||
func plainInstance(connId string, address string) entity.Backend {
|
||||
return entity.Backend{
|
||||
WsConnectionId: connId,
|
||||
ModuleId: "plain-module-id",
|
||||
ModuleName: "plain-module",
|
||||
Address: address,
|
||||
}
|
||||
}
|
||||
|
||||
func newService(logger log.Logger) subscription.Service {
|
||||
func newService(logger log.Logger, rooms *etp.Rooms, backendRepo subscription.BackendRepo) subscription.Service {
|
||||
return subscription.NewService(
|
||||
&moduleRepoSpy{},
|
||||
&backendRepoSpy{},
|
||||
backendRepo,
|
||||
&configRepoSpy{},
|
||||
&etp.Rooms{},
|
||||
rooms,
|
||||
&emitterSpy{},
|
||||
&variableServiceSpy{},
|
||||
logger,
|
||||
|
||||
Reference in new issue
Block a user