Skip to content

Commit

Permalink
Update default Debezium Server HTTP port to use random freeport inste…
Browse files Browse the repository at this point in the history
…ad of 8080 (#1691)

- Fetching free port dynamically by connecting to Port 0. Generally it returns 5 digit port numbers so there shouldn't be conflicts with standard ports like 5432, 8080 etc
  • Loading branch information
sanyamsinghal authored Nov 28, 2024
1 parent 8731fc1 commit 1507df2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions yb-voyager/src/dbzm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Config struct {
var baseConfigTemplate = `
debezium.format.value=connect
debezium.format.key=connect
quarkus.http.port=%d
quarkus.log.console.json=false
quarkus.log.level=%s
`
Expand Down Expand Up @@ -295,10 +296,19 @@ func (c *Config) String() string {
} else {
log.Infof("QUEUE_SEGMENT_MAX_BYTES: %d", queueSegmentMaxBytes)
}

quarkusLogPort, err := utils.GetFreePort()
if err != nil {
log.Warnf("failed to get a free port for quarkus http server, falling back to 8080: %v", err)
quarkusLogPort = 8080
}
log.Infof("using port number %d for quarkus http server", quarkusLogPort)

var conf string
switch c.SourceDBType {
case "postgresql":
conf = fmt.Sprintf(postgresConfigTemplate,
quarkusLogPort,
c.LogLevel,
c.Username,
c.SnapshotMode,
Expand Down Expand Up @@ -332,6 +342,7 @@ func (c *Config) String() string {
case "yugabytedb":
if !c.UseYBgRPCConnector {
conf = fmt.Sprintf(yugabyteLogicalReplicationConfigTemplate,
quarkusLogPort,
c.LogLevel,
c.Username,
"never",
Expand Down Expand Up @@ -359,6 +370,7 @@ func (c *Config) String() string {
}
} else {
conf = fmt.Sprintf(yugabyteConfigTemplate,
quarkusLogPort,
c.LogLevel,
c.Username,
"never",
Expand Down Expand Up @@ -391,6 +403,7 @@ func (c *Config) String() string {
}
case "oracle":
conf = fmt.Sprintf(oracleConfigTemplate,
quarkusLogPort,
c.LogLevel,
c.Username,
c.SnapshotMode,
Expand Down Expand Up @@ -422,6 +435,7 @@ func (c *Config) String() string {

case "mysql":
conf = fmt.Sprintf(mysqlConfigTemplate,
quarkusLogPort,
c.LogLevel,
c.Username,
c.SnapshotMode,
Expand Down
14 changes: 14 additions & 0 deletions yb-voyager/src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,17 @@ func ChangeFileExtension(filePath string, newExt string) string {

return filePath + newExt
}

// Port 0 generally returns port number in range 30xxx - 60xxx but it also depends on OS and network configuration
func GetFreePort() (int, error) {
// Listen on port 0, which tells the OS to assign an available port
listener, err := net.Listen("tcp", ":0")
if err != nil {
return 0, fmt.Errorf("failed to listen on a port: %v", err)
}
defer listener.Close()

// Retrieve the assigned port
addr := listener.Addr().(*net.TCPAddr)
return addr.Port, nil
}

0 comments on commit 1507df2

Please sign in to comment.