Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: precheck server port availability (#3469)

Tianzhou (天舟)e5f1a4fbd1c5
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 217e8e2d10..62795621be 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -4,6 +4,7 @@ package cmd
import (
"context"
"fmt"
+ "net"
"net/http"
"os"
"os/signal"
@@ -234,6 +235,15 @@ func checkCloudBackupFlags() error {
return nil
}
+// Check the port availability by trying to bind and immediately release it.
+func checkPort(port int) error {
+ l, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
+ if err != nil {
+ return err
+ }
+ return l.Close()
+}
+
func start() {
if flags.debug {
log.SetLevel(zap.DebugLevel)
@@ -255,8 +265,29 @@ func start() {
log.Error("invalid flags for cloud backup", zap.Error(err))
return
}
+
profile := activeProfile(flags.dataDir)
+ // The ideal bootstrap order is:
+ // 1. Connect to the metadb
+ // 2. Start echo server
+ // 3. Start various background runners
+ //
+ // Strangely, when the port is unavailable, echo server would return OK response for /healthz
+ // and then complain unable to bind port. Thus we cannot rely on checking /healthz. As a
+ // workaround, we check whether the port is available here.
+ if err := checkPort(flags.port); err != nil {
+ log.Error(fmt.Sprintf("server port %d is not available", flags.port), zap.Error(err))
+ return
+ }
+
+ if profile.UseEmbedDB() {
+ if err := checkPort(profile.DatastorePort); err != nil {
+ log.Error(fmt.Sprintf("database port %d is not available", profile.DatastorePort), zap.Error(err))
+ return
+ }
+ }
+
var s *server.Server
// Setup signal handlers.
ctx, cancel := context.WithCancel(context.Background())
diff --git a/server/config.go b/server/config.go
index 5304f910ef..3d67ba5090 100644
--- a/server/config.go
+++ b/server/config.go
@@ -67,6 +67,7 @@ type Profile struct {
DisableMetric bool
}
-func (prof *Profile) useEmbedDB() bool {
+// UseEmbedDB returns whether to use embedDB.
+func (prof *Profile) UseEmbedDB() bool {
return len(prof.PgURL) == 0
}
diff --git a/server/server.go b/server/server.go
index cde495c82b..4b83890b16 100644
--- a/server/server.go
+++ b/server/server.go
@@ -152,7 +152,7 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {
// Install Postgres.
var pgDataDir string
- if prof.useEmbedDB() {
+ if prof.UseEmbedDB() {
pgDataDir = common.GetPostgresDataDir(prof.DataDir)
}
log.Info("-----Embedded Postgres Config BEGIN-----")
@@ -171,7 +171,7 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {
s.pgInstance = pgInstance
// New MetadataDB instance.
- if prof.useEmbedDB() {
+ if prof.UseEmbedDB() {
s.metaDB = store.NewMetadataDBWithEmbedPg(pgInstance, prof.PgUser, prof.DemoDataDir, prof.Mode)
} else {
s.metaDB = store.NewMetadataDBWithExternalPg(pgInstance, prof.PgURL, prof.DemoDataDir, prof.Mode)
@@ -521,7 +521,7 @@ func (s *Server) Run(ctx context.Context, port int) error {
s.cancel = cancel
if !s.profile.Readonly {
if err := s.TaskScheduler.ClearRunningTasks(ctx); err != nil {
- return errors.Wrap(err, "failed to clear existing RUNNING tasks before start the task scheduler")
+ return errors.Wrap(err, "failed to clear existing RUNNING tasks before starting the task scheduler")
}
// runnerWG waits for all goroutines to complete.
s.runnerWG.Add(1)
@@ -550,8 +550,8 @@ func (s *Server) Run(ctx context.Context, port int) error {
// Shutdown will shut down the server.
func (s *Server) Shutdown(ctx context.Context) error {
- log.Info("Trying to stop Bytebase ....")
- log.Info("Trying to gracefully shutdown server")
+ log.Info("Trying to stop Bytebase...")
+ log.Info("Trying to gracefully shutdown server...")
// Close the metric reporter
if s.MetricReporter != nil {