forked from vitessio/vitess
/
replication_watcher.go
177 lines (153 loc) · 4.63 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
// Copyright 2016, 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 (
"sync"
"time"
log "github.com/golang/glog"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/mysqlconn/replication"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/binlog"
"github.com/youtube/vitess/go/vt/binlog/eventtoken"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/schema"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/tabletenv"
binlogdatapb "github.com/youtube/vitess/go/vt/proto/binlogdata"
querypb "github.com/youtube/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 {
// 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.Publish("EventTokenTimestamp", stats.IntFunc(func() int64 {
if e := rpw.EventToken(); e != nil {
return e.Timestamp
}
return 0
}))
})
return rpw
}
// Open starts the ReplicationWatcher service.
func (rpw *ReplicationWatcher) Open(dbconfigs dbconfigs.DBConfigs, mysqld mysqlctl.MysqlDaemon) {
if rpw.isOpen || !rpw.watchReplication {
return
}
ctx, cancel := context.WithCancel(tabletenv.LocalContext())
rpw.cancel = cancel
rpw.wg.Add(1)
go rpw.Process(ctx, dbconfigs, mysqld)
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, mysqld mysqlctl.MysqlDaemon) {
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.App.DbName, mysqld, rpw.se, nil /*clientCharset*/, replication.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
}