forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_configuration.go
596 lines (515 loc) · 18.4 KB
/
cluster_configuration.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package coordinator
import (
"bytes"
log "code.google.com/p/log4go"
"common"
"configuration"
"encoding/gob"
"errors"
"fmt"
"parser"
"sync"
"sync/atomic"
"time"
)
/*
This struct stores all the metadata confiugration information about a running cluster. This includes
the servers in the cluster and their state, databases, users, and which continuous queries are running.
ClusterVersion is a monotonically increasing int that keeps track of different server configurations.
For example, when you spin up a cluster and start writing data, the version will be 1. If you expand the
cluster the version will be bumped. Using this the cluster is able to run two versions simultaneously
while the new servers are being brought online.
*/
type ClusterConfiguration struct {
createDatabaseLock sync.RWMutex
databaseReplicationFactors map[string]uint8
usersLock sync.RWMutex
clusterAdmins map[string]*clusterAdmin
dbUsers map[string]map[string]*dbUser
servers []*ClusterServer
serversLock sync.RWMutex
continuousQueries map[string][]*ContinuousQuery
continuousQueriesLock sync.RWMutex
parsedContinuousQueries map[string]map[uint32]*parser.SelectQuery
continuousQueryTimestamp time.Time
hasRunningServers bool
localServerId uint32
ClusterVersion uint32
config *configuration.Configuration
addedLocalServerWait chan bool
addedLocalServer bool
}
type ContinuousQuery struct {
Id uint32
Query string
}
type Database struct {
Name string `json:"name"`
ReplicationFactor uint8 `json:"replicationFactor"`
}
func NewClusterConfiguration(config *configuration.Configuration) *ClusterConfiguration {
return &ClusterConfiguration{
databaseReplicationFactors: make(map[string]uint8),
clusterAdmins: make(map[string]*clusterAdmin),
dbUsers: make(map[string]map[string]*dbUser),
continuousQueries: make(map[string][]*ContinuousQuery),
parsedContinuousQueries: make(map[string]map[uint32]*parser.SelectQuery),
servers: make([]*ClusterServer, 0),
config: config,
addedLocalServerWait: make(chan bool, 1),
}
}
func (self *ClusterConfiguration) IsSingleServer() bool {
return len(self.servers) < 2
}
func (self *ClusterConfiguration) Servers() []*ClusterServer {
return self.servers
}
// This function will wait until the configuration has received an addPotentialServer command for
// this local server.
func (self *ClusterConfiguration) WaitForLocalServerLoaded() {
// It's possible during initialization if Raft hasn't finished relpaying the log file or joining
// the cluster that the cluster config won't have any servers. Wait for a little bit and retry, but error out eventually.
<-self.addedLocalServerWait
}
func (self *ClusterConfiguration) GetReplicationFactor(database *string) uint8 {
return self.databaseReplicationFactors[*database]
}
func (self *ClusterConfiguration) IsActive() bool {
return self.hasRunningServers
}
func (self *ClusterConfiguration) SetActive() {
self.serversLock.Lock()
defer self.serversLock.Unlock()
for _, server := range self.servers {
server.State = Running
}
atomic.AddUint32(&self.ClusterVersion, uint32(1))
}
func (self *ClusterConfiguration) GetServerByRaftName(name string) *ClusterServer {
for _, server := range self.servers {
if server.RaftName == name {
return server
}
}
return nil
}
func (self *ClusterConfiguration) GetServerById(id *uint32) *ClusterServer {
for _, server := range self.servers {
if server.Id == *id {
return server
}
}
log.Warn("Couldn't find server with id: ", *id, self.servers)
return nil
}
type serverToQuery struct {
server *ClusterServer
ringLocationsToQuery uint32
}
// This function will return an array of servers to query and the number of ring locations to return per server.
// Queries are issued to every nth server in the cluster where n is the replication factor. We need the local host id
// because we want to issue the query locally and the nth servers from there.
// Optimally, the number of servers in a cluster will be evenly divisible by the replication factors used. For example,
// if you have a cluster with databases with RFs of 1, 2, and 3: optimal cluster sizes would be 6, 12, 18, 24, 30, etc.
// If that's not the case, one or more servers will have to filter out data from other servers on the fly, which could
// be a little more expensive.
func (self *ClusterConfiguration) GetServersToMakeQueryTo(database *string) (servers []*serverToQuery, replicationFactor uint32) {
replicationFactor = uint32(self.GetReplicationFactor(database))
replicationFactorInt := int(replicationFactor)
index := 0
for i, s := range self.servers {
if s.Id == self.localServerId {
index = i % replicationFactorInt
break
}
}
servers = make([]*serverToQuery, 0, len(self.servers)/replicationFactorInt)
serverCount := len(self.servers)
for ; index < serverCount; index += replicationFactorInt {
server := self.servers[index]
servers = append(servers, &serverToQuery{server, replicationFactor})
}
// need to maybe add a server and set which ones filter their data
if serverCount%replicationFactorInt != 0 {
/*
Here's what this looks like with a few different ring sizes and replication factors.
server indexes
0 1 2 3 4
0 2 4 6 - 0 only sends his
1 3 5 - add 0 and have 1 only send his
0 3 6 - have 0 only send his and 4
1 4 7 - have 1 only send his and 0
2 5 - add 0 and have 2 send only his and 1
server indexes
0 1 2 3 4 5 6 7
0 3 6 9 - have 0 only send his and 7
1 4 7 10 - have 1 only send his and 0
2 5 8 - add 0 and have 2 only send his and 1
We see that there are the number of cases equal to the replication factor. The last case
always has us adding the first server in the ring. Then we're determining how much data
the local server should be ignoring.
*/
lastIndexAdded := index - replicationFactorInt
if serverCount-lastIndexAdded == replicationFactorInt {
servers[0].ringLocationsToQuery = uint32(replicationFactorInt - 1)
servers = append(servers, &serverToQuery{self.servers[0], replicationFactor})
} else {
servers[0].ringLocationsToQuery = uint32(replicationFactorInt - 1)
}
}
return servers, replicationFactor
}
// This method returns a function that can be passed into the datastore's ExecuteQuery method. It will tell the datastore
// if each point is something that should be returned in the query based on its ring location and if the query calls for
// data from different replicas.
//
// Params:
// - database: the name of the database
// - countOfServersToInclude: the number of replicas that this server will return data for
//
// Returns a function that returns true if the point should be filtered out. Otherwise the point should be included
// in the yielded time series
func (self *ClusterConfiguration) GetRingFilterFunction(database string, countOfServersToInclude uint32) func(database, series *string, time *int64) bool {
serversToInclude := make([]*ClusterServer, 0, countOfServersToInclude)
countServers := int(countOfServersToInclude)
for i, s := range self.servers {
if s.Id == self.localServerId {
serversToInclude = append(serversToInclude, s)
for j := i - 1; j >= 0 && len(serversToInclude) < countServers; j-- {
serversToInclude = append(serversToInclude, self.servers[j])
}
if len(serversToInclude) < countServers {
for j := len(self.servers) - 1; len(serversToInclude) < countServers; j-- {
serversToInclude = append(serversToInclude, self.servers[j])
}
}
}
}
f := func(database, series *string, time *int64) bool {
location := common.RingLocation(database, series, time)
server := self.servers[location%len(self.servers)]
for _, s := range serversToInclude {
if s.Id == server.Id {
return false
}
}
return true
}
return f
}
func (self *ClusterConfiguration) GetServerIndexByLocation(location *int) int {
return *location % len(self.servers)
}
func (self *ClusterConfiguration) GetOwnerIdByLocation(location *int) *uint32 {
return &self.servers[self.GetServerIndexByLocation(location)].Id
}
func (self *ClusterConfiguration) GetServersByRingLocation(database *string, location *int) []*ClusterServer {
index := self.GetServerIndexByLocation(location)
_, replicas := self.GetServersByIndexAndReplicationFactor(database, &index)
return replicas
}
// This function returns the replicas of the given server
func (self *ClusterConfiguration) GetReplicas(server *ClusterServer, database *string) (*ClusterServer, []*ClusterServer) {
for index, s := range self.servers {
if s.Id == server.Id {
return self.GetServersByIndexAndReplicationFactor(database, &index)
}
}
return nil, nil
}
// This function returns the server that owns the ring location and a set of servers that are replicas (which include the onwer)
func (self *ClusterConfiguration) GetServersByIndexAndReplicationFactor(database *string, index *int) (*ClusterServer, []*ClusterServer) {
replicationFactor := int(self.GetReplicationFactor(database))
serverCount := len(self.servers)
owner := self.servers[*index]
if replicationFactor >= serverCount {
return owner, self.servers
}
owners := make([]*ClusterServer, 0, replicationFactor)
ownerCount := 0
for i := *index; i < serverCount && ownerCount < replicationFactor; i++ {
owners = append(owners, self.servers[i])
ownerCount++
}
for i := 0; ownerCount < replicationFactor; i++ {
owners = append(owners, self.servers[i])
ownerCount++
}
return owner, owners
}
func (self *ClusterConfiguration) GetServerByProtobufConnectionString(connectionString string) *ClusterServer {
for _, server := range self.servers {
if server.ProtobufConnectionString == connectionString {
return server
}
}
return nil
}
func (self *ClusterConfiguration) UpdateServerState(serverId uint32, state ServerState) error {
self.serversLock.Lock()
defer self.serversLock.Unlock()
atomic.AddUint32(&self.ClusterVersion, uint32(1))
for _, server := range self.servers {
if server.Id == serverId {
if state == Running {
self.hasRunningServers = true
}
server.State = state
return nil
}
}
return errors.New(fmt.Sprintf("No server with id %d", serverId))
}
func (self *ClusterConfiguration) AddPotentialServer(server *ClusterServer) {
self.serversLock.Lock()
defer self.serversLock.Unlock()
server.State = Potential
self.servers = append(self.servers, server)
server.Id = uint32(len(self.servers))
log.Info("Added server to cluster config: %d, %s, %s", server.Id, server.RaftConnectionString, server.ProtobufConnectionString)
log.Info("Checking whether this is the local server new: %s, local: %s\n", self.config.ProtobufConnectionString(), server.ProtobufConnectionString)
if server.ProtobufConnectionString != self.config.ProtobufConnectionString() {
log.Info("Connecting to ProtobufServer: %s", server.ProtobufConnectionString)
server.Connect()
} else if !self.addedLocalServer {
log.Info("Added the local server")
self.localServerId = server.Id
self.addedLocalServerWait <- true
self.addedLocalServer = true
}
}
func (self *ClusterConfiguration) GetDatabases() []*Database {
self.createDatabaseLock.RLock()
defer self.createDatabaseLock.RUnlock()
dbs := make([]*Database, 0, len(self.databaseReplicationFactors))
for name, rf := range self.databaseReplicationFactors {
dbs = append(dbs, &Database{Name: name, ReplicationFactor: rf})
}
return dbs
}
func (self *ClusterConfiguration) CreateDatabase(name string, replicationFactor uint8) error {
self.createDatabaseLock.Lock()
defer self.createDatabaseLock.Unlock()
if _, ok := self.databaseReplicationFactors[name]; ok {
return fmt.Errorf("database %s exists", name)
}
self.databaseReplicationFactors[name] = replicationFactor
return nil
}
func (self *ClusterConfiguration) DropDatabase(name string) error {
self.createDatabaseLock.Lock()
defer self.createDatabaseLock.Unlock()
if _, ok := self.databaseReplicationFactors[name]; !ok {
return fmt.Errorf("Database %s doesn't exist", name)
}
delete(self.databaseReplicationFactors, name)
self.usersLock.Lock()
defer self.usersLock.Unlock()
delete(self.dbUsers, name)
return nil
}
func (self *ClusterConfiguration) CreateContinuousQuery(db string, query string) error {
self.continuousQueriesLock.Lock()
defer self.continuousQueriesLock.Unlock()
if self.continuousQueries == nil {
self.continuousQueries = map[string][]*ContinuousQuery{}
}
if self.parsedContinuousQueries == nil {
self.parsedContinuousQueries = map[string]map[uint32]*parser.SelectQuery{}
}
maxId := uint32(0)
for _, query := range self.continuousQueries[db] {
if query.Id > maxId {
maxId = query.Id
}
}
selectQuery, err := parser.ParseSelectQuery(query)
if err != nil {
return fmt.Errorf("Failed to parse continuous query: %s", query)
}
queryId := maxId + 1
if self.parsedContinuousQueries[db] == nil {
self.parsedContinuousQueries[db] = map[uint32]*parser.SelectQuery{queryId: selectQuery}
} else {
self.parsedContinuousQueries[db][queryId] = selectQuery
}
self.continuousQueries[db] = append(self.continuousQueries[db], &ContinuousQuery{queryId, query})
return nil
}
func (self *ClusterConfiguration) SetContinuousQueryTimestamp(timestamp time.Time) error {
self.continuousQueriesLock.Lock()
defer self.continuousQueriesLock.Unlock()
self.continuousQueryTimestamp = timestamp
return nil
}
func (self *ClusterConfiguration) DeleteContinuousQuery(db string, id uint32) error {
self.continuousQueriesLock.Lock()
defer self.continuousQueriesLock.Unlock()
for i, query := range self.continuousQueries[db] {
if query.Id == id {
q := self.continuousQueries[db]
q[len(q)-1], q[i], q = nil, q[len(q)-1], q[:len(q)-1]
self.continuousQueries[db] = q
delete(self.parsedContinuousQueries[db], id)
break
}
}
return nil
}
func (self *ClusterConfiguration) GetContinuousQueries(db string) []*ContinuousQuery {
self.continuousQueriesLock.Lock()
defer self.continuousQueriesLock.Unlock()
return self.continuousQueries[db]
}
func (self *ClusterConfiguration) GetDbUsers(db string) (names []string) {
self.usersLock.RLock()
defer self.usersLock.RUnlock()
dbUsers := self.dbUsers[db]
for name, _ := range dbUsers {
names = append(names, name)
}
return
}
func (self *ClusterConfiguration) GetDbUser(db, username string) *dbUser {
self.usersLock.RLock()
defer self.usersLock.RUnlock()
dbUsers := self.dbUsers[db]
if dbUsers == nil {
return nil
}
return dbUsers[username]
}
func (self *ClusterConfiguration) SaveDbUser(u *dbUser) {
self.usersLock.Lock()
defer self.usersLock.Unlock()
db := u.GetDb()
dbUsers := self.dbUsers[db]
if u.IsDeleted() {
if dbUsers == nil {
return
}
delete(dbUsers, u.GetName())
return
}
if dbUsers == nil {
dbUsers = map[string]*dbUser{}
self.dbUsers[db] = dbUsers
}
dbUsers[u.GetName()] = u
}
func (self *ClusterConfiguration) ChangeDbUserPassword(db, username, hash string) error {
self.usersLock.Lock()
defer self.usersLock.Unlock()
dbUsers := self.dbUsers[db]
if dbUsers == nil {
return fmt.Errorf("Invalid database name %s", db)
}
if dbUsers[username] == nil {
return fmt.Errorf("Invalid username %s", username)
}
dbUsers[username].changePassword(hash)
return nil
}
func (self *ClusterConfiguration) GetClusterAdmins() (names []string) {
self.usersLock.RLock()
defer self.usersLock.RUnlock()
clusterAdmins := self.clusterAdmins
for name, _ := range clusterAdmins {
names = append(names, name)
}
return
}
func (self *ClusterConfiguration) GetClusterAdmin(username string) *clusterAdmin {
self.usersLock.RLock()
defer self.usersLock.RUnlock()
return self.clusterAdmins[username]
}
func (self *ClusterConfiguration) SaveClusterAdmin(u *clusterAdmin) {
self.usersLock.Lock()
defer self.usersLock.Unlock()
if u.IsDeleted() {
delete(self.clusterAdmins, u.GetName())
return
}
self.clusterAdmins[u.GetName()] = u
}
func (self *ClusterConfiguration) GetDatabaseReplicationFactor(name string) uint8 {
self.createDatabaseLock.RLock()
defer self.createDatabaseLock.RUnlock()
return self.databaseReplicationFactors[name]
}
func (self *ClusterConfiguration) Save() ([]byte, error) {
log.Debug("Dumping the cluster configuration")
data := struct {
Databases map[string]uint8
Admins map[string]*clusterAdmin
DbUsers map[string]map[string]*dbUser
Servers []*ClusterServer
HasRunningServers bool
LocalServerId uint32
ClusterVersion uint32
}{
self.databaseReplicationFactors,
self.clusterAdmins,
self.dbUsers,
self.servers,
self.hasRunningServers,
self.localServerId,
self.ClusterVersion,
}
b := bytes.NewBuffer(nil)
err := gob.NewEncoder(b).Encode(&data)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (self *ClusterConfiguration) Recovery(b []byte) error {
log.Debug("Recovering the cluster configuration")
data := struct {
Databases map[string]uint8
Admins map[string]*clusterAdmin
DbUsers map[string]map[string]*dbUser
Servers []*ClusterServer
HasRunningServers bool
LocalServerId uint32
ClusterVersion uint32
}{}
err := gob.NewDecoder(bytes.NewReader(b)).Decode(&data)
if err != nil {
return err
}
self.databaseReplicationFactors = data.Databases
self.clusterAdmins = data.Admins
self.dbUsers = data.DbUsers
// copy the protobuf client from the old servers
oldServers := map[string]*ProtobufClient{}
for _, server := range self.servers {
oldServers[server.ProtobufConnectionString] = server.protobufClient
}
self.servers = data.Servers
for _, server := range self.servers {
server.protobufClient = oldServers[server.ProtobufConnectionString]
if server.protobufClient == nil {
server.Connect()
}
}
self.hasRunningServers = data.HasRunningServers
self.localServerId = data.LocalServerId
self.ClusterVersion = data.ClusterVersion
if self.addedLocalServer {
return nil
}
for _, server := range self.servers {
log.Info("Checking whether this is the local server new: %s, local: %s\n", self.config.ProtobufConnectionString(), server.ProtobufConnectionString)
if server.ProtobufConnectionString != self.config.ProtobufConnectionString() {
continue
}
log.Info("Added the local server")
self.addedLocalServerWait <- true
self.addedLocalServer = true
break
}
return nil
}