Skip to content

Commit

Permalink
feat: add node status debug interface (#1295)
Browse files Browse the repository at this point in the history
* feat: add node status debug interface

* feat: refactor code

* fix: ci error
  • Loading branch information
zengchen221 authored Dec 21, 2020
1 parent 63a9785 commit c3d6545
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 35 deletions.
2 changes: 1 addition & 1 deletion chain/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (ls *LedgerService) Init() error {
duration := time.Duration(ls.cfg.DBOptimize.PeriodDay*24) * time.Hour
go ls.clean(duration, ls.cfg.DBOptimize.HeightInterval)
}
go ls.ledgerMonitor(time.Duration(24) * time.Hour)
go ls.ledgerMonitor(time.Duration(1) * time.Hour)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_v10.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func defaultOptimize() *DBOptimize {
PeriodDay: 7,
HeightInterval: 1000,
SyncWriteHeight: 400000,
MaxUsage: 90,
MaxUsage: 95,
}
}
2 changes: 1 addition & 1 deletion config/config_v10_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func defaultOptimize() *DBOptimize {
PeriodDay: 7,
HeightInterval: 1000,
SyncWriteHeight: 0,
MaxUsage: 90,
MaxUsage: 95,
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ require (
github.com/fatih/color v1.10.0
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.4.2
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.2
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/grpc-gateway v1.15.2
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/golang-lru v0.5.4
github.com/influxdata/influxdb1-client v0.0.0-20190809212627-fc22c7df067e
github.com/ipfs/go-log v1.0.4
Expand Down Expand Up @@ -48,7 +48,7 @@ require (
gitlab.com/samli88/go-x11-hash v0.0.0-20180610202919-e5ce9e6dea1c
go.uber.org/atomic v1.7.0
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
google.golang.org/grpc v1.33.2
Expand Down
19 changes: 9 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down Expand Up @@ -224,8 +224,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0 h1:bM6ZAFZmc/wPFaRDi0d5L7hGEZEx/2u+Tmr2evNHDiI=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.15.2 h1:HC+hWRWf+v5zTMPyoaYTKIJih+4sd4XRWmj0qlG87Co=
github.com/grpc-ecosystem/grpc-gateway v1.15.2/go.mod h1:vO11I9oWA+KsxmfFQPhLnnIb1VDE24M+pdxZFiuZcA8=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
github.com/hashicorp/consul v1.4.2/go.mod h1:mFrjN1mfidgJfYP1xrJCF+AfRhr6Eaqhb2+sfyn/OOI=
Expand Down Expand Up @@ -902,8 +902,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -943,8 +942,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0 h1:2mqDk8w/o6UmeUCu5Qiq2y7iMf6anbx+YA8d1JFoFrs=
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -987,6 +985,7 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepx
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1 h1:a/mKvvZr9Jcc8oKfcmgzyp7OwF73JPWsQLvH1z2Kxck=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -1029,6 +1028,7 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbO
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down Expand Up @@ -1063,8 +1063,7 @@ google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.32.0 h1:zWTV+LMdc3kaiJMSTOFz2UgSBgx8RNQoTGiZu3fR9S0=
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
92 changes: 88 additions & 4 deletions rpc/api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.uber.org/zap"

qctx "github.com/qlcchain/go-qlc/chain/context"
"github.com/qlcchain/go-qlc/chain/version"
"github.com/qlcchain/go-qlc/common"
"github.com/qlcchain/go-qlc/common/event"
"github.com/qlcchain/go-qlc/common/storage"
Expand All @@ -25,10 +26,12 @@ import (
)

type DebugApi struct {
ledger ledger.Store
logger *zap.SugaredLogger
eb event.EventBus
feb *event.FeedEventBus
ledger ledger.Store
logger *zap.SugaredLogger
eb event.EventBus
feb *event.FeedEventBus
cc *qctx.ChainContext

cfgFile string
}

Expand All @@ -40,6 +43,7 @@ func NewDebugApi(cfgFile string, eb event.EventBus) *DebugApi {
eb: eb,
feb: cc.FeedEventBus(),
cfgFile: cfgFile,
cc: cc,
}
}

Expand Down Expand Up @@ -806,3 +810,83 @@ func (l *DebugApi) BadgerTableSize(keyPrefixs []storage.KeyPrefix) (int64, error
}
return size, nil
}

type NodeStatus struct {
BuildTime string `json:"buildTime"`
GitRev string `json:"gitRev"`
Version string `json:"version"`
Mode string `json:"mode"`
LedgerStatus *LedgerStatus `json:"ledgerStatus"`
NetStatus *NetStatus `json:"netStatus"`
POVStatus *POVStatus `json:"povStatus"`
}

type LedgerStatus struct {
BlockCount uint64 `json:"blockCount"`
UnCheckedBlockCount uint64 `json:"unCheckedBlockCount"`
}

type NetStatus struct {
PeerId string `json:"peerId"`
ConnectedPeers []*types.PeerInfo `json:"connectedPeers"`
OnlineRepresentatives []types.Address `json:"onlineRepresentatives"`
OnlineRepresentativeInfos *OnlineRepTotal `json:"onlineRepresentativeInfos"`
}

type POVStatus struct {
PovEnabled bool `json:"povEnabled"`
SyncState int `json:"syncState"`
SyncStateStr string `json:"syncStateStr"`
PovLedgerStats *PovLedgerStats `json:"povLedgerStats"`
PovMiningInfo *PovApiGetMiningInfo `json:"povMiningInfo"`
LatestHeader *PovApiHeader `json:"latestHeader"`
}

func (l *DebugApi) NodeStatus() (*NodeStatus, error) {
nodeStatus := new(NodeStatus)
nodeStatus.BuildTime = version.BuildTime
nodeStatus.Mode = version.Mode
nodeStatus.GitRev = version.GitRev
nodeStatus.Version = version.Version
sbCount, err := l.ledger.CountStateBlocks()
if err != nil {
return nil, err
}
unCount, err := l.ledger.CountUncheckedBlocksStore()
if err != nil {
return nil, err
}
nodeStatus.LedgerStatus = &LedgerStatus{
BlockCount: sbCount,
UnCheckedBlockCount: unCount,
}
nodeStatus.NetStatus = new(NetStatus)
cfg, _ := l.cc.Config()
nodeStatus.NetStatus.PeerId = cfg.P2P.ID.PeerID
p := l.cc.GetConnectPeersInfo()
nodeStatus.NetStatus.ConnectedPeers = p
nodeStatus.NetStatus.OnlineRepresentativeInfos = onlineRepsInfo(l.ledger)
as, _ := l.ledger.GetOnlineRepresentations()
nodeStatus.NetStatus.OnlineRepresentatives = as

nodeStatus.POVStatus = new(POVStatus)
povLedger, err := getLedgerStats(l.ledger)
if err != nil {
return nil, err
}
nodeStatus.POVStatus.PovLedgerStats = povLedger
if cfg.PoV.PovEnabled {
if MiningInfo, err := getMiningInfo(l.ledger, l.feb); err != nil {
return nil, err
} else {
nodeStatus.POVStatus.PovMiningInfo = MiningInfo
}
}
nodeStatus.POVStatus.PovEnabled = cfg.PoV.PovEnabled
ss := l.cc.PoVState()
nodeStatus.POVStatus.SyncState = int(ss)
nodeStatus.POVStatus.SyncStateStr = ss.String()
latestHeader, _ := getLatestHeader(l.ledger)
nodeStatus.POVStatus.LatestHeader = latestHeader
return nodeStatus, nil
}
59 changes: 59 additions & 0 deletions rpc/api/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"math/big"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -424,3 +425,61 @@ func TestDebugApi_UncheckBlocks(t *testing.T) {
t.Fatal(err)
}
}

