From c4591fae917ff13c29e3af6a0500f73075f6cc79 Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 19 Oct 2023 16:20:52 +0800 Subject: [PATCH] feat: split the coordinator cron to single process --- coordinator/cmd/{ => api}/app/app.go | 14 +-- coordinator/cmd/{ => api}/app/app_test.go | 0 coordinator/cmd/{ => api}/app/flags.go | 0 coordinator/cmd/{ => api}/app/mock_app.go | 0 coordinator/cmd/api/main.go | 7 ++ coordinator/cmd/cron/app/app.go | 88 +++++++++++++++++++ coordinator/cmd/cron/app/app_test.go | 19 ++++ coordinator/cmd/cron/app/flags.go | 49 +++++++++++ coordinator/cmd/cron/main.go | 7 ++ coordinator/cmd/main.go | 7 -- .../internal/controller/cron/collect_proof.go | 2 +- tests/integration-test/integration_test.go | 2 +- 12 files changed, 176 insertions(+), 19 deletions(-) rename coordinator/cmd/{ => api}/app/app.go (92%) rename coordinator/cmd/{ => api}/app/app_test.go (100%) rename coordinator/cmd/{ => api}/app/flags.go (100%) rename coordinator/cmd/{ => api}/app/mock_app.go (100%) create mode 100644 coordinator/cmd/api/main.go create mode 100644 coordinator/cmd/cron/app/app.go create mode 100644 coordinator/cmd/cron/app/app_test.go create mode 100644 coordinator/cmd/cron/app/flags.go create mode 100644 coordinator/cmd/cron/main.go delete mode 100644 coordinator/cmd/main.go diff --git a/coordinator/cmd/app/app.go b/coordinator/cmd/api/app/app.go similarity index 92% rename from coordinator/cmd/app/app.go rename to coordinator/cmd/api/app/app.go index 16d7b385d2..6586ca3421 100644 --- a/coordinator/cmd/app/app.go +++ b/coordinator/cmd/api/app/app.go @@ -22,7 +22,6 @@ import ( "scroll-tech/coordinator/internal/config" "scroll-tech/coordinator/internal/controller/api" - "scroll-tech/coordinator/internal/controller/cron" "scroll-tech/coordinator/internal/route" ) @@ -51,28 +50,23 @@ func action(ctx *cli.Context) error { log.Crit("failed to load config file", "config file", cfgFile, "error", err) } - subCtx, cancel := context.WithCancel(ctx.Context) db, err := database.InitDB(cfg.DB) if err != nil { log.Crit("failed to init db connection", "err", err) } - - registry := prometheus.DefaultRegisterer - observability.Server(ctx, db) - - proofCollector := cron.NewCollector(subCtx, db, cfg, registry) defer func() { - proofCollector.Stop() - cancel() if err = database.CloseDB(db); err != nil { log.Error("can not close db connection", "error", err) } }() + registry := prometheus.DefaultRegisterer + observability.Server(ctx, db) + apiSrv := apiServer(ctx, cfg, db, registry) log.Info( - "coordinator start successfully", + "Start coordinator successfully.", "version", version.Version, ) diff --git a/coordinator/cmd/app/app_test.go b/coordinator/cmd/api/app/app_test.go similarity index 100% rename from coordinator/cmd/app/app_test.go rename to coordinator/cmd/api/app/app_test.go diff --git a/coordinator/cmd/app/flags.go b/coordinator/cmd/api/app/flags.go similarity index 100% rename from coordinator/cmd/app/flags.go rename to coordinator/cmd/api/app/flags.go diff --git a/coordinator/cmd/app/mock_app.go b/coordinator/cmd/api/app/mock_app.go similarity index 100% rename from coordinator/cmd/app/mock_app.go rename to coordinator/cmd/api/app/mock_app.go diff --git a/coordinator/cmd/api/main.go b/coordinator/cmd/api/main.go new file mode 100644 index 0000000000..9b4c40be42 --- /dev/null +++ b/coordinator/cmd/api/main.go @@ -0,0 +1,7 @@ +package main + +import "scroll-tech/coordinator/cmd/api/app" + +func main() { + app.Run() +} diff --git a/coordinator/cmd/cron/app/app.go b/coordinator/cmd/cron/app/app.go new file mode 100644 index 0000000000..20f61784b9 --- /dev/null +++ b/coordinator/cmd/cron/app/app.go @@ -0,0 +1,88 @@ +package app + +import ( + "context" + "fmt" + "os" + "os/signal" + + "github.com/prometheus/client_golang/prometheus" + "github.com/scroll-tech/go-ethereum/log" + "github.com/urfave/cli/v2" + + "scroll-tech/common/database" + "scroll-tech/common/observability" + "scroll-tech/common/utils" + "scroll-tech/common/version" + + "scroll-tech/coordinator/internal/config" + "scroll-tech/coordinator/internal/controller/cron" +) + +var app *cli.App + +func init() { + // Set up coordinator app info. + app = cli.NewApp() + app.Action = action + app.Name = "coordinator cron" + app.Usage = "The Scroll L2 Coordinator cron" + app.Version = version.Version + app.Flags = append(app.Flags, utils.CommonFlags...) + app.Flags = append(app.Flags, apiFlags...) + app.Before = func(ctx *cli.Context) error { + return utils.LogSetup(ctx) + } + // Register `coordinator-cron-test` app for integration-cron-test. + utils.RegisterSimulation(app, utils.CoordinatorCron) +} + +func action(ctx *cli.Context) error { + cfgFile := ctx.String(utils.ConfigFileFlag.Name) + cfg, err := config.NewConfig(cfgFile) + if err != nil { + log.Crit("failed to load config file", "config file", cfgFile, "error", err) + } + + subCtx, cancel := context.WithCancel(ctx.Context) + db, err := database.InitDB(cfg.DB) + if err != nil { + log.Crit("failed to init db connection", "err", err) + } + + registry := prometheus.DefaultRegisterer + observability.Server(ctx, db) + + proofCollector := cron.NewCollector(subCtx, db, cfg, registry) + defer func() { + proofCollector.Stop() + cancel() + if err = database.CloseDB(db); err != nil { + log.Error("can not close db connection", "error", err) + } + }() + + log.Info( + "coordinator cron start successfully", + "version", version.Version, + ) + + // Catch CTRL-C to ensure a graceful shutdown. + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + + // Wait until the interrupt signal is received from an OS signal. + <-interrupt + + log.Info("coordinator cron exiting success") + return nil +} + +// Run coordinator. +func Run() { + // RunApp the coordinator. + if err := app.Run(os.Args); err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/coordinator/cmd/cron/app/app_test.go b/coordinator/cmd/cron/app/app_test.go new file mode 100644 index 0000000000..ee22e37d4e --- /dev/null +++ b/coordinator/cmd/cron/app/app_test.go @@ -0,0 +1,19 @@ +package app + +import ( + "fmt" + "testing" + "time" + + "scroll-tech/common/cmd" + "scroll-tech/common/version" +) + +func TestRunCoordinatorCron(t *testing.T) { + coordinator := cmd.NewCmd("coordinator-cron-test", "--version") + defer coordinator.WaitExit() + + // wait result + coordinator.ExpectWithTimeout(t, true, time.Second*3, fmt.Sprintf("coordinator version %s", version.Version)) + coordinator.RunApp(nil) +} diff --git a/coordinator/cmd/cron/app/flags.go b/coordinator/cmd/cron/app/flags.go new file mode 100644 index 0000000000..68ca985e8a --- /dev/null +++ b/coordinator/cmd/cron/app/flags.go @@ -0,0 +1,49 @@ +package app + +import "github.com/urfave/cli/v2" + +var ( + apiFlags = []cli.Flag{ + // http flags + &httpEnabledFlag, + &httpListenAddrFlag, + &httpPortFlag, + // ws flags + &wsEnabledFlag, + &wsListenAddrFlag, + &wsPortFlag, + } + // httpEnabledFlag enable rpc server. + httpEnabledFlag = cli.BoolFlag{ + Name: "http", + Usage: "Enable the HTTP-RPC server", + Value: false, + } + // httpListenAddrFlag set the http address. + httpListenAddrFlag = cli.StringFlag{ + Name: "http.addr", + Usage: "HTTP-RPC server listening interface", + Value: "localhost", + } + // httpPortFlag set http.port. + httpPortFlag = cli.IntFlag{ + Name: "http.port", + Usage: "HTTP-RPC server listening port", + Value: 8390, + } + wsEnabledFlag = cli.BoolFlag{ + Name: "ws", + Usage: "Enable the WS-RPC server", + } + wsListenAddrFlag = cli.StringFlag{ + Name: "ws.addr", + Usage: "WS-RPC server listening interface", + Value: "localhost", + } + // websocket port + wsPortFlag = cli.IntFlag{ + Name: "ws.port", + Usage: "WS-RPC server listening port", + Value: 8391, + } +) diff --git a/coordinator/cmd/cron/main.go b/coordinator/cmd/cron/main.go new file mode 100644 index 0000000000..512aff4ad8 --- /dev/null +++ b/coordinator/cmd/cron/main.go @@ -0,0 +1,7 @@ +package main + +import "scroll-tech/coordinator/cmd/cron/app" + +func main() { + app.Run() +} diff --git a/coordinator/cmd/main.go b/coordinator/cmd/main.go deleted file mode 100644 index 29a0632ca5..0000000000 --- a/coordinator/cmd/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "scroll-tech/coordinator/cmd/app" - -func main() { - app.Run() -} diff --git a/coordinator/internal/controller/cron/collect_proof.go b/coordinator/internal/controller/cron/collect_proof.go index b27f6a6a4f..734995846b 100644 --- a/coordinator/internal/controller/cron/collect_proof.go +++ b/coordinator/internal/controller/cron/collect_proof.go @@ -76,7 +76,7 @@ func NewCollector(ctx context.Context, db *gorm.DB, cfg *config.Config, reg prom go c.checkBatchAllChunkReady() go c.cleanupChallenge() - log.Info("Start coordinator successfully.") + log.Info("Start coordinator cron successfully.") return c } diff --git a/tests/integration-test/integration_test.go b/tests/integration-test/integration_test.go index 996b04c0ec..e15a5cccba 100644 --- a/tests/integration-test/integration_test.go +++ b/tests/integration-test/integration_test.go @@ -17,7 +17,7 @@ import ( "scroll-tech/database/migrate" - capp "scroll-tech/coordinator/cmd/app" + capp "scroll-tech/coordinator/cmd/api/app" "scroll-tech/common/database" "scroll-tech/common/docker"