forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rowcache_invalidator.go
262 lines (237 loc) · 8.05 KB
/
rowcache_invalidator.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
// 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 (
"fmt"
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/tb"
"github.com/youtube/vitess/go/vt/binlog"
blproto "github.com/youtube/vitess/go/vt/binlog/proto"
"github.com/youtube/vitess/go/vt/mysqlctl"
myproto "github.com/youtube/vitess/go/vt/mysqlctl/proto"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/tabletserver/planbuilder"
"golang.org/x/net/context"
)
// RowcacheInvalidator runs the service to invalidate
// the rowcache based on binlog events.
type RowcacheInvalidator struct {
qe *QueryEngine
checker MySQLChecker
dbname string
mysqld mysqlctl.MysqlDaemon
svm sync2.ServiceManager
posMutex sync.Mutex
pos myproto.ReplicationPosition
lagSeconds sync2.AtomicInt64
}
// AppendGTID updates the current replication position by appending a GTID to
// the set of transactions that have been processed.
func (rci *RowcacheInvalidator) AppendGTID(gtid myproto.GTID) {
rci.posMutex.Lock()
defer rci.posMutex.Unlock()
rci.pos = myproto.AppendGTID(rci.pos, gtid)
}
// SetPosition sets the current ReplicationPosition.
func (rci *RowcacheInvalidator) SetPosition(rp myproto.ReplicationPosition) {
rci.posMutex.Lock()
defer rci.posMutex.Unlock()
rci.pos = rp
}
// Position returns the current ReplicationPosition.
func (rci *RowcacheInvalidator) Position() myproto.ReplicationPosition {
rci.posMutex.Lock()
defer rci.posMutex.Unlock()
return rci.pos
}
// PositionString returns the current ReplicationPosition as a string.
func (rci *RowcacheInvalidator) PositionString() string {
return rci.Position().String()
}
// NewRowcacheInvalidator creates a new RowcacheInvalidator.
// Just like QueryEngine, this is a singleton class.
// You must call this only once.
func NewRowcacheInvalidator(statsPrefix string, checker MySQLChecker, qe *QueryEngine, enablePublishStats bool) *RowcacheInvalidator {
rci := &RowcacheInvalidator{checker: checker, qe: qe}
if enablePublishStats {
stats.Publish(statsPrefix+"RowcacheInvalidatorState", stats.StringFunc(rci.svm.StateName))
stats.Publish(statsPrefix+"RowcacheInvalidatorPosition", stats.StringFunc(rci.PositionString))
stats.Publish(statsPrefix+"RowcacheInvalidatorLagSeconds", stats.IntFunc(rci.lagSeconds.Get))
}
return rci
}
// Open runs the invalidation loop.
func (rci *RowcacheInvalidator) Open(dbname string, mysqld mysqlctl.MysqlDaemon) {
// Perform an early check to see if we're already running.
if rci.svm.State() == sync2.SERVICE_RUNNING {
return
}
rp, err := mysqld.MasterPosition()
if err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "Rowcache invalidator aborting: cannot determine replication position: %v", err))
}
if mysqld.Cnf().BinLogPath == "" {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "Rowcache invalidator aborting: binlog path not specified"))
}
err = rci.qe.ClearRowcache(context.Background())
if err != nil {
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "Rowcahe is not reachable"))
}
rci.dbname = dbname
rci.mysqld = mysqld
rci.SetPosition(rp)
ok := rci.svm.Go(rci.run)
if ok {
log.Infof("Rowcache invalidator starting, dbname: %s, path: %s, position: %v", dbname, mysqld.Cnf().BinLogPath, rp)
} else {
log.Infof("Rowcache invalidator already running")
}
}
// Close terminates the invalidation loop. It returns only of the
// loop has terminated.
func (rci *RowcacheInvalidator) Close() {
rci.svm.Stop()
}
func (rci *RowcacheInvalidator) run(ctx *sync2.ServiceContext) error {
for {
evs := binlog.NewEventStreamer(rci.dbname, rci.mysqld, rci.Position(), rci.processEvent)
// We wrap this code in a func so we can catch all panics.
// If an error is returned, we log it, wait 1 second, and retry.
// This loop can only be stopped by calling Close.
err := func() (inner error) {
defer func() {
if x := recover(); x != nil {
inner = fmt.Errorf("%v: uncaught panic:\n%s", x, tb.Stack(4))
}
}()
return evs.Stream(ctx)
}()
if err == nil || !ctx.IsRunning() {
break
}
if IsConnErr(err) {
rci.checker.CheckMySQL()
}
log.Errorf("binlog.ServeUpdateStream returned err '%v', retrying in 1 second.", err.Error())
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
time.Sleep(1 * time.Second)
}
log.Infof("Rowcache invalidator stopped")
return nil
}
func (rci *RowcacheInvalidator) handleInvalidationError(event *blproto.StreamEvent) {
if x := recover(); x != nil {
terr, ok := x.(*TabletError)
if !ok {
log.Errorf("Uncaught panic for %+v:\n%v\n%s", event, x, tb.Stack(4))
rci.qe.queryServiceStats.InternalErrors.Add("Panic", 1)
return
}
log.Errorf("%v: %+v", terr, event)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
}
}
func (rci *RowcacheInvalidator) processEvent(event *blproto.StreamEvent) error {
defer rci.handleInvalidationError(event)
switch event.Category {
case "DDL":
log.Infof("DDL invalidation: %s", event.Sql)
rci.handleDDLEvent(event.Sql)
case "DML":
rci.handleDMLEvent(event)
case "ERR":
rci.handleUnrecognizedEvent(event.Sql)
case "POS":
gtid, err := myproto.DecodeGTID(event.TransactionID)
if err != nil {
return err
}
rci.AppendGTID(gtid)
default:
log.Errorf("unknown event: %#v", event)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return nil
}
rci.lagSeconds.Set(time.Now().Unix() - event.Timestamp)
return nil
}
func (rci *RowcacheInvalidator) handleDMLEvent(event *blproto.StreamEvent) {
invalidations := int64(0)
tableInfo := rci.qe.schemaInfo.GetTable(event.TableName)
if tableInfo == nil {
panic(NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "Table %s not found", event.TableName))
}
if tableInfo.CacheType == schema.CACHE_NONE {
return
}
for _, pkTuple := range event.PrimaryKeyValues {
newKey := validateKey(tableInfo, buildKey(pkTuple), rci.qe.queryServiceStats)
if newKey == "" {
continue
}
tableInfo.Cache.Delete(context.Background(), newKey)
invalidations++
}
tableInfo.invalidations.Add(invalidations)
}
func (rci *RowcacheInvalidator) handleDDLEvent(ddl string) {
ddlPlan := planbuilder.DDLParse(ddl)
if ddlPlan.Action == "" {
panic(NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "DDL is not understood"))
}
if ddlPlan.TableName != "" && ddlPlan.TableName != ddlPlan.NewName {
// It's a drop or rename.
rci.qe.schemaInfo.DropTable(ddlPlan.TableName)
}
if ddlPlan.NewName != "" {
rci.qe.schemaInfo.CreateOrUpdateTable(context.Background(), ddlPlan.NewName)
}
}
func (rci *RowcacheInvalidator) handleUnrecognizedEvent(sql string) {
statement, err := sqlparser.Parse(sql)
if err != nil {
log.Errorf("Error: %v: %s", err, sql)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return
}
var table *sqlparser.TableName
switch stmt := statement.(type) {
case *sqlparser.Insert:
// Inserts don't affect rowcache.
return
case *sqlparser.Update:
table = stmt.Table
case *sqlparser.Delete:
table = stmt.Table
default:
log.Errorf("Unrecognized: %s", sql)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return
}
// Ignore cross-db statements.
if table.Qualifier != "" && string(table.Qualifier) != rci.qe.dbconfigs.App.DbName {
return
}
// Ignore if it's an uncached table.
tableName := string(table.Name)
tableInfo := rci.qe.schemaInfo.GetTable(tableName)
if tableInfo == nil {
log.Errorf("Table %s not found: %s", tableName, sql)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return
}
if tableInfo.CacheType == schema.CACHE_NONE {
return
}
// Treat the statement as a DDL.
// It will conservatively invalidate all rows of the table.
log.Warningf("Treating '%s' as DDL for table %s", sql, tableName)
rci.qe.schemaInfo.CreateOrUpdateTable(context.Background(), tableName)
}