forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
/
memcache_stats.go
397 lines (370 loc) · 10.5 KB
/
memcache_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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2013, 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 tabletserver
import (
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/timer"
)
var mainStringMetrics = map[string]bool{
"accepting_conns": false,
"auth_cmds": false,
"auth_errors": false,
"bytes_read": false,
"bytes_written": false,
"bytes": false,
"cas_badval": false,
"cas_hits": false,
"cas_misses": false,
"cmd_flush": false,
"cmd_get": false,
"cmd_set": false,
"cmd_touch": false,
"conn_yields": false,
"connection_structures": false,
"curr_connections": false,
"curr_items": false,
"decr_hits": false,
"decr_misses": false,
"delete_hits": false,
"delete_misses": false,
"evicted_unfetched": false,
"evictions": false,
"expired_unfetched": false,
"get_hits": false,
"get_misses": false,
"hash_bytes": false,
"hash_is_expanding": false,
"hash_power_level": false,
"incr_hits": false,
"incr_misses": false,
"libevent": true,
"limit_maxbytes": false,
"listen_disabled_num": false,
"pid": false,
"pointer_size": false,
"reclaimed": false,
"reserved_fds": false,
"rusage_system": true,
"rusage_user": true,
"threads": false,
"time": false,
"total_connections": false,
"total_items": false,
"touch_hits": false,
"touch_misses": false,
"uptime": false,
"version": true,
}
var slabsSingleMetrics = map[string]bool{
"active_slabs": true,
"cas_badval": false,
"cas_hits": false,
"chunk_size": false,
"chunks_per_page": false,
"cmd_set": false,
"decr_hits": false,
"delete_hits": false,
"free_chunks_end": false,
"free_chunks": false,
"get_hits": false,
"incr_hits": false,
"mem_requested": false,
"total_chunks": false,
"total_malloced": true,
"total_pages": false,
"touch_hits": false,
"used_chunks": false,
}
var itemsMetrics = []string{
"age",
"evicted",
"evicted_nonzero",
"evicted_time",
"evicted_unfetched",
"expired_unfetched",
"number",
"outofmemory",
"reclaimed",
"tailrepairs",
}
// RetrieveCacheStats returns current memcache stats.
type RetrieveCacheStats func(key string) string
// MemcacheStats exports the Memcache internal stats through stats package.
type MemcacheStats struct {
ticks *timer.Timer
mu sync.Mutex
main map[string]string
slabs map[string]map[string]int64
items map[string]map[string]int64
statsPrefix string
statsFunc RetrieveCacheStats
queryServiceStats *QueryServiceStats
flags int64
}
const (
enableMain = 1 << iota
enableSlabs
enableItems
)
// NewMemcacheStats creates a new MemcacheStats.
// main, slabs and items specify the categories of stats that need to be exported.
func NewMemcacheStats(
statsPrefix string,
refreshFreq time.Duration,
flags int64,
queryServiceStats *QueryServiceStats,
statsFunc RetrieveCacheStats) *MemcacheStats {
memstats := &MemcacheStats{
ticks: timer.NewTimer(refreshFreq),
statsPrefix: statsPrefix,
statsFunc: statsFunc,
main: make(map[string]string),
slabs: make(map[string]map[string]int64),
items: make(map[string]map[string]int64),
queryServiceStats: queryServiceStats,
flags: flags,
}
if flags&enableMain > 0 {
memstats.publishMainStats()
}
if flags&enableSlabs > 0 {
memstats.publishSlabsStats()
}
if flags*enableItems > 0 {
memstats.publishItemsStats()
}
return memstats
}
// Open starts exporting the stats.
func (memstats *MemcacheStats) Open() {
memstats.ticks.Start(func() { memstats.update() })
}
// Close clears the variable values and stops exporting the stats.
func (memstats *MemcacheStats) Close() {
memstats.ticks.Stop()
memstats.mu.Lock()
defer memstats.mu.Unlock()
for key := range memstats.main {
if mainStringMetrics[key] {
memstats.main[key] = ""
} else {
memstats.main[key] = "0"
}
}
for key := range memstats.slabs {
memstats.slabs[key] = make(map[string]int64)
}
for key := range memstats.items {
memstats.items[key] = make(map[string]int64)
}
}
func (memstats *MemcacheStats) update() {
if memstats.flags&enableMain > 0 {
memstats.updateMainStats()
}
if memstats.flags&enableSlabs > 0 {
memstats.updateSlabsStats()
}
if memstats.flags&enableItems > 0 {
memstats.updateItemsStats()
}
}
func (memstats *MemcacheStats) publishMainStats() {
memstats.mu.Lock()
defer memstats.mu.Unlock()
for k, isstr := range mainStringMetrics {
key := k
if isstr {
memstats.main[key] = ""
stats.Publish(memstats.statsPrefix+"Memcache"+formatKey(key), stats.StringFunc(func() string {
memstats.mu.Lock()
defer memstats.mu.Unlock()
return memstats.main[key]
}))
} else {
memstats.main[key] = "0"
stats.Publish(memstats.statsPrefix+"Memcache"+formatKey(key), stats.IntFunc(func() int64 {
memstats.mu.Lock()
defer memstats.mu.Unlock()
ival, err := strconv.ParseInt(memstats.main[key], 10, 64)
if err != nil {
log.Errorf("value '%v' for key %v is not an int", memstats.main[key], key)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return -1
}
return ival
}))
}
}
}
func (memstats *MemcacheStats) updateMainStats() {
memstats.readStats("", func(sKey, sValue string) {
memstats.main[sKey] = sValue
})
}
func (memstats *MemcacheStats) publishSlabsStats() {
memstats.mu.Lock()
defer memstats.mu.Unlock()
for key, isSingle := range slabsSingleMetrics {
key := key
memstats.slabs[key] = make(map[string]int64)
if isSingle {
stats.Publish(memstats.statsPrefix+"MemcacheSlabs"+formatKey(key), stats.IntFunc(func() int64 {
memstats.mu.Lock()
defer memstats.mu.Unlock()
return memstats.slabs[key][""]
}))
} else {
stats.Publish(memstats.statsPrefix+"MemcacheSlabs"+formatKey(key), stats.CountersFunc(func() map[string]int64 {
memstats.mu.Lock()
defer memstats.mu.Unlock()
return copyMap(memstats.slabs[key])
}))
}
}
}
func (memstats *MemcacheStats) updateSlabsStats() {
memstats.readStats("slabs", func(sKey, sValue string) {
ival, err := strconv.ParseInt(sValue, 10, 64)
if err != nil {
log.Error(err)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
if slabsSingleMetrics[sKey] {
m, ok := memstats.slabs[sKey]
if !ok {
log.Errorf("Unknown memcache slabs stats %v: %v", sKey, ival)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
m[""] = ival
return
}
subkey, slabid, err := parseSlabKey(sKey)
if err != nil {
log.Error(err)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
m, ok := memstats.slabs[subkey]
if !ok {
log.Errorf("Unknown memcache slabs stats %v %v: %v", subkey, slabid, ival)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
m[slabid] = ival
})
}
func (memstats *MemcacheStats) publishItemsStats() {
memstats.mu.Lock()
defer memstats.mu.Unlock()
for _, key := range itemsMetrics {
key := key // create local var to keep current key
memstats.items[key] = make(map[string]int64)
stats.Publish(memstats.statsPrefix+"MemcacheItems"+formatKey(key), stats.CountersFunc(func() map[string]int64 {
memstats.mu.Lock()
defer memstats.mu.Unlock()
return copyMap(memstats.items[key])
}))
}
}
func (memstats *MemcacheStats) updateItemsStats() {
memstats.readStats("items", func(sKey, sValue string) {
ival, err := strconv.ParseInt(sValue, 10, 64)
if err != nil {
log.Error(err)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
subkey, slabid, err := parseItemKey(sKey)
if err != nil {
log.Error(err)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
m, ok := memstats.items[subkey]
if !ok {
log.Errorf("Unknown memcache items stats %v %v: %v", subkey, slabid, ival)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return
}
m[slabid] = ival
})
}
func (memstats *MemcacheStats) readStats(k string, proc func(key, value string)) {
defer func() {
if x := recover(); x != nil {
_, ok := x.(*TabletError)
if !ok {
log.Errorf("Uncaught panic when reading memcache stats: %v", x)
} else {
log.Errorf("Could not read memcache stats: %v", x)
}
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
}
}()
stats := memstats.statsFunc(k)
if stats == "" {
return
}
memstats.mu.Lock()
defer memstats.mu.Unlock()
lines := strings.Split(stats, "\n")
for _, line := range lines {
if line == "" {
continue
}
items := strings.Split(line, " ")
//if using apt-get, memcached info would be:STAT version 1.4.14 (Ubuntu)
//so less then 3 would be compatible with original memcached
if len(items) < 3 {
log.Errorf("Unexpected stats: %v", line)
memstats.queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
continue
}
proc(items[1], items[2])
}
}
func formatKey(key string) string {
key = regexp.MustCompile("^[a-z]").ReplaceAllStringFunc(key, func(item string) string {
return strings.ToUpper(item)
})
key = regexp.MustCompile("_[a-z]").ReplaceAllStringFunc(key, func(item string) string {
return strings.ToUpper(item[1:])
})
return key
}
// parseSlabKey splits a slab key into the subkey and slab id:
// "1:chunk_size" -> "chunk_size", 1
func parseSlabKey(key string) (subkey string, slabid string, err error) {
tokens := strings.Split(key, ":")
if len(tokens) != 2 {
return "", "", fmt.Errorf("invalid slab key: %v", key)
}
return tokens[1], tokens[0], nil
}
// parseItemKey splits an item key into the subkey and slab id:
// "items:1:number" -> "number", 1
func parseItemKey(key string) (subkey string, slabid string, err error) {
tokens := strings.Split(key, ":")
if len(tokens) != 3 {
return "", "", fmt.Errorf("invalid slab key: %v", key)
}
return tokens[2], tokens[1], nil
}
func copyMap(src map[string]int64) map[string]int64 {
dst := make(map[string]int64, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}