forked from dolthub/vitess
/
engine.go
229 lines (209 loc) · 6.2 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
// Copyright 2017, 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 messager
import (
"fmt"
"sync"
"time"
log "github.com/golang/glog"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/connpool"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/schema"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/tabletenv"
"github.com/youtube/vitess/go/vt/vterrors"
querypb "github.com/youtube/vitess/go/vt/proto/query"
vtrpcpb "github.com/youtube/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 {
mu sync.Mutex
isOpen bool
managers map[string]*messageManager
tsv TabletService
se *schema.Engine
conns *connpool.Pool
}
// 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,
),
managers: make(map[string]*messageManager),
}
}
// Open starts the Engine service.
func (me *Engine) Open(dbconfigs dbconfigs.DBConfigs) error {
if me.isOpen {
return nil
}
me.conns.Open(&dbconfigs.App, &dbconfigs.Dba)
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(name string, send func(*sqltypes.Result) error) (done chan struct{}, err 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", name)
}
rcv, done := newMessageReceiver(send)
mm.Subscribe(rcv)
return done, 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() {
combined := make(map[string]struct{})
for name := range newMessages {
combined[name] = struct{}{}
}
for name := range changedMessages {
combined[name] = struct{}{}
}
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)
}
}
}()
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
}
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)
}
}
// GenerateAckQuery returns the query and bind vars for acking a message.
func (me *Engine) GenerateAckQuery(name string, ids []string) (string, map[string]interface{}, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return "", nil, fmt.Errorf("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]interface{}, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return "", nil, fmt.Errorf("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]interface{}, error) {
me.mu.Lock()
defer me.mu.Unlock()
mm := me.managers[name]
if mm == nil {
return "", nil, fmt.Errorf("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 {
// TODO(sougou): Increment internal error counter.
log.Errorf("Newly created table alread exists in messages: %s", name)
continue
}
mm := newMessageManager(me.tsv, t, me.conns)
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)
}
}