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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package main
import (
"context"
"errors"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/gitlab-org/gitaly/internal/log"
"gitlab.com/gitlab-org/gitaly/internal/praefect"
"gitlab.com/gitlab-org/gitaly/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/internal/praefect/conn"
"gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry"
"gitlab.com/gitlab-org/labkit/tracing"
)
var (
flagConfig = flag.String("config", "", "Location for the config.toml")
flagVersion = flag.Bool("version", false, "Print version and exit")
logger = log.Default()
errNoConfigFile = errors.New("the config flag must be passed")
)
func main() {
flag.Parse()
// If invoked with -version
if *flagVersion {
fmt.Println(praefect.GetVersionString())
os.Exit(0)
}
conf, err := configure()
if err != nil {
logger.Fatal(err)
}
listeners, err := getListeners(conf.SocketPath, conf.ListenAddr)
if err != nil {
logger.Fatalf("%s", err)
}
if err := run(listeners, conf); err != nil {
logger.Fatalf("%v", err)
}
}
func configure() (config.Config, error) {
var conf config.Config
if *flagConfig == "" {
return conf, errNoConfigFile
}
conf, err := config.FromFile(*flagConfig)
if err != nil {
return conf, fmt.Errorf("error reading config file: %v", err)
}
if err := conf.Validate(); err != nil {
return conf, err
}
logger = conf.ConfigureLogger()
tracing.Initialize(tracing.WithServiceName("praefect"))
if conf.PrometheusListenAddr != "" {
logger.WithField("address", conf.PrometheusListenAddr).Info("Starting prometheus listener")
promMux := http.NewServeMux()
promMux.Handle("/metrics", promhttp.Handler())
go func() {
http.ListenAndServe(conf.PrometheusListenAddr, promMux)
}()
}
registerServerVersionPromGauge()
logger.WithField("version", praefect.GetVersionString()).Info("Starting Praefect")
return conf, nil
}
func run(listeners []net.Listener, conf config.Config) error {
clientConnections := conn.NewClientConnections()
for _, node := range conf.Nodes {
if err := clientConnections.RegisterNode(node.Storage, node.Address, node.Token); err != nil {
return fmt.Errorf("failed to register %s: %s", node.Address, err)
}
logger.WithField("node_address", node.Address).Info("registered gitaly node")
}
var (
// top level server dependencies
datastore = praefect.NewMemoryDatastore(conf)
coordinator = praefect.NewCoordinator(logger, datastore, clientConnections, conf, protoregistry.GitalyProtoFileDescriptors...)
repl = praefect.NewReplMgr("default", logger, datastore, clientConnections)
srv = praefect.NewServer(coordinator, repl, nil, logger, clientConnections, conf)
// signal related
signals = []os.Signal{syscall.SIGTERM, syscall.SIGINT}
termCh = make(chan os.Signal, len(signals))
serverErrors = make(chan error, 1)
)
signal.Notify(termCh, signals...)
for _, l := range listeners {
go func(lis net.Listener) { serverErrors <- srv.Start(lis) }(l)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() { serverErrors <- repl.ProcessBacklog(ctx) }()
go coordinator.FailoverRotation()
select {
case s := <-termCh:
logger.WithField("signal", s).Warn("received signal, shutting down gracefully")
cancel() // cancels the replicator job processing
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
if shutdownErr := srv.Shutdown(ctx); shutdownErr != nil {
logger.Warnf("error received during shutting down: %v", shutdownErr)
return shutdownErr
}
case err := <-serverErrors:
return err
}
return nil
}
func getListeners(socketPath, listenAddr string) ([]net.Listener, error) {
var listeners []net.Listener
if socketPath != "" {
if err := os.RemoveAll(socketPath); err != nil {
return nil, err
}
cleanPath := strings.TrimPrefix(socketPath, "unix:")
l, err := net.Listen("unix", cleanPath)
if err != nil {
return nil, err
}
listeners = append(listeners, l)
logger.WithField("address", socketPath).Info("listening on unix socket")
}
if listenAddr != "" {
l, err := net.Listen("tcp", listenAddr)
if err != nil {
return nil, err
}
listeners = append(listeners, l)
logger.WithField("address", listenAddr).Info("listening at tcp address")
}
return listeners, nil
}
// registerServerVersionPromGauge registers a label with the current server version
// making it easy to see what versions of Gitaly are running across a cluster
func registerServerVersionPromGauge() {
gitlabBuildInfoGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "gitlab_build_info",
Help: "Current build info for this GitLab Service",
ConstLabels: prometheus.Labels{
"version": praefect.GetVersion(),
"built": praefect.GetBuildTime(),
},
})
prometheus.MustRegister(gitlabBuildInfoGauge)
gitlabBuildInfoGauge.Set(1)
}
|