/
filecrc.go
131 lines (110 loc) · 2.77 KB
/
filecrc.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
// 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 (
"fmt"
"time"
)
// FileCrc defines the crc of a file
type FileCrc struct {
crc uint32
count int
meta *FileMetadata
}
func newFileCrc(volCrc uint32) (fc *FileCrc) {
fc = new(FileCrc)
fc.crc = volCrc
fc.count = 1
return
}
type fileCrcSorter []*FileCrc
func (fileCrcArr fileCrcSorter) Less(i, j int) bool {
return fileCrcArr[i].count < fileCrcArr[j].count
}
func (fileCrcArr fileCrcSorter) Swap(i, j int) {
fileCrcArr[i], fileCrcArr[j] = fileCrcArr[j], fileCrcArr[i]
}
func (fileCrcArr fileCrcSorter) Len() (length int) {
length = len(fileCrcArr)
return
}
func (fileCrcArr fileCrcSorter) log() (msg string) {
for _, fileCrc := range fileCrcArr {
addr := fileCrc.meta.getLocationAddr()
count := fileCrc.count
crc := fileCrc.crc
msg = fmt.Sprintf(msg+" addr:%v count:%v crc:%v ", addr, count, crc)
}
return
}
func (fc *FileInCore) shouldCheckCrc() bool {
return time.Now().Unix()-fc.LastModify > defaultIntervalToCheckCrc
}
func (fc *FileInCore) needCrcRepair(liveReplicas []*DataReplica) (fms []*FileMetadata, needRepair bool) {
var baseCrc uint32
fms = make([]*FileMetadata, 0)
for i := 0; i < len(liveReplicas); i++ {
vol := liveReplicas[i]
if fm, ok := fc.getFileMetaByAddr(vol); ok {
fms = append(fms, fm)
}
}
if len(fms) == 0 {
return
}
baseCrc = fms[0].Crc
for _, fm := range fms {
if fm.getFileCrc() == EmptyCrcValue || fm.getFileCrc() == 0 {
needRepair = false
return
}
if fm.getFileCrc() != baseCrc {
needRepair = true
return
}
}
return
}
func hasSameSize(fms []*FileMetadata) (same bool) {
sentry := fms[0].Size
for _, fm := range fms {
if fm.Size != sentry {
return
}
}
return true
}
func (fc *FileInCore) calculateCrc(badVfNodes []*FileMetadata) (fileCrcArr []*FileCrc) {
badLen := len(badVfNodes)
fileCrcArr = make([]*FileCrc, 0)
for i := 0; i < badLen; i++ {
crcKey := badVfNodes[i].getFileCrc()
isFound := false
var crc *FileCrc
for _, crc = range fileCrcArr {
if crc.crc == crcKey {
isFound = true
break
}
}
if isFound == false {
crc = newFileCrc(crcKey)
crc.meta = badVfNodes[i]
fileCrcArr = append(fileCrcArr, crc)
} else {
crc.count++
}
}
return
}