|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/bnb-chain/gnfd-qa-test-monitor/abci" |
| 6 | + "github.com/bnb-chain/gnfd-qa-test-monitor/utils" |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 9 | + "github.com/tidwall/gjson" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +type Code uint32 |
| 14 | + |
| 15 | +const ( |
| 16 | + OK Code = iota |
| 17 | + GetBlockHeightErr |
| 18 | + GetObjectTotalCountErr |
| 19 | + GetObjectSealCountErr |
| 20 | + CheckObjectTotalCountErr |
| 21 | + CheckObjectSealCountErr |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + TestNetRpc = "https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org:443" |
| 26 | + MainNetRpc = "https://greenfield-chain.bnbchain.org:443" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + MainNetSpHosts = []string{ |
| 31 | + "greenfield-sp.bnbchain.org", |
| 32 | + "greenfield-sp.defibit.io", |
| 33 | + "greenfield-sp.ninicoin.io", |
| 34 | + "greenfield-sp.nariox.org", |
| 35 | + "greenfield-sp.lumibot.org", |
| 36 | + "greenfield-sp.voltbot.io", |
| 37 | + "greenfield-sp.nodereal.io", |
| 38 | + } |
| 39 | + |
| 40 | + TestNetSpHosts = []string{ |
| 41 | + "gnfd-testnet-sp1.bnbchain.org", |
| 42 | + "gnfd-testnet-sp2.bnbchain.org", |
| 43 | + "gnfd-testnet-sp3.bnbchain.org", |
| 44 | + "gnfd-testnet-sp4.bnbchain.org", |
| 45 | + "gnfd-testnet-sp1.nodereal.io", |
| 46 | + "gnfd-testnet-sp2.nodereal.io", |
| 47 | + "gnfd-testnet-sp3.nodereal.io", |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +type DbShard struct { |
| 52 | + checkEnv string |
| 53 | + checkRpc string |
| 54 | + checkSpHosts []string |
| 55 | + blockMetrics prometheus.Gauge |
| 56 | + spErrCodeMetrics []prometheus.Gauge |
| 57 | +} |
| 58 | + |
| 59 | +func NewCheckDbShard(checkEnv, checkRpc string, checkSpHosts []string) *DbShard { |
| 60 | + checkSpErrCodeMetrics := make([]prometheus.Gauge, len(checkSpHosts)) |
| 61 | + for i, spHost := range checkSpHosts { |
| 62 | + metricsSpHost := strings.Replace(spHost, "-", "_", -1) |
| 63 | + metricsSpHost = strings.Replace(metricsSpHost, ".", "_", -1) |
| 64 | + checkSpErrCodeMetrics[i] = promauto.NewGauge(prometheus.GaugeOpts{Name: fmt.Sprintf("%v_sp_db_shard_error_code_%v", checkEnv, metricsSpHost)}) |
| 65 | + } |
| 66 | + |
| 67 | + return &DbShard{ |
| 68 | + checkEnv: checkEnv, |
| 69 | + checkRpc: checkRpc, |
| 70 | + checkSpHosts: checkSpHosts, |
| 71 | + blockMetrics: promauto.NewGauge(prometheus.GaugeOpts{Name: fmt.Sprintf("%v_sp_db_shard_check_block_height", checkEnv)}), |
| 72 | + spErrCodeMetrics: checkSpErrCodeMetrics, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (s *DbShard) CheckDbShard() { |
| 77 | + lastChainHeight, err := abci.LastBlockHeight(s.checkRpc) |
| 78 | + if err != nil { |
| 79 | + s.blockMetrics.Set(float64(GetBlockHeightErr)) |
| 80 | + return |
| 81 | + } |
| 82 | + calcHeight := lastChainHeight / 3600 * 3600 |
| 83 | + s.blockMetrics.Set(float64(calcHeight)) |
| 84 | + |
| 85 | + objCountArr := make([][]gjson.Result, len(s.checkSpHosts)) |
| 86 | + sealObjCountArr := make([][]gjson.Result, len(s.checkSpHosts)) |
| 87 | + isErr := false |
| 88 | + for i, spHost := range s.checkSpHosts { |
| 89 | + objCount, sealCount, errCode := s.getSpDbData(spHost, calcHeight) |
| 90 | + if errCode != OK { |
| 91 | + s.spErrCodeMetrics[i].Set(float64(errCode)) |
| 92 | + isErr = true |
| 93 | + } |
| 94 | + objCountArr[i] = objCount |
| 95 | + sealObjCountArr[i] = sealCount |
| 96 | + } |
| 97 | + |
| 98 | + if isErr { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + spIndex, errCode := s.checkDbData(objCountArr, sealObjCountArr) |
| 103 | + if errCode != OK { |
| 104 | + s.spErrCodeMetrics[spIndex].Set(float64(errCode)) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + for _, metric := range s.spErrCodeMetrics { |
| 109 | + metric.Set(float64(OK)) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func (s *DbShard) getSpDbData(spHost string, height int64) (objCount, objSealCount []gjson.Result, errCode Code) { |
| 114 | + xmlResult, err := abci.BsDBInfoBlockHeight(spHost, height) |
| 115 | + if err != nil { |
| 116 | + return nil, nil, GetBlockHeightErr |
| 117 | + } |
| 118 | + |
| 119 | + objectResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectTotalCount") |
| 120 | + if objectResString == "" { |
| 121 | + fmt.Printf("ObjectTotalCount error, %v\n", fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height)) |
| 122 | + return nil, nil, GetObjectTotalCountErr |
| 123 | + } else { |
| 124 | + objectTotalCount := gjson.Parse(objectResString).Array() |
| 125 | + objCount = objectTotalCount |
| 126 | + } |
| 127 | + |
| 128 | + objectSealResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectSealCount") |
| 129 | + if objectSealResString == "" { |
| 130 | + fmt.Printf("ObjectSealCount error, %v\n", fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height)) |
| 131 | + return nil, nil, GetObjectSealCountErr |
| 132 | + } else { |
| 133 | + ObjectSealCount := gjson.Parse(objectSealResString).Array() |
| 134 | + objSealCount = ObjectSealCount |
| 135 | + } |
| 136 | + |
| 137 | + return objCount, objSealCount, OK |
| 138 | +} |
| 139 | + |
| 140 | +func (s *DbShard) checkDbData(spObjCounts, spObjSealCounts [][]gjson.Result) (spIndex int, errCode Code) { |
| 141 | + for i := 0; i < 64; i++ { |
| 142 | + sumObject := int64(0) |
| 143 | + sumSp1 := int64(0) |
| 144 | + for _, objectCount := range spObjCounts { |
| 145 | + sumObject = sumObject + objectCount[i].Int() |
| 146 | + sumSp1++ |
| 147 | + } |
| 148 | + sumSealedObject := int64(0) |
| 149 | + sumSp2 := int64(0) |
| 150 | + for _, sealObjectCount := range spObjSealCounts { |
| 151 | + sumSealedObject = sumSealedObject + sealObjectCount[i].Int() |
| 152 | + sumSp2++ |
| 153 | + } |
| 154 | + |
| 155 | + objectAverage := sumObject / sumSp1 |
| 156 | + for spIndex, eachValue := range spObjCounts { |
| 157 | + if objectAverage != eachValue[i].Int() { |
| 158 | + return spIndex, CheckObjectTotalCountErr |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + sealObjectAverage := sumSealedObject / sumSp2 |
| 163 | + for _, eachValue := range spObjSealCounts { |
| 164 | + if sealObjectAverage != eachValue[i].Int() { |
| 165 | + return spIndex, CheckObjectSealCountErr |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return 0, OK |
| 171 | +} |
0 commit comments