forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_task.go
129 lines (109 loc) · 3.45 KB
/
admin_task.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
// Copyright 2018 The Chubao Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 proto
import (
"fmt"
"time"
)
const (
TaskFailed = 2
TaskStart = 0
TaskSucceeds = 1
TaskRunning = 3
ResponseInterval = 5
ResponseTimeOut = 100
MaxSendCount = 5
)
// AdminTask defines the administration task.
type AdminTask struct {
ID string
PartitionID uint64
OpCode uint8
OperatorAddr string
Status int8
SendTime int64
CreateTime int64
SendCount uint8
Request interface{}
Response interface{}
}
// ToString returns the string format of the task.
func (t *AdminTask) ToString() (msg string) {
msg = fmt.Sprintf("ID[%v] Status[%d] LastSendTime[%v] SendCount[%v] Request[%v] Response[%v]",
t.ID, t.Status, t.SendTime, t.SendCount, t.Request, t.Response)
return
}
func (t *AdminTask) IdString() string {
return fmt.Sprintf("id:%s_sendTime_%d_createTime_%d", t.ID, t.SendTime, t.CreateTime)
}
// CheckTaskNeedSend checks if the task needs to be sent out.
func (t *AdminTask) CheckTaskNeedSend() (needRetry bool) {
if (int)(t.SendCount) < MaxSendCount && time.Now().Unix()-t.SendTime > (int64)(ResponseInterval) {
needRetry = true
}
return
}
// CheckTaskTimeOut checks if the task is timed out.
func (t *AdminTask) CheckTaskTimeOut() (notResponse bool) {
if (int)(t.SendCount) >= MaxSendCount || (t.SendTime > 0 && (time.Now().Unix()-t.SendTime > int64(ResponseTimeOut))) {
notResponse = true
}
return
}
// SetStatus sets the status of the task.
func (t *AdminTask) SetStatus(status int8) {
t.Status = status
}
// IsTaskSuccessful returns if the task has been executed successful.
func (t *AdminTask) IsTaskSuccessful() (isSuccess bool) {
if t.Status == TaskSucceeds {
isSuccess = true
}
return
}
// IsTaskFailed returns if the task failed.
func (t *AdminTask) IsTaskFailed() (isFail bool) {
if t.Status == TaskFailed {
isFail = true
}
return
}
// IsUrgentTask returns if the task is urgent.
func (t *AdminTask) IsUrgentTask() bool {
return t.isCreateTask() || t.isLoadTask() || t.isUpdateMetaPartitionTask()
}
// isUpdateMetaPartitionTask checks if the task is to update the meta partition.
func (t *AdminTask) isUpdateMetaPartitionTask() bool {
return t.OpCode == OpUpdateMetaPartition
}
func (t *AdminTask) isLoadTask() bool {
return t.OpCode == OpLoadDataPartition
}
func (t *AdminTask) isCreateTask() bool {
return t.OpCode == OpCreateDataPartition || t.OpCode == OpCreateMetaPartition
}
// IsHeartbeatTask returns if the task is a heartbeat task.
func (t *AdminTask) IsHeartbeatTask() bool {
return t.OpCode == OpDataNodeHeartbeat || t.OpCode == OpMetaNodeHeartbeat
}
// NewAdminTask returns a new adminTask.
func NewAdminTask(opCode uint8, opAddr string, request interface{}) (t *AdminTask) {
t = new(AdminTask)
t.OpCode = opCode
t.Request = request
t.OperatorAddr = opAddr
t.ID = fmt.Sprintf("addr[%v]_op[%v]", t.OperatorAddr, t.OpCode)
t.CreateTime = time.Now().Unix()
return
}