Skip to content

Commit

Permalink
refactor topology using new peerlist container
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiazlo committed Nov 22, 2018
1 parent 7b0e88a commit 6092393
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions gossip/topology.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gossip

import (
"math/rand"
"sync"

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

type PeerList struct {
L []*member.Peer
}

type Filter func(m *member.Peer) bool

func (l *PeerList) Filter(f Filter) *PeerList {
var b PeerList
b.L = l.L[:0]
for _, x := range l.L {
if f(x) {
b.L = append(b.L, x)
}
}

return &b
}

func (l *PeerList) Exclude(list *PeerList) *PeerList {
return l.Filter(func(p *member.Peer) bool {
for _, x := range list.L {
if x.Name == p.Name {
return false
}
}
return true
})
}

func (l PeerList) All() PeerList {
return l
}

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]
})
return &s
}

func (l *PeerList) Update(m *member.Peer) error {
for _, e := range l.L {
if e.Name == m.Name {
e = m
}
}
return nil
}

func (l *PeerList) Delete(m *member.Peer) error {
for i, e := range l.L {
if e.Name == m.Name {
l.L[i] = l.L[len(l.L)-1]
l.L[len(l.L)-1] = nil
l.L = l.L[:len(l.L)-1]
}
}
return nil
}

type Topology struct {
m []PeerList
sync.Mutex
}

func NewTopology() *Topology {
m := make([]PeerList, member.MaxType)
for i := member.Auditor; i < member.MaxType; i++ {
m[i] = PeerList{
L: make([]*member.Peer, 0),
}
}
return &Topology{
m: m,
}
}

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
}

func (t *Topology) Delete(p *member.Peer) error {
t.Lock()
defer t.Unlock()
t.m[p.Meta.Role].Delete(p)
return nil
}

func (t *Topology) Get(kind member.Type) PeerList {
t.Lock()
defer t.Unlock()
return t.m[kind]
}

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)

if len(auditors.L) > n {
auditors.L = auditors.L[:n]
}
if len(monitors.L) > n {
monitors.L = monitors.L[:n]
}
if len(publishers.L) > n {
publishers.L = publishers.L[:n]
}
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 6092393

Please sign in to comment.