Skip to content

Commit

Permalink
update check sp db
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiAbyss committed Aug 8, 2024
1 parent c28733c commit 34bb88b
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 64 deletions.
54 changes: 0 additions & 54 deletions .github/workflows/build.yml

This file was deleted.

34 changes: 34 additions & 0 deletions abci/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package abci

import (
"fmt"
"github.com/bnb-chain/gnfd-qa-test-monitor/utils"
"github.com/tidwall/gjson"
"io"
"net/http"
)

func LastBlockHeight() (int64, error) {
url := `https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org/abci_info?last_block_height`
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer utils.CloseBody(resp.Body)
body, err := io.ReadAll(resp.Body)

Check failure on line 18 in abci/abci.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22.x, ubuntu-latest)

ineffectual assignment to err (ineffassign)
result := gjson.GetBytes(body, "result.response.last_block_height")
return result.Int(), nil
}

func BsDBInfoBlockHeight(spHost string, height int64) (string, error) {
// https://gnfd-testnet-sp1.bnbchain.org/?bsdb-info&block_height=11037600
url := fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height)
//fmt.Println(url)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer utils.CloseBody(resp.Body)
body, err := io.ReadAll(resp.Body)

Check failure on line 32 in abci/abci.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22.x, ubuntu-latest)

ineffectual assignment to err (ineffassign)
return string(body), nil
}
108 changes: 108 additions & 0 deletions checks/CheckSpDbObjectNumsPeriod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package checks

import (
"fmt"
"github.com/bnb-chain/gnfd-qa-test-monitor/abci"
"github.com/bnb-chain/gnfd-qa-test-monitor/utils"
"github.com/tidwall/gjson"
)

var SelfMainNetSpHost = []string{
"greenfield-sp.bnbchain.org",
"greenfield-sp.defibit.io",
"greenfield-sp.ninicoin.io",
"greenfield-sp.nariox.org",
"greenfield-sp.lumibot.org",
"greenfield-sp.voltbot.io",
"greenfield-sp.nodereal.io",
}

var SelfTestNetSpHost = []string{
"gnfd-testnet-sp1.bnbchain.org",
"gnfd-testnet-sp2.bnbchain.org",
"gnfd-testnet-sp3.bnbchain.org",
"gnfd-testnet-sp4.bnbchain.org",
"gnfd-testnet-sp1.nodereal.io",
"gnfd-testnet-sp2.nodereal.io",
"gnfd-testnet-sp3.nodereal.io",
}

type Code float64

const (
OK Code = iota
GetObjectTotalCountErr
GetObjectSealCountErr
CheckObjectTotalCountErr
CheckObjectSealCountErr
)

func CheckSpDbObjectNumsPeriod(spHostArray []string) (block int64, errCode Code) {
chainHeight, err := abci.LastBlockHeight()
if err != nil {
fmt.Println(err)
}
fmt.Printf("height: %d\n", chainHeight)
calcHeight := chainHeight / 3600 * 3600
fmt.Printf("calcHeight: %d\n", calcHeight)

resObjectCount := make(map[string][]gjson.Result)
resObjectSealCount := make(map[string][]gjson.Result)

// get everyone sp object count
for _, spHost := range spHostArray {
xmlResult, err := abci.BsDBInfoBlockHeight(spHost, calcHeight)
if err != nil {
fmt.Println(err)
}

objectResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectTotalCount")
if objectResString == "" {
fmt.Printf("sp: %v, ObjectTotalCount error\n", spHost)
return calcHeight, GetObjectTotalCountErr
} else {
ObjectTotalCount := gjson.Parse(objectResString).Array()
resObjectCount[spHost] = ObjectTotalCount
}

objectSealResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectSealCount")
if objectSealResString == "" {
fmt.Printf("sp: %v, ObjectSealCount error\n", spHost)
return calcHeight, GetObjectSealCountErr
} else {
ObjectSealCount := gjson.Parse(objectSealResString).Array()
resObjectSealCount[spHost] = ObjectSealCount
}
}

// check sp object count
for i := 0; i < 64; i++ {
sumObject := int64(0)
sumSp1 := int64(0)
for _, objectCount := range resObjectCount {
sumObject = sumObject + objectCount[i].Int()
sumSp1++
}
sumSealedObject := int64(0)
sumSp2 := int64(0)
for _, sealObjectCount := range resObjectSealCount {
sumSealedObject = sumSealedObject + sealObjectCount[i].Int()
sumSp2++
}

objectAverage := sumObject / sumSp1
sealObjectAverage := sumSealedObject / sumSp2
for _, eachValue := range resObjectCount {
if objectAverage != eachValue[i].Int() {
return calcHeight, CheckObjectTotalCountErr
}
}
for _, eachValue := range resObjectSealCount {
if sealObjectAverage != eachValue[i].Int() {
return calcHeight, CheckObjectSealCountErr
}
}
}

return calcHeight, OK
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ go 1.21
require github.com/prometheus/client_golang v1.17.0

require (
github.com/antchfx/xmlquery v1.3.17
github.com/tidwall/gjson v1.14.2
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

require (
github.com/antchfx/xpath v1.2.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
43 changes: 43 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
github.com/antchfx/xmlquery v1.3.17 h1:d0qWjPp/D+vtRw7ivCwT5ApH/3CkQU8JOeo3245PpTk=
github.com/antchfx/xmlquery v1.3.17/go.mod h1:Afkq4JIeXut75taLSuI31ISJ/zeq+3jG7TunF7noreA=
github.com/antchfx/xpath v1.2.4 h1:dW1HB/JxKvGtJ9WyVGJ0sIoEcqftV3SqIstujI+B9XY=
github.com/antchfx/xpath v1.2.4/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand All @@ -21,9 +27,46 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
Expand Down
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"github.com/bnb-chain/gnfd-qa-test-monitor/checks"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -9,24 +11,26 @@ import (
)

func recordMetrics() {
checkBlock := promauto.NewGauge(prometheus.GaugeOpts{
Name: "sp_db_object_nums_period_check_block_height",
})
checkErrCode := promauto.NewGauge(prometheus.GaugeOpts{
Name: "sp_db_object_nums_period_check_error_code",
})

go func() {
for {
opsProcessed.Inc()
time.Sleep(2 * time.Second)
block, result := checks.CheckSpDbObjectNumsPeriod(checks.SelfTestNetSpHost)
fmt.Printf("block: %v, result: %v \n", block, result)
checkBlock.Set(float64(block))
checkErrCode.Set(float64(result))
time.Sleep(time.Minute * 10)
}
}()
}

var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "myapp_processed_ops_total",
Help: "The total number of processed events",
})
)

func main() {
recordMetrics()

http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(":24367", nil)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions utils/httputils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import (
"github.com/antchfx/xmlquery"
"io"
"strings"
)

func GetXmlPath(res, path string) string {
doc, err := xmlquery.Parse(strings.NewReader(res))
if err != nil {
panic(err)
}
result := ""
if n := doc.SelectElement(path); n != nil {
result = n.InnerText()
}
return result
}

func CloseBody(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}

0 comments on commit 34bb88b

Please sign in to comment.