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.
106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package tests_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"isp-config-service/entity"
|
|
"isp-config-service/service/rqlite/db"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/txix-open/isp-kit/cluster"
|
|
)
|
|
|
|
const (
|
|
recomputeModule = "leaderrecompute"
|
|
recomputeHost = "10.2.9.21"
|
|
)
|
|
|
|
// nolint:tagliatelle
|
|
type recomputeTotalRow struct {
|
|
Total int `json:"total"`
|
|
}
|
|
|
|
// nolint:tagliatelle
|
|
type recomputeModuleRow struct {
|
|
Id string `json:"id"`
|
|
}
|
|
|
|
//nolint:paralleltest
|
|
func TestModuleLeaderRecomputeEventsInQueue(t *testing.T) {
|
|
require, logger, testDb := setupTestApp(t)
|
|
|
|
instance := startModuleInstance(t, recomputeModule, recomputeHost, cluster.ModuleLeaderModeRequestElection, logger)
|
|
|
|
moduleId := requireRecomputeModuleId(t, testDb, recomputeModule)
|
|
|
|
require.Eventually(func() bool {
|
|
return countLeaderRecomputeEvents(t, testDb, moduleId) == 1
|
|
}, announceTimeout, announceTick, "a connected instance must add exactly one leader recompute event")
|
|
require.Equal(1, countLeaderRecomputeEventsByReason(t, testDb, moduleId, entity.ModuleLeaderReasonReady))
|
|
|
|
instance.disconnect(t)
|
|
|
|
require.Eventually(func() bool {
|
|
return countLeaderRecomputeEvents(t, testDb, moduleId) == 2
|
|
}, announceTimeout, announceTick, "a disconnected instance must add exactly one leader recompute event")
|
|
require.Equal(1, countLeaderRecomputeEventsByReason(t, testDb, moduleId, entity.ModuleLeaderReasonDisconnect))
|
|
}
|
|
|
|
func requireRecomputeModuleId(t *testing.T, testDb db.DB, moduleName string) string {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
|
|
var row recomputeModuleRow
|
|
require.Eventually(func() bool {
|
|
found := recomputeModuleRow{}
|
|
err := testDb.SelectRow(t.Context(), &found, `
|
|
select id
|
|
from isp_config_service__module
|
|
where name = ?`, moduleName)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
row = found
|
|
return row.Id != ""
|
|
}, announceTimeout, announceTick, "module %s must be registered", moduleName)
|
|
|
|
return row.Id
|
|
}
|
|
|
|
func countLeaderRecomputeEvents(t *testing.T, testDb db.DB, moduleId string) int {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
|
|
row := recomputeTotalRow{}
|
|
err := testDb.SelectRow(t.Context(), &row, `
|
|
select count(*) as total
|
|
from isp_config_service__event
|
|
where cast(payload as text) like ?
|
|
and cast(payload as text) like ?`,
|
|
"%moduleLeaderRecompute%", moduleIdLikePattern(moduleId))
|
|
require.NoError(err)
|
|
|
|
return row.Total
|
|
}
|
|
|
|
func countLeaderRecomputeEventsByReason(t *testing.T, testDb db.DB, moduleId string, reason string) int {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
|
|
row := recomputeTotalRow{}
|
|
err := testDb.SelectRow(t.Context(), &row, `
|
|
select count(*) as total
|
|
from isp_config_service__event
|
|
where cast(payload as text) like ?
|
|
and cast(payload as text) like ?
|
|
and cast(payload as text) like ?`,
|
|
"%moduleLeaderRecompute%", moduleIdLikePattern(moduleId), "%\"reason\":\""+reason+"\"%")
|
|
require.NoError(err)
|
|
|
|
return row.Total
|
|
}
|
|
|
|
func moduleIdLikePattern(moduleId string) string {
|
|
return "%\"moduleId\":\"" + moduleId + "\"%"
|
|
}
|