-
Notifications
You must be signed in to change notification settings - Fork 20
/
dtypes.go
98 lines (86 loc) · 2.66 KB
/
dtypes.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
package assets
import (
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/node/scheduler/db"
)
// AssetHash is an identifier for a asset.
type AssetHash string
func (c AssetHash) String() string {
return string(c)
}
// NodePulledResult represents a result of a node pulling assets
type NodePulledResult struct {
Status int64
BlocksCount int64
Size int64
NodeID string
}
// AssetPullingInfo represents asset pull information
type AssetPullingInfo struct {
State AssetState
Hash AssetHash
CID string
Size int64
Blocks int64
EdgeReplicas int64
CandidateReplicas int64
Bandwidth int64
EdgeReplicaSucceeds []string
CandidateReplicaSucceeds []string
EdgeWaitings int64
CandidateWaitings int64
RetryCount int64
ReplenishReplicas int64
Requester string
Details string
SeedNodeID string
}
// ToAssetRecord converts AssetPullingInfo to types.AssetRecord
func (state *AssetPullingInfo) ToAssetRecord() *types.AssetRecord {
return &types.AssetRecord{
CID: state.CID,
Hash: state.Hash.String(),
NeedEdgeReplica: state.EdgeReplicas,
TotalSize: state.Size,
TotalBlocks: state.Blocks,
State: state.State.String(),
NeedCandidateReplicas: state.CandidateReplicas,
RetryCount: state.RetryCount,
ReplenishReplicas: state.ReplenishReplicas,
}
}
// assetPullingInfoFrom converts types.AssetRecord to AssetPullingInfo
func assetPullingInfoFrom(info *types.AssetRecord, assetDB *db.SQLDB) *AssetPullingInfo {
cInfo := &AssetPullingInfo{
CID: info.CID,
State: AssetState(info.State),
Hash: AssetHash(info.Hash),
EdgeReplicas: info.NeedEdgeReplica,
Size: info.TotalSize,
Blocks: info.TotalBlocks,
CandidateReplicas: info.NeedCandidateReplicas,
RetryCount: info.RetryCount,
ReplenishReplicas: info.ReplenishReplicas,
Bandwidth: info.NeedBandwidth,
}
for _, r := range info.ReplicaInfos {
switch r.Status {
case types.ReplicaStatusSucceeded:
if r.IsCandidate {
deactivateTime, err := assetDB.LoadDeactivateNodeTime(r.NodeID)
if err == nil && deactivateTime == 0 {
cInfo.CandidateReplicaSucceeds = append(cInfo.CandidateReplicaSucceeds, r.NodeID)
}
} else {
cInfo.EdgeReplicaSucceeds = append(cInfo.EdgeReplicaSucceeds, r.NodeID)
}
case types.ReplicaStatusPulling, types.ReplicaStatusWaiting:
if r.IsCandidate {
cInfo.CandidateWaitings++
} else {
cInfo.EdgeWaitings++
}
}
}
return cInfo
}