forked from ExploratoryEngineering/clusterfunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfcluster_state.go
83 lines (71 loc) · 1.82 KB
/
cfcluster_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
package funk
import (
"time"
"github.com/lab5e/clusterfunk/pkg/lg"
)
// This file contains the internal state methods
func (c *clusterfunkCluster) logStateChange() {
lg.Debug("State changed state=%s role=%s", c.state, c.role)
}
func (c *clusterfunkCluster) sendEvent(ev Event) {
c.mutex.Lock()
defer c.mutex.Unlock()
for _, v := range c.eventChannels {
select {
case v <- ev:
// great success
case <-time.After(1 * time.Second):
// drop event
}
}
}
func (c *clusterfunkCluster) setState(newState NodeState) {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
if c.state != newState {
c.state = newState
c.logStateChange()
c.sendEvent(Event{State: newState, Role: c.role})
}
}
func (c *clusterfunkCluster) State() NodeState {
c.stateMutex.RLock()
defer c.stateMutex.RUnlock()
return c.state
}
func (c *clusterfunkCluster) Role() NodeRole {
c.stateMutex.RLock()
defer c.stateMutex.RUnlock()
return c.role
}
func (c *clusterfunkCluster) setRole(newRole NodeRole) {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
c.role = newRole
c.logStateChange()
}
func (c *clusterfunkCluster) ProcessedIndex() uint64 {
c.stateMutex.RLock()
defer c.stateMutex.RUnlock()
return c.processedIndex
}
// SetProcessedIndex sets the last processed index. The returned value is
// the current value of the processed index.
func (c *clusterfunkCluster) setProcessedIndex(index uint64) uint64 {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
if index > c.processedIndex {
c.processedIndex = index
}
return c.processedIndex
}
func (c *clusterfunkCluster) CurrentShardMapIndex() uint64 {
c.stateMutex.RLock()
defer c.stateMutex.RUnlock()
return c.currentShardMapIndex
}
func (c *clusterfunkCluster) setCurrentShardMapIndex(index uint64) {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
c.currentShardMapIndex = index
}