forked from libi/dcron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodepool.go
141 lines (127 loc) · 3.01 KB
/
nodepool.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
package dcron
import (
"context"
"log"
"sync"
"time"
"github.com/BugKillerPro/dcron/consistenthash"
"github.com/BugKillerPro/dcron/dlog"
"github.com/BugKillerPro/dcron/driver"
)
// NodePool is a node pool
type NodePool struct {
serviceName string
nodeID string
rwMut sync.RWMutex
nodes *consistenthash.Map
driver driver.DriverV2
hashReplicas int
hashFn consistenthash.Hash
updateDuration time.Duration
logger dlog.Logger
stopChan chan int
preNodes []string // sorted
}
func NewNodePool(serviceName string, drv driver.DriverV2, updateDuration time.Duration, hashReplicas int, logger dlog.Logger) INodePool {
np := &NodePool{
serviceName: serviceName,
driver: drv,
hashReplicas: hashReplicas,
updateDuration: updateDuration,
logger: &dlog.StdLogger{
Log: log.Default(),
},
stopChan: make(chan int, 1),
}
if logger != nil {
np.logger = logger
}
np.driver.Init(serviceName,
driver.NewTimeoutOption(updateDuration),
driver.NewLoggerOption(np.logger))
return np
}
func (np *NodePool) Start(ctx context.Context) (err error) {
err = np.driver.Start(ctx)
if err != nil {
np.logger.Errorf("start pool error: %v", err)
return
}
np.nodeID = np.driver.NodeID()
nowNodes, err := np.driver.GetNodes(ctx)
if err != nil {
np.logger.Errorf("get nodes error: %v", err)
return
}
np.updateHashRing(nowNodes)
go np.waitingForHashRing()
return
}
// Check if this job can be run in this node.
func (np *NodePool) CheckJobAvailable(jobName string) bool {
np.rwMut.RLock()
defer np.rwMut.RUnlock()
if np.nodes == nil {
np.logger.Errorf("nodeID=%s, np.nodes is nil", np.nodeID)
}
if np.nodes.IsEmpty() {
return false
}
targetNode := np.nodes.Get(jobName)
if np.nodeID == targetNode {
np.logger.Infof("job %s, running in node: %s", jobName, targetNode)
}
return np.nodeID == targetNode
}
func (np *NodePool) Stop(ctx context.Context) error {
np.stopChan <- 1
np.driver.Stop(ctx)
np.preNodes = make([]string, 0)
return nil
}
func (np *NodePool) GetNodeID() string {
return np.nodeID
}
func (np *NodePool) waitingForHashRing() {
tick := time.NewTicker(np.updateDuration)
for {
select {
case <-tick.C:
nowNodes, err := np.driver.GetNodes(context.Background())
if err != nil {
np.logger.Errorf("get nodes error %v", err)
continue
}
np.updateHashRing(nowNodes)
case <-np.stopChan:
return
}
}
}
func (np *NodePool) updateHashRing(nodes []string) {
np.rwMut.Lock()
defer np.rwMut.Unlock()
if np.equalRing(nodes) {
//np.logger.Infof("nowNodes=%v, preNodes=%v", nodes, np.preNodes)
return
}
np.logger.Infof("update hashRing nodes=%+v", nodes)
np.preNodes = make([]string, len(nodes))
copy(np.preNodes, nodes)
np.nodes = consistenthash.New(np.hashReplicas, np.hashFn)
for _, v := range nodes {
np.nodes.Add(v)
}
}
func (np *NodePool) equalRing(a []string) bool {
if len(a) == len(np.preNodes) {
la := len(a)
for i := 0; i < la; i++ {
if a[i] != np.preNodes[i] {
return false
}
}
return true
}
return false
}