forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
229 lines (202 loc) · 5.7 KB
/
status.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
// Copyright 2016, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gateway
import (
"strings"
"sync"
"time"
"github.com/youtube/vitess/go/stats"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
const (
aggrChanSize = 10000
)
var (
// aggrChan buffers queryInfo objects to be processed.
aggrChan chan *queryInfo
// muAggr protects below vars.
muAggr sync.Mutex
// aggregators holds all Aggregators created.
aggregators []*TabletStatusAggregator
// gatewayStatsChanFull tracks the number of times
// aggrChan becomes full.
gatewayStatsChanFull *stats.Int
)
func init() {
// init global goroutines to aggregate stats.
aggrChan = make(chan *queryInfo, aggrChanSize)
gatewayStatsChanFull = stats.NewInt("GatewayStatsChanFullCount")
go resetAggregators()
go processQueryInfo()
}
// registerAggregator registers an aggregator to the global list.
func registerAggregator(a *TabletStatusAggregator) {
muAggr.Lock()
defer muAggr.Unlock()
aggregators = append(aggregators, a)
}
// resetAggregators resets the next stats slot for all aggregators every second.
func resetAggregators() {
ticker := time.NewTicker(time.Second)
for range ticker.C {
muAggr.Lock()
for _, a := range aggregators {
a.resetNextSlot()
}
muAggr.Unlock()
}
}
// processQueryInfo processes the next queryInfo object.
func processQueryInfo() {
for qi := range aggrChan {
qi.aggr.processQueryInfo(qi)
}
}
//
// TabletCacheStatus definitions
//
// TabletCacheStatus contains the status per tablet for a gateway.
type TabletCacheStatus struct {
Keyspace string
Shard string
TabletType topodatapb.TabletType
Name string
Addr string
QueryCount uint64
QueryError uint64
QPS uint64
AvgLatency float64 // in milliseconds
}
//
// TabletStatusAggregator definitions
//
// TabletStatusAggregator tracks tablet status for a gateway.
type TabletStatusAggregator struct {
Keyspace string
Shard string
TabletType topodatapb.TabletType
Name string // the alternative name of a tablet
Addr string // the host:port of a tablet
// mu protects below fields.
mu sync.RWMutex
QueryCount uint64
QueryError uint64
// for QPS and latency (avg value over a minute)
tick uint32
queryCountInMinute [60]uint64
latencyInMinute [60]time.Duration
}
// queryInfo is sent over the aggregators channel to update the stats.
type queryInfo struct {
aggr *TabletStatusAggregator
addr string
tabletType topodatapb.TabletType
elapsed time.Duration
hasError bool
}
// NewTabletStatusAggregator creates a TabletStatusAggregator.
func NewTabletStatusAggregator(keyspace, shard string, tabletType topodatapb.TabletType, name string) *TabletStatusAggregator {
tsa := &TabletStatusAggregator{
Keyspace: keyspace,
Shard: shard,
TabletType: tabletType,
Name: name,
}
registerAggregator(tsa)
return tsa
}
// UpdateQueryInfo updates the aggregator with the given information about a query.
func (tsa *TabletStatusAggregator) UpdateQueryInfo(addr string, tabletType topodatapb.TabletType, elapsed time.Duration, hasError bool) {
qi := &queryInfo{
aggr: tsa,
addr: addr,
tabletType: tabletType,
elapsed: elapsed,
hasError: hasError,
}
select {
case aggrChan <- qi:
default:
gatewayStatsChanFull.Add(1)
}
}
func (tsa *TabletStatusAggregator) processQueryInfo(qi *queryInfo) {
tsa.mu.Lock()
defer tsa.mu.Unlock()
if tsa.TabletType != qi.tabletType {
tsa.TabletType = qi.tabletType
// reset counters
tsa.QueryCount = 0
tsa.QueryError = 0
for i := 0; i < len(tsa.queryCountInMinute); i++ {
tsa.queryCountInMinute[i] = 0
}
for i := 0; i < len(tsa.latencyInMinute); i++ {
tsa.latencyInMinute[i] = 0
}
}
if qi.addr != "" {
tsa.Addr = qi.addr
}
tsa.QueryCount++
tsa.queryCountInMinute[tsa.tick]++
tsa.latencyInMinute[tsa.tick] += qi.elapsed
if qi.hasError {
tsa.QueryError++
}
}
// GetCacheStatus returns a TabletCacheStatus representing the current gateway status.
func (tsa *TabletStatusAggregator) GetCacheStatus() *TabletCacheStatus {
status := &TabletCacheStatus{
Keyspace: tsa.Keyspace,
Shard: tsa.Shard,
Name: tsa.Name,
}
tsa.mu.RLock()
defer tsa.mu.RUnlock()
status.TabletType = tsa.TabletType
status.Addr = tsa.Addr
status.QueryCount = tsa.QueryCount
status.QueryError = tsa.QueryError
var totalQuery uint64
for _, c := range tsa.queryCountInMinute {
totalQuery += c
}
var totalLatency time.Duration
for _, d := range tsa.latencyInMinute {
totalLatency += d
}
status.QPS = totalQuery / 60
if totalQuery > 0 {
status.AvgLatency = float64(totalLatency.Nanoseconds()) / float64(totalQuery) / 1000000
}
return status
}
// resetNextSlot resets the next tracking slot.
func (tsa *TabletStatusAggregator) resetNextSlot() {
tsa.mu.Lock()
defer tsa.mu.Unlock()
tsa.tick = (tsa.tick + 1) % 60
tsa.queryCountInMinute[tsa.tick] = 0
tsa.latencyInMinute[tsa.tick] = time.Duration(0)
}
//
// TabletCacheStatusList definitions
//
// TabletCacheStatusList is a slice of TabletCacheStatus.
type TabletCacheStatusList []*TabletCacheStatus
// Len is part of sort.Interface.
func (gtcsl TabletCacheStatusList) Len() int {
return len(gtcsl)
}
// Less is part of sort.Interface.
func (gtcsl TabletCacheStatusList) Less(i, j int) bool {
iKey := strings.Join([]string{gtcsl[i].Keyspace, gtcsl[i].Shard, string(gtcsl[i].TabletType), gtcsl[i].Name}, ".")
jKey := strings.Join([]string{gtcsl[j].Keyspace, gtcsl[j].Shard, string(gtcsl[j].TabletType), gtcsl[j].Name}, ".")
return iKey < jKey
}
// Swap is part of sort.Interface.
func (gtcsl TabletCacheStatusList) Swap(i, j int) {
gtcsl[i], gtcsl[j] = gtcsl[j], gtcsl[i]
}