Skip to content

Commit

Permalink
Merge branch 'refs/heads/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiAbyss committed Aug 14, 2024
2 parents 7bfbae3 + 4ac667d commit 4062ed9
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 86 deletions.
54 changes: 0 additions & 54 deletions .github/workflows/build.yml

This file was deleted.

19 changes: 3 additions & 16 deletions .github/workflows/docker-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ jobs:

- name: Build image
run: |
docker build . \
--label "org.opencontainers.image.source=${IMAGE_SOURCE}" \
--label "org.opencontainers.image.revision=$(git rev-parse HEAD)" \
--label "org.opencontainers.image.version=$(git describe --tags --abbrev=0)" \
-f ./server-distroless.dockerfile -t "${IMAGE_NAME}:server-distroless"
docker build . \
--label "org.opencontainers.image.source=${IMAGE_SOURCE}" \
--label "org.opencontainers.image.revision=$(git rev-parse HEAD)" \
--label "org.opencontainers.image.version=$(git describe --tags --abbrev=0)" \
-f ./syncer-distroless.dockerfile -t "${IMAGE_NAME}:syncer-distroless"
docker build -t "${IMAGE_NAME}:server-monitor" .
- name: Login to GHCR
uses: docker/login-action@v2
Expand All @@ -47,10 +37,7 @@ jobs:
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "master" ] && VERSION=latest
echo IMAGE_NAME=$IMAGE_NAME
echo VERSION=$VERSION
docker tag ${IMAGE_NAME}:server-distroless $IMAGE_NAME:$VERSION-server-distroless
docker tag ${IMAGE_NAME}:syncer-distroless $IMAGE_NAME:$VERSION-syncer-distroless
docker push $IMAGE_NAME:$VERSION-server-distroless
docker push $IMAGE_NAME:$VERSION-syncer-distroless
docker tag ${IMAGE_NAME}:server-monitor $IMAGE_NAME:$VERSION-server-monitor
docker push $IMAGE_NAME:$VERSION-server-monitor
46 changes: 42 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
FROM alpine
FROM golang:1.20-alpine AS builder

WORKDIR /build
RUN apk add --no-cache make git bash protoc

COPY monitor .
ADD . /gnfd-qa-test-monitor

CMD ["./monitor"]
ENV CGO_ENABLED=1
ENV GO111MODULE=on

# For Private REPO
ARG GH_TOKEN=""
RUN go env -w GOPRIVATE="github.com/bnb-chain/*"
RUN git config --global url."https://${GH_TOKEN}@github.com".insteadOf "https://github.com"

RUN apk add --no-cache build-base libc-dev

RUN cd /gnfd-qa-test-monitor \
&& go build -o build/monitor main.go

# Pull greenfield into a second stage deploy alpine container
FROM alpine:3.17

ARG USER=sp
ARG USER_UID=1000
ARG USER_GID=1000

ENV PACKAGES libstdc++ ca-certificates bash curl
ENV WORKDIR=/app

