This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstate.go
582 lines (452 loc) · 13 KB
/
state.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
package curator
import (
"errors"
"fmt"
"log"
"net"
"sync"
"sync/atomic"
"time"
"github.com/samuel/go-zookeeper/zk"
)
const MAX_BACKGROUND_ERRORS = 10
var (
ErrConnectionLoss = errors.New("connection loss")
ErrTimeout = errors.New("timeout")
)
type zookeeperHelper interface {
GetConnectionString() string
GetZookeeperConnection() (ZookeeperConnection, error)
}
type zookeeperFactory struct {
holder *handleHolder
}
func (f *zookeeperFactory) GetConnectionString() string { return "" }
func (f *zookeeperFactory) GetZookeeperConnection() (ZookeeperConnection, error) {
connectString := f.holder.ensembleProvider.ConnectionString()
conn, events, err := f.holder.zookeeperDialer.Dial(connectString, f.holder.sessionTimeout, f.holder.canBeReadOnly)
if err != nil {
return nil, err
}
f.holder.SetHelper(&zookeeperCache{connectString, conn})
if events != nil {
go NewWatchers(f.holder.watcher).Watch(events)
}
return conn, err
}
type zookeeperCache struct {
connnectString string
conn ZookeeperConnection
}
func (c *zookeeperCache) GetConnectionString() string { return c.connnectString }
func (c *zookeeperCache) GetZookeeperConnection() (ZookeeperConnection, error) { return c.conn, nil }
type handleHolder struct {
zookeeperDialer ZookeeperDialer
ensembleProvider EnsembleProvider
watcher Watcher
sessionTimeout time.Duration
canBeReadOnly bool
sync.RWMutex // This mutex only protects helper yet
helper zookeeperHelper
}
// SetHelper sets the inner zookeeperHelper atomically
func (h *handleHolder) SetHelper(helper zookeeperHelper) {
h.Lock()
defer h.Unlock()
h.helper = helper
}
// Helper gets the inner zookeeperHelper atomically
func (h *handleHolder) Helper() zookeeperHelper {
h.RLock()
defer h.RUnlock()
return h.helper
}
func (h *handleHolder) getConnectionString() string {
helper := h.Helper()
if helper != nil {
return helper.GetConnectionString()
}
return ""
}
func (h *handleHolder) hasNewConnectionString() bool {
helper := h.Helper()
if helper != nil {
return h.ensembleProvider.ConnectionString() != helper.GetConnectionString()
}
return false
}
func (h *handleHolder) getZookeeperConnection() (ZookeeperConnection, error) {
helper := h.Helper()
if helper != nil {
return helper.GetZookeeperConnection()
}
return nil, nil
}
func (h *handleHolder) closeAndClear() error {
if _, ok := h.Helper().(*zookeeperFactory); ok {
return nil
}
err := h.internalClose()
h.SetHelper(nil)
return err
}
func (h *handleHolder) closeAndReset() error {
if err := h.internalClose(); err != nil {
return err
}
h.SetHelper(&zookeeperFactory{holder: h})
return nil
}
func (h *handleHolder) internalClose() error {
if h.Helper() != nil {
if conn, err := h.getZookeeperConnection(); err != nil {
return err
} else if conn != nil {
conn.Close()
}
}
return nil
}
type connectionState struct {
ensembleProvider EnsembleProvider
sessionTimeout time.Duration
connectionTimeout time.Duration
tracer TracerDriver
parentWatchers *Watchers
zooKeeper *handleHolder
instanceIndex int64
connectionStart *atomic.Value
isConnected AtomicBool
backgroundErrors chan error
}
func newConnectionState(zookeeperDialer ZookeeperDialer, ensembleProvider EnsembleProvider, sessionTimeout, connectionTimeout time.Duration,
parentWatcher Watcher, tracer TracerDriver, canBeReadOnly bool) *connectionState {
s := &connectionState{
ensembleProvider: ensembleProvider,
sessionTimeout: sessionTimeout,
connectionTimeout: connectionTimeout,
tracer: tracer,
parentWatchers: NewWatchers(),
connectionStart: new(atomic.Value),
backgroundErrors: make(chan error, MAX_BACKGROUND_ERRORS),
}
s.connectionStart.Store(time.Now())
if zookeeperDialer == nil {
zookeeperDialer = &DefaultZookeeperDialer{Dialer: net.DialTimeout}
}
s.zooKeeper = &handleHolder{
zookeeperDialer: zookeeperDialer,
ensembleProvider: ensembleProvider,
watcher: s,
sessionTimeout: sessionTimeout,
canBeReadOnly: canBeReadOnly,
}
if parentWatcher != nil {
s.parentWatchers.Add(parentWatcher)
}
return s
}
func (s *connectionState) Connected() bool {
return s.isConnected.Load()
}
func (s *connectionState) InstanceIndex() int64 {
return atomic.LoadInt64(&s.instanceIndex)
}
func (s *connectionState) Conn() (ZookeeperConnection, error) {
if err := s.dequeBackgroundException(); err != nil {
return nil, err
}
if !s.isConnected.Load() {
if err := s.checkTimeout(); err != nil {
return nil, err
}
}
return s.zooKeeper.getZookeeperConnection()
}
func (s *connectionState) Start() error {
if err := s.ensembleProvider.Start(); err != nil {
return err
}
return s.reset()
}
func (s *connectionState) Close() error {
CloseQuietly(s.ensembleProvider)
err := s.zooKeeper.closeAndClear()
s.isConnected.Set(false)
return err
}
func (s *connectionState) reset() error {
atomic.AddInt64(&s.instanceIndex, 1)
s.isConnected.Set(false)
if err := s.zooKeeper.closeAndReset(); err != nil {
return err
}
_, err := s.zooKeeper.getZookeeperConnection() // initiate connection
return err
}
func (s *connectionState) AddParentWatcher(watcher Watcher) Watcher {
return s.parentWatchers.Add(watcher)
}
func (s *connectionState) RemoveParentWatcher(watcher Watcher) Watcher {
return s.parentWatchers.Remove(watcher)
}
func (s *connectionState) checkTimeout() error {
var minTimeout, maxTimeout time.Duration
if s.sessionTimeout > s.connectionTimeout {
minTimeout = s.connectionTimeout
maxTimeout = s.sessionTimeout
} else {
minTimeout = s.sessionTimeout
maxTimeout = s.connectionTimeout
}
elapsed := time.Since(s.connectionStart.Load().(time.Time))
if elapsed >= minTimeout {
if s.zooKeeper.hasNewConnectionString() {
s.handleNewConnectionString()
} else if elapsed >= maxTimeout {
log.Printf("Connection attempt unsuccessful after %v (greater than max timeout of %v). Resetting connection and trying again with a new connection.", elapsed, maxTimeout)
s.tracer.AddCount("session-timed-out", 1)
return s.reset()
} else {
log.Printf("Connection timed out for connection string (%s) and timeout (%v) / elapsed (%v)", s.zooKeeper.getConnectionString(), s.connectionTimeout, elapsed)
s.tracer.AddCount("connections-timed-out", 1)
return ErrConnectionLoss
}
}
return nil
}
func (s *connectionState) process(event *zk.Event) {
//log.Printf("connectionState.process received %v with %d watchers", event, s.parentWatchers.Len())
for _, watcher := range s.parentWatchers.watchers {
if watcher == nil {
continue
}
go func(w Watcher) {
tracer := newTimeTracer("connection-state-parent-process", s.tracer)
defer tracer.Commit()
w.process(event)
}(watcher)
}
if event.Type == zk.EventSession {
wasConnected := s.isConnected.Load()
if newIsConnected := s.checkState(event.State, event.Err, wasConnected); newIsConnected != wasConnected {
s.isConnected.Set(newIsConnected)
s.connectionStart.Store(time.Now())
}
}
}
func (s *connectionState) checkState(state zk.State, err error, wasConnected bool) bool {
isConnected := wasConnected
checkNewConnectionString := true
switch state {
case zk.StateHasSession:
isConnected = true
case zk.StateExpired:
isConnected = false
checkNewConnectionString = false
s.handleExpiredSession()
case zk.StateConnecting, zk.StateConnected, zk.StateDisconnected:
isConnected = false
default:
isConnected = false
}
if checkNewConnectionString && s.zooKeeper.hasNewConnectionString() {
isConnected = false
s.handleNewConnectionString()
}
return isConnected
}
func (s *connectionState) handleNewConnectionString() {
log.Print("Connection string changed")
s.tracer.AddCount("connection-string-changed", 1)
if err := s.reset(); err != nil {
s.queueBackgroundException(err)
}
}
func (s *connectionState) handleExpiredSession() {
log.Print("Session expired event received")
s.tracer.AddCount("session-expired", 1)
if err := s.reset(); err != nil {
s.queueBackgroundException(err)
}
}
func (s *connectionState) queueBackgroundException(err error) {
for {
select {
case s.backgroundErrors <- err:
return
default:
}
if _, ok := <-s.backgroundErrors; !ok {
return
} else {
s.tracer.AddCount("connection-drop-background-error", 1)
}
}
}
func (s *connectionState) dequeBackgroundException() error {
select {
case err := <-s.backgroundErrors:
if err != nil {
s.tracer.AddCount("background-exceptions", 1)
return err
}
default:
}
return nil
}
type ConnectionState int32
const (
UNKNOWN ConnectionState = iota
CONNECTED // Sent for the first successful connection to the server.
SUSPENDED // There has been a loss of connection. Leaders, locks, etc.
RECONNECTED // A suspended, lost, or read-only connection has been re-established
LOST // The connection is confirmed to be lost. Close any locks, leaders, etc.
READ_ONLY // The connection has gone into read-only mode.
)
var connectionStateNames = []string{
"UNKNOWN", "CONNECTED", "SUSPENDED", "RECONNECTED", "LOST", "READ_ONLY",
}
func (s ConnectionState) Connected() bool {
return s == CONNECTED || s == RECONNECTED || s == READ_ONLY
}
func (s ConnectionState) String() string {
return connectionStateNames[s]
}
const STATE_QUEUE_SIZE = 25
type connectionStateManager struct {
client CuratorFramework
listeners ConnectionStateListenable
state State
currentConnectionState ConnectionState
lock sync.Mutex
initialConnectMessageSent AtomicBool
events chan ConnectionState
QueueSize int
closed chan struct{}
}
func newConnectionStateManager(client CuratorFramework) *connectionStateManager {
return &connectionStateManager{
client: client,
listeners: new(connectionStateListenerContainer),
QueueSize: STATE_QUEUE_SIZE,
}
}
func (m *connectionStateManager) Start() error {
if !m.state.Change(LATENT, STARTED) {
return fmt.Errorf("Cannot be started more than once")
}
m.events = make(chan ConnectionState, m.QueueSize)
m.closed = make(chan struct{})
go m.processEvents()
return nil
}
func (m *connectionStateManager) Close() {
if !m.state.Change(STARTED, STOPPED) {
return
}
close(m.closed)
m.listeners.Clear()
}
func (m *connectionStateManager) Listenable() ConnectionStateListenable {
return m.listeners
}
// Change to ConnectionState.SUSPENDED only if not already suspended and not lost
func (m *connectionStateManager) SetToSuspended() bool {
m.lock.Lock()
defer m.lock.Unlock()
if m.state.Value() != STARTED {
return false
}
if m.currentConnectionState == LOST || m.currentConnectionState == SUSPENDED {
return false
}
m.currentConnectionState = SUSPENDED
m.postState(SUSPENDED)
return true
}
// Post a state change. If the manager is already in that state the change is ignored.
// Otherwise the change is queued for listeners.
func (m *connectionStateManager) AddStateChange(newConnectionState ConnectionState) bool {
m.lock.Lock()
defer m.lock.Unlock()
if m.state.Value() != STARTED {
return false
}
if m.currentConnectionState == newConnectionState {
return false
}
m.currentConnectionState = newConnectionState
localState := newConnectionState
switch newConnectionState {
case LOST, SUSPENDED, READ_ONLY:
break
default:
if m.initialConnectMessageSent.CompareAndSwap(false, true) {
localState = CONNECTED
}
}
m.postState(localState)
return true
}
func (m *connectionStateManager) BlockUntilConnected(maxWaitTime time.Duration) error {
if m.currentConnectionState.Connected() {
return nil
}
var isConnected = make(chan struct{}, 1)
listener := NewConnectionStateListener(func(client CuratorFramework, newState ConnectionState) {
if newState.Connected() {
select {
case isConnected <- struct{}{}:
default:
}
}
})
m.listeners.AddListener(listener)
defer m.listeners.RemoveListener(listener)
// Double-check that we are still not connected.
// To make sure we didn't miss the event while adding listener.
if m.currentConnectionState.Connected() {
return nil
}
if maxWaitTime == 0 {
<-isConnected
return nil
}
select {
case <-isConnected:
return nil
case <-time.After(maxWaitTime):
return ErrTimeout
}
}
func (m *connectionStateManager) Connected() bool {
m.lock.Lock()
defer m.lock.Unlock()
return m.currentConnectionState.Connected()
}
func (m *connectionStateManager) postState(state ConnectionState) {
for {
select {
case <-m.closed:
return
case m.events <- state:
return
default:
// Event queue is full - dropping events to make room.
<-m.events
}
}
}
func (m *connectionStateManager) processEvents() {
for {
select {
case <-m.closed:
return
case newState := <-m.events:
m.listeners.ForEach(func(listener interface{}) {
listener.(ConnectionStateListener).StateChanged(m.client, newState)
})
}
}
}