forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
242 lines (203 loc) · 6.77 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package server
import (
"admin"
"api/graphite"
"api/http"
"api/udp"
"cluster"
"configuration"
"coordinator"
"datastore"
"runtime"
"time"
"wal"
log "code.google.com/p/log4go"
influxdb "github.com/influxdb/influxdb-go"
)
type Server struct {
RaftServer *coordinator.RaftServer
ProtobufServer *coordinator.ProtobufServer
ClusterConfig *cluster.ClusterConfiguration
HttpApi *http.HttpServer
GraphiteApi *graphite.Server
UdpApi *udp.Server
UdpServers []*udp.Server
AdminServer *admin.HttpServer
Coordinator coordinator.Coordinator
Config *configuration.Configuration
RequestHandler *coordinator.ProtobufRequestHandler
stopped bool
writeLog *wal.WAL
shardStore *datastore.LevelDbShardDatastore
}
func NewServer(config *configuration.Configuration) (*Server, error) {
log.Info("Opening database at %s", config.DataDir)
shardDb, err := datastore.NewLevelDbShardDatastore(config)
if err != nil {
return nil, err
}
newClient := func(connectString string) cluster.ServerConnection {
return coordinator.NewProtobufClient(connectString, config.ProtobufTimeout.Duration)
}
writeLog, err := wal.NewWAL(config)
if err != nil {
return nil, err
}
clusterConfig := cluster.NewClusterConfiguration(config, writeLog, shardDb, newClient)
raftServer := coordinator.NewRaftServer(config, clusterConfig)
clusterConfig.LocalRaftName = raftServer.GetRaftName()
clusterConfig.SetShardCreator(raftServer)
clusterConfig.CreateFutureShardsAutomaticallyBeforeTimeComes()
coord := coordinator.NewCoordinatorImpl(config, raftServer, clusterConfig)
requestHandler := coordinator.NewProtobufRequestHandler(coord, clusterConfig)
protobufServer := coordinator.NewProtobufServer(config.ProtobufListenString(), requestHandler)
raftServer.AssignCoordinator(coord)
httpApi := http.NewHttpServer(config.ApiHttpPortString(), config.ApiReadTimeout, config.AdminAssetsDir, coord, coord, clusterConfig, raftServer)
httpApi.EnableSsl(config.ApiHttpSslPortString(), config.ApiHttpCertPath)
graphiteApi := graphite.NewServer(config, coord, clusterConfig)
adminServer := admin.NewHttpServer(config.AdminAssetsDir, config.AdminHttpPortString())
return &Server{
RaftServer: raftServer,
ProtobufServer: protobufServer,
ClusterConfig: clusterConfig,
HttpApi: httpApi,
GraphiteApi: graphiteApi,
Coordinator: coord,
AdminServer: adminServer,
Config: config,
RequestHandler: requestHandler,
writeLog: writeLog,
shardStore: shardDb}, nil
}
func (self *Server) ListenAndServe() error {
err := self.RaftServer.ListenAndServe()
if err != nil {
return err
}
log.Info("Waiting for local server to be added")
self.ClusterConfig.WaitForLocalServerLoaded()
self.writeLog.SetServerId(self.ClusterConfig.ServerId())
time.Sleep(5 * time.Second)
// check to make sure that the raft connection string hasn't changed
raftConnectionString := self.Config.RaftConnectionString()
if self.ClusterConfig.LocalServer.ProtobufConnectionString != self.Config.ProtobufConnectionString() ||
self.ClusterConfig.LocalServer.RaftConnectionString != raftConnectionString {
log.Info("Sending change connection string command (%s,%s) (%s,%s)",
self.ClusterConfig.LocalServer.ProtobufConnectionString,
self.Config.ProtobufConnectionString(),
self.ClusterConfig.LocalServer.RaftConnectionString,
raftConnectionString,
)
err := self.RaftServer.ChangeConnectionString(
self.ClusterConfig.LocalRaftName,
self.Config.ProtobufConnectionString(),
self.Config.RaftConnectionString(),
true, // force the rename
)
if err != nil {
panic(err)
}
log.Info("Connection string changed successfully")
}
go self.ProtobufServer.ListenAndServe()
log.Info("Recovering from log...")
err = self.ClusterConfig.RecoverFromWAL()
if err != nil {
return err
}
log.Info("recovered")
err = self.Coordinator.(*coordinator.CoordinatorImpl).ConnectToProtobufServers(self.RaftServer.GetRaftName())
if err != nil {
return err
}
log.Info("Starting admin interface on port %d", self.Config.AdminHttpPort)
go self.AdminServer.ListenAndServe()
if self.Config.GraphiteEnabled {
if self.Config.GraphitePort <= 0 || self.Config.GraphiteDatabase == "" {
log.Warn("Cannot start graphite server. please check your configuration")
} else {
log.Info("Starting Graphite Listener on port %d", self.Config.GraphitePort)
go self.GraphiteApi.ListenAndServe()
}
}
// UDP input
for _, udpInput := range self.Config.UdpServers {
port := udpInput.Port
database := udpInput.Database
if port <= 0 || database == "" {
log.Warn("Cannot start udp server. please check your configuration")
continue
}
log.Info("Starting UDP Listener on port %d to database %s", port, database)
addr := self.Config.UdpInputPortString(port)
server := udp.NewServer(addr, database, self.Coordinator, self.ClusterConfig)
self.UdpServers = append(self.UdpServers, server)
go server.ListenAndServe()
}
log.Debug("ReportingDisabled: %s", self.Config.ReportingDisabled)
if !self.Config.ReportingDisabled {
go self.startReportingLoop()
}
// start processing continuous queries
self.RaftServer.StartProcessingContinuousQueries()
log.Info("Starting Http Api server on port %d", self.Config.ApiHttpPort)
self.HttpApi.ListenAndServe()
return nil
}
func (self *Server) startReportingLoop() chan struct{} {
log.Debug("Starting Reporting Loop")
self.reportStats()
ticker := time.NewTicker(24 * time.Hour)
for {
select {
case <-ticker.C:
self.reportStats()
}
}
}
func (self *Server) reportStats() {
client, err := influxdb.NewClient(&influxdb.ClientConfig{
Database: "reporting",
Host: "m.influxdb.com:8086",
Username: "reporter",
Password: "influxdb",
})
if err != nil {
log.Error("Couldn't create client for reporting: %s", err)
} else {
series := &influxdb.Series{
Name: "reports",
Columns: []string{"os", "arch", "id", "version"},
Points: [][]interface{}{
[]interface{}{runtime.GOOS, runtime.GOARCH, self.RaftServer.GetRaftName(), self.Config.InfluxDBVersion},
},
}
log.Info("Reporting stats: %#v", series)
client.WriteSeries([]*influxdb.Series{series})
}
}
func (self *Server) Stop() {
if self.stopped {
return
}
log.Info("Stopping server")
self.stopped = true
log.Info("Stopping api server")
self.HttpApi.Close()
log.Info("Api server stopped")
log.Info("Stopping admin server")
self.AdminServer.Close()
log.Info("admin server stopped")
log.Info("Stopping raft server")
self.RaftServer.Close()
log.Info("Raft server stopped")
log.Info("Stopping protobuf server")
self.ProtobufServer.Close()
log.Info("protobuf server stopped")
log.Info("Stopping wal")
self.writeLog.Close()
log.Info("wal stopped")
log.Info("Stopping shard store")
self.shardStore.Close()
log.Info("shard store stopped")
}