From a276b6b2e52001a7feb79380fd601b49b3c2aebd Mon Sep 17 00:00:00 2001 From: laizy Date: Tue, 11 Jul 2017 16:02:50 +0800 Subject: [PATCH] export internal status for monitor and debug currently have exported the status of local node, peers, connections, transaction pool and ChainStore, which can be accessed from http://nodeip:HttpJsonPort/debug/vars Signed-off-by: laizy --- .gitignore | 1 - core/store/ChainStore/ChainStore.go | 8 +++- core/store/ChainStore/debug.go | 53 ++++++++++++++++++++++ net/net.go | 2 + net/node/debug.go | 68 +++++++++++++++++++++++++++++ net/node/transactionPool.go | 6 +++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 core/store/ChainStore/debug.go create mode 100644 net/node/debug.go diff --git a/.gitignore b/.gitignore index 10a97cdd..bbbc7134 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,5 @@ Chain/* /core/*/*_test.go /core/*/*/*_test.go /config/* -node nodectl cscope* diff --git a/core/store/ChainStore/ChainStore.go b/core/store/ChainStore/ChainStore.go index 91fafcaa..0c713429 100644 --- a/core/store/ChainStore/ChainStore.go +++ b/core/store/ChainStore/ChainStore.go @@ -73,7 +73,7 @@ func NewChainStore(file string) (*ChainStore, error) { return nil, err } - return &ChainStore{ + chain := &ChainStore{ st: st, headerIndex: map[uint32]Uint256{}, blockCache: map[Uint256]*Block{}, @@ -81,7 +81,11 @@ func NewChainStore(file string) (*ChainStore, error) { currentBlockHeight: 0, storedHeaderCount: 0, disposed: false, - }, nil + } + + ExportStoreStatus(chain) + + return chain, nil } func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defaultBookKeeper []*crypto.PubKey) (uint32, error) { diff --git a/core/store/ChainStore/debug.go b/core/store/ChainStore/debug.go new file mode 100644 index 00000000..3db3314a --- /dev/null +++ b/core/store/ChainStore/debug.go @@ -0,0 +1,53 @@ +package ChainStore + +import ( + // . "DNA/common" + "expvar" + "fmt" + // "time" +) + +type storeStatus struct { + CurrHeaderHeight uint32 + CurrBlockHeight uint32 + HeaderCache map[uint32]string + BlockCache map[uint32]string + HeaderIndex map[uint32]string +} + +func expvarStore(store *ChainStore) func() interface{} { + + return func() interface{} { + headerHeight := store.GetHeaderHeight() + blockHeight := store.GetHeight() + store.mu.RLock() + defer store.mu.RUnlock() + + ss := storeStatus{ + HeaderIndex: make(map[uint32]string, len(store.headerIndex)), + CurrHeaderHeight: headerHeight, + CurrBlockHeight: blockHeight, + HeaderCache: make(map[uint32]string, len(store.headerCache)), + BlockCache: make(map[uint32]string, len(store.blockCache)), + } + + for k, hash := range store.headerIndex { + ss.HeaderIndex[k] = fmt.Sprintf("%x", hash) + } + + for k, header := range store.headerCache { + ss.HeaderCache[header.Blockdata.Height] = fmt.Sprintf("%x", k) + } + for k, block := range store.blockCache { + ss.BlockCache[block.Blockdata.Height] = fmt.Sprintf("%x", k) + } + + return ss + } + +} + +func ExportStoreStatus(store *ChainStore) { + + expvar.Publish("dna_store", expvar.Func(expvarStore(store))) +} diff --git a/net/net.go b/net/net.go index 1248858a..ee9204b2 100644 --- a/net/net.go +++ b/net/net.go @@ -25,5 +25,7 @@ func StartProtocol(pubKey *crypto.PubKey) protocol.Noder { net := node.InitNode(pubKey) net.ConnectSeeds() + node.ExportNodeStatus(net) + return net } diff --git a/net/node/debug.go b/net/node/debug.go new file mode 100644 index 00000000..8c22d95b --- /dev/null +++ b/net/node/debug.go @@ -0,0 +1,68 @@ +package node + +import ( + . "DNA/net/protocol" + "expvar" + "fmt" + "time" +) + +type PeerStatus struct { + State uint32 + FlightHeights []uint32 + LastContact string + TryTimes uint32 + Addr string // The address of the node + Height uint64 // The node latest block height +} + +type TxPoolStatus struct { + TxCount int +} + +type nodeStatus struct { + Id uint64 + TxnCnt uint64 // The transactions be transmit by this node + RxTxnCnt uint64 // The transaction received by this node + PublicKey string + Peers []PeerStatus + Connectings []string + TxPool TxPoolStatus +} + +func expvarNodeInfo(node *node) func() interface{} { + + return func() interface{} { + pbkey, _ := node.publicKey.EncodePoint(true) + + ns := nodeStatus{ + Id: node.id, + TxnCnt: node.txnCnt, + RxTxnCnt: node.rxTxnCnt, + PublicKey: fmt.Sprintf("%x", pbkey), + TxPool: TxPoolStatus{TxCount: node.TXNPool.Len()}, + Connectings: node.ConnectingAddrs, + } + + node.nbrNodes.RLock() + for _, n := range node.nbrNodes.List { + peer := PeerStatus{ + State: n.state, + Height: n.height, + FlightHeights: n.flightHeights, + LastContact: fmt.Sprintf("%gs", float64(time.Now().Sub(n.link.time))/float64(time.Second)), + TryTimes: n.tryTimes, + Addr: fmt.Sprintf("%s:%d", n.link.addr, n.link.port), + } + + ns.Peers = append(ns.Peers, peer) + } + node.nbrNodes.RUnlock() + + return ns + } +} + +func ExportNodeStatus(nd Noder) { + expvar.Publish("dna_node", expvar.Func(expvarNodeInfo(nd.(*node)))) +} diff --git a/net/node/transactionPool.go b/net/node/transactionPool.go index 58378752..1a66c3fb 100644 --- a/net/node/transactionPool.go +++ b/net/node/transactionPool.go @@ -29,6 +29,12 @@ func (this *TXNPool) init() { this.txnList = make(map[common.Uint256]*transaction.Transaction) } +func (this *TXNPool) Len() int { + this.RLock() + defer this.RUnlock() + return len(this.txnList) +} + //append transaction to txnpool when check ok. //1.check transaction. 2.check with ledger(db) 3.check with pool func (this *TXNPool) AppendTxnPool(txn *transaction.Transaction) bool {