func TestDebugApi_BadgerTableSize(t *testing.T) {
teardownTestCase, _, debugApi := setupDefaultDebugAPI(t)
defer teardownTestCase(t)
prefixs := []storage.KeyPrefix{storage.KeyPrefixBlock}
size, err := debugApi.BadgerTableSize(prefixs)
if err != nil {
t.Fatal(err)
}
t.Log(size)
}

func TestDebugApi_NodeStatus(t *testing.T) {
dir := filepath.Join(config.QlcTestDataDir(), "debug", uuid.New().String())
_ = os.RemoveAll(dir)
cm := config.NewCfgManager(dir)
cfg, _ := cm.Load()
cfg.PoV.PovEnabled = false
cm.Save()

l := ledger.NewLedger(cm.ConfigFile)
cc := qlcchainctx.NewChainContext(cm.ConfigFile)
eb := cc.EventBus()
debugApi := NewDebugApi(cm.ConfigFile, eb)

defer func() {
_ = l.Close()
_ = os.RemoveAll(dir)
}()

address := mock.Address()
ac := mock.AccountMeta(address)
ac.CoinBalance = types.Balance{Int: big.NewInt(int64(20000000000000000))}
ac.CoinVote = types.Balance{Int: big.NewInt(int64(10000000000000000))}
benefit := &types.Benefit{
Vote: ac.CoinVote,
Storage: ac.CoinStorage,
Network: ac.CoinNetwork,
Oracle: ac.CoinOracle,
Balance: ac.CoinBalance,
Total: ac.TotalBalance(),
}
var addrs []*types.Address
addrs = append(addrs, &address)
err := l.SetOnlineRepresentations(addrs)
if err != nil {
t.Fatal(err)
}
err = l.AddRepresentation(address, benefit, l.Cache().GetCache())
if err != nil {
t.Fatal(err)
}
status, err := debugApi.NodeStatus()
if err != nil {
t.Fatal(err)
}
t.Log(status)
}
8 changes: 6 additions & 2 deletions rpc/api/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func (q *NetApi) OnlineRepresentatives() []types.Address {
}

func (q *NetApi) OnlineRepsInfo() *OnlineRepTotal {
as, _ := q.ledger.GetOnlineRepresentations()
return onlineRepsInfo(q.ledger)
}

func onlineRepsInfo(ledger ledger.Store) *OnlineRepTotal {
as, _ := ledger.GetOnlineRepresentations()
if as == nil {
return &OnlineRepTotal{}
}
Expand All @@ -60,7 +64,7 @@ func (q *NetApi) OnlineRepsInfo() *OnlineRepTotal {
minWeight, _ := supply.Div(common.DposVoteDivisor)

for _, account := range as {
weight := q.ledger.Weight(account)
weight := ledger.Weight(account)
oi := &OnlineRepInfo{
Account: account,
Vote: weight,
Expand Down
Loading

0 comments on commit c3d6545

Please sign in to comment.