Skip to content

Commit 4062ed9

Browse files
committed
Merge branch 'refs/heads/develop'
2 parents 7bfbae3 + 4ac667d commit 4062ed9

File tree

9 files changed

+342
-86
lines changed

9 files changed

+342
-86
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 54 deletions
This file was deleted.

.github/workflows/docker-release.yml

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ jobs:
2121

2222
- name: Build image
2323
run: |
24-
docker build . \
25-
--label "org.opencontainers.image.source=${IMAGE_SOURCE}" \
26-
--label "org.opencontainers.image.revision=$(git rev-parse HEAD)" \
27-
--label "org.opencontainers.image.version=$(git describe --tags --abbrev=0)" \
28-
-f ./server-distroless.dockerfile -t "${IMAGE_NAME}:server-distroless"
29-
30-
docker build . \
31-
--label "org.opencontainers.image.source=${IMAGE_SOURCE}" \
32-
--label "org.opencontainers.image.revision=$(git rev-parse HEAD)" \
33-
--label "org.opencontainers.image.version=$(git describe --tags --abbrev=0)" \
34-
-f ./syncer-distroless.dockerfile -t "${IMAGE_NAME}:syncer-distroless"
24+
docker build -t "${IMAGE_NAME}:server-monitor" .
3525
3626
- name: Login to GHCR
3727
uses: docker/login-action@v2
@@ -47,10 +37,7 @@ jobs:
4737
# Strip "v" prefix from tag name
4838
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
4939
# Use Docker `latest` tag convention
50-
[ "$VERSION" == "master" ] && VERSION=latest
5140
echo IMAGE_NAME=$IMAGE_NAME
5241
echo VERSION=$VERSION
53-
docker tag ${IMAGE_NAME}:server-distroless $IMAGE_NAME:$VERSION-server-distroless
54-
docker tag ${IMAGE_NAME}:syncer-distroless $IMAGE_NAME:$VERSION-syncer-distroless
55-
docker push $IMAGE_NAME:$VERSION-server-distroless
56-
docker push $IMAGE_NAME:$VERSION-syncer-distroless
42+
docker tag ${IMAGE_NAME}:server-monitor $IMAGE_NAME:$VERSION-server-monitor
43+
docker push $IMAGE_NAME:$VERSION-server-monitor

Dockerfile

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
1-
FROM alpine
1+
FROM golang:1.20-alpine AS builder
22

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

5-
COPY monitor .
5+
ADD . /gnfd-qa-test-monitor
66

7-
CMD ["./monitor"]
7+
ENV CGO_ENABLED=1
8+
ENV GO111MODULE=on
9+
10+
# For Private REPO
11+
ARG GH_TOKEN=""
12+
RUN go env -w GOPRIVATE="github.com/bnb-chain/*"
13+
RUN git config --global url."https://${GH_TOKEN}@github.com".insteadOf "https://github.com"
14+
15+
RUN apk add --no-cache build-base libc-dev
16+
17+
RUN cd /gnfd-qa-test-monitor \
18+
&& go build -o build/monitor main.go
19+
20+
# Pull greenfield into a second stage deploy alpine container
21+
FROM alpine:3.17
22+
23+
ARG USER=sp
24+
ARG USER_UID=1000
25+
ARG USER_GID=1000
26+
27+
ENV PACKAGES libstdc++ ca-certificates bash curl
28+
ENV WORKDIR=/app
29+
30+
RUN apk add --no-cache $PACKAGES \
31+
&& rm -rf /var/cache/apk/* \
32+
&& addgroup -g ${USER_GID} ${USER} \
33+
&& adduser -u ${USER_UID} -G ${USER} --shell /sbin/nologin --no-create-home -D ${USER} \
34+
&& addgroup ${USER} tty \
35+
&& sed -i -e "s/bin\/sh/bin\/bash/" /etc/passwd
36+
37+
RUN echo "[ ! -z \"\$TERM\" -a -r /etc/motd ] && cat /etc/motd" >> /etc/bash/bashrc
38+
39+
WORKDIR ${WORKDIR}
40+
41+
COPY --from=builder /gnfd-qa-test-monitor/build/* ${WORKDIR}/
42+
RUN chown -R ${USER_UID}:${USER_GID} ${WORKDIR}
43+
USER ${USER_UID}:${USER_GID}
44+
45+
ENTRYPOINT ["/app/monitor"]

abci/abci.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package abci
2+
3+
import (
4+
"fmt"
5+
"github.com/bnb-chain/gnfd-qa-test-monitor/utils"
6+
"github.com/tidwall/gjson"
7+
"io"
8+
"net/http"
9+
)
10+
11+
func LastBlockHeight(rpc string) (int64, error) {
12+
// https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org/abci_info?last_block_height
13+
url := fmt.Sprintf("%v/abci_info?last_block_height", rpc)
14+
resp, err := http.Get(url)
15+
if err != nil {
16+
return 0, err
17+
}
18+
defer utils.CloseBody(resp.Body)
19+
body, err := io.ReadAll(resp.Body)
20+
if err != nil {
21+
return 0, err
22+
}
23+
result := gjson.GetBytes(body, "result.response.last_block_height")
24+
return result.Int(), nil
25+
}
26+
27+
func BsDBInfoBlockHeight(spHost string, height int64) (string, error) {
28+
// https://gnfd-testnet-sp1.bnbchain.org/?bsdb-info&block_height=11037600
29+
url := fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height)
30+
resp, err := http.Get(url)
31+
if err != nil {
32+
return "", err
33+
}
34+
defer utils.CloseBody(resp.Body)
35+
body, err := io.ReadAll(resp.Body)
36+
if err != nil {
37+
return "", err
38+
}
39+
return string(body), nil
40+
}

checks/dbshard.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ go 1.21
55
require github.com/prometheus/client_golang v1.17.0
66

77
require (
8+
github.com/antchfx/xmlquery v1.3.17
9+
github.com/tidwall/gjson v1.14.2
10+
github.com/tidwall/match v1.1.1 // indirect
11+
github.com/tidwall/pretty v1.2.0 // indirect
12+
)
13+
14+
require (
15+
github.com/antchfx/xpath v1.2.4 // indirect
816
github.com/beorn7/perks v1.0.1 // indirect
917
github.com/cespare/xxhash/v2 v2.2.0 // indirect
18+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
1019
github.com/golang/protobuf v1.5.3 // indirect
1120
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
1221
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
1322
github.com/prometheus/common v0.44.0 // indirect
1423
github.com/prometheus/procfs v0.11.1 // indirect
24+
golang.org/x/net v0.10.0 // indirect
1525
golang.org/x/sys v0.11.0 // indirect
26+
golang.org/x/text v0.9.0 // indirect
1627
google.golang.org/protobuf v1.31.0 // indirect
1728
)

0 commit comments

Comments
 (0)