forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
/
cache_pool.go
346 lines (321 loc) · 9 KB
/
cache_pool.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
// Copyright 2012, 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 (
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/cacheservice"
"github.com/youtube/vitess/go/pools"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
"golang.org/x/net/context"
)
// CachePool re-exposes ResourcePool as a pool of Memcache connection objects.
type CachePool struct {
name string
pool *pools.ResourcePool
maxPrefix sync2.AtomicInt64
cmd *exec.Cmd
rowCacheConfig RowCacheConfig
capacity int
socket string
idleTimeout time.Duration
memcacheStats *MemcacheStats
queryServiceStats *QueryServiceStats
mu sync.Mutex
statsURL string
}
// NewCachePool creates a new pool for rowcache connections.
func NewCachePool(
name string,
rowCacheConfig RowCacheConfig,
idleTimeout time.Duration,
statsURL string,
enablePublishStats bool,
queryServiceStats *QueryServiceStats) *CachePool {
cp := &CachePool{
name: name,
idleTimeout: idleTimeout,
statsURL: statsURL,
queryServiceStats: queryServiceStats,
}
if name != "" && enablePublishStats {
cp.memcacheStats = NewMemcacheStats(
rowCacheConfig.StatsPrefix+name, 10*time.Second, enableMain,
queryServiceStats,
func(key string) string {
conn := cp.Get(context.Background())
// This is not the same as defer cachePool.Put(conn)
defer func() { cp.Put(conn) }()
stats, err := conn.Stats(key)
if err != nil {
conn.Close()
conn = nil
log.Errorf("Cannot export memcache %v stats: %v", key, err)
queryServiceStats.InternalErrors.Add("MemcacheStats", 1)
return ""
}
return string(stats)
})
stats.Publish(name+"ConnPoolCapacity", stats.IntFunc(cp.Capacity))
stats.Publish(name+"ConnPoolAvailable", stats.IntFunc(cp.Available))
stats.Publish(name+"ConnPoolMaxCap", stats.IntFunc(cp.MaxCap))
stats.Publish(name+"ConnPoolWaitCount", stats.IntFunc(cp.WaitCount))
stats.Publish(name+"ConnPoolWaitTime", stats.DurationFunc(cp.WaitTime))
stats.Publish(name+"ConnPoolIdleTimeout", stats.DurationFunc(cp.IdleTimeout))
}
http.Handle(statsURL, cp)
if rowCacheConfig.Binary == "" {
return cp
}
cp.rowCacheConfig = rowCacheConfig
// Start with memcached defaults
cp.capacity = 1024 - 50
if rowCacheConfig.Connections > 0 {
if rowCacheConfig.Connections <= 50 {
log.Fatalf("insufficient capacity: %d", rowCacheConfig.Connections)
}
cp.capacity = rowCacheConfig.Connections - 50
}
return cp
}
// Open opens the pool. It launches memcache and waits till it's up.
func (cp *CachePool) Open() {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.pool != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "rowcache is already open"))
}
if cp.rowCacheConfig.Binary == "" {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "rowcache binary not specified"))
}
cp.socket = generateFilename(cp.rowCacheConfig.Socket)
cp.startCacheService()
log.Infof("rowcache is enabled")
f := func() (pools.Resource, error) {
return cacheservice.Connect(cacheservice.Config{
Address: cp.socket,
Timeout: 10 * time.Second,
})
}
cp.pool = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout)
if cp.memcacheStats != nil {
cp.memcacheStats.Open()
}
}
// generateFilename generates a unique file name. It's convoluted.
// There are race conditions when we have to come up with unique
// names. So, this is a best effort.
func generateFilename(hint string) string {
dir, base := path.Split(hint)
f, err := ioutil.TempFile(dir, base)
if err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "error creating socket file: %v", err))
}
name := f.Name()
err = f.Close()
if err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "error closing socket file: %v", err))
}
err = os.Remove(name)
if err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "error removing socket file: %v", err))
}
log.Infof("sock filename: %v", name)
return name
}
func (cp *CachePool) startCacheService() {
commandLine := cp.rowCacheConfig.GetSubprocessFlags(cp.socket)
cp.cmd = exec.Command(commandLine[0], commandLine[1:]...)
if err := cp.cmd.Start(); err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "can't start memcache: %v", err))
}
attempts := 0
for {
c, err := cacheservice.Connect(cacheservice.Config{
Address: cp.socket,
Timeout: 30 * time.Millisecond,
})
if err != nil {
attempts++
if attempts >= 50 {
cp.cmd.Process.Kill()
// Avoid zombies
go cp.cmd.Wait()
// FIXME(sougou): Throw proper error if we can recover
log.Fatalf("Can't connect to cache service: %s", cp.socket)
}
time.Sleep(100 * time.Millisecond)
continue
}
if _, err = c.Set("health", 0, 0, []byte("ok")); err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "can't communicate with cache service: %v", err))
}
c.Close()
break
}
}
// Close closes the CachePool. It also shuts down memcache.
// You can call Open again after Close.
func (cp *CachePool) Close() {
// Close the underlying pool first.
// You cannot close the pool while holding the
// lock because we have to still allow Put to
// return outstanding connections, if any.
pool := cp.getPool()
if pool == nil {
return
}
pool.Close()
// No new operations will be allowed now.
// Safe to cleanup.
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.pool == nil {
return
}
if cp.memcacheStats != nil {
cp.memcacheStats.Close()
}
cp.cmd.Process.Kill()
// Avoid zombies
go cp.cmd.Wait()
_ = os.Remove(cp.socket)
cp.socket = ""
cp.pool = nil
}
// IsClosed returns true if CachePool is closed.
func (cp *CachePool) IsClosed() bool {
cp.mu.Lock()
defer cp.mu.Unlock()
return cp.pool == nil
}
func (cp *CachePool) getPool() *pools.ResourcePool {
cp.mu.Lock()
defer cp.mu.Unlock()
return cp.pool
}
// Get returns a memcache connection from the pool.
// You must call Put after Get.
func (cp *CachePool) Get(ctx context.Context) cacheservice.CacheService {
pool := cp.getPool()
if pool == nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "cache pool is not open"))
}
r, err := pool.Get(ctx)
if err != nil {
panic(NewTabletErrorSQL(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, err))
}
return r.(cacheservice.CacheService)
}
// Put returns the connection to the pool.
func (cp *CachePool) Put(conn cacheservice.CacheService) {
pool := cp.getPool()
if pool == nil {
return
}
if conn == nil {
pool.Put(nil)
} else {
pool.Put(conn)
}
}
// StatsJSON returns a JSON version of the CachePool stats.
func (cp *CachePool) StatsJSON() string {
pool := cp.getPool()
if pool == nil {
return "{}"
}
return pool.StatsJSON()
}
// Capacity returns the current capacity of the pool.
func (cp *CachePool) Capacity() int64 {
pool := cp.getPool()
if pool == nil {
return 0
}
return pool.Capacity()
}
// Available returns the number of available connections in the pool.
func (cp *CachePool) Available() int64 {
pool := cp.getPool()
if pool == nil {
return 0
}
return pool.Available()
}
// MaxCap returns the extent to which the pool capacity can be increased.
func (cp *CachePool) MaxCap() int64 {
pool := cp.getPool()
if pool == nil {
return 0
}
return pool.MaxCap()
}
// WaitCount returns the number of times we had to wait to get a connection
// from the pool.
func (cp *CachePool) WaitCount() int64 {
pool := cp.getPool()
if pool == nil {
return 0
}
return pool.WaitCount()
}
// WaitTime returns the total amount of time spent waiting for a connection.
func (cp *CachePool) WaitTime() time.Duration {
pool := cp.getPool()
if pool == nil {
return 0
}
return pool.WaitTime()
}
// IdleTimeout returns the connection idle timeout.
func (cp *CachePool) IdleTimeout() time.Duration {
pool := cp.getPool()
if pool == nil {
return 0
}
return pool.IdleTimeout()
}
// ServeHTTP serves memcache stats as HTTP.
func (cp *CachePool) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.MONITORING); err != nil {
acl.SendError(response, err)
return
}
defer func() {
if x := recover(); x != nil {
response.Write(([]byte)(x.(error).Error()))
}
}()
response.Header().Set("Content-Type", "text/plain")
pool := cp.getPool()
if pool == nil {
response.Write(([]byte)("closed"))
return
}
command := request.URL.Path[len(cp.statsURL):]
if command == "stats" {
command = ""
}
conn := cp.Get(context.Background())
// This is not the same as defer cp.Put(conn)
defer func() { cp.Put(conn) }()
r, err := conn.Stats(command)
if err != nil {
conn.Close()
conn = nil
response.Write(([]byte)(err.Error()))
} else {
response.Write(r)
}
}