Skip to content

Commit cfa6f95

Browse files
committed
Merge pull request sorintlab#549 from sgotti/always_use_logger_also_for_initial_checks
*: always use logger also for initial checks
2 parents bff2bea + 7ba8efd commit cfa6f95

File tree

3 files changed

+41
-89
lines changed

3 files changed

+41
-89
lines changed

cmd/keeper/cmd/keeper.go

+30-45
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ var cfg config
113113
func init() {
114114
user, err := util.GetUser()
115115
if err != nil {
116-
die("cannot get current user: %v", err)
116+
log.Fatalf("cannot get current user: %v", err)
117117
}
118118

119119
cmd.AddCommonFlags(CmdKeeper, &cfg.CommonConfig)
@@ -150,21 +150,6 @@ var managedPGParameters = []string{
150150
"synchronous_standby_names",
151151
}
152152

153-
func stderr(format string, a ...interface{}) {
154-
out := fmt.Sprintf(format, a...)
155-
fmt.Fprintln(os.Stderr, strings.TrimSuffix(out, "\n"))
156-
}
157-
158-
func stdout(format string, a ...interface{}) {
159-
out := fmt.Sprintf(format, a...)
160-
fmt.Fprintln(os.Stdout, strings.TrimSuffix(out, "\n"))
161-
}
162-
163-
func die(format string, a ...interface{}) {
164-
stderr(format, a...)
165-
os.Exit(1)
166-
}
167-
168153
func readPasswordFromFile(filepath string) (string, error) {
169154
fi, err := os.Lstat(filepath)
170155
if err != nil {
@@ -485,7 +470,7 @@ func NewPostgresKeeper(cfg *config, end chan error) (*PostgresKeeper, error) {
485470
return nil, fmt.Errorf("failed to load keeper local state file: %v", err)
486471
}
487472
if p.keeperLocalState.UID != "" && p.cfg.uid != "" && p.keeperLocalState.UID != p.cfg.uid {
488-
die("saved uid %q differs from configuration uid: %q", p.keeperLocalState.UID, cfg.uid)
473+
log.Fatalf("saved uid %q differs from configuration uid: %q", p.keeperLocalState.UID, cfg.uid)
489474
}
490475
if p.keeperLocalState.UID == "" {
491476
p.keeperLocalState.UID = cfg.uid
@@ -494,7 +479,7 @@ func NewPostgresKeeper(cfg *config, end chan error) (*PostgresKeeper, error) {
494479
log.Infow("uid generated", "uid", p.keeperLocalState.UID)
495480
}
496481
if err = p.saveKeeperLocalState(); err != nil {
497-
die("error: %v", err)
482+
log.Fatalf("error: %v", err)
498483
}
499484
}
500485

@@ -1737,7 +1722,7 @@ func keeper(c *cobra.Command, args []string) {
17371722
case "debug":
17381723
slog.SetLevel(zap.DebugLevel)
17391724
default:
1740-
die("invalid log level: %v", cfg.LogLevel)
1725+
log.Fatalf("invalid log level: %v", cfg.LogLevel)
17411726
}
17421727
if cfg.debug {
17431728
slog.SetDebug()
@@ -1748,19 +1733,19 @@ func keeper(c *cobra.Command, args []string) {
17481733
}
17491734

17501735
if cfg.dataDir == "" {
1751-
die("data dir required")
1736+
log.Fatalf("data dir required")
17521737
}
17531738

17541739
if err = cmd.CheckCommonConfig(&cfg.CommonConfig); err != nil {
1755-
die(err.Error())
1740+
log.Fatalf(err.Error())
17561741
}
17571742

17581743
if err = os.MkdirAll(cfg.dataDir, 0700); err != nil {
1759-
die("cannot create data dir: %v", err)
1744+
log.Fatalf("cannot create data dir: %v", err)
17601745
}
17611746

17621747
if cfg.pgListenAddress == "" {
1763-
die("--pg-listen-address is required")
1748+
log.Fatalf("--pg-listen-address is required")
17641749
}
17651750

17661751
ip := net.ParseIP(cfg.pgListenAddress)
@@ -1776,73 +1761,73 @@ func keeper(c *cobra.Command, args []string) {
17761761
}
17771762
}
17781763
if cfg.pgListenAddress == "" {
1779-
die("--pg-listen-address is required")
1764+
log.Fatalf("--pg-listen-address is required")
17801765
}
17811766

17821767
if _, ok := validAuthMethods[cfg.pgReplAuthMethod]; !ok {
1783-
die("--pg-repl-auth-method must be one of: md5, trust")
1768+
log.Fatalf("--pg-repl-auth-method must be one of: md5, trust")
17841769
}
17851770
if cfg.pgReplUsername == "" {
1786-
die("--pg-repl-username is required")
1771+
log.Fatalf("--pg-repl-username is required")
17871772
}
17881773
if cfg.pgReplAuthMethod == "trust" {
1789-
stdout("warning: not utilizing a password for replication between hosts is extremely dangerous")
1774+
log.Warn("not utilizing a password for replication between hosts is extremely dangerous")
17901775
if cfg.pgReplPassword != "" || cfg.pgReplPasswordFile != "" {
1791-
die("can not utilize --pg-repl-auth-method trust together with --pg-repl-password or --pg-repl-passwordfile")
1776+
log.Fatalf("can not utilize --pg-repl-auth-method trust together with --pg-repl-password or --pg-repl-passwordfile")
17921777
}
17931778
}
17941779
if cfg.pgSUAuthMethod == "trust" {
1795-
stdout("warning: not utilizing a password for superuser is extremely dangerous")
1780+
log.Warn("not utilizing a password for superuser is extremely dangerous")
17961781
if cfg.pgSUPassword != "" || cfg.pgSUPasswordFile != "" {
1797-
die("can not utilize --pg-su-auth-method trust together with --pg-su-password or --pg-su-passwordfile")
1782+
log.Fatalf("can not utilize --pg-su-auth-method trust together with --pg-su-password or --pg-su-passwordfile")
17981783
}
17991784
}
18001785
if cfg.pgReplAuthMethod != "trust" && cfg.pgReplPassword == "" && cfg.pgReplPasswordFile == "" {
1801-
die("one of --pg-repl-password or --pg-repl-passwordfile is required")
1786+
log.Fatalf("one of --pg-repl-password or --pg-repl-passwordfile is required")
18021787
}
18031788
if cfg.pgReplAuthMethod != "trust" && cfg.pgReplPassword != "" && cfg.pgReplPasswordFile != "" {
1804-
die("only one of --pg-repl-password or --pg-repl-passwordfile must be provided")
1789+
log.Fatalf("only one of --pg-repl-password or --pg-repl-passwordfile must be provided")
18051790
}
18061791
if _, ok := validAuthMethods[cfg.pgSUAuthMethod]; !ok {
1807-
die("--pg-su-auth-method must be one of: md5, password, trust")
1792+
log.Fatalf("--pg-su-auth-method must be one of: md5, password, trust")
18081793
}
18091794
if cfg.pgSUAuthMethod != "trust" && cfg.pgSUPassword == "" && cfg.pgSUPasswordFile == "" {
1810-
die("one of --pg-su-password or --pg-su-passwordfile is required")
1795+
log.Fatalf("one of --pg-su-password or --pg-su-passwordfile is required")
18111796
}
18121797
if cfg.pgSUAuthMethod != "trust" && cfg.pgSUPassword != "" && cfg.pgSUPasswordFile != "" {
1813-
die("only one of --pg-su-password or --pg-su-passwordfile must be provided")
1798+
log.Fatalf("only one of --pg-su-password or --pg-su-passwordfile must be provided")
18141799
}
18151800

18161801
if cfg.pgSUUsername == cfg.pgReplUsername {
1817-
stdout("warning: superuser name and replication user name are the same. Different users are suggested.")
1802+
log.Warn("superuser name and replication user name are the same. Different users are suggested.")
18181803
if cfg.pgReplAuthMethod != cfg.pgSUAuthMethod {
1819-
die("do not support different auth methods when utilizing superuser for replication.")
1804+
log.Fatalf("do not support different auth methods when utilizing superuser for replication.")
18201805
}
18211806
if cfg.pgSUPassword != cfg.pgReplPassword && cfg.pgSUAuthMethod != "trust" && cfg.pgReplAuthMethod != "trust" {
1822-
die("provided superuser name and replication user name are the same but provided passwords are different")
1807+
log.Fatalf("provided superuser name and replication user name are the same but provided passwords are different")
18231808
}
18241809
}
18251810

18261811
if cfg.pgReplPasswordFile != "" {
18271812
cfg.pgReplPassword, err = readPasswordFromFile(cfg.pgReplPasswordFile)
18281813
if err != nil {
1829-
die("cannot read pg replication user password: %v", err)
1814+
log.Fatalf("cannot read pg replication user password: %v", err)
18301815
}
18311816
}
18321817
if cfg.pgSUPasswordFile != "" {
18331818
cfg.pgSUPassword, err = readPasswordFromFile(cfg.pgSUPasswordFile)
18341819
if err != nil {
1835-
die("cannot read pg superuser password: %v", err)
1820+
log.Fatalf("cannot read pg superuser password: %v", err)
18361821
}
18371822
}
18381823

18391824
// Open (and create if needed) the lock file.
18401825
// There is no need to clean up this file since we don't use the file as an actual lock. We get a lock
1841-
// on the file. So the lock get released when our process stops (or dies).
1826+
// on the file. So the lock get released when our process stops (or log.Fatalfs).
18421827
lockFileName := filepath.Join(cfg.dataDir, "lock")
18431828
lockFile, err := os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0644)
18441829
if err != nil {
1845-
die("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
1830+
log.Fatalf("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
18461831
}
18471832

18481833
// Get a lock on our lock file.
@@ -1855,14 +1840,14 @@ func keeper(c *cobra.Command, args []string) {
18551840

18561841
err = syscall.FcntlFlock(lockFile.Fd(), syscall.F_SETLK, ft)
18571842
if err != nil {
1858-
die("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
1843+
log.Fatalf("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
18591844
}
18601845

18611846
log.Infow("exclusive lock on data dir taken")
18621847

18631848
if cfg.uid != "" {
18641849
if !pg.IsValidReplSlotName(cfg.uid) {
1865-
die("keeper uid %q not valid. It can contain only lower-case letters, numbers and the underscore character", cfg.uid)
1850+
log.Fatalf("keeper uid %q not valid. It can contain only lower-case letters, numbers and the underscore character", cfg.uid)
18661851
}
18671852
}
18681853

@@ -1885,7 +1870,7 @@ func keeper(c *cobra.Command, args []string) {
18851870

18861871
p, err := NewPostgresKeeper(&cfg, end)
18871872
if err != nil {
1888-
die("cannot create keeper: %v", err)
1873+
log.Fatalf("cannot create keeper: %v", err)
18891874
}
18901875
go p.Start(ctx)
18911876

cmd/proxy/cmd/proxy.go

+8-25
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"fmt"
2020
"net"
2121
"net/http"
22-
"os"
23-
"strings"
2422
"sync"
2523
"time"
2624

@@ -76,21 +74,6 @@ func init() {
7674
CmdProxy.PersistentFlags().MarkDeprecated("debug", "use --log-level=debug instead")
7775
}
7876

79-
func stderr(format string, a ...interface{}) {
80-
out := fmt.Sprintf(format, a...)
81-
fmt.Fprintln(os.Stderr, strings.TrimSuffix(out, "\n"))
82-
}
83-
84-
func stdout(format string, a ...interface{}) {
85-
out := fmt.Sprintf(format, a...)
86-
fmt.Fprintln(os.Stdout, strings.TrimSuffix(out, "\n"))
87-
}
88-
89-
func die(format string, a ...interface{}) {
90-
stderr(format, a...)
91-
os.Exit(1)
92-
}
93-
9477
type ClusterChecker struct {
9578
uid string
9679
listenAddress string
@@ -347,7 +330,7 @@ func proxy(c *cobra.Command, args []string) {
347330
case "debug":
348331
slog.SetLevel(zap.DebugLevel)
349332
default:
350-
die("invalid log level: %v", cfg.LogLevel)
333+
log.Fatalf("invalid log level: %v", cfg.LogLevel)
351334
}
352335
if cfg.debug {
353336
slog.SetDebug()
@@ -366,17 +349,17 @@ func proxy(c *cobra.Command, args []string) {
366349
}
367350

368351
if err := cmd.CheckCommonConfig(&cfg.CommonConfig); err != nil {
369-
die(err.Error())
352+
log.Fatalf(err.Error())
370353
}
371354

372355
if cfg.keepAliveIdle < 0 {
373-
die("tcp keepalive idle value must be greater or equal to 0")
356+
log.Fatalf("tcp keepalive idle value must be greater or equal to 0")
374357
}
375358
if cfg.keepAliveCount < 0 {
376-
die("tcp keepalive idle value must be greater or equal to 0")
359+
log.Fatalf("tcp keepalive idle value must be greater or equal to 0")
377360
}
378361
if cfg.keepAliveInterval < 0 {
379-
die("tcp keepalive idle value must be greater or equal to 0")
362+
log.Fatalf("tcp keepalive idle value must be greater or equal to 0")
380363
}
381364

382365
uid := common.UID()
@@ -387,16 +370,16 @@ func proxy(c *cobra.Command, args []string) {
387370
go func() {
388371
err := http.ListenAndServe(cfg.MetricsListenAddress, nil)
389372
if err != nil {
390-
die("metrics http server error", zap.Error(err))
373+
log.Fatalf("metrics http server error", zap.Error(err))
391374
}
392375
}()
393376
}
394377

395378
clusterChecker, err := NewClusterChecker(uid, cfg)
396379
if err != nil {
397-
die("cannot create cluster checker: %v", err)
380+
log.Fatalf("cannot create cluster checker: %v", err)
398381
}
399382
if err = clusterChecker.Start(); err != nil {
400-
die("cluster checker ended with error: %v", err)
383+
log.Fatalf("cluster checker ended with error: %v", err)
401384
}
402385
}

cmd/sentinel/cmd/sentinel.go

+3-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"os/signal"
2626
"reflect"
2727
"sort"
28-
"strings"
2928
"sync"
3029
"syscall"
3130
"time"
@@ -76,21 +75,6 @@ func init() {
7675
CmdSentinel.PersistentFlags().MarkDeprecated("debug", "use --log-level=debug instead")
7776
}
7877

79-
func stderr(format string, a ...interface{}) {
80-
out := fmt.Sprintf(format, a...)
81-
fmt.Fprintln(os.Stderr, strings.TrimSuffix(out, "\n"))
82-
}
83-
84-
func stdout(format string, a ...interface{}) {
85-
out := fmt.Sprintf(format, a...)
86-
fmt.Fprintln(os.Stdout, strings.TrimSuffix(out, "\n"))
87-
}
88-
89-
func die(format string, a ...interface{}) {
90-
stderr(format, a...)
91-
os.Exit(1)
92-
}
93-
9478
func (s *Sentinel) electionLoop(ctx context.Context) {
9579
for {
9680
log.Infow("Trying to acquire sentinels leadership")
@@ -1947,7 +1931,7 @@ func sentinel(c *cobra.Command, args []string) {
19471931
case "debug":
19481932
slog.SetLevel(zap.DebugLevel)
19491933
default:
1950-
die("invalid log level: %v", cfg.LogLevel)
1934+
log.Fatalf("invalid log level: %v", cfg.LogLevel)
19511935
}
19521936
if cfg.debug {
19531937
slog.SetDebug()
@@ -1958,7 +1942,7 @@ func sentinel(c *cobra.Command, args []string) {
19581942
}
19591943

19601944
if err := cmd.CheckCommonConfig(&cfg.CommonConfig); err != nil {
1961-
die(err.Error())
1945+
log.Fatalf(err.Error())
19621946
}
19631947

19641948
uid := common.UID()
@@ -1983,7 +1967,7 @@ func sentinel(c *cobra.Command, args []string) {
19831967

19841968
s, err := NewSentinel(uid, &cfg, end)
19851969
if err != nil {
1986-
die("cannot create sentinel: %v", err)
1970+
log.Fatalf("cannot create sentinel: %v", err)
19871971
}
19881972
go s.Start(ctx)
19891973

0 commit comments

Comments
 (0)