-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.go
65 lines (60 loc) · 2.21 KB
/
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
package statistics
import (
"github.com/deepfabric/prophet/core"
)
// ResourceStats records a list of resources' statistics and distribution status.
type ResourceStats struct {
Count int `json:"count"`
EmptyCount int `json:"empty_count"`
StorageSize int64 `json:"storage_size"`
StorageKeys int64 `json:"storage_keys"`
ContainerLeaderCount map[uint64]int `json:"container_leader_count"`
ContainerPeerCount map[uint64]int `json:"container_peer_count"`
ContainerLeaderSize map[uint64]int64 `json:"container_leader_size"`
ContainerLeaderKeys map[uint64]int64 `json:"container_leader_keys"`
ContainerPeerSize map[uint64]int64 `json:"container_peer_size"`
ContainerPeerKeys map[uint64]int64 `json:"container_peer_keys"`
}
// GetResourceStats sums resources' statistics.
func GetResourceStats(resources []*core.CachedResource) *ResourceStats {
stats := newResourceStats()
for _, resource := range resources {
stats.Observe(resource)
}
return stats
}
func newResourceStats() *ResourceStats {
return &ResourceStats{
ContainerLeaderCount: make(map[uint64]int),
ContainerPeerCount: make(map[uint64]int),
ContainerLeaderSize: make(map[uint64]int64),
ContainerLeaderKeys: make(map[uint64]int64),
ContainerPeerSize: make(map[uint64]int64),
ContainerPeerKeys: make(map[uint64]int64),
}
}
// Observe adds a resource's statistics into ResourceStats.
func (s *ResourceStats) Observe(r *core.CachedResource) {
s.Count++
approximateKeys := r.GetApproximateKeys()
approximateSize := r.GetApproximateSize()
if approximateSize <= core.EmptyResourceApproximateSize {
s.EmptyCount++
}
s.StorageSize += approximateSize
s.StorageKeys += approximateKeys
leader := r.GetLeader()
if leader != nil {
containerID := leader.GetContainerID()
s.ContainerLeaderCount[containerID]++
s.ContainerLeaderSize[containerID] += approximateSize
s.ContainerLeaderKeys[containerID] += approximateKeys
}
peers := r.Meta.Peers()
for _, p := range peers {
containerID := p.GetContainerID()
s.ContainerPeerCount[containerID]++
s.ContainerPeerSize[containerID] += approximateSize
s.ContainerPeerKeys[containerID] += approximateKeys
}
}