Fix updating the process-node map

This commit is contained in:
Ingo Oppermann 2023-07-12 12:22:37 +02:00
parent 51d8b30e8f
commit c0c118340b
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E
3 changed files with 76 additions and 2 deletions

View File

@ -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{

22
maps/equal.go Normal file
View File

@ -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
}

52
maps/equal_test.go Normal file
View File

@ -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))
}