Handle disconnected nodes better in puller

This commit is contained in:
Jakob Borg 2014-07-21 10:48:22 +02:00
parent 75d4d2df8b
commit 0fcc193197
2 changed files with 10 additions and 7 deletions

View File

@ -242,17 +242,20 @@ func BenchmarkRequest(b *testing.B) {
} }
func TestActivityMap(t *testing.T) { func TestActivityMap(t *testing.T) {
isValid := func(protocol.NodeID) bool {
return true
}
m := make(activityMap) m := make(activityMap)
if node := m.leastBusyNode([]protocol.NodeID{node1}); node != node1 { if node := m.leastBusyNode([]protocol.NodeID{node1}, isValid); node != node1 {
t.Errorf("Incorrect least busy node %q", node) t.Errorf("Incorrect least busy node %q", node)
} }
if node := m.leastBusyNode([]protocol.NodeID{node2}); node != node2 { if node := m.leastBusyNode([]protocol.NodeID{node2}, isValid); node != node2 {
t.Errorf("Incorrect least busy node %q", node) t.Errorf("Incorrect least busy node %q", node)
} }
if node := m.leastBusyNode([]protocol.NodeID{node1, node2}); node != node1 { if node := m.leastBusyNode([]protocol.NodeID{node1, node2}, isValid); node != node1 {
t.Errorf("Incorrect least busy node %q", node) t.Errorf("Incorrect least busy node %q", node)
} }
if node := m.leastBusyNode([]protocol.NodeID{node1, node2}); node != node2 { if node := m.leastBusyNode([]protocol.NodeID{node1, node2}, isValid); node != node2 {
t.Errorf("Incorrect least busy node %q", node) t.Errorf("Incorrect least busy node %q", node)
} }
} }

View File

@ -41,12 +41,12 @@ type openFile struct {
type activityMap map[protocol.NodeID]int type activityMap map[protocol.NodeID]int
func (m activityMap) leastBusyNode(availability []protocol.NodeID) protocol.NodeID { func (m activityMap) leastBusyNode(availability []protocol.NodeID, isValid func(protocol.NodeID) bool) protocol.NodeID {
var low int = 2<<30 - 1 var low int = 2<<30 - 1
var selected protocol.NodeID var selected protocol.NodeID
for _, node := range availability { for _, node := range availability {
usage := m[node] usage := m[node]
if usage < low { if usage < low && isValid(node) {
low = usage low = usage
selected = node selected = node
} }
@ -533,7 +533,7 @@ func (p *puller) handleRequestBlock(b bqBlock) bool {
panic("bug: request for non-open file") panic("bug: request for non-open file")
} }
node := p.oustandingPerNode.leastBusyNode(of.availability) node := p.oustandingPerNode.leastBusyNode(of.availability, p.model.ConnectedTo)
if node == (protocol.NodeID{}) { if node == (protocol.NodeID{}) {
of.err = errNoNode of.err = errNoNode
if of.file != nil { if of.file != nil {