network: structured output for kademlia table (#1586)
This commit is contained in:
committed by
GitHub
parent
090197227f
commit
9720da34db
@ -39,19 +39,16 @@ func NewInspector(api *API, hive *network.Hive, netStore *storage.NetStore) *Ins
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Hive prints the kademlia table
|
// Hive prints the kademlia table
|
||||||
func (inspector *Inspector) Hive() string {
|
func (i *Inspector) Hive() string {
|
||||||
return inspector.hive.String()
|
return i.hive.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (inspector *Inspector) ListKnown() []string {
|
// KademliaInfo returns structured output of the Kademlia state that we can check for equality
|
||||||
res := []string{}
|
func (i *Inspector) KademliaInfo() network.KademliaInfo {
|
||||||
for _, v := range inspector.hive.Kademlia.ListKnown() {
|
return i.hive.KademliaInfo()
|
||||||
res = append(res, fmt.Sprintf("%v", v))
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (inspector *Inspector) IsPullSyncing() bool {
|
func (i *Inspector) IsPullSyncing() bool {
|
||||||
lastReceivedChunksMsg := metrics.GetOrRegisterGauge("network.stream.received_chunks", nil)
|
lastReceivedChunksMsg := metrics.GetOrRegisterGauge("network.stream.received_chunks", nil)
|
||||||
|
|
||||||
// last received chunks msg time
|
// last received chunks msg time
|
||||||
@ -63,11 +60,12 @@ func (inspector *Inspector) IsPullSyncing() bool {
|
|||||||
return lrct.After(time.Now().Add(-15 * time.Second))
|
return lrct.After(time.Now().Add(-15 * time.Second))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (inspector *Inspector) DeliveriesPerPeer() map[string]int64 {
|
// DeliveriesPerPeer returns the sum of chunks we received from a given peer
|
||||||
|
func (i *Inspector) DeliveriesPerPeer() map[string]int64 {
|
||||||
res := map[string]int64{}
|
res := map[string]int64{}
|
||||||
|
|
||||||
// iterate connection in kademlia
|
// iterate connection in kademlia
|
||||||
inspector.hive.Kademlia.EachConn(nil, 255, func(p *network.Peer, po int) bool {
|
i.hive.Kademlia.EachConn(nil, 255, func(p *network.Peer, po int) bool {
|
||||||
// get how many chunks we receive for retrieve requests per peer
|
// get how many chunks we receive for retrieve requests per peer
|
||||||
peermetric := fmt.Sprintf("chunk.delivery.%x", p.Over()[:16])
|
peermetric := fmt.Sprintf("chunk.delivery.%x", p.Over()[:16])
|
||||||
|
|
||||||
@ -82,10 +80,10 @@ func (inspector *Inspector) DeliveriesPerPeer() map[string]int64 {
|
|||||||
// Has checks whether each chunk address is present in the underlying datastore,
|
// Has checks whether each chunk address is present in the underlying datastore,
|
||||||
// the bool in the returned structs indicates if the underlying datastore has
|
// the bool in the returned structs indicates if the underlying datastore has
|
||||||
// the chunk stored with the given address (true), or not (false)
|
// the chunk stored with the given address (true), or not (false)
|
||||||
func (inspector *Inspector) Has(chunkAddresses []storage.Address) string {
|
func (i *Inspector) Has(chunkAddresses []storage.Address) string {
|
||||||
hostChunks := []string{}
|
hostChunks := []string{}
|
||||||
for _, addr := range chunkAddresses {
|
for _, addr := range chunkAddresses {
|
||||||
has, err := inspector.netStore.Has(context.Background(), addr)
|
has, err := i.netStore.Has(context.Background(), addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -93,6 +94,14 @@ type Kademlia struct {
|
|||||||
nDepthSig []chan struct{} // signals when neighbourhood depth nDepth is changed
|
nDepthSig []chan struct{} // signals when neighbourhood depth nDepth is changed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type KademliaInfo struct {
|
||||||
|
Depth int `json:"depth"`
|
||||||
|
TotalConnections int `json:"total_connections"`
|
||||||
|
TotalKnown int `json:"total_known"`
|
||||||
|
Connections [][]string `json:"connections"`
|
||||||
|
Known [][]string `json:"known"`
|
||||||
|
}
|
||||||
|
|
||||||
// NewKademlia creates a Kademlia table for base address addr
|
// NewKademlia creates a Kademlia table for base address addr
|
||||||
// with parameters as in params
|
// with parameters as in params
|
||||||
// if params is nil, it uses default values
|
// if params is nil, it uses default values
|
||||||
@ -422,18 +431,6 @@ func (k *Kademlia) Off(p *Peer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Kademlia) ListKnown() []*BzzAddr {
|
|
||||||
res := []*BzzAddr{}
|
|
||||||
|
|
||||||
k.addrs.Each(func(val pot.Val) bool {
|
|
||||||
e := val.(*entry)
|
|
||||||
res = append(res, e.BzzAddr)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// EachConn is an iterator with args (base, po, f) applies f to each live peer
|
// EachConn is an iterator with args (base, po, f) applies f to each live peer
|
||||||
// that has proximity order po or less as measured from the base
|
// that has proximity order po or less as measured from the base
|
||||||
// if base is nil, kademlia base address is used
|
// if base is nil, kademlia base address is used
|
||||||
@ -576,6 +573,56 @@ func (k *Kademlia) BaseAddr() []byte {
|
|||||||
return k.base
|
return k.base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *Kademlia) KademliaInfo() KademliaInfo {
|
||||||
|
k.lock.RLock()
|
||||||
|
defer k.lock.RUnlock()
|
||||||
|
return k.kademliaInfo()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *Kademlia) kademliaInfo() (ki KademliaInfo) {
|
||||||
|
ki.Depth = depthForPot(k.conns, k.NeighbourhoodSize, k.base)
|
||||||
|
ki.TotalConnections = k.conns.Size()
|
||||||
|
ki.TotalKnown = k.addrs.Size()
|
||||||
|
ki.Connections = make([][]string, k.MaxProxDisplay)
|
||||||
|
ki.Known = make([][]string, k.MaxProxDisplay)
|
||||||
|
|
||||||
|
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
|
||||||
|
if po >= k.MaxProxDisplay {
|
||||||
|
po = k.MaxProxDisplay - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
row := []string{}
|
||||||
|
f(func(val pot.Val) bool {
|
||||||
|
e := val.(*Peer)
|
||||||
|
row = append(row, fmt.Sprintf("%x", e.Address()))
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
sort.Strings(row)
|
||||||
|
ki.Connections[po] = row
|
||||||
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
|
||||||
|
if po >= k.MaxProxDisplay {
|
||||||
|
po = k.MaxProxDisplay - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
row := []string{}
|
||||||
|
f(func(val pot.Val) bool {
|
||||||
|
e := val.(*entry)
|
||||||
|
row = append(row, fmt.Sprintf("%x", e.Address()))
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
sort.Strings(row)
|
||||||
|
ki.Known[po] = row
|
||||||
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// String returns kademlia table + kaddb table displayed with ascii
|
// String returns kademlia table + kaddb table displayed with ascii
|
||||||
func (k *Kademlia) String() string {
|
func (k *Kademlia) String() string {
|
||||||
k.lock.RLock()
|
k.lock.RLock()
|
||||||
|
Reference in New Issue
Block a user