-
Notifications
You must be signed in to change notification settings - Fork 20
/
fill_node_disk.go
401 lines (320 loc) · 8.77 KB
/
fill_node_disk.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
package assets
import (
"context"
"sort"
"strings"
"time"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/node/cidutil"
"github.com/Filecoin-Titan/titan/node/scheduler/node"
)
const (
// Interval to dispatch tasks to populate node disks
fillDiskInterval = 5 * time.Minute
)
// initFillDiskTimer dispatch tasks to populate node disks
func (m *Manager) initFillDiskTimer() {
ticker := time.NewTicker(fillDiskInterval)
defer ticker.Stop()
edgeCount := m.getFillAssetEdgeCount()
candidateCount := int64(m.GetCandidateReplicaCount())
for {
<-ticker.C
if m.fillSwitch {
m.fillDiskTasks(edgeCount, candidateCount)
}
}
}
func (m *Manager) getFillAssetEdgeCount() int64 {
cfg, err := m.config()
if err != nil {
return 4000
}
return cfg.FillAssetEdgeCount
}
func (m *Manager) StartFillDiskTimer() {
m.fillSwitch = true
}
func (m *Manager) StopFillDiskTimer() {
m.fillSwitch = false
}
func (m *Manager) autoRefillAssetReplicas(edgeCount int64) bool {
info, err := m.LoadNeedRefillAssetRecords(m.nodeMgr.ServerID, edgeCount, Servicing.String())
if err != nil {
log.Errorf("autoRefillAssetReplicas LoadNeedRefillAssetRecords err:%s", err.Error())
return false
}
if info == nil {
return false
}
info.NeedEdgeReplica = edgeCount
// do replenish replicas
err = m.replenishAssetReplicas(info, 0, string(m.nodeMgr.ServerID), info.Note, CandidatesSelect, "")
if err != nil {
log.Errorf("autoRefillAssetReplicas replenishAssetReplicas err: %s", err.Error())
return false
}
return true
}
func (m *Manager) autoRestartAssetReplicas() bool {
rows, err := m.LoadAssetRecords([]string{EdgesFailed.String()}, 10, 0, m.nodeMgr.ServerID)
if err != nil {
log.Errorf("autoRestartAssetReplicas LoadAssetRecords err:%s", err.Error())
return false
}
defer rows.Close()
isPulling := false
// loading assets to local
for rows.Next() {
cInfo := &types.AssetRecord{}
err = rows.StructScan(cInfo)
if err != nil {
log.Errorf("asset StructScan err: %s", err.Error())
continue
}
if cInfo.Note == "" {
continue
}
if !strings.Contains(cInfo.State, "Failed") {
continue
}
err = m.RestartPullAssets([]types.AssetHash{types.AssetHash(cInfo.Hash)})
if err != nil {
log.Errorf("autoRestartAssetReplicas RestartPullAssets err:%s", err.Error())
continue
}
isPulling = true
}
return isPulling
}
func (m *Manager) pullAssetFromAWSs(edgeCount, candidateCount int64) {
task := m.getPullAssetTask()
if task != nil {
log.Infof("awsTask cur task : %s , count: %d/%d ,cid:%s", task.Bucket, task.ResponseCount, task.TotalCount, task.Cid)
if float64(task.ResponseCount) >= float64(task.TotalCount)*0.6 || task.Expiration.Before(time.Now()) {
defer m.fillAssets.Delete(task.Bucket)
// is ok
// start asset task
hash, err := cidutil.CIDToHash(task.Cid)
if err != nil {
log.Errorf("awsTask CIDToHash %s err:%s", task.Cid, err.Error())
return
}
err = m.CreateAssetPullTask(&types.PullAssetReq{
CID: task.Cid,
Replicas: edgeCount,
Expiration: time.Now().Add(360 * 24 * time.Hour),
Hash: hash,
UserID: task.Bucket,
})
if err != nil {
log.Errorf("awsTask CreateAssetPullTask %s err:%s", task.Cid, err.Error())
return
}
return
}
return
}
if m.nodeMgr.Edges < int(edgeCount) {
return
}
// download data from aws
list, err := m.ListAWSData(1, 0, false)
if err != nil {
log.Warnf("pullAssetFromAWS ListAWSData err:%s", err.Error())
return
}
dataLen := len(list)
if dataLen == 0 {
return
}
info := list[0]
info.IsDistribute = true
err = m.UpdateAWSData(info)
if err != nil {
log.Errorf("pullAssetFromAWS UpdateAWSData err:%s", err.Error())
return
}
m.updateFillAssetInfo(info.Bucket, candidateCount+edgeCount)
go m.requestNodePullAsset(info.Bucket, info.Cid, info.Size, edgeCount, candidateCount)
}
func (m *Manager) fillDiskTasks(edgeCount, candidateCount int64) {
pullCount := m.getPullingAssetLen()
limitCount := m.getAssetPullTaskLimit()
if pullCount >= limitCount {
log.Infof("The asset in the pulling exceeds the limit %d, please wait", limitCount)
return
}
log.Infof("awsTask cur task is nil, edge count : %d , limit count : %d pullCount : %d limitCount : %d", m.nodeMgr.Edges, edgeCount, pullCount, limitCount)
m.pullAssetFromAWSs(edgeCount, candidateCount)
if m.autoRefillAssetReplicas(edgeCount) {
return
}
// if m.autoRestartAssetReplicas() {
// return
// }
return
}
func (m *Manager) requestNodePullAsset(bucket, cid string, size float64, edgeCount, candidateCount int64) {
cNodes := m.nodeMgr.GetCandidateNodes(m.nodeMgr.Candidates, true)
sort.Slice(cNodes, func(i, j int) bool {
return cNodes[i].TitanDiskUsage < cNodes[j].TitanDiskUsage
})
cCount := 0
for i := 0; i < len(cNodes); i++ {
if cCount >= int(candidateCount) {
break
}
node := cNodes[i]
if exist, err := m.checkAssetIfExist(node, cid); err != nil {
// log.Warnf("requestNodePullAsset checkAssetIfExist error %s", err)
continue
} else if exist {
continue
}
if err := m.pullAssetFromAWS(node, bucket); err == nil {
// log.Warnf("requestNodePullAsset pullAssetFromAWS error %s", err.Error())
cCount++
}
}
eNodes := m.nodeMgr.GetAllEdgeNode()
sort.Slice(eNodes, func(i, j int) bool {
return eNodes[i].TitanDiskUsage < eNodes[j].TitanDiskUsage
})
eCount := 0
for i := 0; i < len(eNodes); i++ {
if eCount >= int(edgeCount) {
break
}
node := eNodes[i]
if !node.DiskEnough(size) {
continue
}
if exist, err := m.checkAssetIfExist(node, cid); err != nil {
// log.Warnf("requestNodePullAsset checkAssetIfExist error %s", err)
continue
} else if exist {
continue
}
if err := m.pullAssetFromAWS(node, bucket); err == nil {
// log.Warnf("requestNodePullAsset pullAssetFromAWS error %s", err.Error())
eCount++
}
}
}
func (m *Manager) pullAssetFromAWS(node *node.Node, bucket string) error {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
return node.PullAssetFromAWS(ctx, bucket, "")
}
func (m *Manager) checkAssetIfExist(node *node.Node, cid string) (bool, error) {
if cid == "" {
return false, nil
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
pullResult, err := node.GetAssetProgresses(ctx, []string{cid})
if err != nil {
return false, err
}
for _, asset := range pullResult.Progresses {
if asset.Status == types.ReplicaStatusSucceeded {
return true, nil
}
}
return false, nil
}
func (m *Manager) storeNodeToFillAsset(cid string, nNode *node.Node) {
info := &fillAssetNodeInfo{
edgeList: make([]*node.Node, 0),
candidateList: make([]*node.Node, 0),
}
infoI, exist := m.fillAssetNodes.LoadOrStore(cid, info)
if exist && infoI != nil {
info = infoI.(*fillAssetNodeInfo)
}
if nNode.Type == types.NodeEdge {
info.edgeList = append(info.edgeList, nNode)
} else {
info.candidateList = append(info.candidateList, nNode)
}
m.fillAssetNodes.Store(cid, info)
}
func (m *Manager) getNodesFromFillAsset(cid string) *fillAssetNodeInfo {
infoI, exist := m.fillAssetNodes.Load(cid)
if exist && infoI != nil {
info := infoI.(*fillAssetNodeInfo)
return info
}
return nil
}
func (m *Manager) removeNodesFromFillAsset(cid string, cleanCandidate bool) {
if !cleanCandidate {
m.fillAssetNodes.Delete(cid)
return
}
infoI, exist := m.fillAssetNodes.Load(cid)
if exist && infoI != nil {
info := infoI.(*fillAssetNodeInfo)
if info != nil {
info.candidateList = nil
m.fillAssetNodes.Store(cid, info)
}
}
}
func (m *Manager) updateFillAssetInfo(bucket string, count int64) {
info := &fillAssetInfo{
Expiration: time.Now().Add(2 * time.Hour),
Bucket: bucket,
}
infoI, exist := m.fillAssets.Load(bucket)
if exist && infoI != nil {
info = infoI.(*fillAssetInfo)
}
info.TotalCount = count
m.fillAssets.Store(bucket, info)
}
func (m *Manager) UpdateFillAssetResponseCount(bucket, cid, nodeID string) {
infoI, exist := m.fillAssets.Load(bucket)
if !exist || infoI == nil {
return
}
info := infoI.(*fillAssetInfo)
if info == nil || info.Bucket == "" {
return
}
info.ResponseCount++
if cid != "" {
info.Cid = cid
node := m.nodeMgr.GetNode(nodeID)
if node != nil {
m.storeNodeToFillAsset(cid, node)
}
}
m.fillAssets.Store(bucket, info)
}
func (m *Manager) getPullAssetTask() *fillAssetInfo {
var fInfo *fillAssetInfo
m.fillAssets.Range(func(key, value interface{}) bool {
info := value.(*fillAssetInfo)
if info == nil || info.Bucket == "" {
return true
}
fInfo = info
return false
})
return fInfo
}
type fillAssetInfo struct {
TotalCount int64
ResponseCount int
Expiration time.Time
Cid string
Bucket string
}
type fillAssetNodeInfo struct {
candidateList []*node.Node
edgeList []*node.Node
}