forked from vitessio/vitess
/
memcache_stats.go
404 lines (376 loc) · 9.45 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
398
399
400
401
402
403
404
// 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"
)
var interval = 5 * time.Second
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",
}
type mainStats struct {
mu sync.Mutex
lastQuery time.Time
stats map[string]string
}
func (s *mainStats) clear() {
s.mu.Lock()
defer s.mu.Unlock()
for key := range s.stats {
if mainStringMetrics[key] {
s.stats[key] = ""
} else {
s.stats[key] = "0"
}
}
}
type slabsStats struct {
mu sync.Mutex
lastQuery time.Time
stats map[string]map[string]int64
}
func (s *slabsStats) clear() {
s.mu.Lock()
defer s.mu.Unlock()
for k1 := range s.stats {
s.stats[k1] = make(map[string]int64)
}
}
type itemsStats struct {
mu sync.Mutex
lastQuery time.Time
stats map[string]map[string]int64
}
func (s *itemsStats) clear() {
s.mu.Lock()
defer s.mu.Unlock()
for k1 := range s.stats {
s.stats[k1] = make(map[string]int64)
}
}
// MemcacheStats exports the Memcache internal stats through stats package.
type MemcacheStats struct {
cachePool *CachePool
main mainStats
slabs slabsStats
items itemsStats
}
// NewMemcacheStats creates a new MemcacheStats based on given CachePool.
func NewMemcacheStats(cachePool *CachePool) *MemcacheStats {
m := MemcacheStats{
main: mainStats{stats: make(map[string]string)},
slabs: slabsStats{stats: make(map[string]map[string]int64)},
items: itemsStats{stats: make(map[string]map[string]int64)},
cachePool: cachePool,
}
m.publishMainStats()
m.publishSlabsStats()
m.publishItemsStats()
return &m
}
// Open provides a common function API, and does nothing.
func (s *MemcacheStats) Open() {
// noop
}
// Close clears the variable values.
func (s *MemcacheStats) Close() {
s.main.clear()
s.slabs.clear()
s.items.clear()
}
func (s *MemcacheStats) updateMainStats() {
if time.Now().Sub(s.main.lastQuery) <= interval {
return
}
s.readStats("", func(sKey, sValue string) {
s.main.stats[sKey] = sValue
})
s.main.lastQuery = time.Now()
}
func (s *MemcacheStats) publishMainStats() {
s.main.mu.Lock()
defer s.main.mu.Unlock()
for key, isstr := range mainStringMetrics {
key := key
if isstr {
s.main.stats[key] = ""
f := func() string {
s.main.mu.Lock()
defer s.main.mu.Unlock()
s.updateMainStats()
return s.main.stats[key]
}
stats.Publish(s.cachePool.name+"Memcache"+formatKey(key), stats.StringFunc(f))
continue
}
s.main.stats[key] = "0"
f := func() int64 {
s.main.mu.Lock()
defer s.main.mu.Unlock()
s.updateMainStats()
ival, err := strconv.ParseInt(s.main.stats[key], 10, 64)
if err != nil {
log.Errorf("value '%v' for key %v is not an int", s.main.stats[key], key)
return -1
}
return ival
}
stats.Publish(s.cachePool.name+"Memcache"+formatKey(key), stats.IntFunc(f))
}
}
func (s *MemcacheStats) updateSlabsStats() {
if time.Now().Sub(s.slabs.lastQuery) <= interval {
return
}
s.readStats("slabs", func(sKey, sValue string) {
ival, err := strconv.ParseInt(sValue, 10, 64)
if err != nil {
log.Error(err)
return
}
if slabsSingleMetrics[sKey] {
m, ok := s.slabs.stats[sKey]
if !ok {
log.Errorf("Unknown memcache slabs stats %v: %v", sKey, ival)
return
}
m[""] = ival
return
}
subkey, slabid, err := parseSlabKey(sKey)
if err != nil {
log.Error(err)
return
}
m, ok := s.slabs.stats[subkey]
if !ok {
log.Errorf("Unknown memcache slabs stats %v %v: %v", subkey, slabid, ival)
return
}
m[slabid] = ival
})
s.slabs.lastQuery = time.Now()
}
func (s *MemcacheStats) publishSlabsStats() {
s.slabs.mu.Lock()
defer s.slabs.mu.Unlock()
for key, isSingle := range slabsSingleMetrics {
key := key
s.slabs.stats[key] = make(map[string]int64)
if isSingle {
f := func() int64 {
s.slabs.mu.Lock()
defer s.slabs.mu.Unlock()
s.updateSlabsStats()
return s.slabs.stats[key][""]
}
stats.Publish(s.cachePool.name+"MemcacheSlabs"+formatKey(key), stats.IntFunc(f))
continue
}
f := func() map[string]int64 {
s.slabs.mu.Lock()
defer s.slabs.mu.Unlock()
s.updateSlabsStats()
return copyMap(s.slabs.stats[key])
}
stats.Publish(s.cachePool.name+"MemcacheSlabs"+formatKey(key), stats.CountersFunc(f))
}
}
func (s *MemcacheStats) updateItemsStats() {
if time.Now().Sub(s.items.lastQuery) <= interval {
return
}
s.readStats("items", func(sKey, sValue string) {
ival, err := strconv.ParseInt(sValue, 10, 64)
if err != nil {
log.Error(err)
return
}
subkey, slabid, err := parseItemKey(sKey)
if err != nil {
log.Error(err)
return
}
m, ok := s.items.stats[subkey]
if !ok {
log.Errorf("Unknown memcache items stats %v %v: %v", subkey, slabid, ival)
return
}
m[slabid] = ival
})
s.items.lastQuery = time.Now()
}
func (s *MemcacheStats) publishItemsStats() {
s.items.mu.Lock()
defer s.items.mu.Unlock()
for _, key := range itemsMetrics {
key := key // create local var to keep current key
s.items.stats[key] = make(map[string]int64)
f := func() map[string]int64 {
s.items.mu.Lock()
defer s.items.mu.Unlock()
s.updateItemsStats()
return copyMap(s.items.stats[key])
}
stats.Publish(s.cachePool.name+"MemcacheItems"+formatKey(key), stats.CountersFunc(f))
}
}
func (s *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)
return
}
// ignore if cache pool is closed
}
}()
conn := s.cachePool.Get()
if conn == nil {
return
}
defer conn.Recycle()
stats, err := conn.Stats(k)
if err != nil {
log.Errorf("Cannot export memcache %v stats: %v", k, err)
return
}
st := string(stats)
lines := strings.Split(st, "\n")
for _, line := range lines {
if line == "" {
continue
}
items := strings.Split(line, " ")
if len(items) != 3 {
log.Errorf("Unexpected stats: %v", line)
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 {
if src == nil {
return nil
}
dst := make(map[string]int64, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}