Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (110 loc) · 3.23 KB
/
Copy pathmain.go
File metadata and controls
125 lines (110 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/cybertec-postgresql/vip-manager/checker"
"github.com/cybertec-postgresql/vip-manager/ipmanager"
"github.com/cybertec-postgresql/vip-manager/vipconfig"
"github.com/spf13/pflag"
"go.uber.org/zap"
)
var (
// vip-manager version definition
version = "master"
commit = "none"
date = "unknown"
)
func main() {
if (len(os.Args) > 1) && (os.Args[1] == "--version") {
fmt.Printf("version: %s\n", version)
fmt.Printf("commit: %s\n", commit)
fmt.Printf("date: %s\n", date)
return
}
// the whole lifecycle lives in run() so that the shutdown path, which
// removes the virtual IP, is always reached before the process exits
os.Exit(run())
}
func run() int {
conf, err := vipconfig.NewConfig()
if errors.Is(err, pflag.ErrHelp) {
return 0
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
log := conf.Logger.Sugar()
defer func() { _ = conf.Logger.Sync() }()
lc, err := checker.NewLeaderChecker(conf)
if err != nil {
log.Errorf("Failed to initialize leader checker: %s", err)
return 1
}
states := make(chan bool)
manager, err := ipmanager.NewIPManager(conf, states)
if err != nil {
log.Errorf("Problems with generating the virtual ip manager: %s", err)
return 1
}
// SIGTERM is what systemd sends on "systemctl stop|restart" and what a
// container runtime sends on stop. Without it the process is terminated
// right away and the virtual IP stays assigned to this machine, while
// Patroni promotes another node. Two hosts then answer on the same address.
mainCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
go func() {
<-mainCtx.Done()
log.Info("Shutting down, the virtual IP is removed if it is assigned here")
}()
var (
wg sync.WaitGroup
// written by the checker goroutine, read after wg.Wait()
checkerErr error
)
wg.Add(1)
go func() {
defer wg.Done()
// A checker that gives up must not take the process down on the spot:
// os.Exit() would skip the removal of the virtual IP. Cancel the
// context instead and let the manager shut down in an orderly fashion.
defer cancel()
err := lc.GetChangeNotificationStream(mainCtx, states)
checkerErr = checkerFailure(mainCtx.Err(), err)
if checkerErr != nil {
log.Errorw("Leader checker failed", zap.Error(checkerErr))
}
}()
wg.Add(1)
go func() {
defer wg.Done()
manager.SyncStates(mainCtx, states)
}()
wg.Wait()
if checkerErr != nil {
return 1
}
return 0
}
// checkerFailure returns the error to exit with after the leader checker
// returned err, or nil when the return was part of a shutdown. ctxErr is the
// error of the main context at that moment. The context decides whether the
// return is a failure, not err: a cancelled gRPC call does not wrap
// context.Canceled, and a checker can return context.Canceled from a timeout of
// its own.
func checkerFailure(ctxErr, err error) error {
if ctxErr != nil {
return nil
}
// A nil error is a failure as well: Restart=on-failure does not restart a
// service that exits with 0, and the node would stay without vip-manager.
if err == nil {
return errors.New("leader checker stopped before the shutdown")
}
return err
}