forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_partition.go
551 lines (498 loc) · 16 KB
/
meta_partition.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// 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 (
"sync"
"fmt"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/errors"
"github.com/chubaofs/chubaofs/util/log"
"strings"
"time"
)
// MetaReplica defines the replica of a meta partition
type MetaReplica struct {
Addr string
start uint64 // lower bound of the inode id
end uint64 // upper bound of the inode id
nodeID uint64
ReportTime int64
Status int8 // unavailable, readOnly, readWrite
IsLeader bool
metaNode *MetaNode
}
// MetaPartition defines the structure of a meta partition
type MetaPartition struct {
PartitionID uint64
Start uint64
End uint64
MaxNodeID uint64
Replicas []*MetaReplica
ReplicaNum uint8
Status int8
volID uint64
volName string
Hosts []string
Peers []proto.Peer
MissNodes map[string]int64
LoadResponse []*proto.MetaPartitionLoadResponse
sync.RWMutex
}
func newMetaReplica(start, end uint64, metaNode *MetaNode) (mr *MetaReplica) {
mr = &MetaReplica{start: start, end: end, nodeID: metaNode.ID, Addr: metaNode.Addr}
mr.metaNode = metaNode
mr.ReportTime = time.Now().Unix()
return
}
func newMetaPartition(partitionID, start, end uint64, replicaNum uint8, volName string, volID uint64) (mp *MetaPartition) {
mp = &MetaPartition{PartitionID: partitionID, Start: start, End: end, volName: volName, volID: volID}
mp.ReplicaNum = replicaNum
mp.Replicas = make([]*MetaReplica, 0)
mp.Status = proto.Unavailable
mp.MissNodes = make(map[string]int64, 0)
mp.Peers = make([]proto.Peer, 0)
mp.Hosts = make([]string, 0)
mp.LoadResponse = make([]*proto.MetaPartitionLoadResponse, 0)
return
}
func (mp *MetaPartition) setPeers(peers []proto.Peer) {
mp.Peers = peers
}
func (mp *MetaPartition) setHosts(hosts []string) {
mp.Hosts = hosts
}
func (mp *MetaPartition) hostsToString() (hosts string) {
return strings.Join(mp.Hosts, underlineSeparator)
}
func (mp *MetaPartition) addReplica(mr *MetaReplica) {
for _, m := range mp.Replicas {
if m.Addr == mr.Addr {
return
}
}
mp.Replicas = append(mp.Replicas, mr)
return
}
func (mp *MetaPartition) removeReplica(mr *MetaReplica) {
var newReplicas []*MetaReplica
for _, m := range mp.Replicas {
if m.Addr == mr.Addr {
continue
}
newReplicas = append(newReplicas, m)
}
mp.Replicas = newReplicas
return
}
func (mp *MetaPartition) removeReplicaByAddr(addr string) {
var newReplicas []*MetaReplica
for _, m := range mp.Replicas {
if m.Addr == addr {
continue
}
newReplicas = append(newReplicas, m)
}
mp.Replicas = newReplicas
return
}
func (mp *MetaPartition) updateInodeIDRangeForAllReplicas() {
for _, mr := range mp.Replicas {
mr.end = mp.End
}
}
func (mp *MetaPartition) updateInodeIDRange(c *Cluster, end uint64) (err error) {
// overflow
if end > (defaultMaxMetaPartitionInodeID - defaultMetaPartitionInodeIDStep) {
log.LogWarnf("action[updateInodeIDRange] clusterID[%v] partitionID[%v] nextStart[%v] "+
"to prevent overflow ,not update end", c.Name, mp.PartitionID, end)
return
}
tasks := make([]*proto.AdminTask, 0)
oldEnd := mp.End
mp.End = end
t := mp.createTaskToUpdateMetaReplica(c.Name, mp.PartitionID, end)
// if there is no leader, we don't update the end
if t == nil {
mp.End = oldEnd
return
}
if err = c.syncUpdateMetaPartition(mp); err != nil {
mp.End = oldEnd
log.LogErrorf("action[updateInodeIDRange] partitionID[%v] err[%v]", mp.PartitionID, err)
return proto.ErrPersistenceByRaft
}
mp.updateInodeIDRangeForAllReplicas()
tasks = append(tasks, t)
c.addMetaNodeTasks(tasks)
if err = c.createMetaPartition(mp.volName, mp.End+1, defaultMaxMetaPartitionInodeID); err != nil {
Warn(c.Name, fmt.Sprintf("action[updateInodeIDRange] clusterID[%v] partitionID[%v] create meta partition err[%v]",
c.Name, mp.PartitionID, err))
goto errHandler
}
log.LogWarnf("action[updateInodeIDRange] partitionID[%v] end[%v] success", mp.PartitionID, mp.End)
return
errHandler:
log.LogErrorf("action[updateInodeIDRange] partitionID[%v] err[%v]", mp.PartitionID, err)
return
}
func (mp *MetaPartition) checkEnd(c *Cluster, maxPartitionID uint64) {
if mp.PartitionID < maxPartitionID {
return
}
vol, err := c.getVol(mp.volName)
if err != nil {
log.LogWarnf("action[checkEnd] vol[%v] not exist", mp.volName)
return
}
mp.Lock()
defer mp.Unlock()
curMaxPartitionID := vol.maxPartitionID()
if mp.PartitionID != curMaxPartitionID {
log.LogWarnf("action[checkEnd] partition[%v] not max partition[%v]", mp.PartitionID, curMaxPartitionID)
return
}
if mp.End != defaultMaxMetaPartitionInodeID {
oldEnd := mp.End
mp.End = defaultMaxMetaPartitionInodeID
if err := c.syncUpdateMetaPartition(mp); err != nil {
mp.End = oldEnd
log.LogErrorf("action[checkEnd] partitionID[%v] err[%v]", mp.PartitionID, err)
}
}
log.LogWarnf("action[checkEnd] partitionID[%v] end[%v]", mp.PartitionID, mp.End)
}
func (mp *MetaPartition) getMetaReplica(addr string) (mr *MetaReplica, err error) {
for _, mr = range mp.Replicas {
if mr.Addr == addr {
return
}
}
return nil, metaReplicaNotFound(addr)
}
func (mp *MetaPartition) removeMissingReplica(addr string) {
if _, ok := mp.MissNodes[addr]; ok {
delete(mp.MissNodes, addr)
}
}
func (mp *MetaPartition) checkLeader() {
mp.Lock()
defer mp.Unlock()
for _, mr := range mp.Replicas {
if !mr.isActive() {
mr.IsLeader = false
}
}
return
}
func (mp *MetaPartition) checkStatus(writeLog bool, replicaNum int) {
mp.Lock()
defer mp.Unlock()
liveReplicas := mp.getLiveReplicas()
if len(liveReplicas) <= replicaNum/2 {
mp.Status = proto.Unavailable
} else {
mr, err := mp.getMetaReplicaLeader()
if err != nil {
mp.Status = proto.Unavailable
}
mp.Status = mr.Status
}
if writeLog {
log.LogInfof("action[checkMPStatus],id:%v,status:%v,replicaNum:%v,liveReplicas:%v persistenceHosts:%v",
mp.PartitionID, mp.Status, mp.ReplicaNum, len(liveReplicas), mp.Hosts)
}
}
func (mp *MetaPartition) getMetaReplicaLeader() (mr *MetaReplica, err error) {
for _, mr = range mp.Replicas {
if mr.IsLeader {
return
}
}
err = proto.ErrNoLeader
return
}
func (mp *MetaPartition) checkReplicaNum(c *Cluster, volName string, replicaNum uint8) {
mp.RLock()
defer mp.RUnlock()
if mp.ReplicaNum != replicaNum {
msg := fmt.Sprintf("FIX MetaPartition replicaNum clusterID[%v] vol[%v] replica num[%v],current num[%v]",
c.Name, volName, replicaNum, mp.ReplicaNum)
Warn(c.Name, msg)
}
}
func (mp *MetaPartition) removeIllegalReplica() (excessAddr string, t *proto.AdminTask, err error) {
mp.RLock()
defer mp.RUnlock()
for _, mr := range mp.Replicas {
if !contains(mp.Hosts, mr.Addr) {
t = mr.createTaskToDeleteReplica(mp.PartitionID)
err = proto.ErrIllegalMetaReplica
break
}
}
return
}
func (mp *MetaPartition) missingReplicaAddrs() (lackAddrs []string) {
mp.RLock()
defer mp.RUnlock()
var liveReplicas []string
for _, mr := range mp.Replicas {
liveReplicas = append(liveReplicas, mr.Addr)
}
for _, host := range mp.Hosts {
if !contains(liveReplicas, host) {
lackAddrs = append(lackAddrs, host)
break
}
}
return
}
func (mp *MetaPartition) updateMetaPartition(mgr *proto.MetaPartitionReport, metaNode *MetaNode) {
if !contains(mp.Hosts, metaNode.Addr) {
return
}
mp.Lock()
defer mp.Unlock()
mr, err := mp.getMetaReplica(metaNode.Addr)
if err != nil {
mr = newMetaReplica(mp.Start, mp.End, metaNode)
mp.addReplica(mr)
}
mp.MaxNodeID = mgr.MaxInodeID
mr.updateMetric(mgr)
mp.removeMissingReplica(metaNode.Addr)
}
func (mp *MetaPartition) canBeOffline(nodeAddr string, replicaNum int) (err error) {
liveReplicas := mp.getLiveReplicas()
if len(liveReplicas) < int(mp.ReplicaNum/2+1) {
err = proto.ErrNoEnoughReplica
return
}
liveAddrs := mp.getLiveReplicasAddr(liveReplicas)
if len(liveReplicas) == (replicaNum/2+1) && contains(liveAddrs, nodeAddr) {
err = fmt.Errorf("live replicas num will be less than majority after offline nodeAddr: %v", nodeAddr)
return
}
return
}
func (mp *MetaPartition) getLiveReplicasAddr(liveReplicas []*MetaReplica) (addrs []string) {
addrs = make([]string, 0)
for _, mr := range liveReplicas {
addrs = append(addrs, mr.Addr)
}
return
}
func (mp *MetaPartition) getLiveReplicas() (liveReplicas []*MetaReplica) {
liveReplicas = make([]*MetaReplica, 0)
for _, mr := range mp.Replicas {
if mr.isActive() {
liveReplicas = append(liveReplicas, mr)
}
}
return
}
func (mp *MetaPartition) persistToRocksDB(newHosts []string, newPeers []proto.Peer, volName string, c *Cluster) (err error) {
oldHosts := make([]string, len(mp.Hosts))
copy(oldHosts, mp.Hosts)
oldPeers := make([]proto.Peer, len(mp.Peers))
copy(oldPeers, mp.Peers)
mp.Hosts = newHosts
mp.Peers = newPeers
if err = c.syncUpdateMetaPartition(mp); err != nil {
mp.Hosts = oldHosts
mp.Peers = oldPeers
log.LogWarnf("action[persistToRocksDB] failed,partitionID:%v old hosts:%v new hosts:%v oldPeers:%v newPeers:%v",
mp.PartitionID, mp.Hosts, newHosts, mp.Peers, newPeers)
return
}
log.LogWarnf("action[persistToRocksDB] success,partitionID:%v old hosts:%v new hosts:%v oldPeers:%v newPeers:%v ",
mp.PartitionID, oldHosts, mp.Hosts, oldPeers, mp.Peers)
return
}
func (mp *MetaPartition) getActiveAddrs() (liveAddrs []string) {
liveAddrs = make([]string, 0)
for _, mr := range mp.Replicas {
if mr.isActive() {
liveAddrs = append(liveAddrs, mr.Addr)
}
}
return liveAddrs
}
func (mp *MetaPartition) isMissingReplica(addr string) bool {
return !contains(mp.getActiveAddrs(), addr)
}
func (mp *MetaPartition) shouldReportMissingReplica(addr string, interval int64) (isWarn bool) {
lastWarningTime, ok := mp.MissNodes[addr]
if !ok {
isWarn = true
mp.MissNodes[addr] = time.Now().Unix()
} else if (time.Now().Unix() - lastWarningTime) > interval {
isWarn = true
mp.MissNodes[addr] = time.Now().Unix()
}
return false
}
func (mp *MetaPartition) reportMissingReplicas(clusterID string, seconds, interval int64) {
mp.Lock()
defer mp.Unlock()
for _, replica := range mp.Replicas {
// reduce the alarm frequency
if contains(mp.Hosts, replica.Addr) && replica.isMissing() && mp.shouldReportMissingReplica(replica.Addr, interval) {
metaNode := replica.metaNode
var (
lastReportTime time.Time
)
isActive := true
if metaNode != nil {
lastReportTime = metaNode.ReportTime
isActive = metaNode.IsActive
}
msg := fmt.Sprintf("action[reportMissingReplicas], clusterID[%v] volName[%v] partition:%v on Node:%v "+
"miss time > :%v vlocLastRepostTime:%v dnodeLastReportTime:%v nodeisActive:%v",
clusterID, mp.volName, mp.PartitionID, replica.Addr, seconds, replica.ReportTime, lastReportTime, isActive)
Warn(clusterID, msg)
}
}
for _, addr := range mp.Hosts {
if mp.isMissingReplica(addr) && mp.shouldReportMissingReplica(addr, interval) {
msg := fmt.Sprintf("action[reportMissingReplicas],clusterID[%v] volName[%v] partition:%v on Node:%v "+
"miss time > %v ",
clusterID, mp.volName, mp.PartitionID, addr, defaultMetaPartitionTimeOutSec)
Warn(clusterID, msg)
}
}
}
func (mp *MetaPartition) replicaCreationTasks(clusterID, volName string) (tasks []*proto.AdminTask) {
var msg string
tasks = make([]*proto.AdminTask, 0)
if addr, task, err := mp.removeIllegalReplica(); err != nil {
msg = fmt.Sprintf("action[%v],clusterID[%v] metaPartition:%v excess replication"+
" on :%v err:%v persistenceHosts:%v",
deleteIllegalReplicaErr, clusterID, mp.PartitionID, addr, err.Error(), mp.Hosts)
log.LogWarn(msg)
tasks = append(tasks, task)
}
if addrs := mp.missingReplicaAddrs(); addrs != nil {
msg = fmt.Sprintf("action[missingReplicaAddrs],clusterID[%v] metaPartition:%v lack replication"+
" on :%v Hosts:%v",
clusterID, mp.PartitionID, addrs, mp.Hosts)
Warn(clusterID, msg)
}
return
}
func (mp *MetaPartition) buildNewMetaPartitionTasks(specifyAddrs []string, peers []proto.Peer, volName string) (tasks []*proto.AdminTask) {
tasks = make([]*proto.AdminTask, 0)
hosts := make([]string, 0)
req := &proto.CreateMetaPartitionRequest{
Start: mp.Start,
End: mp.End,
PartitionID: mp.PartitionID,
Members: peers,
VolName: volName,
}
if specifyAddrs == nil {
hosts = mp.Hosts
} else {
hosts = specifyAddrs
}
for _, addr := range hosts {
t := proto.NewAdminTask(proto.OpCreateMetaPartition, addr, req)
resetMetaPartitionTaskID(t, mp.PartitionID)
tasks = append(tasks, t)
}
return
}
func (mp *MetaPartition) createTaskToDecommissionReplica(volName string, removePeer proto.Peer, addPeer proto.Peer) (t *proto.AdminTask, err error) {
mr, err := mp.getMetaReplicaLeader()
if err != nil {
return nil, errors.NewError(err)
}
req := &proto.MetaPartitionDecommissionRequest{PartitionID: mp.PartitionID, VolName: volName, RemovePeer: removePeer, AddPeer: addPeer}
t = proto.NewAdminTask(proto.OpDecommissionMetaPartition, mr.Addr, req)
resetMetaPartitionTaskID(t, mp.PartitionID)
return
}
func resetMetaPartitionTaskID(t *proto.AdminTask, partitionID uint64) {
t.ID = fmt.Sprintf("%v_pid[%v]", t.ID, partitionID)
}
func (mp *MetaPartition) createTaskToUpdateMetaReplica(clusterID string, partitionID uint64, end uint64) (t *proto.AdminTask) {
mr, err := mp.getMetaReplicaLeader()
if err != nil {
msg := fmt.Sprintf("action[createTaskToUpdateMetaReplica] clusterID[%v] meta partition %v no leader",
clusterID, mp.PartitionID)
Warn(clusterID, msg)
return
}
req := &proto.UpdateMetaPartitionRequest{PartitionID: partitionID, End: end, VolName: mp.volName}
t = proto.NewAdminTask(proto.OpUpdateMetaPartition, mr.Addr, req)
resetMetaPartitionTaskID(t, mp.PartitionID)
return
}
func (mr *MetaReplica) createTaskToDeleteReplica(partitionID uint64) (t *proto.AdminTask) {
req := &proto.DeleteMetaPartitionRequest{PartitionID: partitionID}
t = proto.NewAdminTask(proto.OpDeleteMetaPartition, mr.Addr, req)
resetMetaPartitionTaskID(t, partitionID)
return
}
func (mr *MetaReplica) createTaskToLoadMetaPartition(partitionID uint64) (t *proto.AdminTask) {
req := &proto.MetaPartitionLoadRequest{PartitionID: partitionID}
t = proto.NewAdminTask(proto.OpLoadMetaPartition, mr.Addr, req)
resetMetaPartitionTaskID(t, partitionID)
return
}
func (mr *MetaReplica) isMissing() (miss bool) {
return time.Now().Unix()-mr.ReportTime > defaultMetaPartitionTimeOutSec
}
func (mr *MetaReplica) isActive() (active bool) {
return mr.metaNode.IsActive && mr.Status != proto.Unavailable &&
time.Now().Unix()-mr.ReportTime < defaultMetaPartitionTimeOutSec
}
func (mr *MetaReplica) setLastReportTime() {
mr.ReportTime = time.Now().Unix()
}
func (mr *MetaReplica) updateMetric(mgr *proto.MetaPartitionReport) {
mr.Status = (int8)(mgr.Status)
mr.IsLeader = mgr.IsLeader
mr.setLastReportTime()
}
func (mp *MetaPartition) updateMetricByRaft(mpv *metaPartitionValue) {
mp.Start = mpv.Start
mp.End = mpv.End
mp.Peers = mpv.Peers
mp.Hosts = strings.Split(mpv.Hosts, underlineSeparator)
}
func (mp *MetaPartition) afterCreation(nodeAddr string, c *Cluster) (err error) {
metaNode, err := c.metaNode(nodeAddr)
if err != nil {
return err
}
mr := newMetaReplica(mp.Start, mp.End, metaNode)
mr.Status = proto.ReadWrite
mp.addReplica(mr)
mp.removeMissingReplica(mr.Addr)
return
}
func (mp *MetaPartition) addOrReplaceLoadResponse(response *proto.MetaPartitionLoadResponse) {
mp.Lock()
defer mp.Unlock()
loadResponse := make([]*proto.MetaPartitionLoadResponse, 0)
for _, lr := range mp.LoadResponse {
if lr.Addr == response.Addr {
continue
}
loadResponse = append(loadResponse, lr)
}
loadResponse = append(loadResponse, response)
mp.LoadResponse = loadResponse
}