2018-01-18 02:49:35 +00:00
|
|
|
package ipfscluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
2018-05-01 16:01:59 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/rpcutil"
|
2019-06-14 10:41:11 +00:00
|
|
|
|
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
|
|
|
"go.opencensus.io/trace"
|
2018-01-18 02:49:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectGraph returns a description of which cluster peers and ipfs
|
|
|
|
// daemons are connected to each other
|
|
|
|
func (c *Cluster) ConnectGraph() (api.ConnectGraph, error) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx, span := trace.StartSpan(c.ctx, "cluster/ConnectGraph")
|
|
|
|
defer span.End()
|
|
|
|
|
2018-01-18 02:49:35 +00:00
|
|
|
cg := api.ConnectGraph{
|
2019-02-27 21:54:09 +00:00
|
|
|
ClusterID: c.host.ID(),
|
2019-02-27 17:04:35 +00:00
|
|
|
IPFSLinks: make(map[string][]peer.ID),
|
|
|
|
ClusterLinks: make(map[string][]peer.ID),
|
|
|
|
ClustertoIPFS: make(map[string]peer.ID),
|
2018-01-18 02:49:35 +00:00
|
|
|
}
|
2018-06-27 04:03:15 +00:00
|
|
|
members, err := c.consensus.Peers(ctx)
|
2018-01-18 02:49:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return cg, err
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
peers := make([][]*api.ID, len(members), len(members))
|
2018-05-01 16:01:59 +00:00
|
|
|
|
2018-06-27 04:03:15 +00:00
|
|
|
ctxs, cancels := rpcutil.CtxsWithCancel(ctx, len(members))
|
2018-05-08 11:10:38 +00:00
|
|
|
defer rpcutil.MultiCancel(cancels)
|
2018-05-01 16:01:59 +00:00
|
|
|
|
|
|
|
errs := c.rpcClient.MultiCall(
|
|
|
|
ctxs,
|
|
|
|
members,
|
|
|
|
"Cluster",
|
|
|
|
"Peers",
|
|
|
|
struct{}{},
|
2019-02-27 17:04:35 +00:00
|
|
|
rpcutil.CopyIDSliceToIfaces(peers),
|
2018-05-01 16:01:59 +00:00
|
|
|
)
|
2018-01-18 02:49:35 +00:00
|
|
|
|
|
|
|
for i, err := range errs {
|
2019-02-27 17:04:35 +00:00
|
|
|
p := peer.IDB58Encode(members[i])
|
2018-01-18 02:49:35 +00:00
|
|
|
cg.ClusterLinks[p] = make([]peer.ID, 0)
|
|
|
|
if err != nil { // Only setting cluster connections when no error occurs
|
2019-02-27 17:04:35 +00:00
|
|
|
logger.Debugf("RPC error reaching cluster peer %s: %s", p, err.Error())
|
2018-01-18 02:49:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
selfConnection, pID := c.recordClusterLinks(&cg, p, peers[i])
|
2018-01-18 02:49:35 +00:00
|
|
|
|
|
|
|
// IPFS connections
|
|
|
|
if !selfConnection {
|
2019-02-27 17:04:35 +00:00
|
|
|
logger.Warningf("cluster peer %s not its own peer. No ipfs info ", p)
|
2018-01-18 02:49:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.recordIPFSLinks(&cg, pID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cg, nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
func (c *Cluster) recordClusterLinks(cg *api.ConnectGraph, p string, peers []*api.ID) (bool, *api.ID) {
|
2018-01-18 02:49:35 +00:00
|
|
|
selfConnection := false
|
2019-02-27 17:04:35 +00:00
|
|
|
var pID *api.ID
|
|
|
|
for _, id := range peers {
|
2018-01-18 02:49:35 +00:00
|
|
|
if id.Error != "" {
|
2019-02-27 17:04:35 +00:00
|
|
|
logger.Debugf("Peer %s errored connecting to its peer %s", p, id.ID.Pretty())
|
2018-01-18 02:49:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
if peer.IDB58Encode(id.ID) == p {
|
2018-01-18 02:49:35 +00:00
|
|
|
selfConnection = true
|
|
|
|
pID = id
|
|
|
|
} else {
|
|
|
|
cg.ClusterLinks[p] = append(cg.ClusterLinks[p], id.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selfConnection, pID
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
func (c *Cluster) recordIPFSLinks(cg *api.ConnectGraph, pID *api.ID) {
|
2018-01-18 02:49:35 +00:00
|
|
|
ipfsID := pID.IPFS.ID
|
|
|
|
if pID.IPFS.Error != "" { // Only setting ipfs connections when no error occurs
|
|
|
|
logger.Warningf("ipfs id: %s has error: %s. Skipping swarm connections", ipfsID.Pretty(), pID.IPFS.Error)
|
|
|
|
return
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
|
|
|
|
pid := peer.IDB58Encode(pID.ID)
|
|
|
|
ipfsPid := peer.IDB58Encode(ipfsID)
|
|
|
|
|
|
|
|
if _, ok := cg.IPFSLinks[pid]; ok {
|
2018-01-18 02:49:35 +00:00
|
|
|
logger.Warningf("ipfs id: %s already recorded, one ipfs daemon in use by multiple cluster peers", ipfsID.Pretty())
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
cg.ClustertoIPFS[pid] = ipfsID
|
|
|
|
cg.IPFSLinks[ipfsPid] = make([]peer.ID, 0)
|
|
|
|
var swarmPeers []peer.ID
|
|
|
|
err := c.rpcClient.Call(
|
|
|
|
pID.ID,
|
2019-05-04 20:36:10 +00:00
|
|
|
"IPFSConnector",
|
|
|
|
"SwarmPeers",
|
2018-01-18 02:49:35 +00:00
|
|
|
struct{}{},
|
2019-02-27 17:04:35 +00:00
|
|
|
&swarmPeers,
|
2018-01-18 02:49:35 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
cg.IPFSLinks[ipfsPid] = swarmPeers
|
2018-01-18 02:49:35 +00:00
|
|
|
}
|