-
Notifications
You must be signed in to change notification settings - Fork 2k
/
engine.go
170 lines (151 loc) · 5.37 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
/*
Copyright 2019 The Vitess Authors.
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 (
"context"
"sync"
"golang.org/x/sync/semaphore"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
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 {
tabletenv.Env
PostponeMessages(ctx context.Context, target *querypb.Target, querygen QueryGenerator, ids []string) (count int64, err error)
PurgeMessages(ctx context.Context, target *querypb.Target, querygen QueryGenerator, timeCutoff int64) (count int64, err error)
}
// VStreamer defines the functions of VStreamer
// that the messager needs.
type VStreamer interface {
Stream(ctx context.Context, startPos string, tablePKs []*binlogdatapb.TableLastPK, filter *binlogdatapb.Filter, throttlerApp throttlerapp.Name, send func([]*binlogdatapb.VEvent) error) error
StreamResults(ctx context.Context, query string, send func(*binlogdatapb.VStreamResultsResponse) error) 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
vs VStreamer
postponeSema *semaphore.Weighted
}
// NewEngine creates a new Engine.
func NewEngine(tsv TabletService, se *schema.Engine, vs VStreamer) *Engine {
return &Engine{
tsv: tsv,
se: se,
vs: vs,
postponeSema: semaphore.NewWeighted(int64(tsv.Config().MessagePostponeParallelism)),
managers: make(map[string]*messageManager),
}
}
// Open starts the Engine service.
func (me *Engine) Open() {
me.mu.Lock()
if me.isOpen {
me.mu.Unlock()
return
}
me.isOpen = true
me.mu.Unlock()
log.Info("Messager: opening")
// Unlock before invoking RegisterNotifier because it
// obtains the same lock.
me.se.RegisterNotifier("messages", me.schemaChanged, true)
}
// Close closes the Engine service.
func (me *Engine) Close() {
log.Infof("messager Engine - started execution of Close. Acquiring mu lock")
me.mu.Lock()
log.Infof("messager Engine - acquired mu lock")
defer me.mu.Unlock()
if !me.isOpen {
log.Infof("messager Engine is not open")
return
}
me.isOpen = false
log.Infof("messager Engine - unregistering notifiers")
me.se.UnregisterNotifier("messages")
log.Infof("messager Engine - closing all managers")
for _, mm := range me.managers {
mm.Close()
}
me.managers = make(map[string]*messageManager)
log.Info("Messager: closed")
}
func (me *Engine) GetGenerator(name string) (QueryGenerator, 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, nil
}
// 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 primary 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
}
func (me *Engine) schemaChanged(tables map[string]*schema.Table, created, altered, dropped []*schema.Table) {
me.mu.Lock()
defer me.mu.Unlock()
for _, table := range append(dropped, altered...) {
name := table.Name.String()
mm := me.managers[name]
if mm == nil {
continue
}
log.Infof("Stopping messager for dropped/updated table: %v", name)
mm.Close()
delete(me.managers, name)
}
for _, t := range append(created, altered...) {
name := t.Name.String()
if t.Type != schema.Message {
continue
}
if me.managers[name] != nil {
me.tsv.Stats().InternalErrors.Add("Messages", 1)
log.Errorf("Newly created table already exists in messages: %s", name)
continue
}
mm := newMessageManager(me.tsv, me.vs, t, me.postponeSema)
me.managers[name] = mm
log.Infof("Starting messager for table: %v", name)
mm.Open()
}
}