2012-06-07 22:55:06 +04:00
|
|
|
// 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.
|
2012-02-25 11:30:03 +04:00
|
|
|
|
|
|
|
package tabletserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2013-04-11 02:43:10 +04:00
|
|
|
|
2013-07-19 05:18:20 +04:00
|
|
|
"github.com/youtube/vitess/go/cache"
|
|
|
|
"github.com/youtube/vitess/go/mysql/proto"
|
2012-02-25 11:30:03 +04:00
|
|
|
)
|
|
|
|
|
2013-01-30 11:34:09 +04:00
|
|
|
var (
|
|
|
|
waitError = NewTabletError(FAIL, "Error waiting for consolidation")
|
|
|
|
)
|
|
|
|
|
2012-02-25 11:30:03 +04:00
|
|
|
type Consolidator struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
queries map[string]*Result
|
2012-03-09 03:13:53 +04:00
|
|
|
consolidations *cache.LRUCache
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsolidator() *Consolidator {
|
2013-04-11 02:43:10 +04:00
|
|
|
co := &Consolidator{queries: make(map[string]*Result), consolidations: cache.NewLRUCache(1000)}
|
|
|
|
http.Handle("/debug/consolidations", co)
|
|
|
|
return co
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
executing sync.RWMutex
|
|
|
|
consolidator *Consolidator
|
|
|
|
sql string
|
2012-09-25 02:14:48 +04:00
|
|
|
Result *proto.QueryResult
|
2012-03-21 04:33:11 +04:00
|
|
|
Err error
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 02:43:10 +04:00
|
|
|
func (co *Consolidator) Create(sql string) (r *Result, created bool) {
|
|
|
|
co.mu.Lock()
|
|
|
|
defer co.mu.Unlock()
|
|
|
|
if r, ok := co.queries[sql]; ok {
|
2012-02-25 11:30:03 +04:00
|
|
|
return r, false
|
|
|
|
}
|
2013-01-30 11:34:09 +04:00
|
|
|
// Preset the error. If there was an unexpected panic during the main
|
|
|
|
// query, then all those who waited will return the waitError.
|
2013-04-11 02:43:10 +04:00
|
|
|
r = &Result{consolidator: co, sql: sql, Err: waitError}
|
2012-02-25 11:30:03 +04:00
|
|
|
r.executing.Lock()
|
2013-04-11 02:43:10 +04:00
|
|
|
co.queries[sql] = r
|
2012-02-25 11:30:03 +04:00
|
|
|
return r, true
|
|
|
|
}
|
|
|
|
|
2013-04-11 02:43:10 +04:00
|
|
|
func (co *Consolidator) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
|
|
|
items := co.consolidations.Items()
|
2012-02-25 11:30:03 +04:00
|
|
|
response.Header().Set("Content-Type", "text/plain")
|
|
|
|
if items == nil {
|
|
|
|
response.Write([]byte("empty\n"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response.Write([]byte(fmt.Sprintf("Length: %d\n", len(items))))
|
|
|
|
for _, v := range items {
|
2013-04-11 02:43:10 +04:00
|
|
|
response.Write([]byte(fmt.Sprintf("%v: %s\n", v.Value.(*ccount).Get(), v.Key)))
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 02:43:10 +04:00
|
|
|
func (co *Consolidator) record(sql string) {
|
|
|
|
if v, ok := co.consolidations.Get(sql); ok {
|
|
|
|
v.(*ccount).Add(1)
|
2012-02-25 11:30:03 +04:00
|
|
|
} else {
|
|
|
|
c := ccount(1)
|
2013-04-11 02:43:10 +04:00
|
|
|
co.consolidations.Set(sql, &c)
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 02:43:10 +04:00
|
|
|
func (rs *Result) Broadcast() {
|
|
|
|
rs.consolidator.mu.Lock()
|
|
|
|
defer rs.consolidator.mu.Unlock()
|
|
|
|
delete(rs.consolidator.queries, rs.sql)
|
|
|
|
rs.executing.Unlock()
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 02:43:10 +04:00
|
|
|
func (rs *Result) Wait() {
|
|
|
|
rs.consolidator.record(rs.sql)
|
2012-02-25 11:30:03 +04:00
|
|
|
defer waitStats.Record("Consolidations", time.Now())
|
2013-04-11 02:43:10 +04:00
|
|
|
rs.executing.RLock()
|
2012-02-25 11:30:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ccount int64
|
|
|
|
|
2013-04-11 02:43:10 +04:00
|
|
|
func (cc *ccount) Size() int {
|
2012-02-25 11:30:03 +04:00
|
|
|
return 1
|
|
|
|
}
|
2013-04-11 02:43:10 +04:00
|
|
|
|
|
|
|
func (cc *ccount) Add(n int64) int64 {
|
|
|
|
return atomic.AddInt64((*int64)(cc), n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *ccount) Set(n int64) {
|
|
|
|
atomic.StoreInt64((*int64)(cc), n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *ccount) Get() int64 {
|
|
|
|
return atomic.LoadInt64((*int64)(cc))
|
|
|
|
}
|