/
work_shard_getter.go
275 lines (241 loc) · 7.57 KB
/
work_shard_getter.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
// Copyright 2022 The CubeFS 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 blobnode
import (
"context"
"fmt"
"sort"
"sync"
"github.com/cubefs/cubefs/blobstore/blobnode/base/workutils"
"github.com/cubefs/cubefs/blobstore/blobnode/client"
"github.com/cubefs/cubefs/blobstore/common/codemode"
"github.com/cubefs/cubefs/blobstore/common/proto"
"github.com/cubefs/cubefs/blobstore/common/trace"
"github.com/cubefs/cubefs/blobstore/util/errors"
)
// duties
// 1.get chunk shards meta list by assign chunk
// 2.get benchmark bids of volume(not allowed tp be miss any bid which has written success by user,
// but the rubbish bid which can recover can be allowed to be included in benchmark bids)
var (
ErrNotEnoughWellReplicaCnt = errors.New("well replicas cnt is not enough")
ErrNotEnoughBidsInTasklet = errors.New("check len of tasklet and bids is not equal")
ErrTaskletSizeInvalid = errors.New("tasklet size is invalid")
ErrBidSizeOverTaskletSize = errors.New("bid size is over tasklet size")
ErrUnexpected = errors.New("unexpected error when get bench bids")
)
// ShardInfoSimple with blob id and size
type ShardInfoSimple struct {
Bid proto.BlobID
Size int64
}
// ShardInfoWithCrc with blob id and size and crc
type ShardInfoWithCrc struct {
Bid proto.BlobID
Size int64
Crc32 uint32
}
// GetSingleVunitNormalBids returns single volume unit bids info
func GetSingleVunitNormalBids(ctx context.Context, cli client.IBlobNode, replica proto.VunitLocation) (bids []*ShardInfoWithCrc, err error) {
shards, err := cli.ListShards(ctx, replica)
if err != nil {
return nil, err
}
for _, shardInfo := range shards {
if !shardInfo.Normal() {
continue
}
bidInfo := ShardInfoWithCrc{
Bid: shardInfo.Bid,
Size: shardInfo.Size,
Crc32: shardInfo.Crc,
}
bids = append(bids, &bidInfo)
}
return bids, nil
}
// ReplicaBidsRet with bids info and error message
type ReplicaBidsRet struct {
RetErr error
Bids map[proto.BlobID]*client.ShardInfo
}
// GetReplicasBids returns replicas bids info
func GetReplicasBids(ctx context.Context, cli client.IBlobNode, replicas Vunits) map[proto.Vuid]*ReplicaBidsRet {
result := make(map[proto.Vuid]*ReplicaBidsRet)
wg := sync.WaitGroup{}
var mu sync.Mutex
for idx := range replicas {
replica := replicas[idx]
vuid := replica.Vuid
wg.Add(1)
_, tmpCtx := trace.StartSpanFromContext(ctx, "ListShard")
go func() {
defer wg.Done()
bids, err := cli.ListShards(tmpCtx, replica)
bidMap := make(map[proto.BlobID]*client.ShardInfo, len(bids))
for _, bid := range bids {
bidMap[bid.Bid] = bid
}
mu.Lock()
defer mu.Unlock()
result[vuid] = &ReplicaBidsRet{RetErr: err, Bids: bidMap}
}()
}
wg.Wait()
return result
}
// MergeBids merge bids
func MergeBids(replicasBids map[proto.Vuid]*ReplicaBidsRet) []*ShardInfoSimple {
allBidsMap := make(map[proto.BlobID]*client.ShardInfo)
for _, info := range replicasBids {
if info.RetErr == nil {
for _, bidInfo := range info.Bids {
allBidsMap[bidInfo.Bid] = bidInfo
}
}
}
var allBidsList []*ShardInfoSimple
for _, bid := range allBidsMap {
bidInfo := ShardInfoSimple{Bid: bid.Bid, Size: bid.Size}
allBidsList = append(allBidsList, &bidInfo)
}
return allBidsList
}
// GetBenchmarkBids returns bench mark bids
func GetBenchmarkBids(ctx context.Context, cli client.IBlobNode, replicas Vunits,
mode codemode.CodeMode, badIdxs []uint8) (bids []*ShardInfoSimple, err error) {
span := trace.SpanFromContextSafe(ctx)
globalReplicas := replicas.IntactGlobalSet(mode, badIdxs)
replicasBids := GetReplicasBids(ctx, cli, globalReplicas)
wellCnt := 0
for _, replBids := range replicasBids {
if replBids.RetErr == nil {
wellCnt++
}
}
if wellCnt < minWellReplicasCnt(mode) {
span.Errorf("well replicas cnt is not enough: wellCnt[%d], minWellReplicasCnt[%d]",
wellCnt, minWellReplicasCnt(mode))
return nil, ErrNotEnoughWellReplicaCnt
}
allBidsList := MergeBids(replicasBids)
benchMark := []*ShardInfoSimple{}
for _, bid := range allBidsList {
markDel := false
existStatus := workutils.NewBidExistStatus(mode)
notExistCnt := 0
for vuid, replBids := range replicasBids {
if replBids.RetErr != nil {
continue
}
info, ok := replBids.Bids[bid.Bid]
if !ok {
notExistCnt++
continue
}
if info.MarkDeleted() {
markDel = true
break
}
existStatus.Exist(vuid.Index())
}
if markDel {
workutils.DroppedBidRecorderInst().Write(
ctx,
replicas[0].Vuid.Vid(),
bid.Bid,
"mark deleted",
)
continue
}
if existStatus.CanRecover() {
bidInfo := ShardInfoSimple{Bid: bid.Bid, Size: bid.Size}
benchMark = append(benchMark, &bidInfo)
continue
}
if notExistCnt > allowFailCnt(mode) {
workutils.DroppedBidRecorderInst().Write(
ctx,
replicas[0].Vuid.Vid(),
bid.Bid,
fmt.Sprintf("can't recover:notExist %d exist %d", notExistCnt, existStatus.ExistCnt()),
)
continue
}
span.Errorf("unexpect when get benchmark bids: vid[%d], bid[%d], existCnt[%d], notExistCnt[%d], allowFailCnt[%d]",
replicas[0].Vuid.Vid(), bid.Bid, existStatus.ExistCnt(), notExistCnt, allowFailCnt(mode))
return nil, ErrUnexpected
}
return benchMark, nil
}
// minWellReplicasCnt:It is the mini count of well replicas which can determine
// whether a bid is repaired or discarded
func minWellReplicasCnt(mode codemode.CodeMode) int {
return mode.Tactic().N + allowFailCnt(mode)
}
func allowFailCnt(mode codemode.CodeMode) int {
modeInfo := mode.Tactic()
return modeInfo.N + modeInfo.M - modeInfo.PutQuorum
}
// BidsSplit split bids list to many tasklets by taskletSize
func BidsSplit(ctx context.Context, bids []*ShardInfoSimple, taskletSize int) ([]Tasklet, *WorkError) {
span := trace.SpanFromContextSafe(ctx)
if len(bids) == 0 {
return []Tasklet{}, nil
}
if taskletSize == 0 {
span.Errorf("BidsSplit taskletSize size can not zero")
return nil, OtherError(ErrTaskletSizeInvalid)
}
sortByBid(bids)
var tasks []Tasklet
var taskShardDataSize uint64 = 0
task := Tasklet{}
for _, bid := range bids {
if bid.Size > int64(taskletSize) {
span.Errorf("bid size is too big: bid[%d], bid size[%d], tasklet size[%d]", bid.Bid, bid.Size, taskletSize)
return nil, OtherError(ErrBidSizeOverTaskletSize)
}
if taskShardDataSize+uint64(bid.Size) > uint64(taskletSize) {
tasks = append(tasks, task)
task = Tasklet{}
taskShardDataSize = 0
}
taskShardDataSize += uint64(bid.Size)
task.bids = append(task.bids, bid)
}
taskletBidCnt := 0
tasks = append(tasks, task)
for _, task := range tasks {
taskletBidCnt += len(task.bids)
}
if taskletBidCnt != len(bids) {
span.Errorf("check len of tasklet and bids is not equal: taskletBidCnt[%d], len(bids)[%d]", taskletBidCnt, len(bids))
return tasks, OtherError(ErrNotEnoughBidsInTasklet)
}
return tasks, nil
}
func sortByBid(bids []*ShardInfoSimple) {
sort.Slice(bids, func(i, j int) bool {
return bids[i].Bid < bids[j].Bid
})
}
// GetBids returns bids
func GetBids(shardMetas []*ShardInfoSimple) []proto.BlobID {
bids := make([]proto.BlobID, len(shardMetas))
for i, info := range shardMetas {
bids[i] = info.Bid
}
return bids
}