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
|
|
|
|
2014-05-21 06:43:32 +04:00
|
|
|
"github.com/youtube/vitess/go/acl"
|
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 (
|
2015-01-06 20:37:34 +03:00
|
|
|
waitError = NewTabletError(ErrFail, "Error waiting for consolidation")
|
2013-01-30 11:34:09 +04:00
|
|
|
)
|
|
|
|
|
2014-06-13 22:50:29 +04:00
|
|
|
// Consolidator consolidates duplicate queries from executing simulaneously
|
|
|
|
// and shares results between them.
|
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
|
|
|
}
|
|
|
|
|
2014-06-13 22:50:29 +04:00
|
|
|
// NewConsolidator creates a new Consolidator
|
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
|
|
|
}
|
|
|
|
|
2014-06-13 22:50:29 +04:00
|
|
|
// Result is a wrapper for QueryResult of a query.
|
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
|
|
|
}
|
|
|
|
|
2014-06-13 22:50:29 +04:00
|
|
|
// Create adds a query to currently executing queries and acquires a
|
|
|
|
// lock on its Result if it is not already present. If the query is
|
|
|
|
// a duplicate, Create returns false.
|
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) {
|
2014-05-21 06:43:32 +04:00
|
|
|
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
|
|
|
|
acl.SendError(response, err)
|
|
|
|
return
|
|
|
|
}
|
2013-04-11 02:43:10 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 22:50:29 +04:00
|
|
|
// Broadcast removes the entry from current queries and releases the
|
|
|
|
// lock on its Result. Broadcast should be invoked when original
|
|
|
|
// query completes execution.
|
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
|
|
|
}
|
|
|
|
|
2014-06-13 22:50:29 +04:00
|
|
|
// Wait waits for the original query to complete execution. Wait should
|
|
|
|
// be invoked for duplicate queries.
|
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))
|
|
|
|
}
|