-
Notifications
You must be signed in to change notification settings - Fork 634
/
operate_util.go
226 lines (196 loc) · 5.81 KB
/
operate_util.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
// 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 master
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/errors"
"github.com/chubaofs/chubaofs/util/exporter"
"github.com/chubaofs/chubaofs/util/log"
"math/rand"
"strings"
"time"
)
func newCreateDataPartitionRequest(volName string, ID uint64, members []proto.Peer, dataPartitionSize int, hosts []string, createType int) (req *proto.CreateDataPartitionRequest) {
req = &proto.CreateDataPartitionRequest{
PartitionId: ID,
PartitionSize: dataPartitionSize,
VolumeId: volName,
Members: members,
Hosts: hosts,
CreateType: createType,
}
return
}
func newDeleteDataPartitionRequest(ID uint64) (req *proto.DeleteDataPartitionRequest) {
req = &proto.DeleteDataPartitionRequest{
PartitionId: ID,
}
return
}
func newAddDataPartitionRaftMemberRequest(ID uint64, addPeer proto.Peer) (req *proto.AddDataPartitionRaftMemberRequest) {
req = &proto.AddDataPartitionRaftMemberRequest{
PartitionId: ID,
AddPeer: addPeer,
}
return
}
func newRemoveDataPartitionRaftMemberRequest(ID uint64, removePeer proto.Peer) (req *proto.RemoveDataPartitionRaftMemberRequest) {
req = &proto.RemoveDataPartitionRaftMemberRequest{
PartitionId: ID,
RemovePeer: removePeer,
}
return
}
func newLoadDataPartitionMetricRequest(ID uint64) (req *proto.LoadDataPartitionRequest) {
req = &proto.LoadDataPartitionRequest{
PartitionId: ID,
}
return
}
func unmarshalTaskResponse(task *proto.AdminTask) (err error) {
bytes, err := json.Marshal(task.Response)
if err != nil {
return
}
var response interface{}
switch task.OpCode {
case proto.OpDataNodeHeartbeat:
response = &proto.DataNodeHeartbeatResponse{}
case proto.OpDeleteDataPartition:
response = &proto.DeleteDataPartitionResponse{}
case proto.OpLoadDataPartition:
response = &proto.LoadDataPartitionResponse{}
case proto.OpDeleteFile:
response = &proto.DeleteFileResponse{}
case proto.OpMetaNodeHeartbeat:
response = &proto.MetaNodeHeartbeatResponse{}
case proto.OpDeleteMetaPartition:
response = &proto.DeleteMetaPartitionResponse{}
case proto.OpUpdateMetaPartition:
response = &proto.UpdateMetaPartitionResponse{}
case proto.OpDecommissionMetaPartition:
response = &proto.MetaPartitionDecommissionResponse{}
default:
log.LogError(fmt.Sprintf("unknown operate code(%v)", task.OpCode))
}
if response == nil {
return fmt.Errorf("unmarshalTaskResponse failed")
}
if err = json.Unmarshal(bytes, response); err != nil {
return
}
task.Response = response
return
}
func contains(arr []string, element string) (ok bool) {
if arr == nil || len(arr) == 0 {
return
}
for _, e := range arr {
if e == element {
ok = true
break
}
}
return
}
func containsID(arr []uint64, element uint64) (ok bool) {
if arr == nil || len(arr) == 0 {
return
}
for _, e := range arr {
if e == element {
ok = true
break
}
}
return
}
func reshuffleHosts(oldHosts []string) (newHosts []string, err error) {
if oldHosts == nil || len(oldHosts) == 0 {
log.LogError(fmt.Sprintf("action[reshuffleHosts],err:%v", proto.ErrReshuffleArray))
err = proto.ErrReshuffleArray
return
}
lenOldHosts := len(oldHosts)
newHosts = make([]string, lenOldHosts)
if lenOldHosts == 1 {
copy(newHosts, oldHosts)
return
}
for i := lenOldHosts; i > 1; i-- {
rand.Seed(time.Now().UnixNano())
oCurrPos := rand.Intn(i)
oldHosts[i-1], oldHosts[oCurrPos] = oldHosts[oCurrPos], oldHosts[i-1]
}
copy(newHosts, oldHosts)
return
}
// Warn provides warnings when exits
func Warn(clusterID, msg string) {
key := fmt.Sprintf("%s_%s", clusterID, ModuleName)
WarnBySpecialKey(key, msg)
}
// WarnBySpecialKey provides warnings when exits
func WarnBySpecialKey(key, msg string) {
log.LogWarn(msg)
exporter.Warning(msg)
}
func keyNotFound(name string) (err error) {
return errors.NewErrorf("parameter %v not found", name)
}
func unmatchedKey(name string) (err error) {
return errors.NewErrorf("parameter %v not match", name)
}
func notFoundMsg(name string) (err error) {
return errors.NewErrorf("%v not found", name)
}
func metaPartitionNotFound(id uint64) (err error) {
return notFoundMsg(fmt.Sprintf("meta partition[%v]", id))
}
func metaReplicaNotFound(addr string) (err error) {
return notFoundMsg(fmt.Sprintf("meta replica[%v]", addr))
}
func dataPartitionNotFound(id uint64) (err error) {
return notFoundMsg(fmt.Sprintf("data partition[%v]", id))
}
func dataReplicaNotFound(addr string) (err error) {
return notFoundMsg(fmt.Sprintf("data replica[%v]", addr))
}
func zoneNotFound(name string) (err error) {
return notFoundMsg(fmt.Sprintf("zone[%v]", name))
}
func dataNodeNotFound(addr string) (err error) {
return notFoundMsg(fmt.Sprintf("data node[%v]", addr))
}
func metaNodeNotFound(addr string) (err error) {
return notFoundMsg(fmt.Sprintf("meta node[%v]", addr))
}
func volNotFound(name string) (err error) {
return notFoundMsg(fmt.Sprintf("vol[%v]", name))
}
func matchKey(serverKey, clientKey string) bool {
h := md5.New()
_, err := h.Write([]byte(serverKey))
if err != nil {
log.LogWarnf("action[matchKey] write server key[%v] failed,err[%v]", serverKey, err)
return false
}
cipherStr := h.Sum(nil)
return strings.ToLower(clientKey) == strings.ToLower(hex.EncodeToString(cipherStr))
}