-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime_resource.go
99 lines (87 loc) · 2.25 KB
/
runtime_resource.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
package prophet
// ResourceRuntime resource runtime info
type ResourceRuntime struct {
meta Resource
leaderPeer *Peer
downPeers []*PeerStats
pendingPeers []*Peer
}
func newResourceRuntime(meta Resource, leader *Peer) *ResourceRuntime {
return &ResourceRuntime{
meta: meta,
leaderPeer: leader,
}
}
// Clone returns a clone resource runtime
func (res *ResourceRuntime) Clone() *ResourceRuntime {
value := &ResourceRuntime{}
value.meta = res.meta.Clone()
if res.leaderPeer != nil {
value.leaderPeer = res.leaderPeer.Clone()
}
value.downPeers = make([]*PeerStats, len(res.downPeers))
for idx, ps := range res.downPeers {
value.downPeers[idx] = ps.Clone()
}
value.pendingPeers = make([]*Peer, len(res.pendingPeers))
for idx, p := range res.pendingPeers {
value.pendingPeers[idx] = p.Clone()
}
return value
}
// RemoveContainerPeer remove container peer
func (res *ResourceRuntime) RemoveContainerPeer(id uint64) {
var peers []*Peer
for _, peer := range res.meta.Peers() {
if peer.ContainerID != id {
peers = append(peers, peer)
}
}
res.meta.SetPeers(peers)
}
// GetPendingPeer returns pending peer
func (res *ResourceRuntime) GetPendingPeer(peerID uint64) *Peer {
for _, peer := range res.pendingPeers {
if peer.ID == peerID {
return peer
}
}
return nil
}
// GetPeer return the peer
func (res *ResourceRuntime) GetPeer(peerID uint64) *Peer {
for _, peer := range res.meta.Peers() {
if peer.ID == peerID {
return peer
}
}
return nil
}
// GetContainerPeer returns the peer in the container
func (res *ResourceRuntime) GetContainerPeer(containerID uint64) *Peer {
for _, peer := range res.meta.Peers() {
if peer.ContainerID == containerID {
return peer
}
}
return nil
}
// GetContainerIDs returns all container id
func (res *ResourceRuntime) GetContainerIDs() map[uint64]struct{} {
peers := res.meta.Peers()
ids := make(map[uint64]struct{}, len(peers))
for _, peer := range peers {
ids[peer.ContainerID] = struct{}{}
}
return ids
}
func (res *ResourceRuntime) getFollowers() map[uint64]*Peer {
peers := res.meta.Peers()
followers := make(map[uint64]*Peer, len(peers))
for _, p := range peers {
if res.leaderPeer == nil || res.leaderPeer.ID != p.ID {
followers[p.ContainerID] = p
}
}
return followers
}