RUN apk add --no-cache $PACKAGES \
&& rm -rf /var/cache/apk/* \
&& addgroup -g ${USER_GID} ${USER} \
&& adduser -u ${USER_UID} -G ${USER} --shell /sbin/nologin --no-create-home -D ${USER} \
&& addgroup ${USER} tty \
&& sed -i -e "s/bin\/sh/bin\/bash/" /etc/passwd

RUN echo "[ ! -z \"\$TERM\" -a -r /etc/motd ] && cat /etc/motd" >> /etc/bash/bashrc

WORKDIR ${WORKDIR}

COPY --from=builder /gnfd-qa-test-monitor/build/* ${WORKDIR}/
RUN chown -R ${USER_UID}:${USER_GID} ${WORKDIR}
USER ${USER_UID}:${USER_GID}

ENTRYPOINT ["/app/monitor"]
40 changes: 40 additions & 0 deletions abci/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package abci

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

func LastBlockHeight(rpc string) (int64, error) {
// https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org/abci_info?last_block_height
url := fmt.Sprintf("%v/abci_info?last_block_height", rpc)
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer utils.CloseBody(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
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)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer utils.CloseBody(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
171 changes: 171 additions & 0 deletions checks/dbshard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package checks

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

type Code uint32

const (
OK Code = iota
GetBlockHeightErr
GetObjectTotalCountErr
GetObjectSealCountErr
CheckObjectTotalCountErr
CheckObjectSealCountErr
)

const (
TestNetRpc = "https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org:443"
MainNetRpc = "https://greenfield-chain.bnbchain.org:443"
)

var (
MainNetSpHosts = []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",
}

TestNetSpHosts = []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 DbShard struct {
checkEnv string
checkRpc string
checkSpHosts []string
blockMetrics prometheus.Gauge
spErrCodeMetrics []prometheus.Gauge
}

func NewCheckDbShard(checkEnv, checkRpc string, checkSpHosts []string) *DbShard {
checkSpErrCodeMetrics := make([]prometheus.Gauge, len(checkSpHosts))
for i, spHost := range checkSpHosts {
metricsSpHost := strings.Replace(spHost, "-", "_", -1)
metricsSpHost = strings.Replace(metricsSpHost, ".", "_", -1)
checkSpErrCodeMetrics[i] = promauto.NewGauge(prometheus.GaugeOpts{Name: fmt.Sprintf("%v_sp_db_shard_error_code_%v", checkEnv, metricsSpHost)})
}

return &DbShard{
checkEnv: checkEnv,
checkRpc: checkRpc,
checkSpHosts: checkSpHosts,
blockMetrics: promauto.NewGauge(prometheus.GaugeOpts{Name: fmt.Sprintf("%v_sp_db_shard_check_block_height", checkEnv)}),
spErrCodeMetrics: checkSpErrCodeMetrics,
}
}

func (s *DbShard) CheckDbShard() {
lastChainHeight, err := abci.LastBlockHeight(s.checkRpc)
if err != nil {
s.blockMetrics.Set(float64(GetBlockHeightErr))
return
}
calcHeight := lastChainHeight / 3600 * 3600
s.blockMetrics.Set(float64(calcHeight))

objCountArr := make([][]gjson.Result, len(s.checkSpHosts))
sealObjCountArr := make([][]gjson.Result, len(s.checkSpHosts))
isErr := false
for i, spHost := range s.checkSpHosts {
objCount, sealCount, errCode := s.getSpDbData(spHost, calcHeight)
if errCode != OK {
s.spErrCodeMetrics[i].Set(float64(errCode))
isErr = true
}
objCountArr[i] = objCount
sealObjCountArr[i] = sealCount
}

if isErr {
return
}

spIndex, errCode := s.checkDbData(objCountArr, sealObjCountArr)
if errCode != OK {
s.spErrCodeMetrics[spIndex].Set(float64(errCode))
return
}

for _, metric := range s.spErrCodeMetrics {
metric.Set(float64(OK))
}
}

func (s *DbShard) getSpDbData(spHost string, height int64) (objCount, objSealCount []gjson.Result, errCode Code) {
xmlResult, err := abci.BsDBInfoBlockHeight(spHost, height)
if err != nil {
return nil, nil, GetBlockHeightErr
}

objectResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectTotalCount")
if objectResString == "" {
fmt.Printf("ObjectTotalCount error, %v\n", fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height))
return nil, nil, GetObjectTotalCountErr
} else {
objectTotalCount := gjson.Parse(objectResString).Array()
objCount = objectTotalCount
}

objectSealResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectSealCount")
if objectSealResString == "" {
fmt.Printf("ObjectSealCount error, %v\n", fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height))
return nil, nil, GetObjectSealCountErr
} else {
ObjectSealCount := gjson.Parse(objectSealResString).Array()
objSealCount = ObjectSealCount
}

return objCount, objSealCount, OK
}

func (s *DbShard) checkDbData(spObjCounts, spObjSealCounts [][]gjson.Result) (spIndex int, errCode Code) {
for i := 0; i < 64; i++ {
sumObject := int64(0)
sumSp1 := int64(0)
for _, objectCount := range spObjCounts {
sumObject = sumObject + objectCount[i].Int()
sumSp1++
}
sumSealedObject := int64(0)
sumSp2 := int64(0)
for _, sealObjectCount := range spObjSealCounts {
sumSealedObject = sumSealedObject + sealObjectCount[i].Int()
sumSp2++
}

objectAverage := sumObject / sumSp1
for spIndex, eachValue := range spObjCounts {
if objectAverage != eachValue[i].Int() {
return spIndex, CheckObjectTotalCountErr
}
}

sealObjectAverage := sumSealedObject / sumSp2
for _, eachValue := range spObjSealCounts {
if sealObjectAverage != eachValue[i].Int() {
return spIndex, CheckObjectSealCountErr
}
}
}

return 0, 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
)
Loading

0 comments on commit 4062ed9

Please sign in to comment.