diff --git a/cluster/leader.go b/cluster/leader.go index 6a3c3752..13fac49d 100644 --- a/cluster/leader.go +++ b/cluster/leader.go @@ -13,6 +13,7 @@ import ( "github.com/datarhei/core/v16/cluster/proxy" "github.com/datarhei/core/v16/cluster/store" "github.com/datarhei/core/v16/log" + "github.com/datarhei/core/v16/maps" "github.com/datarhei/core/v16/restream/app" ) @@ -679,8 +680,7 @@ func (c *cluster) doSynchronize(emergency bool) { opStack, _, reality := synchronize(wish, want, have, nodesMap, c.nodeRecoverTimeout) - if !emergency && len(opStack) != 0 { - // TODO: only apply a new map if there are actually changes + if !emergency && !maps.Equal(wish, reality) { cmd := &store.Command{ Operation: store.OpSetProcessNodeMap, Data: store.CommandSetProcessNodeMap{ diff --git a/maps/equal.go b/maps/equal.go new file mode 100644 index 00000000..0b0569b5 --- /dev/null +++ b/maps/equal.go @@ -0,0 +1,22 @@ +package maps + +// Equal returns whether two maps are equal, same keys and +// same value for matching keys. +func Equal[A, B comparable](a map[A]B, b map[A]B) bool { + if len(a) != len(b) { + return false + } + + for akey, avalue := range a { + bvalue, ok := b[akey] + if !ok { + return false + } + + if avalue != bvalue { + return false + } + } + + return true +} diff --git a/maps/equal_test.go b/maps/equal_test.go new file mode 100644 index 00000000..5b2b2dbf --- /dev/null +++ b/maps/equal_test.go @@ -0,0 +1,52 @@ +package maps + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestEqual(t *testing.T) { + a := map[string]string{ + "foo": "bar", + "boz": "foz", + } + + b := map[string]string{ + "boz": "foz", + "foo": "bar", + } + + require.True(t, Equal(a, b)) + require.True(t, Equal(b, a)) +} + +func TestNotEqual(t *testing.T) { + a := map[string]string{ + "foo": "bar", + "boz": "foz", + } + + b := map[string]string{ + "boz": "foz", + } + + require.False(t, Equal(a, b)) + require.False(t, Equal(b, a)) + + c := map[string]string{ + "foo": "baz", + "boz": "foz", + } + + require.False(t, Equal(a, c)) + require.False(t, Equal(c, a)) + + d := map[string]string{ + "foo": "bar", + "baz": "foz", + } + + require.False(t, Equal(a, d)) + require.False(t, Equal(d, a)) +}