From f98f89848c7f8b37d9ae3fa6931f544e372e8743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20D=C3=ADaz?= Date: Fri, 23 Nov 2018 16:31:09 +0100 Subject: [PATCH] Fix topology peerlist update() and shuffle() --- cmd/agent_auditor.go | 2 +- gossip/topology.go | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/agent_auditor.go b/cmd/agent_auditor.go index 42bb362eb..b8f6a8911 100644 --- a/cmd/agent_auditor.go +++ b/cmd/agent_auditor.go @@ -48,7 +48,7 @@ func newAgentAuditorCommand(ctx *agentContext) *cobra.Command { if err != nil { log.Fatalf("Failed to join the cluster: %v", err) } - log.Debugf("Number of nodes contacted: %d", contacted) + log.Debugf("Number of nodes contacted: %d (%v)", contacted, agentConfig.StartJoin) agent.Start() defer agent.Shutdown() diff --git a/gossip/topology.go b/gossip/topology.go index fcadaff81..4d023d9d0 100644 --- a/gossip/topology.go +++ b/gossip/topology.go @@ -17,7 +17,6 @@ import ( "sync" "github.com/bbva/qed/gossip/member" - "github.com/bbva/qed/log" ) type PeerList struct { @@ -39,6 +38,9 @@ func (l *PeerList) Filter(f Filter) *PeerList { } func (l *PeerList) Exclude(list *PeerList) *PeerList { + if list == nil { + return l + } return l.Filter(func(p *member.Peer) bool { for _, x := range list.L { if x.Name == p.Name { @@ -54,20 +56,23 @@ func (l PeerList) All() PeerList { } func (l *PeerList) Shuffle() *PeerList { - var s PeerList - s.L = l.L[:0] rand.Shuffle(len(l.L), func(i, j int) { - s.L[i], s.L[j] = s.L[j], s.L[i] + l.L[i], l.L[j] = l.L[j], l.L[i] }) - return &s + return l } func (l *PeerList) Update(m *member.Peer) error { + var add bool = true for _, e := range l.L { if e.Name == m.Name { e = m + add = false } } + if add { + l.L = append(l.L, m) + } return nil } @@ -102,7 +107,6 @@ func NewTopology() *Topology { func (t *Topology) Update(p *member.Peer) error { t.Lock() defer t.Unlock() - log.Debugf("Updating topology with member: %+v", p) t.m[p.Meta.Role].Update(p) return nil } @@ -123,9 +127,9 @@ func (t *Topology) Get(kind member.Type) PeerList { func (t *Topology) Each(n int, p *PeerList) *PeerList { var b PeerList - auditors := t.m[member.Auditor].Exclude(p) - monitors := t.m[member.Monitor].Exclude(p) - publishers := t.m[member.Publisher].Exclude(p) + auditors := t.m[member.Auditor].Exclude(p).Shuffle() + monitors := t.m[member.Monitor].Exclude(p).Shuffle() + publishers := t.m[member.Publisher].Exclude(p).Shuffle() if len(auditors.L) > n { auditors.L = auditors.L[:n] @@ -139,5 +143,6 @@ func (t *Topology) Each(n int, p *PeerList) *PeerList { b.L = append(b.L, auditors.L...) b.L = append(b.L, auditors.L...) b.L = append(b.L, auditors.L...) + return &b }