added bootstrap logging
Juan Batiz-Benet committed
Dec 18, 2014 at 12:23 UTC
7952d95bbf1fc0b63742258405c5c0d4a8cab8eb
3 files changed
+43
-4
core/bootstrap.go
+34
-2
@@ -1,17 +1,21 @@
1
package core
2
3
import (
4
+ "errors"
5
+ "fmt"
6
"math/rand"
7
"sync"
8
"time"
9
8
- context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
- ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10
config "github.com/jbenet/go-ipfs/config"
11
inet "github.com/jbenet/go-ipfs/net"
12
peer "github.com/jbenet/go-ipfs/peer"
13
dht "github.com/jbenet/go-ipfs/routing/dht"
14
+ lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
15
math2 "github.com/jbenet/go-ipfs/util/math2"
16
+
17
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
18
+ ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
19
)
20
21
const (
@@ -50,14 +54,23 @@ func bootstrap(ctx context.Context,
54
55
connectedPeers := n.Peers()
56
if len(connectedPeers) >= recoveryThreshold {
57
+ log.Event(ctx, "bootstrapSkip", n.LocalPeer())
58
+ log.Debugf("%s bootstrap skipped -- connected to %d (> %d) nodes",
59
+ n.LocalPeer(), len(connectedPeers), recoveryThreshold)
60
+
61
return nil
62
}
63
numCxnsToCreate := recoveryThreshold - len(connectedPeers)
64
65
+ log.Event(ctx, "bootstrapStart", n.LocalPeer())
66
+ log.Debugf("%s bootstrapping to %d more nodes", n.LocalPeer(), numCxnsToCreate)
67
+
68
var bootstrapPeers []peer.PeerInfo
69
for _, bootstrap := range boots {
70
p, err := toPeer(bootstrap)
71
if err != nil {
72
+ log.Event(ctx, "bootstrapError", n.LocalPeer(), lgbl.Error(err))
73
+ log.Errorf("%s bootstrap error: %s", n.LocalPeer(), err)
74
return err
75
}
76
bootstrapPeers = append(bootstrapPeers, p)
@@ -70,14 +83,30 @@ func bootstrap(ctx context.Context,
83
}
84
}
85
86
+ if len(notConnected) < 1 {
87
+ s := "must bootstrap to %d more nodes, but already connected to all candidates"
88
+ err := fmt.Errorf(s, numCxnsToCreate)
89
+ log.Event(ctx, "bootstrapError", n.LocalPeer(), lgbl.Error(err))
90
+ log.Errorf("%s bootstrap error: %s", n.LocalPeer(), err)
91
+ return err
92
+ }
93
+
94
var randomSubset = randomSubsetOfPeers(notConnected, numCxnsToCreate)
95
+
96
+ log.Debugf("%s bootstrapping to %d nodes: %s", n.LocalPeer(), numCxnsToCreate, randomSubset)
97
if err := connect(ctx, ps, r, randomSubset); err != nil {
98
+ log.Event(ctx, "bootstrapError", n.LocalPeer(), lgbl.Error(err))
99
+ log.Errorf("%s bootstrap error: %s", n.LocalPeer(), err)
100
return err
101
}
102
return nil
103
}
104
105
func connect(ctx context.Context, ps peer.Peerstore, r *dht.IpfsDHT, peers []peer.PeerInfo) error {
106
+ if len(peers) < 1 {
107
+ return errors.New("bootstrap set empty")
108
+ }
109
+
110
var wg sync.WaitGroup
111
for _, p := range peers {
112
@@ -88,6 +117,9 @@ func connect(ctx context.Context, ps peer.Peerstore, r *dht.IpfsDHT, peers []pee
117
wg.Add(1)
118
go func(p peer.PeerInfo) {
119
defer wg.Done()
120
+ log.Event(ctx, "bootstrapDial", r.LocalPeer(), p.ID)
121
+ log.Debugf("%s bootstrapping to %s", r.LocalPeer(), p.ID)
122
+
123
ps.AddAddresses(p.ID, p.Addrs)
124
err := r.Connect(ctx, p.ID)
125
if err != nil {
net/net.go
+4
-2
@@ -135,6 +135,7 @@ func (n *network) newConnHandler(c *swarm.Conn) {
135
// DialPeer attempts to establish a connection to a given peer.
136
// Respects the context.
137
func (n *network) DialPeer(ctx context.Context, p peer.ID) error {
138
+ log.Debugf("[%s] network dialing peer [%s]", n.local, p)
139
sc, err := n.swarm.Dial(ctx, p)
140
if err != nil {
141
return err
@@ -242,8 +243,9 @@ func (n *network) Connectedness(p peer.ID) Connectedness {
243
// NewStream returns a new stream to given peer p.
244
// If there is no connection to p, attempts to create one.
245
// If ProtocolID is "", writes no header.
245
-func (c *network) NewStream(pr ProtocolID, p peer.ID) (Stream, error) {
246
- s, err := c.swarm.NewStreamWithPeer(p)
246
+func (n *network) NewStream(pr ProtocolID, p peer.ID) (Stream, error) {
247
+ log.Debugf("[%s] network opening stream to peer [%s]: %s", n.local, p, pr)
248
+ s, err := n.swarm.NewStreamWithPeer(p)
249
if err != nil {
250
return nil, err
251
}
routing/dht/dht.go
+5
@@ -77,6 +77,11 @@ func NewDHT(ctx context.Context, p peer.ID, n inet.Network, dstore ds.ThreadSafe
77
return dht
78
}
79
80
+// LocalPeer returns the peer.Peer of the dht.
81
+func (dht *IpfsDHT) LocalPeer() peer.ID {
82
+ return dht.self
83
+}
84
+
85
// Connect to a new peer at the given address, ping and add to the routing table
86
func (dht *IpfsDHT) Connect(ctx context.Context, npeer peer.ID) error {
87
if err := dht.network.DialPeer(ctx, npeer); err != nil {