-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
engine.go
279 lines (251 loc) · 8.26 KB
/
engine.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package messager
import (
"sort"
"sync"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// TabletService defines the functions of TabletServer
// that the messager needs for callback.
type TabletService interface {
CheckMySQL()
PostponeMessages(ctx context.Context, target *querypb.Target, name string, ids []string) (count int64, err error)
PurgeMessages(ctx context.Context, target *querypb.Target, name string, timeCutoff int64) (count int64, err error)
}
// Engine is the engine for handling messages.
type Engine struct {
dbconfigs *dbconfigs.DBConfigs
mu sync.Mutex
isOpen bool
managers map[string]*messageManager
tsv TabletService
se *schema.Engine
conns *connpool.Pool
postponeSema *sync2.Semaphore
}
// NewEngine creates a new Engine.
func NewEngine(tsv TabletService, se *schema.Engine, config tabletenv.TabletConfig) *Engine {
return &Engine{
tsv: tsv,
se: se,
conns: connpool.New(
config.PoolNamePrefix+"MessagerPool",
config.MessagePoolSize,
time.Duration(config.IdleTimeout*1e9),
tsv,
),
postponeSema: sync2.NewSemaphore(config.MessagePostponeCap, 0),
managers: make(map[string]*messageManager),
}
}
// InitDBConfig must be called before Open.
func (me *Engine) InitDBConfig(dbcfgs *dbconfigs.DBConfigs) {
me.dbconfigs = dbcfgs
}
// Open starts the Engine service.
func (me *Engine) Open() error {
if me.isOpen {
return nil
}
me.conns.Open(me.dbconfigs.AppWithDB(), me.dbconfigs.DbaWithDB(), me.dbconfigs.AppDebugWithDB())
me.se.RegisterNotifier("messages", me.schemaChanged)
me.isOpen = true
return nil
}
// Close closes the Engine service.
func (me *Engine) Close() {
me.mu.Lock()
defer me.mu.Unlock()
if !me.isOpen {
return
}
me.isOpen = false
me.se.UnregisterNotifier("messages")
for _, mm := range me.managers {
mm.Close()
}
me.managers = make(map[string]*messageManager)
me.conns.Close()
}
// Subscribe subscribes to messages from the requested table.
// The function returns a done channel that will be closed when
// the subscription ends, which can be initiated by the send function
// returning io.EOF. The engine can also end a subscription which is
// usually triggered by Close. It's the responsibility of the send
// function to promptly return if the done channel is closed. Otherwise,
// the engine's Close function will hang indefinitely.
func (me *Engine) Subscribe(ctx context.Context, name string, send func(*sqltypes.Result) error) (done <-chan struct{}, err error) {
me.mu.Lock()
defer me.mu.Unlock()
if !me.isOpen {
return nil, vterrors.Errorf(vtrpcpb.Code_UNAVAILABLE, "messager engine is closed, probably because this is not a master any more")
}
mm := me.managers[name]
if mm == nil {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "message table %s not found", name)
}
return mm.Subscribe(ctx, send), nil
}
// LockDB obtains db locks for all messages that need to
// be updated and returns the counterpart unlock function.
func (me *Engine) LockDB(newMessages map[string][]*MessageRow, changedMessages map[string][]string) func() {
// Build the set of affected messages tables.
combined := make(map[string]struct{})
for name := range newMessages {
combined[name] = struct{}{}
}
for name := range changedMessages {
combined[name] = struct{}{}
}
// Build the list of manager objects (one per table).
var mms []*messageManager
// Don't do DBLock while holding lock on mu.
// It causes deadlocks.
func() {
me.mu.Lock()
defer me.mu.Unlock()
for name := range combined {
if mm := me.managers[name]; mm != nil {
mms = append(mms, mm)
}
}
}()
if len(mms) > 1 {
// Always use the same order in which manager objects are locked to avoid deadlocks.
// The previous order in "mms" is not guaranteed for multiple reasons:
// - We use a Go map above which does not guarantee an iteration order.
// - Transactions may not always use the same order when writing to multiple
// messages tables.
sort.Slice(mms, func(i, j int) bool { return mms[i].name.String() < mms[j].name.String() })
}
// Lock each manager/messages table.
for _, mm := range mms {
mm.DBLock.Lock()
}
return func() {
for _, mm := range mms {
mm.DBLock.Unlock()
}
}
}
// UpdateCaches updates the caches for the committed changes.
func (me *Engine) UpdateCaches(newMessages map[string][]*MessageRow, changedMessages map[string][]string) {
me.mu.Lock()
defer me.mu.Unlock()
now := time.Now().UnixNano()
for name, mrs := range newMessages {
mm := me.managers[name]
if mm == nil {
continue
}
MessageStats.Add([]string{name, "Queued"}, int64(len(mrs)))
for _, mr := range mrs {
if mr.TimeNext > now {
// We don't handle future messages yet.
continue
}
mm.Add(mr)
}
}
for name, ids := range changedMessages {
mm := me.managers[name]
if mm == nil {
continue
}
mm.cache.Discard(ids)
}
}
// GenerateLoadMessagesQuery returns the ParsedQuery for loading messages by pk.
// The results of the query can be used in a BuildMessageRow call.
func (me *Engine) GenerateLoadMessagesQuery(name string) (*sqlparser.ParsedQuery, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "message table %s not found in schema", name)
}
return mm.loadMessagesQuery, nil
}
// GenerateAckQuery returns the query and bind vars for acking a message.
func (me *Engine) GenerateAckQuery(name string, ids []string) (string, map[string]*querypb.BindVariable, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return "", nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "message table %s not found in schema", name)
}
query, bv := mm.GenerateAckQuery(ids)
return query, bv, nil
}
// GeneratePostponeQuery returns the query and bind vars for postponing a message.
func (me *Engine) GeneratePostponeQuery(name string, ids []string) (string, map[string]*querypb.BindVariable, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return "", nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "message table %s not found in schema", name)
}
query, bv := mm.GeneratePostponeQuery(ids)
return query, bv, nil
}
// GeneratePurgeQuery returns the query and bind vars for purging messages.
func (me *Engine) GeneratePurgeQuery(name string, timeCutoff int64) (string, map[string]*querypb.BindVariable, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return "", nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "message table %s not found in schema", name)
}
query, bv := mm.GeneratePurgeQuery(timeCutoff)
return query, bv, nil
}
func (me *Engine) schemaChanged(tables map[string]*schema.Table, created, altered, dropped []string) {
me.mu.Lock()
defer me.mu.Unlock()
for _, name := range created {
t := tables[name]
if t.Type != schema.Message {
continue
}
if me.managers[name] != nil {
tabletenv.InternalErrors.Add("Messages", 1)
log.Errorf("Newly created table alread exists in messages: %s", name)
continue
}
mm := newMessageManager(me.tsv, t, me.conns, me.postponeSema)
me.managers[name] = mm
mm.Open()
}
// TODO(sougou): Update altered tables.
for _, name := range dropped {
mm := me.managers[name]
if mm == nil {
continue
}
mm.Close()
delete(me.managers, name)
}
}