-
Notifications
You must be signed in to change notification settings - Fork 712
/
store_stats.go
151 lines (130 loc) · 3.97 KB
/
store_stats.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
// Copyright 2020 TiKV Project 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 core
import (
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/tikv/pd/pkg/movingaverage"
"github.com/tikv/pd/pkg/utils/syncutil"
)
type storeStats struct {
mu syncutil.RWMutex
rawStats *pdpb.StoreStats
// avgAvailable is used to make available smooth, aka no sudden changes.
avgAvailable *movingaverage.HMA
}
func newStoreStats() *storeStats {
return &storeStats{
rawStats: &pdpb.StoreStats{},
avgAvailable: movingaverage.NewHMA(60), // take 10 minutes sample under 10s heartbeat rate
}
}
func (ss *storeStats) updateRawStats(rawStats *pdpb.StoreStats) {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.rawStats = rawStats
if ss.avgAvailable == nil {
return
}
ss.avgAvailable.Add(float64(rawStats.GetAvailable()))
}
// GetStoreStats returns the statistics information of the store.
func (ss *storeStats) GetStoreStats() *pdpb.StoreStats {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats
}
// CloneStoreStats returns the statistics information cloned from the store.
func (ss *storeStats) CloneStoreStats() *pdpb.StoreStats {
ss.mu.RLock()
b, _ := ss.rawStats.Marshal()
ss.mu.RUnlock()
stats := &pdpb.StoreStats{}
stats.Unmarshal(b)
return stats
}
// GetCapacity returns the capacity size of the store.
func (ss *storeStats) GetCapacity() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetCapacity()
}
// GetAvailable returns the available size of the store.
func (ss *storeStats) GetAvailable() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetAvailable()
}
// GetUsedSize returns the used size of the store.
func (ss *storeStats) GetUsedSize() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetUsedSize()
}
// GetBytesWritten returns the bytes written for the store during this period.
func (ss *storeStats) GetBytesWritten() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetBytesWritten()
}
// GetBytesRead returns the bytes read for the store during this period.
func (ss *storeStats) GetBytesRead() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetBytesRead()
}
// GetKeysWritten returns the keys written for the store during this period.
func (ss *storeStats) GetKeysWritten() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetKeysWritten()
}
// GetKeysRead returns the keys read for the store during this period.
func (ss *storeStats) GetKeysRead() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetKeysRead()
}
// IsBusy returns if the store is busy.
func (ss *storeStats) IsBusy() bool {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetIsBusy()
}
// GetSendingSnapCount returns the current sending snapshot count of the store.
func (ss *storeStats) GetSendingSnapCount() uint32 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetSendingSnapCount()
}
// GetReceivingSnapCount returns the current receiving snapshot count of the store.
func (ss *storeStats) GetReceivingSnapCount() uint32 {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.rawStats.GetReceivingSnapCount()
}
// GetAvgAvailable returns available size after the spike changes has been smoothed.
func (ss *storeStats) GetAvgAvailable() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
if ss.avgAvailable == nil {
return ss.rawStats.Available
}
return climp0(ss.avgAvailable.Get())
}
func climp0(v float64) uint64 {
if v <= 0 {
return 0
}
return uint64(v)
}