Merge pull request #1855 from weaveworks/gossip-broadcast-stall-3
don't stall gossip broadcasting when there are blocked connections; LGTM
This commit is contained in:
commit
bbd0b15792
2 files changed
+118
-97
No files matched your search
+97
-26
@@ -34,18 +34,28 @@ type Gossiper interface {
|
|||||||
// and sends it when possible.
|
// and sends it when possible.
|
||||||
type GossipSender struct {
|
type GossipSender struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
send func(GossipData)
|
makeMsg func(msg []byte) ProtocolMsg
|
||||||
cell GossipData
|
makeBroadcastMsg func(srcName PeerName, msg []byte) ProtocolMsg
|
||||||
stop chan<- struct{}
|
sender ProtocolSender
|
||||||
more chan<- struct{}
|
gossip GossipData
|
||||||
flush chan<- chan<- bool // for testing
|
broadcasts map[PeerName]GossipData
|
||||||
|
stop chan<- struct{}
|
||||||
|
more chan<- struct{}
|
||||||
|
flush chan<- chan<- bool // for testing
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGossipSender(send func(GossipData)) *GossipSender {
|
func NewGossipSender(makeMsg func(msg []byte) ProtocolMsg, makeBroadcastMsg func(srcName PeerName, msg []byte) ProtocolMsg, sender ProtocolSender) *GossipSender {
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
more := make(chan struct{}, 1)
|
more := make(chan struct{}, 1)
|
||||||
flush := make(chan chan<- bool)
|
flush := make(chan chan<- bool)
|
||||||
s := &GossipSender{send: send, stop: stop, more: more, flush: flush}
|
s := &GossipSender{
|
||||||
|
makeMsg: makeMsg,
|
||||||
|
makeBroadcastMsg: makeBroadcastMsg,
|
||||||
|
sender: sender,
|
||||||
|
broadcasts: make(map[PeerName]GossipData),
|
||||||
|
stop: stop,
|
||||||
|
more: more,
|
||||||
|
flush: flush}
|
||||||
go s.run(stop, more, flush)
|
go s.run(stop, more, flush)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -57,15 +67,21 @@ func (s *GossipSender) run(stop <-chan struct{}, more <-chan struct{}, flush <-c
|
|||||||
case <-stop:
|
case <-stop:
|
||||||
return
|
return
|
||||||
case <-more:
|
case <-more:
|
||||||
s.deliver()
|
sentSomething, err := s.deliver(stop)
|
||||||
sent = true
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sent = sent || sentSomething
|
||||||
case ch := <-flush: // for testing
|
case ch := <-flush: // for testing
|
||||||
// send anything pending, then reply back whether we sent
|
// send anything pending, then reply back whether we sent
|
||||||
// anything since previous flush
|
// anything since previous flush
|
||||||
select {
|
select {
|
||||||
case <-more:
|
case <-more:
|
||||||
s.deliver()
|
sentSomething, err := s.deliver(stop)
|
||||||
sent = true
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sent = sent || sentSomething
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
ch <- sent
|
ch <- sent
|
||||||
@@ -74,25 +90,83 @@ func (s *GossipSender) run(stop <-chan struct{}, more <-chan struct{}, flush <-c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GossipSender) deliver() {
|
func (s *GossipSender) deliver(stop <-chan struct{}) (bool, error) {
|
||||||
|
sent := false
|
||||||
|
// We must not hold our lock when sending, since that would block
|
||||||
|
// the callers of Send/Broadcast while we are stuck waiting for
|
||||||
|
// network congestion to clear. So we pick and send one piece of
|
||||||
|
// data at a time, only holding the lock during the picking.
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return sent, nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
data, makeProtocolMsg := s.pick()
|
||||||
|
if data == nil {
|
||||||
|
return sent, nil
|
||||||
|
}
|
||||||
|
for _, msg := range data.Encode() {
|
||||||
|
if err := s.sender.SendProtocolMsg(makeProtocolMsg(msg)); err != nil {
|
||||||
|
return sent, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sent = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *GossipSender) pick() (data GossipData, makeProtocolMsg func(msg []byte) ProtocolMsg) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
data := s.cell
|
defer s.Unlock()
|
||||||
s.cell = nil
|
switch {
|
||||||
s.Unlock()
|
case s.gossip != nil: // usually more important than broadcasts
|
||||||
s.send(data)
|
data = s.gossip
|
||||||
|
makeProtocolMsg = s.makeMsg
|
||||||
|
s.gossip = nil
|
||||||
|
case len(s.broadcasts) > 0:
|
||||||
|
for srcName, d := range s.broadcasts {
|
||||||
|
data = d
|
||||||
|
makeProtocolMsg = func(msg []byte) ProtocolMsg { return s.makeBroadcastMsg(srcName, msg) }
|
||||||
|
delete(s.broadcasts, srcName)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GossipSender) Send(data GossipData) {
|
func (s *GossipSender) Send(data GossipData) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
if s.cell == nil {
|
if s.empty() {
|
||||||
s.cell = data
|
defer s.prod()
|
||||||
select {
|
}
|
||||||
case s.more <- void:
|
if s.gossip == nil {
|
||||||
default:
|
s.gossip = data
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
s.cell = s.cell.Merge(data)
|
s.gossip = s.gossip.Merge(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *GossipSender) Broadcast(srcName PeerName, data GossipData) {
|
||||||
|
s.Lock()
|
||||||
|
defer s.Unlock()
|
||||||
|
if s.empty() {
|
||||||
|
defer s.prod()
|
||||||
|
}
|
||||||
|
d, found := s.broadcasts[srcName]
|
||||||
|
if !found {
|
||||||
|
s.broadcasts[srcName] = data
|
||||||
|
} else {
|
||||||
|
s.broadcasts[srcName] = d.Merge(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *GossipSender) empty() bool { return s.gossip == nil && len(s.broadcasts) == 0 }
|
||||||
|
|
||||||
|
func (s *GossipSender) prod() {
|
||||||
|
select {
|
||||||
|
case s.more <- void:
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,9 +247,6 @@ func (router *Router) sendPendingGossip() bool {
|
|||||||
for _, sender := range channel.senders {
|
for _, sender := range channel.senders {
|
||||||
sentSomething = sender.Flush() || sentSomething
|
sentSomething = sender.Flush() || sentSomething
|
||||||
}
|
}
|
||||||
for _, sender := range channel.broadcasters {
|
|
||||||
sentSomething = sender.Flush() || sentSomething
|
|
||||||
}
|
|
||||||
channel.Unlock()
|
channel.Unlock()
|
||||||
}
|
}
|
||||||
return sentSomething
|
return sentSomething
|
||||||
|
|||||||
+21
-71
@@ -9,25 +9,23 @@ import (
|
|||||||
|
|
||||||
type GossipChannel struct {
|
type GossipChannel struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
name string
|
name string
|
||||||
ourself *LocalPeer
|
ourself *LocalPeer
|
||||||
routes *Routes
|
routes *Routes
|
||||||
gossiper Gossiper
|
gossiper Gossiper
|
||||||
senders connectionSenders
|
senders connectionSenders
|
||||||
broadcasters peerSenders
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type connectionSenders map[Connection]*GossipSender
|
type connectionSenders map[Connection]*GossipSender
|
||||||
type peerSenders map[PeerName]*GossipSender
|
|
||||||
|
|
||||||
func NewGossipChannel(channelName string, ourself *LocalPeer, routes *Routes, g Gossiper) *GossipChannel {
|
func NewGossipChannel(channelName string, ourself *LocalPeer, routes *Routes, g Gossiper) *GossipChannel {
|
||||||
return &GossipChannel{
|
return &GossipChannel{
|
||||||
name: channelName,
|
name: channelName,
|
||||||
ourself: ourself,
|
ourself: ourself,
|
||||||
routes: routes,
|
routes: routes,
|
||||||
gossiper: g,
|
gossiper: g,
|
||||||
senders: make(connectionSenders),
|
senders: make(connectionSenders),
|
||||||
broadcasters: make(peerSenders)}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (router *Router) handleGossip(tag ProtocolTag, payload []byte) error {
|
func (router *Router) handleGossip(tag ProtocolTag, payload []byte) error {
|
||||||
@@ -128,56 +126,11 @@ func (c *GossipChannel) relayUnicast(dstPeerName PeerName, buf []byte) (err erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *GossipChannel) relayBroadcast(srcName PeerName, update GossipData) error {
|
func (c *GossipChannel) relayBroadcast(srcName PeerName, update GossipData) error {
|
||||||
names := c.routes.PeerNames() // do this outside the lock so they don't nest
|
|
||||||
c.Lock()
|
|
||||||
// GC - randomly (courtesy of go's map iterator) pick some
|
|
||||||
// existing broadcasters and stop&remove them if their source peer
|
|
||||||
// is unknown. We stop as soon as we encounter a valid entry; the
|
|
||||||
// idea being that when there is little or no garbage then this
|
|
||||||
// executes close to O(1)[1], whereas when there is lots of
|
|
||||||
// garbage we remove it quickly.
|
|
||||||
//
|
|
||||||
// [1] TODO Unfortunately, due to the desire to avoid nested
|
|
||||||
// locks, instead of simply invoking Peers.Fetch(name) below, we
|
|
||||||
// have that Peers.Names() invocation above. That is O(n_peers) at
|
|
||||||
// best.
|
|
||||||
for name, broadcaster := range c.broadcasters {
|
|
||||||
if _, found := names[name]; !found {
|
|
||||||
delete(c.broadcasters, name)
|
|
||||||
broadcaster.Stop()
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
broadcaster, found := c.broadcasters[srcName]
|
|
||||||
if !found {
|
|
||||||
broadcaster = NewGossipSender(func(pending GossipData) { c.sendBroadcast(srcName, pending) })
|
|
||||||
c.broadcasters[srcName] = broadcaster
|
|
||||||
}
|
|
||||||
c.Unlock()
|
|
||||||
broadcaster.Send(update)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) {
|
|
||||||
c.routes.EnsureRecalculated()
|
c.routes.EnsureRecalculated()
|
||||||
connections := c.ourself.ConnectionsTo(c.routes.BroadcastAll(srcName))
|
for _, sender := range c.sendersFor(c.ourself.ConnectionsTo(c.routes.BroadcastAll(srcName))) {
|
||||||
if len(connections) == 0 {
|
sender.Broadcast(srcName, update)
|
||||||
return
|
|
||||||
}
|
|
||||||
msgs := update.Encode()
|
|
||||||
protocolMsgs := make([]ProtocolMsg, len(msgs), len(msgs))
|
|
||||||
for i, msg := range msgs {
|
|
||||||
protocolMsgs[i] = ProtocolMsg{ProtocolGossipBroadcast, GobEncode(c.name, srcName, msg)}
|
|
||||||
}
|
|
||||||
// FIXME a single blocked connection can stall us
|
|
||||||
for _, conn := range connections {
|
|
||||||
for _, protocolMsg := range protocolMsgs {
|
|
||||||
if conn.(ProtocolSender).SendProtocolMsg(protocolMsg) != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GossipChannel) relay(srcName PeerName, data GossipData) {
|
func (c *GossipChannel) relay(srcName PeerName, data GossipData) {
|
||||||
@@ -218,7 +171,7 @@ func (c *GossipChannel) sendersFor(conns []Connection) []*GossipSender {
|
|||||||
for i, conn := range conns {
|
for i, conn := range conns {
|
||||||
sender, found := c.senders[conn]
|
sender, found := c.senders[conn]
|
||||||
if !found {
|
if !found {
|
||||||
sender = c.makeSender(conn)
|
sender = NewGossipSender(c.makeMsg, c.makeBroadcastMsg, conn.(ProtocolSender))
|
||||||
c.senders[conn] = sender
|
c.senders[conn] = sender
|
||||||
}
|
}
|
||||||
senders[i] = sender
|
senders[i] = sender
|
||||||
@@ -226,15 +179,12 @@ func (c *GossipChannel) sendersFor(conns []Connection) []*GossipSender {
|
|||||||
return senders
|
return senders
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GossipChannel) makeSender(conn Connection) *GossipSender {
|
func (c *GossipChannel) makeMsg(msg []byte) ProtocolMsg {
|
||||||
return NewGossipSender(func(pending GossipData) {
|
return ProtocolMsg{ProtocolGossip, GobEncode(c.name, c.ourself.Name, msg)}
|
||||||
for _, msg := range pending.Encode() {
|
}
|
||||||
protocolMsg := ProtocolMsg{ProtocolGossip, GobEncode(c.name, c.ourself.Name, msg)}
|
|
||||||
if conn.(ProtocolSender).SendProtocolMsg(protocolMsg) != nil {
|
func (c *GossipChannel) makeBroadcastMsg(srcName PeerName, msg []byte) ProtocolMsg {
|
||||||
break
|
return ProtocolMsg{ProtocolGossipBroadcast, GobEncode(c.name, srcName, msg)}
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GossipChannel) log(args ...interface{}) {
|
func (c *GossipChannel) log(args ...interface{}) {
|
||||||
|
|||||||
Reference in new issue
Block a user