-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
planner.go
210 lines (186 loc) · 5.65 KB
/
planner.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
// Copyright 2014, 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 vtgate
// This is a V3 file. Do not intermix with V2.
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
log "github.com/golang/glog"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/cache"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtgate/engine"
"github.com/youtube/vitess/go/vt/vtgate/planbuilder"
"github.com/youtube/vitess/go/vt/vtgate/vindexes"
)
// Planner is used to compute the plan. It contains
// the vschema, and has a cache of previous computed plans.
type Planner struct {
serv topo.SrvTopoServer
cell string
mu sync.Mutex
vschema *vindexes.VSchema
plans *cache.LRUCache
}
var once sync.Once
// NewPlanner creates a new planner for VTGate.
// It will watch the vschema in the topology until the ctx is closed.
func NewPlanner(ctx context.Context, serv topo.SrvTopoServer, cell string, cacheSize int) *Planner {
plr := &Planner{
serv: serv,
cell: cell,
plans: cache.NewLRUCache(int64(cacheSize)),
}
plr.WatchVSchema(ctx)
once.Do(func() {
http.Handle("/debug/query_plans", plr)
http.Handle("/debug/vschema", plr)
})
return plr
}
// WatchVSchema watches the VSchema from the topo. The function does
// not return an error. It instead logs warnings on failure.
// We get the list of keyspaces to watch at the beginning (because it's easier)
// and we don't watch that. So when adding a new keyspace, for now,
// vtgate needs to be restarted (after having rebuilt the serving graph).
// This could be fixed by adding a WatchSrvKeyspaceNames API to topo.Server,
// and when it triggers we'd diff the values, and terminate the missing
// SrvKeyspaces, and start watching the new SrvKeyspaces.
func (plr *Planner) WatchVSchema(ctx context.Context) {
keyspaces, err := plr.serv.GetSrvKeyspaceNames(ctx, plr.cell)
if err != nil {
log.Warningf("Error loading vschema: could not read keyspaces: %v", err)
return
}
// we will wait until we get the first value for each keyspace
// before returning
var wg sync.WaitGroup
// mu protects formal
var mu sync.Mutex
formal := &vindexes.VSchemaFormal{
Keyspaces: make(map[string]vindexes.KeyspaceFormal),
}
processKeyspace := func(keyspace, kschema string) {
// Note we use a closure here to allow for defer()
// to work properly, and also to let the caller
// do more things even if this fails.
// unpack the new VSchema, or skip it
var kformal vindexes.KeyspaceFormal
if err := json.Unmarshal([]byte(kschema), &kformal); err != nil {
log.Warningf("Error unmarshalling vschema for keyspace %s: %v", keyspace, err)
return
}
// rebuild the new component
mu.Lock()
defer mu.Unlock()
formal.Keyspaces[keyspace] = kformal
vschema, err := vindexes.BuildVSchema(formal)
if err != nil {
log.Warningf("Error creating VSchema: %v", err)
return
}
plr.mu.Lock()
plr.vschema = vschema
plr.mu.Unlock()
plr.plans.Clear()
}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
gotFirstValue := false
notifications, err := plr.serv.WatchVSchema(ctx, keyspace)
if err != nil {
log.Warningf("Error watching vschema for keyspace %s, will not watch it: %v", keyspace, err)
return
}
for kschema := range notifications {
processKeyspace(keyspace, kschema)
if !gotFirstValue {
gotFirstValue = true
wg.Done()
}
}
}(keyspace)
}
wg.Wait()
}
// VSchema returns the VSchema.
func (plr *Planner) VSchema() *vindexes.VSchema {
plr.mu.Lock()
defer plr.mu.Unlock()
return plr.vschema
}
// GetPlan computes the plan for the given query. If one is in
// the cache, it reuses it.
func (plr *Planner) GetPlan(sql, keyspace string) (*engine.Plan, error) {
if plr.VSchema() == nil {
return nil, errors.New("vschema not initialized")
}
key := sql
if keyspace != "" {
key = keyspace + ":" + sql
}
if result, ok := plr.plans.Get(key); ok {
return result.(*engine.Plan), nil
}
plan, err := planbuilder.Build(sql, &wrappedVSchema{
vschema: plr.VSchema(),
keyspace: keyspace,
})
if err != nil {
return nil, err
}
plr.plans.Set(sql, plan)
return plan, nil
}
// ServeHTTP shows the current plans in the query cache.
func (plr *Planner) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
if request.URL.Path == "/debug/query_plans" {
keys := plr.plans.Keys()
response.Header().Set("Content-Type", "text/plain")
response.Write([]byte(fmt.Sprintf("Length: %d\n", len(keys))))
for _, v := range keys {
response.Write([]byte(fmt.Sprintf("%#v\n", v)))
if plan, ok := plr.plans.Peek(v); ok {
if b, err := json.MarshalIndent(plan, "", " "); err != nil {
response.Write([]byte(err.Error()))
} else {
response.Write(b)
}
response.Write(([]byte)("\n\n"))
}
}
} else if request.URL.Path == "/debug/vschema" {
response.Header().Set("Content-Type", "application/json; charset=utf-8")
b, err := json.MarshalIndent(plr.VSchema(), "", " ")
if err != nil {
response.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
response.Write(buf.Bytes())
} else {
response.WriteHeader(http.StatusNotFound)
}
}
type wrappedVSchema struct {
vschema *vindexes.VSchema
keyspace string
}
func (vs *wrappedVSchema) Find(keyspace, tablename string) (table *vindexes.Table, err error) {
if keyspace == "" {
keyspace = vs.keyspace
}
return vs.vschema.Find(keyspace, tablename)
}