-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster_handler.go
333 lines (284 loc) · 8.58 KB
/
cluster_handler.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
package gds
import (
"fmt"
"github.com/hashicorp/go-hclog"
"sync"
"time"
"github.com/integration-system/gds/cluster"
"github.com/integration-system/gds/jobs"
"github.com/integration-system/gds/provider"
"github.com/integration-system/gds/store"
"github.com/integration-system/gds/utils"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
const (
assigningInterval = 100 * time.Millisecond
batchSize = 1000
)
var (
json = jsoniter.ConfigFastest
)
type ClusterHandler struct {
cluster *cluster.Client
executor executor
typeProvider provider.TypeProvider
nextPeer *utils.RoundRobinStrings
assignJobsChLock sync.RWMutex
assignJobsCh chan []string
logger hclog.Logger
}
func (cl *ClusterHandler) HandleAddPeerCommand(state store.WritableState, data []byte) (interface{}, error) {
payload := cluster.AddPeer{}
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal cluster.AddPeer")
}
state.AddPeer(payload.PeerID)
cl.nextPeer.Update(state.GetPeers())
return nil, nil
}
func (cl *ClusterHandler) HandleRemovePeerCommand(state store.WritableState, data []byte) (interface{}, error) {
payload := cluster.RemovePeer{}
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal cluster.RemovePeer")
}
state.RemovePeer(payload.PeerID)
cl.nextPeer.Update(state.GetPeers())
if cl.cluster.IsLeader() {
jobs := state.GetPeerJobsKeys(payload.PeerID)
cl.assignJobs(jobs)
}
state.UnassignPeer(payload.PeerID)
return nil, nil
}
func (cl *ClusterHandler) HandleInsertJobCommand(state store.WritableState, data []byte) (interface{}, error) {
payload := cluster.InsertJob{}
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal cluster.InsertJob")
}
f := cl.typeProvider.Get(payload.Type)
job := f()
err = job.Unmarshal(payload.Job)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal job data")
}
err = state.InsertJob(job)
if err != nil {
return nil, err
}
if cl.cluster.IsLeader() {
cl.assignJobs([]string{job.Key()})
}
return nil, nil
}
func (cl *ClusterHandler) HandleDeleteJobCommand(state store.WritableState, data []byte) (interface{}, error) {
payload := cluster.DeleteJob{}
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal cluster.DeleteJob")
}
jobInfo, err := state.GetJob(payload.Key)
if err != nil {
return nil, err
}
if cl.cluster.LocalID() == jobInfo.AssignedPeerID {
cl.executor.CancelJob(payload.Key)
}
state.DeleteJob(payload.Key)
return nil, nil
}
func (cl *ClusterHandler) HandleAcquireJobCommand(state store.WritableState, data []byte) (interface{}, error) {
payload := cluster.AcquireJob{}
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal cluster.AcquireJob")
}
for _, key := range payload.JobKeys {
jobInfo, err := state.GetJob(key)
if err != nil {
continue
}
// if duplicate assignment
if jobInfo.AssignedPeerID == payload.PeerID {
continue
}
if jobInfo.State == jobs.StateExhausted {
continue
}
state.AcquireJob(key, payload.PeerID)
if cl.cluster.LocalID() == payload.PeerID {
cl.executor.AddJob(jobInfo.Job)
}
}
return nil, nil
}
func (cl *ClusterHandler) HandleJobExecutedCommand(state store.WritableState, data []byte) (interface{}, error) {
payload := cluster.JobExecuted{}
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal cluster.JobExecuted")
}
state.ApplyPostExecution(payload.JobKey, payload.Error, payload.ExecutedTime)
jobInfo, err := state.GetJob(payload.JobKey)
if err != nil {
return nil, err
}
if cl.cluster.LocalID() == jobInfo.AssignedPeerID {
if jobInfo.State != jobs.StateExhausted {
cl.executor.AddJob(jobInfo.Job)
}
}
return nil, nil
}
func (cl *ClusterHandler) GetHandlers() map[uint64]func(store.WritableState, []byte) (interface{}, error) {
return map[uint64]func(store.WritableState, []byte) (interface{}, error){
cluster.AddPeerCommand: cl.HandleAddPeerCommand,
cluster.RemovePeerCommand: cl.HandleRemovePeerCommand,
cluster.InsertJobCommand: cl.HandleInsertJobCommand,
cluster.DeleteJobCommand: cl.HandleDeleteJobCommand,
cluster.AcquireJobCommand: cl.HandleAcquireJobCommand,
cluster.JobExecutedCommand: cl.HandleJobExecutedCommand,
}
}
func (cl *ClusterHandler) listenLeaderCh(mainStore *store.Store) {
closeCh := make(chan struct{})
for isLeader := range cl.cluster.LeaderCh() {
if !isLeader {
close(closeCh)
continue
}
closeCh = make(chan struct{})
assignJobsCh := make(chan []string, batchSize)
// TODO get rid of mutex
cl.assignJobsChLock.Lock()
cl.assignJobsCh = assignJobsCh
cl.assignJobsChLock.Unlock()
go cl.checkPeers(closeCh, mainStore.VisitReadonlyState)
go cl.checkJobs(closeCh, mainStore.VisitReadonlyState)
go cl.backgroundAssigningJobs(closeCh, assignJobsCh)
}
}
// Used to detect changes in onlinePeers while there was no leader in cluster (e.g. another peer got down).
func (cl *ClusterHandler) checkPeers(closeCh chan struct{}, visitState func(f func(store.ReadonlyState))) {
defer func() {
if err := recover(); err != nil {
cl.logger.Error(fmt.Sprintf("panic on check peers: %v", err))
}
}()
time.Sleep(200 * time.Millisecond)
var oldServers []string
visitState(func(state store.ReadonlyState) {
oldServers = state.GetPeers()
})
var newServers []string
newServers, err := cl.cluster.Servers()
// error only occurs during leadership transferring, so there will be new leader soon
if err != nil {
return
}
_, deleted := utils.CompareSlices(oldServers, newServers)
for _, peerID := range deleted {
select {
case <-closeCh:
return
default:
}
cmd := cluster.PrepareRemovePeerCommand(peerID)
_, _ = cl.cluster.SyncApplyHelper(cmd, "RemovePeerCommand")
}
}
// Checks all jobs to have assignedPeer.
func (cl *ClusterHandler) checkJobs(closeCh chan struct{}, visitState func(f func(store.ReadonlyState))) {
time.Sleep(300 * time.Millisecond)
var unassignedJobsKeys []string
visitState(func(state store.ReadonlyState) {
unassignedJobsKeys = state.GetUnassignedJobsKeys()
})
select {
case <-closeCh:
return
default:
}
cl.assignJobs(unassignedJobsKeys)
}
func (cl *ClusterHandler) backgroundAssigningJobs(closeCh chan struct{}, assignJobsCh chan []string) {
defer func() {
if err := recover(); err != nil {
cl.logger.Error(fmt.Sprintf("panic on assigning jobs: %v", err))
}
}()
jobKeys := make([]string, 0, batchSize)
ticker := time.NewTicker(assigningInterval)
defer ticker.Stop()
sendEvents := func(keys []string) {
peerID := cl.nextPeer.Get()
cmd := cluster.PrepareAcquireJobCommand(keys, peerID)
_, _ = cl.cluster.SyncApplyHelper(cmd, "AcquireJobCommand")
}
for {
select {
case newJobKeys := <-assignJobsCh:
jobKeys = append(jobKeys, newJobKeys...)
if len(jobKeys) >= 2*batchSize {
// TODO send batches async in select?
var batches [][]string
for batchSize < len(jobKeys) {
jobKeys, batches = jobKeys[batchSize:], append(batches, jobKeys[0:batchSize:batchSize])
}
batches = append(batches, jobKeys)
for _, batch := range batches {
sendEvents(batch)
}
// create new slice to allow gc previous big one
jobKeys = make([]string, 0, batchSize)
} else if len(jobKeys) >= batchSize {
jobKeys = utils.MakeUnique(jobKeys)
sendEvents(jobKeys)
jobKeys = jobKeys[:0]
}
case <-ticker.C:
if len(jobKeys) == 0 {
continue
}
jobKeys = utils.MakeUnique(jobKeys)
sendEvents(jobKeys)
jobKeys = jobKeys[:0]
case <-closeCh:
return
}
}
}
func (cl *ClusterHandler) assignJobs(keys []string) {
if !cl.cluster.IsLeader() {
return
}
if keys == nil {
return
}
cl.assignJobsChLock.RLock()
cl.assignJobsCh <- keys
cl.assignJobsChLock.RUnlock()
}
func (cl *ClusterHandler) handleExecutedJobs(executedJobsCh <-chan cluster.JobExecuted) {
defer func() {
if err := recover(); err != nil {
cl.logger.Error(fmt.Sprintf("panic on executed jobs: %v", err))
}
}()
for payload := range executedJobsCh {
cmd := cluster.PrepareJobExecutedCommand(payload.JobKey, payload.Error, payload.ExecutedTime)
// TODO handle errors. retry?
_, _ = cl.cluster.SyncApplyHelper(cmd, "JobExecutedCommand")
}
}
func NewClusterHandler(typeProvider provider.TypeProvider, executor executor, logger hclog.Logger) *ClusterHandler {
return &ClusterHandler{
typeProvider: typeProvider,
executor: executor,
nextPeer: utils.NewRoundRobinStrings(make([]string, 0)),
logger: logger,
}
}