-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
167 lines (137 loc) · 3.64 KB
/
server.go
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
package bristle
import (
"os"
"os/signal"
"sync"
"syscall"
"github.com/rs/zerolog/log"
"golang.org/x/net/context"
)
type Server struct {
sync.RWMutex
configPath string
ingestService *IngestService
// The following can get reloaded
config *Config
protoRegistry *ProtoRegistry
messageBindingRegistry messageBindingRegistry
clusters []*ClickhouseCluster
writerGroup *writerGroup
debugServer *debugServer
}
func NewServer(configPath string) (*Server, error) {
config, err := LoadConfig(configPath)
if err != nil {
return nil, err
}
s := &Server{
configPath: configPath,
}
err = s.reloadConfig(config)
if err != nil {
return nil, err
}
s.ingestService, err = NewIngestService(s)
if err != nil {
return nil, err
}
return s, nil
}
func (s *Server) reloadConfig(newConfig *Config) error {
var debugServer *debugServer
if newConfig.Debugging != nil {
debugServer = newDebugServer(*newConfig.Debugging)
}
protoRegistry := NewProtoRegistry()
for _, path := range newConfig.ProtoDescriptorPaths {
err := protoRegistry.RegisterPath(path)
if err != nil {
return err
}
}
clusters := []*ClickhouseCluster{}
for _, clusterConfig := range newConfig.Clusters {
clusters = append(clusters, NewClickhouseCluster(clusterConfig))
}
messageBindingRegistry := make(messageBindingRegistry)
messageBindingRegistry.BindFromClusters(clusters, protoRegistry)
if newConfig.Autobind {
if err := messageBindingRegistry.BindFromProtos(clusters, protoRegistry); err != nil {
return err
}
}
writerGroup := newWriterGroup()
for _, cluster := range clusters {
for _, table := range cluster.tables {
tableWriterCount := table.config.Writers
if tableWriterCount == 0 {
tableWriterCount = 1
}
log.Info().
Int("count", tableWriterCount).
Str("table", string(table.Name)).
Msg("server: starting up table writers")
for i := 0; i < tableWriterCount; i++ {
writer, err := NewClickhouseTableWriter(table)
if err != nil {
return err
}
writerGroup.Add(writer)
}
}
}
s.Lock()
if newConfig.LogLevel != "" {
setLogLevel(newConfig.LogLevel)
}
s.config = newConfig
s.protoRegistry = protoRegistry
s.clusters = clusters
s.messageBindingRegistry = messageBindingRegistry
if s.debugServer != nil {
s.debugServer.Close()
}
s.debugServer = debugServer
if debugServer != nil {
go s.debugServer.Run()
}
if s.writerGroup != nil {
go s.writerGroup.Close()
}
s.writerGroup = writerGroup
s.writerGroup.Start()
defer s.Unlock()
return nil
}
func (s *Server) Run() error {
ctx, cancel := context.WithCancel(context.Background())
s.ingestService.Run(ctx)
log.Info().Msg("server: started ingest service")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
for {
sig := <-sigs
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
log.Info().Int("signal", int(syscall.SIGINT)).Msg("server: received shutdown signal")
cancel()
} else if sig == syscall.SIGHUP {
log.Info().Msg("server: received SIGHUP, reloading configuration...")
newConfig, err := LoadConfig(s.configPath)
if err != nil {
log.Error().Err(err).Msg("server: configuration reload encountered error on load, no action taken")
continue
}
err = s.reloadConfig(newConfig)
if err != nil {
log.Error().Err(err).Msg("server: configuration reload encountered error applying, no action taken")
continue
}
log.Info().Msg("server: configuration reload completed")
}
}
}()
<-ctx.Done()
log.Info().Msg("server: exit requested, goodbye")
return nil
}