Skip to content

Commit

Permalink
Merge pull request #234 from bnb-chain/develop
Browse files Browse the repository at this point in the history
release: prepare for v0.0.5
  • Loading branch information
unclezoro authored Oct 28, 2022
2 parents 45dd937 + a0c984e commit 7821959
Show file tree
Hide file tree
Showing 84 changed files with 1,723 additions and 435 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ jobs:
make docker-image
sudo mkdir -p ./deployment/.zkbnb
sudo cp /server/test.keyfile/* ./deployment/.zkbnb
source <(sudo cat /server/.env)
blockNr=$(sudo bash ./deployment/tool/tool.sh blockHeight)
sudo bash ./deployment/tool/tool.sh all
sudo BSC_TESTNET_PRIVATE_KEY=${BSC_TESTNET_PRIVATE_KEY} bash ./deployment/tool/tool.sh all
sudo bash ./deployment/docker-compose/docker-compose.sh down
sudo bash ./deployment/docker-compose/docker-compose.sh up $blockNr
echo "Contract addresses"
Expand Down Expand Up @@ -68,7 +69,7 @@ jobs:
go test -v -failfast -parallel 2 -run TestL.*Suite -timeout 30m
- name: notification via slack
if: always()
if: cancelled() == false
run: |
export SLACK_WEBHOOK_URL=`sudo cat /home/ec2-user/actions-runner/slack-config.json | jq -r '.slack'`
export JOB_STATUS=${{ job.status }}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IMAGE_NAME=ghcr.io/bnb-chain/zkbnb
API_SERVER = ./service/apiserver

api-server:
cd $(API_SERVER) && ${GOBIN}/goctl api go -api server.api -dir .;
cd $(API_SERVER) && goctl api go -api server.api -dir .;
@echo "Done generate server api";

deploy:
Expand Down
30 changes: 30 additions & 0 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,34 @@ var (
Value: 1000,
Usage: "batch size for reading history record from the database",
}
PProfEnabledFlag = &cli.BoolFlag{
Name: "pprof",
Value: false,
Usage: "Enable the pprof HTTP server",
}
PProfPortFlag = &cli.IntFlag{
Name: "pprof.port",
Usage: "pprof HTTP server listening port",
Value: 6060,
}
PProfAddrFlag = &cli.StringFlag{
Name: "pprof.addr",
Usage: "pprof HTTP server listening interface",
Value: "127.0.0.1",
}
MetricsEnabledFlag = &cli.BoolFlag{
Name: "metrics",
Value: false,
Usage: "Enable metrics collection and reporting",
}
MetricsHTTPFlag = &cli.StringFlag{
Name: "metrics.addr",
Usage: "Enable stand-alone metrics HTTP server listening interface",
Value: "127.0.0.1",
}
MetricsPortFlag = &cli.IntFlag{
Name: "metrics.port",
Usage: "Metrics HTTP server listening port",
Value: 6060,
}
)
113 changes: 107 additions & 6 deletions cmd/zkbnb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import (
"runtime"

"github.com/urfave/cli/v2"
"github.com/zeromicro/go-zero/core/logx"

"github.com/bnb-chain/zkbnb/cmd/flags"
"github.com/bnb-chain/zkbnb/common/metrics"
pprofServer "github.com/bnb-chain/zkbnb/common/metrics/pprof"
prometheusServer "github.com/bnb-chain/zkbnb/common/metrics/prometheus"
"github.com/bnb-chain/zkbnb/service/apiserver"
"github.com/bnb-chain/zkbnb/service/committer"
"github.com/bnb-chain/zkbnb/service/fullnode"
"github.com/bnb-chain/zkbnb/service/monitor"
"github.com/bnb-chain/zkbnb/service/prover"
"github.com/bnb-chain/zkbnb/service/sender"
"github.com/bnb-chain/zkbnb/service/witness"
"github.com/bnb-chain/zkbnb/tools/dbinitializer"
"github.com/bnb-chain/zkbnb/tools/recovery"

"net/http"
)

// Build Info (set via linker flags)
Expand Down Expand Up @@ -47,12 +54,18 @@ func main() {
Usage: "Run prover service",
Flags: []cli.Flag{
flags.ConfigFlag,
flags.MetricsEnabledFlag,
flags.MetricsHTTPFlag,
flags.MetricsPortFlag,
flags.PProfEnabledFlag,
flags.PProfAddrFlag,
flags.PProfPortFlag,
},
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

startMetricsServer(cCtx)
return prover.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
Expand All @@ -61,12 +74,18 @@ func main() {
Usage: "Run witness service",
Flags: []cli.Flag{
flags.ConfigFlag,
flags.MetricsEnabledFlag,
flags.MetricsHTTPFlag,
flags.MetricsPortFlag,
flags.PProfEnabledFlag,
flags.PProfAddrFlag,
flags.PProfPortFlag,
},
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

startMetricsServer(cCtx)
return witness.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
Expand All @@ -75,40 +94,72 @@ func main() {
Usage: "Run monitor service",
Flags: []cli.Flag{
flags.ConfigFlag,
flags.MetricsEnabledFlag,
flags.MetricsHTTPFlag,
flags.MetricsPortFlag,
flags.PProfEnabledFlag,
flags.PProfAddrFlag,
flags.PProfPortFlag,
},
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

startMetricsServer(cCtx)
return monitor.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
{
Name: "committer",
Flags: []cli.Flag{
flags.ConfigFlag,
flags.MetricsEnabledFlag,
flags.MetricsHTTPFlag,
flags.MetricsPortFlag,
flags.PProfEnabledFlag,
flags.PProfAddrFlag,
flags.PProfPortFlag,
},
Usage: "Run committer service",
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

startMetricsServer(cCtx)
return committer.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
{
Name: "fullnode",
Flags: []cli.Flag{
flags.ConfigFlag,
},
Usage: "Run fullnode service",
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

return fullnode.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
{
Name: "sender",
Usage: "Run sender service",
Flags: []cli.Flag{
flags.ConfigFlag,
flags.MetricsEnabledFlag,
flags.MetricsHTTPFlag,
flags.MetricsPortFlag,
flags.PProfEnabledFlag,
flags.PProfAddrFlag,
flags.PProfPortFlag,
},
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

startMetricsServer(cCtx)
return sender.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
Expand All @@ -117,12 +168,18 @@ func main() {
Usage: "Run apiserver service",
Flags: []cli.Flag{
flags.ConfigFlag,
flags.MetricsEnabledFlag,
flags.MetricsHTTPFlag,
flags.MetricsPortFlag,
flags.PProfEnabledFlag,
flags.PProfAddrFlag,
flags.PProfPortFlag,
},
Action: func(cCtx *cli.Context) error {
if !cCtx.IsSet(flags.ConfigFlag.Name) {
return cli.ShowSubcommandHelp(cCtx)
}

startMetricsServer(cCtx)
return apiserver.Run(cCtx.String(flags.ConfigFlag.Name))
},
},
Expand Down Expand Up @@ -188,7 +245,51 @@ func main() {
},
},
}

if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}

func startMetricsServer(ctx *cli.Context) {
if !ctx.Bool(flags.PProfEnabledFlag.Name) &&
!ctx.Bool(flags.MetricsEnabledFlag.Name) {
return
}

pprofAddress := fmt.Sprintf("%s:%d",
ctx.String(flags.PProfAddrFlag.Name),
ctx.Int(flags.PProfPortFlag.Name))
metricsAddress := fmt.Sprintf("%s:%d",
ctx.String(flags.MetricsHTTPFlag.Name),
ctx.Int(flags.MetricsPortFlag.Name))

pprofMux := metrics.NewRunOnceHttpMux(http.NewServeMux())
metricsMux := metrics.NewRunOnceHttpMux(http.NewServeMux())
if ctx.Bool(flags.PProfEnabledFlag.Name) && ctx.Bool(flags.MetricsEnabledFlag.Name) &&
metricsAddress == pprofAddress {
// point to the same endpoint
pprofMux = metricsMux
}

pprofServer := pprofServer.NewPProfServer(pprofMux, pprofAddress)
prometheusServer := prometheusServer.NewPrometheusServer(metricsMux, metricsAddress)

if ctx.Bool(flags.MetricsEnabledFlag.Name) {
go func() {
logx.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", pprofAddress))
if err := pprofServer.Start(); err != nil {
logx.Error("Failure in running pprof server", "err", err)
}
}()
}

if ctx.Bool(flags.MetricsEnabledFlag.Name) {
go func() {
logx.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/debug/metrics", metricsAddress))
if err := prometheusServer.Start(); err != nil {
logx.Error("Failure in running metrics server", "err", err)
}
}()
}
}
42 changes: 42 additions & 0 deletions common/gopool/gopool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gopool

import (
"time"

"github.com/panjf2000/ants/v2"
)

var (
// Init a instance pool when importing ants.
defaultPool, _ = ants.NewPool(ants.DefaultAntsPoolSize, ants.WithExpiryDuration(10*time.Second))
)

// Submit submits a task to pool.
func Submit(task func()) error {
return defaultPool.Submit(task)
}

// Running returns the number of the currently running goroutines.
func Running() int {
return defaultPool.Running()
}

// Cap returns the capacity of this default pool.
func Cap() int {
return defaultPool.Cap()
}

// Free returns the available goroutines to work.
func Free() int {
return defaultPool.Free()
}

// Release Closes the default pool.
func Release() {
defaultPool.Release()
}

// Reboot reboots the default pool.
func Reboot() {
defaultPool.Reboot()
}
31 changes: 31 additions & 0 deletions common/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package metrics

import (
"net/http"
"sync"
)

func NewRunOnceHttpMux(mux *http.ServeMux) *RunOnceHttpMux {
return &RunOnceHttpMux{
ServeMux: mux,
once: &sync.Once{},
}
}

type RunOnceHttpMux struct {
*http.ServeMux
once *sync.Once
}

func (mux *RunOnceHttpMux) ListenAndServe(addr string) error {
var err error
mux.once.Do(func() {
err = http.ListenAndServe(addr, mux.ServeMux)
})

return err
}

type MetricsServer interface {
Start() error
}
31 changes: 31 additions & 0 deletions common/metrics/pprof/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pprof

import (
"net/http/pprof"

"github.com/bnb-chain/zkbnb/common/metrics"
)

var _ metrics.MetricsServer = (*PProfServer)(nil)

func NewPProfServer(srv *metrics.RunOnceHttpMux, addr string) metrics.MetricsServer {
srv.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
srv.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
srv.Handle("/debug/pprof/heap", pprof.Handler("heap"))
srv.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
srv.Handle("/debug/pprof/block", pprof.Handler("block"))
srv.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
return &PProfServer{
srv: srv,
addr: addr,
}
}

type PProfServer struct {
srv *metrics.RunOnceHttpMux
addr string
}

func (server *PProfServer) Start() error {
return server.srv.ListenAndServe(server.addr)
}
Loading

0 comments on commit 7821959

Please sign in to comment.