forked from google/seesaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
581 lines (499 loc) · 14.3 KB
/
sync.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
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: [email protected] (Joel Sing)
package engine
// This file contains structures and functions to manage synchronisation
// between Seesaw nodes.
import (
"errors"
"fmt"
"net"
"net/rpc"
"sync"
"time"
"github.com/google/seesaw/common/seesaw"
"github.com/google/seesaw/engine/config"
log "github.com/golang/glog"
)
// TODO(jsing): Consider implementing message authentication.
const (
sessionDeadtime = 2 * time.Minute
sessionNotesQueueSize = 100
syncHeartbeatInterval = 5 * time.Second
syncPollMsgLimit = 100
syncPollTimeout = 30 * time.Second
)
// SyncSessionID specifies a synchronisation session identifier.
type SyncSessionID uint64
// SyncNoteType specifies the type of a synchronisation notification.
type SyncNoteType int
// Values for SyncNoteType.
const (
SNTHeartbeat SyncNoteType = iota
SNTDesync
SNTConfigUpdate
SNTHealthcheck
SNTOverride
)
var syncNoteTypeNames = map[SyncNoteType]string{
SNTHeartbeat: "Heartbeat",
SNTDesync: "Desynchronisation",
SNTConfigUpdate: "Config Update",
SNTHealthcheck: "Healthcheck",
SNTOverride: "Override",
}
// String returns the string representation of a synchronisation notification
// type.
func (snt SyncNoteType) String() string {
if name, ok := syncNoteTypeNames[snt]; ok {
return name
}
return fmt.Sprintf("(Unknown %d)", snt)
}
// SyncNote represents a synchronisation notification.
type SyncNote struct {
Type SyncNoteType
Time time.Time
Config *config.Notification
Healthcheck *SyncHealthCheckNotification
BackendOverride *seesaw.BackendOverride
DestinationOverride *seesaw.DestinationOverride
VserverOverride *seesaw.VserverOverride
}
// SyncNotes specifies a collection of SyncNotes.
type SyncNotes struct {
Notes []SyncNote
}
// SeesawSync provides the synchronisation RPC interface to the Seesaw Engine.
type SeesawSync struct {
sync *syncServer
}
// Register registers our Seesaw peer for synchronisation notifications.
func (s *SeesawSync) Register(node net.IP, id *SyncSessionID) error {
if id == nil {
return errors.New("id is nil")
}
// TODO(jsing): Reject if not master?
session := s.sync.newSession(node)
log.Infof("Synchronisation session %d registered by %v", session.id, node)
*id = session.id
return nil
}
// Deregister deregisters a Seesaw peer for synchronisation.
func (s *SeesawSync) Deregister(id SyncSessionID, reply *int) error {
s.sync.sessionLock.Lock()
session, ok := s.sync.sessions[id]
delete(s.sync.sessions, id)
s.sync.sessionLock.Unlock()
if ok {
log.Infof("Synchronisation session %d deregistered with %v", id, session.node)
}
return nil
}
// Poll returns one or more synchronisation notifications to the caller,
// blocking until at least one notification becomes available or the poll
// timeout is reached.
func (s *SeesawSync) Poll(id SyncSessionID, sn *SyncNotes) error {
if sn == nil {
return errors.New("sync notes is nil")
}
s.sync.sessionLock.RLock()
session, ok := s.sync.sessions[id]
s.sync.sessionLock.RUnlock()
if !ok {
return errors.New("no session with ID %d")
}
// Reset expiry time and check for desynchronisation.
session.Lock()
session.expiryTime = time.Now().Add(sessionDeadtime)
if session.desync {
// TODO(jsing): Discard pending notes?
sn.Notes = append(sn.Notes, SyncNote{Type: SNTDesync, Time: time.Now()})
session.desync = false
session.Unlock()
return nil
}
session.Unlock()
// Block until a notification becomes available or our poll expires.
select {
case note := <-session.notes:
sn.Notes = append(sn.Notes, *note)
case <-time.After(syncPollTimeout):
return errors.New("poll timeout")
}
pollLoop:
for i := 0; i < syncPollMsgLimit; i++ {
select {
case note := <-session.notes:
sn.Notes = append(sn.Notes, *note)
default:
break pollLoop
}
}
log.V(1).Infof("Sync server poll returning %d notifications", len(sn.Notes))
return nil
}
// Config requests the current configuration from the peer Seesaw node.
func (s *SeesawSync) Config(arg int, config *config.Notification) error {
return errors.New("unimplemented")
}
// Failover requests that we relinquish master state.
func (s *SeesawSync) Failover(arg int, reply *int) error {
return s.sync.engine.haManager.requestFailover(true)
}
// Healthchecks requests the current healthchecks from the peer Seesaw node.
func (s *SeesawSync) Healthchecks(arg int, reply *int) error {
return errors.New("unimplemented")
}
// syncSession contains the data needed for a synchronisation session.
type syncSession struct {
id SyncSessionID
node net.IP
desync bool
startTime time.Time
expiryTime time.Time
sync.RWMutex
notes chan *SyncNote
}
// addNote adds a notification to the synchronisation session. If the notes
// channel is full the session is marked as desynchronised and the notification
// is discarded.
func (ss *syncSession) addNote(note *SyncNote) {
select {
case ss.notes <- note:
default:
ss.Lock()
if !ss.desync {
log.Warningf("Sync session with %v is desynchronised", ss.node)
ss.desync = true
}
ss.Unlock()
}
}
// syncServer encapsulates the data for a synchronisation server.
type syncServer struct {
engine *Engine
heartbeatInterval time.Duration
server *rpc.Server
sessionLock sync.RWMutex
nextSessionID SyncSessionID
sessions map[SyncSessionID]*syncSession
}
// newSyncServer returns an initalised synchronisation server.
func newSyncServer(e *Engine) *syncServer {
return &syncServer{
engine: e,
heartbeatInterval: syncHeartbeatInterval,
sessions: make(map[SyncSessionID]*syncSession),
}
}
// newSession allocates a new session ID and starts managing the session with
// the provided node.
func (s *syncServer) newSession(node net.IP) *syncSession {
s.sessionLock.Lock()
defer s.sessionLock.Unlock()
session := &syncSession{
id: s.nextSessionID,
node: node,
desync: true,
startTime: time.Now(),
expiryTime: time.Now().Add(sessionDeadtime),
notes: make(chan *SyncNote, sessionNotesQueueSize),
}
s.nextSessionID++
s.sessions[session.id] = session
return session
}
// serve accepts connections from the given TCP listener and dispatches each
// connection to the RPC server. Connections are only accepted from localhost
// and the seesaw node that we are configured to peer with.
func (s *syncServer) serve(l *net.TCPListener) error {
defer l.Close()
s.server = rpc.NewServer()
s.server.Register(&SeesawSync{s})
for {
c, err := l.AcceptTCP()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
time.Sleep(100 * time.Millisecond)
continue
}
return err
}
raddr := c.RemoteAddr().String()
host, _, err := net.SplitHostPort(raddr)
if err != nil {
log.Errorf("Failed to parse remote address %q: %v", raddr, err)
c.Close()
continue
}
rip := net.ParseIP(host)
if rip == nil || (!rip.IsLoopback() && !rip.Equal(s.engine.config.Peer.IPv4Addr) && !rip.Equal(s.engine.config.Peer.IPv6Addr)) {
log.Warningf("Rejecting connection from non-peer (%s)...", rip)
c.Close()
continue
}
log.Infof("Sync connection established from %s", rip)
go s.server.ServeConn(c)
}
}
// notify queues a synchronisation notification with each of the active
// synchronisation sessions.
func (s *syncServer) notify(sn *SyncNote) {
s.sessionLock.RLock()
sessions := s.sessions
s.sessionLock.RUnlock()
for _, ss := range sessions {
ss.addNote(sn)
}
}
// run runs the synchronisation server, which is responsible for queueing
// heartbeat notifications and removing expired synchronisation sessions.
func (s *syncServer) run() {
for now := range time.Tick(s.heartbeatInterval) {
s.sessionLock.Lock()
for id, ss := range s.sessions {
ss.RLock()
expiry := ss.expiryTime
ss.RUnlock()
if now.After(expiry) {
log.Warningf("Sync session %d with %v has expired", id, ss.node)
delete(s.sessions, id)
continue
}
ss.addNote(&SyncNote{Type: SNTHeartbeat, Time: now})
}
s.sessionLock.Unlock()
}
}
// syncClient contains the data needed by a synchronisation client.
type syncClient struct {
engine *Engine
dispatch func(*SyncNote)
conn *net.TCPConn
client *rpc.Client
enabled bool
refs uint
lock sync.Mutex
quit chan bool
start chan bool
stopped chan bool
}
// newSyncClient returns an initialised synchronisation client.
func newSyncClient(e *Engine) *syncClient {
sc := &syncClient{
engine: e,
quit: make(chan bool),
start: make(chan bool),
stopped: make(chan bool, 1),
}
sc.dispatch = sc.handleNote
sc.stopped <- true
return sc
}
// dial establishes a connection to our peer Seesaw node.
func (sc *syncClient) dial() error {
sc.lock.Lock()
defer sc.lock.Unlock()
if sc.client != nil {
sc.refs++
return nil
}
// TODO(jsing): Make this default to IPv6, if configured.
peer := &net.TCPAddr{
IP: sc.engine.config.Peer.IPv4Addr,
Port: sc.engine.config.SyncPort,
}
self := &net.TCPAddr{
IP: sc.engine.config.Node.IPv4Addr,
}
d := net.Dialer{
Timeout: time.Second * 2,
LocalAddr: self,
}
conn, err := d.Dial("tcp", peer.String())
if err != nil {
return fmt.Errorf("failed to connect: %v", err)
}
sc.client = rpc.NewClient(conn)
sc.refs = 1
return nil
}
// close closes an existing connection to our peer Seesaw node.
func (sc *syncClient) close() error {
sc.lock.Lock()
defer sc.lock.Unlock()
if sc.client == nil {
return nil
}
sc.refs--
if sc.refs > 0 {
return nil
}
if err := sc.client.Close(); err != nil {
sc.client = nil
return fmt.Errorf("client close failed: %v", err)
}
sc.client = nil
return nil
}
// failover requests that the peer node initiate a failover.
func (sc *syncClient) failover() error {
if err := sc.dial(); err != nil {
return err
}
defer sc.close()
return sc.client.Call("SeesawSync.Failover", 0, nil)
}
// runOnce establishes a connection to the synchronisation server, registers
// for notifications, polls for notifications, then deregisters.
func (sc *syncClient) runOnce() {
if err := sc.dial(); err != nil {
log.Warningf("Sync client dial failed: %v", err)
return
}
defer sc.close()
var sid SyncSessionID
self := sc.engine.config.Node.IPv4Addr
// Register for synchronisation events.
// TODO(jsing): Implement timeout on RPC?
if err := sc.client.Call("SeesawSync.Register", self, &sid); err != nil {
log.Warningf("Sync registration failed: %v", err)
return
}
log.Infof("Registered for synchronisation notifications (ID %d)", sid)
// TODO(jsing): Export synchronisation data to ECU/CLI.
sc.poll(sid)
// Attempt to deregister for notifications.
// TODO(jsing): Implement timeout on RPC?
if err := sc.client.Call("SeesawSync.Deregister", sid, nil); err != nil {
log.Warningf("Sync deregistration failed: %v", err)
}
}
// poll polls the synchronisation server for notifications, then dispatches
// them for processing.
func (sc *syncClient) poll(sid SyncSessionID) {
for {
var sn SyncNotes
poll := sc.client.Go("SeesawSync.Poll", sid, &sn, nil)
select {
case <-poll.Done:
if poll.Error != nil {
log.Errorf("Synchronisation polling failed: %v", poll.Error)
return
}
for _, note := range sn.Notes {
sc.dispatch(¬e)
}
case <-sc.quit:
sc.stopped <- true
return
case <-time.After(syncPollTimeout):
log.Warningf("Synchronisation polling timed out after %s", syncPollTimeout)
return
}
}
}
// handleNote dispatches a synchronisation note to the appropriate handler.
func (sc *syncClient) handleNote(note *SyncNote) {
switch note.Type {
case SNTHeartbeat:
log.V(1).Infoln("Sync client received heartbeat")
case SNTDesync:
sc.handleDesync()
case SNTConfigUpdate:
sc.handleConfigUpdate(note)
case SNTHealthcheck:
sc.handleHealthcheck(note)
case SNTOverride:
sc.handleOverride(note)
default:
log.Errorf("Unable to handle sync notification type %s (%d)", note.Type, note.Type)
}
}
// handleDesync handles a desync notification.
func (sc *syncClient) handleDesync() {
log.V(1).Infoln("Sync client desynchronised...")
// TODO(jsing): Fetch all state - config, healthchecks, overrides...
}
// handleConfigUpdate handles a config update notification.
func (sc *syncClient) handleConfigUpdate(sn *SyncNote) {
log.V(1).Infoln("Sync client received config update notification")
// TODO(jsing): Implement.
}
// handleHealthcheck handles a healthcheck notification.
func (sc *syncClient) handleHealthcheck(sn *SyncNote) {
log.V(1).Infoln("Sync client received healthcheck notification")
}
// handleOverride handles an override notification.
func (sc *syncClient) handleOverride(sn *SyncNote) {
log.V(1).Infoln("Sync client received override notification")
if o := sn.VserverOverride; o != nil {
sc.engine.queueOverride(o)
}
if o := sn.DestinationOverride; o != nil {
sc.engine.queueOverride(o)
}
if o := sn.BackendOverride; o != nil {
sc.engine.queueOverride(o)
}
}
// run runs the synchronisation client.
func (sc *syncClient) run() {
for {
select {
case <-sc.stopped:
<-sc.start
log.Infof("Starting sync client...")
default:
sc.runOnce()
select {
// TODO: If we receive on quit inside runOnce, we have to wait the
// 5s here before enable will work again.
case <-time.After(5 * time.Second):
case <-sc.quit:
sc.stopped <- true
}
}
}
}
func (sc *syncClient) peerConfigured() bool {
return sc.engine.config.Peer.IPv4Addr != nil || sc.engine.config.Peer.IPv6Addr != nil
}
// enable enables synchronisation with our peer Seesaw node.
func (sc *syncClient) enable() {
if !sc.peerConfigured() {
return
}
sc.lock.Lock()
start := !sc.enabled
sc.enabled = true
sc.lock.Unlock()
if start {
sc.start <- true
}
}
// disable disables synchronisation with our peer Seesaw node.
func (sc *syncClient) disable() {
if !sc.peerConfigured() {
return
}
sc.lock.Lock()
quit := sc.enabled
sc.enabled = false
sc.lock.Unlock()
if quit {
sc.quit <- true
}
}