Skip to content

Commit

Permalink
Fix topology peerlist update() and shuffle()
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiazlo committed Nov 23, 2018
1 parent 1fc45f1 commit f98f898
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/agent_auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 14 additions & 9 deletions gossip/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"sync"

"github.com/bbva/qed/gossip/member"
"github.com/bbva/qed/log"
)

type PeerList struct {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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]
Expand All @@ -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
}

0 comments on commit f98f898

Please sign in to comment.