Merge pull request #333 from filecoin-project/feat/th-names

townhall: Node names
This commit is contained in:
Łukasz Magiera 2019-10-11 04:55:33 +02:00 committed by GitHub
commit 4334e4d93d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 37 deletions

View File

@ -3,10 +3,13 @@ package metrics
import (
"context"
"encoding/json"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"go.uber.org/fx"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/node/impl/full"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
)
@ -19,46 +22,60 @@ type Update struct {
Type string
}
func SendHeadNotifs(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
ctx := helpers.LifecycleCtx(mctx, lc)
func SendHeadNotifs(nickname string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
ctx := helpers.LifecycleCtx(mctx, lc)
lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
gen, err := chain.Chain.GetGenesis()
if err != nil {
return err
}
topic := baseTopic + gen.Cid().String()
go func() {
if err := sendHeadNotifs(ctx, ps, topic, chain); err != nil {
log.Error("consensus metrics error", err)
return
}
}()
go func() {
sub, err := ps.Subscribe(topic)
lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
gen, err := chain.Chain.GetGenesis()
if err != nil {
return
return err
}
defer sub.Cancel()
for {
if _, err := sub.Next(ctx); err != nil {
topic := baseTopic + gen.Cid().String()
go func() {
if err := sendHeadNotifs(ctx, ps, topic, chain, nickname); err != nil {
log.Error("consensus metrics error", err)
return
}
}
}()
go func() {
sub, err := ps.Subscribe(topic)
if err != nil {
return
}
defer sub.Cancel()
}()
return nil
},
})
for {
if _, err := sub.Next(ctx); err != nil {
return
}
}
return nil
}()
return nil
},
})
return nil
}
}
func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain full.ChainAPI) error {
type message struct {
// TipSet
Cids []cid.Cid
Blocks []*types.BlockHeader
Height uint64
Weight types.BigInt
// Meta
NodeName string
}
func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain full.ChainAPI, nickname string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
@ -72,7 +89,15 @@ func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain
case notif := <-notifs:
n := notif[len(notif)-1]
b, err := json.Marshal(n.Val)
m := message{
Cids: n.Val.Cids(),
Blocks: n.Val.Blocks(),
Height: n.Val.Height(),
Weight: n.Val.Weight(),
NodeName: nickname,
}
b, err := json.Marshal(m)
if err != nil {
return err
}

View File

@ -24,14 +24,17 @@ class App extends React.Component {
let best = Object.keys(this.state).map(k => this.state[k]).reduce((p, n) => p > n.Height ? p : n.Height, -1)
console.log(best)
return Object.keys(this.state).map(k => [k, this.state[k]]).map(([k, v]) => {
let l = <span>{k} {v.Height}</span>
if(best !== v.Height) {
l = <span style={{color: '#f00'}}>{l}</span>
return <table>{Object.keys(this.state).map(k => [k, this.state[k]]).map(([k, v]) => {
let l = [<td>{k}</td>, <td>{v.NodeName}</td>, <td>{v.Height}</td>]
if (best !== v.Height) {
l = <tr style={{color: '#f00'}}>{l}</tr>
} else {
l = <tr>{l}</tr>
}
return <div>{l}</div>
return l
})
}</table>
}
}
export default App;

View File

@ -228,7 +228,7 @@ func Online() Option {
Override(RunHelloKey, modules.RunHello),
Override(RunBlockSyncKey, modules.RunBlockSync),
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),
Override(HeadMetricsKey, metrics.SendHeadNotifs),
Override(HeadMetricsKey, metrics.SendHeadNotifs("")),
Override(new(*discovery.Local), discovery.NewLocal),
Override(new(discovery.PeerResolver), modules.RetrievalResolver),
@ -293,6 +293,10 @@ func Config(cfg *config.Root) Option {
ApplyIf(func(s *Settings) bool { return s.Online },
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)),
),
),
)
}

View File

@ -6,6 +6,8 @@ import "time"
type Root struct {
API API
Libp2p Libp2p
Metrics Metrics
}
// API contains configs for API endpoint
@ -19,6 +21,10 @@ type Libp2p struct {
ListenAddresses []string
}
type Metrics struct {
Nickname string
}
// Default returns the default config
func Default() *Root {
def := Root{