Merge pull request #1773 from weaveworks/1761-lazy-route-calculation-1
lazy route calculation; LGTM
This commit is contained in:
commit
1796582032
1 file changed
+54
-78
+54
-78
@@ -19,6 +19,7 @@ type Routes struct {
|
|||||||
broadcastAll broadcastRoutes // [1]
|
broadcastAll broadcastRoutes // [1]
|
||||||
recalculate chan<- *struct{}
|
recalculate chan<- *struct{}
|
||||||
wait chan<- chan struct{}
|
wait chan<- chan struct{}
|
||||||
|
action chan<- func()
|
||||||
// [1] based on *all* connections, not just established &
|
// [1] based on *all* connections, not just established &
|
||||||
// symmetric ones
|
// symmetric ones
|
||||||
}
|
}
|
||||||
@@ -26,6 +27,7 @@ type Routes struct {
|
|||||||
func NewRoutes(ourself *LocalPeer, peers *Peers) *Routes {
|
func NewRoutes(ourself *LocalPeer, peers *Peers) *Routes {
|
||||||
recalculate := make(chan *struct{}, 1)
|
recalculate := make(chan *struct{}, 1)
|
||||||
wait := make(chan chan struct{})
|
wait := make(chan chan struct{})
|
||||||
|
action := make(chan func())
|
||||||
routes := &Routes{
|
routes := &Routes{
|
||||||
ourself: ourself,
|
ourself: ourself,
|
||||||
peers: peers,
|
peers: peers,
|
||||||
@@ -34,12 +36,13 @@ func NewRoutes(ourself *LocalPeer, peers *Peers) *Routes {
|
|||||||
broadcast: make(broadcastRoutes),
|
broadcast: make(broadcastRoutes),
|
||||||
broadcastAll: make(broadcastRoutes),
|
broadcastAll: make(broadcastRoutes),
|
||||||
recalculate: recalculate,
|
recalculate: recalculate,
|
||||||
wait: wait}
|
wait: wait,
|
||||||
|
action: action}
|
||||||
routes.unicast[ourself.Name] = UnknownPeerName
|
routes.unicast[ourself.Name] = UnknownPeerName
|
||||||
routes.unicastAll[ourself.Name] = UnknownPeerName
|
routes.unicastAll[ourself.Name] = UnknownPeerName
|
||||||
routes.broadcast[ourself.Name] = []PeerName{}
|
routes.broadcast[ourself.Name] = []PeerName{}
|
||||||
routes.broadcastAll[ourself.Name] = []PeerName{}
|
routes.broadcastAll[ourself.Name] = []PeerName{}
|
||||||
go routes.run(recalculate, wait)
|
go routes.run(recalculate, wait, action)
|
||||||
return routes
|
return routes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,15 +71,39 @@ func (routes *Routes) UnicastAll(name PeerName) (PeerName, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (routes *Routes) Broadcast(name PeerName) []PeerName {
|
func (routes *Routes) Broadcast(name PeerName) []PeerName {
|
||||||
routes.RLock()
|
return routes.lookupOrCalculate(name, &routes.broadcast, true)
|
||||||
defer routes.RUnlock()
|
|
||||||
return routes.broadcast[name]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (routes *Routes) BroadcastAll(name PeerName) []PeerName {
|
func (routes *Routes) BroadcastAll(name PeerName) []PeerName {
|
||||||
|
return routes.lookupOrCalculate(name, &routes.broadcastAll, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (routes *Routes) lookupOrCalculate(name PeerName, broadcast *broadcastRoutes, establishedAndSymmetric bool) []PeerName {
|
||||||
routes.RLock()
|
routes.RLock()
|
||||||
defer routes.RUnlock()
|
hops, found := (*broadcast)[name]
|
||||||
return routes.broadcastAll[name]
|
routes.RUnlock()
|
||||||
|
if found {
|
||||||
|
return hops
|
||||||
|
}
|
||||||
|
res := make(chan []PeerName)
|
||||||
|
routes.action <- func() {
|
||||||
|
routes.RLock()
|
||||||
|
hops, found := (*broadcast)[name]
|
||||||
|
routes.RUnlock()
|
||||||
|
if found {
|
||||||
|
res <- hops
|
||||||
|
}
|
||||||
|
routes.peers.RLock()
|
||||||
|
routes.ourself.RLock()
|
||||||
|
hops = routes.calculateBroadcast(name, establishedAndSymmetric)
|
||||||
|
routes.ourself.RUnlock()
|
||||||
|
routes.peers.RUnlock()
|
||||||
|
res <- hops
|
||||||
|
routes.Lock()
|
||||||
|
(*broadcast)[name] = hops
|
||||||
|
routes.Unlock()
|
||||||
|
}
|
||||||
|
return <-res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Choose min(log2(n_peers), n_neighbouring_peers) neighbours, with a
|
// Choose min(log2(n_peers), n_neighbouring_peers) neighbours, with a
|
||||||
@@ -123,6 +150,7 @@ func (routes *Routes) Recalculate() {
|
|||||||
select {
|
select {
|
||||||
case routes.recalculate <- nil:
|
case routes.recalculate <- nil:
|
||||||
default:
|
default:
|
||||||
|
log.Println("route recalculation coalesced")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +161,7 @@ func (routes *Routes) EnsureRecalculated() {
|
|||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
func (routes *Routes) run(recalculate <-chan *struct{}, wait <-chan chan struct{}) {
|
func (routes *Routes) run(recalculate <-chan *struct{}, wait <-chan chan struct{}, action <-chan func()) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-recalculate:
|
case <-recalculate:
|
||||||
@@ -145,6 +173,8 @@ func (routes *Routes) run(recalculate <-chan *struct{}, wait <-chan chan struct{
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
close(done)
|
close(done)
|
||||||
|
case f := <-action:
|
||||||
|
f()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,13 +183,13 @@ func (routes *Routes) calculate() {
|
|||||||
routes.peers.RLock()
|
routes.peers.RLock()
|
||||||
routes.ourself.RLock()
|
routes.ourself.RLock()
|
||||||
var (
|
var (
|
||||||
oldUnicast = routes.unicast
|
|
||||||
oldBroadcast = routes.broadcast
|
|
||||||
unicast = routes.calculateUnicast(true)
|
unicast = routes.calculateUnicast(true)
|
||||||
unicastAll = routes.calculateUnicast(false)
|
unicastAll = routes.calculateUnicast(false)
|
||||||
broadcast = routes.calculateBroadcast(true)
|
broadcast = make(broadcastRoutes)
|
||||||
broadcastAll = routes.calculateBroadcast(false)
|
broadcastAll = make(broadcastRoutes)
|
||||||
)
|
)
|
||||||
|
broadcast[routes.ourself.Name] = routes.calculateBroadcast(routes.ourself.Name, true)
|
||||||
|
broadcastAll[routes.ourself.Name] = routes.calculateBroadcast(routes.ourself.Name, false)
|
||||||
routes.ourself.RUnlock()
|
routes.ourself.RUnlock()
|
||||||
routes.peers.RUnlock()
|
routes.peers.RUnlock()
|
||||||
|
|
||||||
@@ -171,10 +201,8 @@ func (routes *Routes) calculate() {
|
|||||||
onChange := routes.onChange
|
onChange := routes.onChange
|
||||||
routes.Unlock()
|
routes.Unlock()
|
||||||
|
|
||||||
if !unicast.equals(oldUnicast) || !broadcast.equals(oldBroadcast) {
|
for _, callback := range onChange {
|
||||||
for _, callback := range onChange {
|
callback()
|
||||||
callback()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +220,7 @@ func (routes *Routes) calculateUnicast(establishedAndSymmetric bool) unicastRout
|
|||||||
return unicast
|
return unicast
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate all the routes for the question: if we receive a
|
// Calculate the route to answer the question: if we receive a
|
||||||
// broadcast originally from Peer X, which peers should we pass the
|
// broadcast originally from Peer X, which peers should we pass the
|
||||||
// frames on to?
|
// frames on to?
|
||||||
//
|
//
|
||||||
@@ -210,67 +238,15 @@ func (routes *Routes) calculateUnicast(establishedAndSymmetric bool) unicastRout
|
|||||||
// Y =/= Z /\ X.Routes(Y) <= X.Routes(Z) =>
|
// Y =/= Z /\ X.Routes(Y) <= X.Routes(Z) =>
|
||||||
// X.Routes(Y) u [P | Y.HasSymmetricConnectionTo(P)] <= X.Routes(Z)
|
// X.Routes(Y) u [P | Y.HasSymmetricConnectionTo(P)] <= X.Routes(Z)
|
||||||
// where <= is the subset relationship on keys of the returned map.
|
// where <= is the subset relationship on keys of the returned map.
|
||||||
func (routes *Routes) calculateBroadcast(establishedAndSymmetric bool) broadcastRoutes {
|
func (routes *Routes) calculateBroadcast(name PeerName, establishedAndSymmetric bool) []PeerName {
|
||||||
broadcast := make(broadcastRoutes)
|
hops := []PeerName{}
|
||||||
for _, peer := range routes.peers.byName {
|
peer, found := routes.peers.byName[name]
|
||||||
hops := []PeerName{}
|
if !found {
|
||||||
if found, reached := peer.Routes(routes.ourself.Peer, establishedAndSymmetric); found {
|
return hops
|
||||||
routes.ourself.ForEachConnectedPeer(establishedAndSymmetric, reached,
|
|
||||||
func(remotePeer *Peer) { hops = append(hops, remotePeer.Name) })
|
|
||||||
}
|
|
||||||
broadcast[peer.Name] = hops
|
|
||||||
}
|
}
|
||||||
return broadcast
|
if found, reached := peer.Routes(routes.ourself.Peer, establishedAndSymmetric); found {
|
||||||
}
|
routes.ourself.ForEachConnectedPeer(establishedAndSymmetric, reached,
|
||||||
|
func(remotePeer *Peer) { hops = append(hops, remotePeer.Name) })
|
||||||
func (a unicastRoutes) equals(b unicastRoutes) bool {
|
|
||||||
for key, aval := range a {
|
|
||||||
if bval, ok := b[key]; !ok || bval != aval {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return hops
|
||||||
for key := range b {
|
|
||||||
if _, ok := a[key]; !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a broadcastRoutes) equals(b broadcastRoutes) bool {
|
|
||||||
set := make(map[PeerName]struct{})
|
|
||||||
|
|
||||||
for key, aval := range a {
|
|
||||||
bval, ok := b[key]
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, peer := range aval {
|
|
||||||
set[peer] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, peer := range bval {
|
|
||||||
if _, ok := set[peer]; !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(set, peer)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(set) != 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for key := range b {
|
|
||||||
if _, ok := a[key]; !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user