forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
replication_watcher.go
198 lines (169 loc) · 5 KB
/
replication_watcher.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
/*
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 tabletserver
import (
"sync"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/binlog"
"vitess.io/vitess/go/vt/binlog/eventtoken"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// ReplicationWatcher is a tabletserver service that watches the
// replication stream. It can tell you the current event token,
// and it will trigger schema reloads if a DDL is encountered.
type ReplicationWatcher struct {
dbconfigs *dbconfigs.DBConfigs
// Life cycle management vars
isOpen bool
cancel context.CancelFunc
wg sync.WaitGroup
watchReplication bool
se *schema.Engine
mu sync.Mutex
eventToken *querypb.EventToken
}
var replOnce sync.Once
// NewReplicationWatcher creates a new ReplicationWatcher.
func NewReplicationWatcher(se *schema.Engine, config tabletenv.TabletConfig) *ReplicationWatcher {
rpw := &ReplicationWatcher{
watchReplication: config.WatchReplication,
se: se,
}
replOnce.Do(func() {
stats.Publish("EventTokenPosition", stats.StringFunc(func() string {
if e := rpw.EventToken(); e != nil {
return e.Position
}
return ""
}))
stats.NewGaugeFunc(
"EventTokenTimestamp",
"Replication watcher event token timestamp",
func() int64 {
if e := rpw.EventToken(); e != nil {
return e.Timestamp
}
return 0
})
})
return rpw
}
// InitDBConfig must be called before Open.
func (rpw *ReplicationWatcher) InitDBConfig(dbcfgs *dbconfigs.DBConfigs) {
rpw.dbconfigs = dbcfgs
}
// Open starts the ReplicationWatcher service.
func (rpw *ReplicationWatcher) Open() {
if rpw.isOpen || !rpw.watchReplication {
return
}
ctx, cancel := context.WithCancel(tabletenv.LocalContext())
rpw.cancel = cancel
rpw.wg.Add(1)
go rpw.Process(ctx, rpw.dbconfigs)
rpw.isOpen = true
}
// Close stops the ReplicationWatcher service.
func (rpw *ReplicationWatcher) Close() {
if !rpw.isOpen {
return
}
rpw.cancel()
rpw.wg.Wait()
rpw.isOpen = false
}
// Process processes the replication stream.
func (rpw *ReplicationWatcher) Process(ctx context.Context, dbconfigs *dbconfigs.DBConfigs) {
defer func() {
tabletenv.LogError()
rpw.wg.Done()
}()
for {
log.Infof("Starting a binlog Streamer from current replication position to monitor binlogs")
streamer := binlog.NewStreamer(dbconfigs.DbaWithDB(), rpw.se, nil /*clientCharset*/, mysql.Position{}, 0 /*timestamp*/, func(eventToken *querypb.EventToken, statements []binlog.FullBinlogStatement) error {
// Save the event token.
rpw.mu.Lock()
rpw.eventToken = eventToken
rpw.mu.Unlock()
// If it's a DDL, trigger a schema reload.
for _, statement := range statements {
if statement.Statement.Category != binlogdatapb.BinlogTransaction_Statement_BL_DDL {
continue
}
err := rpw.se.Reload(ctx)
log.Infof("Streamer triggered a schema reload, with result: %v", err)
return nil
}
return nil
})
if err := streamer.Stream(ctx); err != nil {
log.Infof("Streamer stopped: %v", err)
}
select {
case <-ctx.Done():
return
case <-time.After(5 * time.Second):
}
}
}
// ComputeExtras returns the requested ResultExtras based on the supplied options.
func (rpw *ReplicationWatcher) ComputeExtras(options *querypb.ExecuteOptions) *querypb.ResultExtras {
if options == nil {
// No options passed in.
return nil
}
if !options.IncludeEventToken && options.CompareEventToken == nil {
// The flags that make extras exist are not there.
return nil
}
et := rpw.EventToken()
if et == nil {
return nil
}
var extras *querypb.ResultExtras
// See if we need to fill in EventToken.
if options.IncludeEventToken {
extras = &querypb.ResultExtras{
EventToken: et,
}
}
// See if we need to compare.
if options.CompareEventToken != nil {
if eventtoken.Fresher(et, options.CompareEventToken) >= 0 {
// For a query, we are fresher if greater or equal
// to the provided compare_event_token.
if extras == nil {
extras = &querypb.ResultExtras{
Fresher: true,
}
} else {
extras.Fresher = true
}
}
}
return extras
}
// EventToken returns the current event token.
func (rpw *ReplicationWatcher) EventToken() *querypb.EventToken {
rpw.mu.Lock()
defer rpw.mu.Unlock()
return rpw.eventToken
}