Derive module leader from connected instances
This commit is contained in:
1 parent
9e24122e75
commit
6cfa1955ea
3 files changed
+210
No files matched your search
@@ -166,6 +166,29 @@ func (r Backend) GetByModuleId(ctx context.Context, moduleId string) ([]entity.B
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r Backend) GetForLeaderElection(ctx context.Context, moduleId string) ([]entity.Backend, error) {
|
||||||
|
ctx = sql_metrics.OperationLabelToContext(ctx, "Backend.GetForLeaderElection")
|
||||||
|
|
||||||
|
// nolint:unqueryvet
|
||||||
|
query, args, err := squirrel.Select("*").
|
||||||
|
From(Table("backend")).
|
||||||
|
Where(squirrel.Eq{
|
||||||
|
"module_id": moduleId,
|
||||||
|
}).OrderBy("created_at asc", "ws_connection_id asc").
|
||||||
|
ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessage(err, "build query")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]entity.Backend, 0)
|
||||||
|
err = r.db.Select(ctx, &result, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessagef(err, "select: %s", query)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r Backend) AllMetricsAutodiscovery(ctx context.Context) ([]entity.MetricsAdWrapper, error) {
|
func (r Backend) AllMetricsAutodiscovery(ctx context.Context) ([]entity.MetricsAdWrapper, error) {
|
||||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Backend.AllMetricsAutodiscovery")
|
ctx = sql_metrics.OperationLabelToContext(ctx, "Backend.AllMetricsAutodiscovery")
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package leader
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/txix-open/isp-kit/cluster"
|
||||||
|
"isp-config-service/entity"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Result struct {
|
||||||
|
HasElection bool
|
||||||
|
Found bool
|
||||||
|
Leader entity.Backend
|
||||||
|
}
|
||||||
|
|
||||||
|
func Derive(backends []entity.Backend) Result {
|
||||||
|
result := Result{
|
||||||
|
HasElection: hasElection(backends),
|
||||||
|
}
|
||||||
|
if !result.HasElection {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
candidates := slices.Clone(backends)
|
||||||
|
slices.SortFunc(candidates, compare)
|
||||||
|
|
||||||
|
result.Found = true
|
||||||
|
result.Leader = candidates[0]
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasElection(backends []entity.Backend) bool {
|
||||||
|
for _, backend := range backends {
|
||||||
|
if backend.ModuleLeaderMode == cluster.ModuleLeaderModeRequestElection {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func compare(a, b entity.Backend) int {
|
||||||
|
byCreatedAt := time.Time(a.CreatedAt).Compare(time.Time(b.CreatedAt))
|
||||||
|
if byCreatedAt != 0 {
|
||||||
|
return byCreatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Compare(a.WsConnectionId, b.WsConnectionId)
|
||||||
|
}
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
package leader_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"isp-config-service/entity"
|
||||||
|
"isp-config-service/entity/xtypes"
|
||||||
|
"isp-config-service/service/module/leader"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/txix-open/isp-kit/cluster"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
electionMode = cluster.ModuleLeaderModeRequestElection
|
||||||
|
noElectionMode = cluster.ModuleLeaderModeNone
|
||||||
|
garbageMode = "SOMETHING_ELSE"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDerive(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
elected := leader.Derive(instances(
|
||||||
|
instance("conn-c", 100, noElectionMode),
|
||||||
|
instance("conn-a", 200, electionMode),
|
||||||
|
instance("conn-b", 100, noElectionMode),
|
||||||
|
))
|
||||||
|
|
||||||
|
require.True(elected.HasElection)
|
||||||
|
require.True(elected.Found)
|
||||||
|
require.Equal("conn-b", elected.Leader.WsConnectionId, "leader is the earliest instance, tie broken by ws_connection_id")
|
||||||
|
|
||||||
|
first := leader.Derive(instances(
|
||||||
|
instance("conn-c", 100, noElectionMode),
|
||||||
|
instance("conn-a", 200, electionMode),
|
||||||
|
instance("conn-b", 100, noElectionMode),
|
||||||
|
))
|
||||||
|
second := leader.Derive(instances(
|
||||||
|
instance("conn-a", 200, electionMode),
|
||||||
|
instance("conn-b", 100, noElectionMode),
|
||||||
|
instance("conn-c", 100, noElectionMode),
|
||||||
|
))
|
||||||
|
|
||||||
|
require.Equal(first, second, "the same set of instances must yield the same leader")
|
||||||
|
require.Equal(elected.Leader.WsConnectionId, first.Leader.WsConnectionId)
|
||||||
|
require.Equal(elected.Leader.WsConnectionId, second.Leader.WsConnectionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDerive_InputOrder(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
base := instances(
|
||||||
|
instance("conn-d", 100, electionMode),
|
||||||
|
instance("conn-a", 300, electionMode),
|
||||||
|
instance("conn-c", 100, noElectionMode),
|
||||||
|
instance("conn-b", 200, noElectionMode),
|
||||||
|
)
|
||||||
|
|
||||||
|
expected := leader.Derive(base)
|
||||||
|
require.Equal("conn-c", expected.Leader.WsConnectionId)
|
||||||
|
|
||||||
|
for _, input := range permutations(base) {
|
||||||
|
original := slices.Clone(base)
|
||||||
|
|
||||||
|
got := leader.Derive(input)
|
||||||
|
|
||||||
|
require.Equal(expected, got, "input order must not change the result")
|
||||||
|
require.Equal(original, base, "Derive must not mutate the caller slice")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDerive_NoElection(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
got := leader.Derive(instances(
|
||||||
|
instance("conn-a", 100, noElectionMode),
|
||||||
|
instance("conn-b", 200, cluster.ModuleLeaderModeImLeader),
|
||||||
|
instance("conn-c", 50, garbageMode),
|
||||||
|
))
|
||||||
|
|
||||||
|
require.False(got.HasElection)
|
||||||
|
require.False(got.Found)
|
||||||
|
require.Equal(entity.Backend{}, got.Leader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDerive_SingleInstance(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
got := leader.Derive(instances(instance("conn-a", 100, electionMode)))
|
||||||
|
|
||||||
|
require.True(got.HasElection)
|
||||||
|
require.True(got.Found)
|
||||||
|
require.Equal("conn-a", got.Leader.WsConnectionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDerive_NoInstances(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
got := leader.Derive(nil)
|
||||||
|
|
||||||
|
require.False(got.HasElection)
|
||||||
|
require.False(got.Found)
|
||||||
|
require.Equal(entity.Backend{}, got.Leader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func instance(wsConnectionId string, createdAt int64, mode string) entity.Backend {
|
||||||
|
return entity.Backend{
|
||||||
|
WsConnectionId: wsConnectionId,
|
||||||
|
ModuleId: "module-id",
|
||||||
|
ModuleName: "module",
|
||||||
|
Address: "10.2.9.1:9999",
|
||||||
|
ModuleLeaderMode: mode,
|
||||||
|
CreatedAt: xtypes.Time(time.Unix(createdAt, 0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func instances(backends ...entity.Backend) []entity.Backend {
|
||||||
|
return backends
|
||||||
|
}
|
||||||
|
|
||||||
|
func permutations(backends []entity.Backend) [][]entity.Backend {
|
||||||
|
reversed := slices.Clone(backends)
|
||||||
|
slices.Reverse(reversed)
|
||||||
|
|
||||||
|
rotated := append(slices.Clone(backends[1:]), backends[0])
|
||||||
|
|
||||||
|
return [][]entity.Backend{backends, reversed, rotated}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user