-
Notifications
You must be signed in to change notification settings - Fork 251
/
data_check.go
369 lines (319 loc) · 11.2 KB
/
data_check.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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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.
*
*/
package scheduler
import (
"fmt"
"time"
alarm "github.com/Tencent/bk-bcs/bcs-common/common/bcs-health/api"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/pkg/scheduler/schetypes"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/manager/store"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/util"
)
// DataCheckMsg DataChecker control message
type DataCheckMsg struct {
// opencheck
// closecheck
// stop
MsgType string
}
// DataCheckMgr DataChecker
type DataCheckMgr struct {
store store.Store
// we need this to call some functions
sched *Scheduler
msgQueue chan *DataCheckMsg
//appCheckStatusTime int64
//appTimeoutTime int64
openCheck bool
}
// CreateDataCheckMgr Create DataChecker
func CreateDataCheckMgr(store store.Store, s *Scheduler) (*DataCheckMgr, error) {
mgr := &DataCheckMgr{
store: store,
sched: s,
openCheck: false,
}
// create msg queue for events
mgr.msgQueue = make(chan *DataCheckMsg, 128)
return mgr, nil
}
func (mgr *DataCheckMgr) stop() {
blog.V(3).Infof("data checker stop...")
close(mgr.msgQueue)
}
// SendMsg Send DataChecker control message
func (mgr *DataCheckMgr) SendMsg(msg *DataCheckMsg) error {
blog.V(3).Infof("data checker: send an msg to datacheck manager")
select {
case mgr.msgQueue <- msg:
default:
blog.Error("data checker: send an msg to datacheck manager fail")
return fmt.Errorf("data checker: mgr queue is full now")
}
return nil
}
// DataCheckManage DataChecker main function
func DataCheckManage(mgr *DataCheckMgr, doRecover bool) {
blog.Info("data checker: goroutine start ...")
/*if doRecover {
time.Sleep(180 * time.Second)
blog.Warn("data checker will set all LOST&FINISH taskgroup to RUNNING !!!!!!!")
mgr.doRecover()
}*/
for {
select {
case req := <-mgr.msgQueue:
blog.Info("data checker: receive msg: %s", req.MsgType)
if req.MsgType == "opencheck" {
mgr.openCheck = true
} else if req.MsgType == "closecheck" {
mgr.openCheck = false
} else if req.MsgType == "stop" {
mgr.stop()
blog.Info("data checker: goroutine finish!")
}
case <-time.After(time.Second * time.Duration(DATA_CHECK_INTERVAL)):
blog.Info("data checker: to do data check")
mgr.doCheck()
}
}
}
func (mgr *DataCheckMgr) doCheck() {
if mgr.openCheck == false {
blog.Info("data checker: opencheck is false, do nothing")
return
}
// add mesos heartbeat checking
mgr.sched.lockService()
heartbeat := mgr.sched.mesosHeartBeatTime
now := time.Now().Unix()
mgr.sched.unlockService()
blog.Info("data checker: doCheck begin.... now(%d)", now)
if now-heartbeat > MESOS_HEARTBEAT_TIMEOUT {
blog.Warn("data checker: mesos heartbeat timeout, now(%d), heatbeat(%d), so skip checking",
now, heartbeat)
return
}
defer func() {
now = time.Now().Unix()
blog.Info("data checker: doCheck end.... now(%d)", now)
}()
runAses, err := mgr.store.ListRunAs()
if err != nil {
blog.Error("data checker: fail to list runAses, err:%s", err.Error())
return
}
//check application taskgroup whether lost
for _, runAs := range runAses {
blog.Info("data checker: to check runAs(%s)", runAs)
appIDs, err := mgr.store.ListApplicationNodes(runAs)
if err != nil {
blog.Error("data checker: fail to list %s, err:%s", runAs, err.Error())
continue
}
if nil == appIDs {
blog.Warn("data checker: no application nodes under runAs:%s", runAs)
continue
}
for _, appID := range appIDs {
blog.Info("data checker: to check application:%s.%s ", runAs, appID)
//mgr.checkApplication(runAs, appID)
mgr.checkTaskgroup(runAs, appID)
}
}
//check daemonsets taskgroup whether lost
daemonsets, err := mgr.store.ListAllDaemonset()
if err != nil {
blog.Error("data checker ListAllDaemonset failed: %s", err.Error())
return
}
for _, daemon := range daemonsets {
blog.Info("data checker: to check daemonest(%s) taskgroup status", daemon.GetUuid())
//lock
util.Lock.Lock(types.BcsDaemonset{}, daemon.GetUuid())
taskgroups, _ := mgr.store.ListDaemonsetTaskGroups(daemon.NameSpace, daemon.Name)
lostNum := mgr.checkTaskgroupWhetherLost(taskgroups, false)
if lostNum == 0 {
util.Lock.UnLock(types.BcsDaemonset{}, daemon.GetUuid())
continue
}
//if have new lost taskgroup, then update daemonset status
mgr.sched.updateDaemonsetStatus(daemon.NameSpace, daemon.Name)
util.Lock.UnLock(types.BcsDaemonset{}, daemon.GetUuid())
}
return
}
func (mgr *DataCheckMgr) checkTaskgroup(runAs, appID string) {
mgr.sched.store.LockApplication(runAs + "." + appID)
defer mgr.sched.store.UnLockApplication(runAs + "." + appID)
blog.Info("data checker: to check taskgroups:%s.%s ", runAs, appID)
taskGroups, _ := mgr.store.ListTaskGroups(runAs, appID)
//check taskgroup whether lost
lostNum := mgr.checkTaskgroupWhetherLost(taskGroups, true)
if lostNum == 0 {
return
}
// get application data from ZK
app, err := mgr.sched.store.FetchApplication(runAs, appID)
if err != nil {
blog.Error("data checker: fetch application(%s.%s) failed, err:%s", runAs, appID, err.Error())
return
}
appStatus := app.Status
isUpdated, _ := mgr.sched.updateApplicationStatus(app)
if isUpdated == true {
if err = mgr.sched.store.SaveApplication(app); err != nil {
blog.Error("data checker: save application(%s.%s) into db failed! err:%s",
app.RunAs, app.ID, err.Error())
return
}
}
mgr.sched.applicationStatusUpdated(app, appStatus)
}
//if taskgroup status long time no update, then show taskgroup lost
//application't taskgroup trigger reschedule, and daemonset't taskgroup don't trigger reschedule
func (mgr *DataCheckMgr) checkTaskgroupWhetherLost(taskGroups []*types.TaskGroup, reschedule bool) int {
now := time.Now().Unix()
lostnum := 0
for _, taskGroup := range taskGroups {
if taskGroup.LastUpdateTime == 0 ||
(taskGroup.Status != types.TASKGROUP_STATUS_RUNNING &&
taskGroup.Status != types.TASKGROUP_STATUS_STAGING &&
taskGroup.Status != types.TASKGROUP_STATUS_STARTING) {
continue
}
var updateInterval int64
switch taskGroup.Status {
case types.TASKGROUP_STATUS_RUNNING, types.TASKGROUP_STATUS_STARTING:
updateInterval = 4 * MAX_DATA_UPDATE_INTERVAL
/*case types.TASKGROUP_STATUS_STAGING:
updateInterval = 4 * MAX_STAGING_UPDATE_INTERVAL*/
}
if taskGroup.LastUpdateTime+updateInterval < now {
for _, task := range taskGroup.Taskgroup {
if task.Status != types.TASK_STATUS_RUNNING && task.Status != types.TASK_STATUS_STAGING && task.Status != types.TASK_STATUS_STARTING {
continue
}
if task.LastUpdateTime+updateInterval < now {
blog.Warn("data checker: task(%s) lost for long time not report status under status(%s)",
task.ID, task.Status)
task.LastStatus = task.Status
task.Status = types.TASK_STATUS_LOST
task.LastUpdateTime = now
mgr.sched.SendHealthMsg(alarm.WarnKind, taskGroup.RunAs, task.ID+"("+taskGroup.HostName+")"+"long time not report status, set status to lost", "", nil)
}
}
lostnum++
blog.Warn("data checker: taskgroup(%s) lost for long time not report status under status(%s)",
taskGroup.ID, taskGroup.Status)
taskGroupStatus := taskGroup.Status
taskGroup.LastStatus = taskGroup.Status
taskGroup.Status = types.TASKGROUP_STATUS_LOST
taskGroup.LastUpdateTime = now
//if reschedule==true, then trigger reschedule function
if reschedule {
mgr.sched.taskGroupStatusUpdated(taskGroup, taskGroupStatus)
mgr.sched.ServiceMgr.TaskgroupUpdate(taskGroup)
}
if err := mgr.sched.store.SaveTaskGroup(taskGroup); err != nil {
blog.Errorf("data checker: save taskgroup(%s) into db failed! err:%s", taskGroup.ID, err.Error())
return 0
}
}
}
return lostnum
}
//donot call this function !!!!!!!!!!!
func (mgr *DataCheckMgr) doRecover() {
now := time.Now().Unix()
blog.Info("data checker: doRecover begin.... now(%d)", now)
defer func() {
now = time.Now().Unix()
blog.Info("data checker: doRecover end.... now(%d)", now)
}()
runAses, err := mgr.store.ListRunAs()
if err != nil {
blog.Error("data checker: fail to list runAses, err:%s", err.Error())
return
}
for _, runAs := range runAses {
appIDs, err := mgr.store.ListApplicationNodes(runAs)
if err != nil {
blog.Error("data checker: fail to list %s, err:%s", runAs, err.Error())
continue
}
if nil == appIDs {
blog.Warn("data checker: no application nodes under runAs:%s", runAs)
continue
}
for _, appID := range appIDs {
blog.Info("data checker: to recover application:%s.%s ", runAs, appID)
mgr.recoverTaskgroup(runAs, appID)
}
}
return
}
func (mgr *DataCheckMgr) recoverTaskgroup(runAs, appID string) {
now := time.Now().Unix()
mgr.sched.store.LockApplication(runAs + "." + appID)
defer mgr.sched.store.UnLockApplication(runAs + "." + appID)
taskGroups, err := mgr.store.ListTaskGroups(runAs, appID)
if err != nil {
blog.Warn("data checker: list taskgroup(%s.%s) return err:%s", runAs, appID, err.Error())
return
}
blog.Info("data checker: to recover taskgroups:%s.%s ", runAs, appID)
recoverNum := 0
for _, taskGroup := range taskGroups {
if taskGroup.Status == types.TASKGROUP_STATUS_LOST || taskGroup.Status == types.TASKGROUP_STATUS_FINISH {
for _, task := range taskGroup.Taskgroup {
if task.Status == types.TASK_STATUS_LOST {
blog.Warn("data checker: recover task %s from %s to running ", task.ID, task.Status)
task.LastStatus = task.Status
task.Status = types.TASK_STATUS_RUNNING
task.LastUpdateTime = now
}
}
recoverNum++
blog.Warn("data checker: recover taskgroup %s from %s to running", taskGroup.ID, taskGroup.Status)
taskGroupStatus := taskGroup.Status
taskGroup.LastStatus = taskGroup.Status
taskGroup.Status = types.TASKGROUP_STATUS_RUNNING
taskGroup.LastUpdateTime = now
mgr.sched.taskGroupStatusUpdated(taskGroup, taskGroupStatus)
mgr.sched.ServiceMgr.TaskgroupUpdate(taskGroup)
if err = mgr.sched.store.SaveTaskGroup(taskGroup); err != nil {
blog.Error("data checker: save taskgroup(%s) into db failed! err:%s", taskGroup.ID, err.Error())
return
}
}
}
if recoverNum > 0 {
app, err := mgr.sched.store.FetchApplication(runAs, appID)
if err != nil {
blog.Error("data checker: fetch application(%s.%s) failed, err:%s", runAs, appID, err.Error())
return
}
appStatus := app.Status
isUpdated, err := mgr.sched.updateApplicationStatus(app)
if isUpdated == true {
if err = mgr.sched.store.SaveApplication(app); err != nil {
blog.Error("data checker: save application(%s.%s) into db failed! err:%s",
app.RunAs, app.ID, err.Error())
return
}
}
mgr.sched.applicationStatusUpdated(app, appStatus)
}
}