forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatestreamctl.go
321 lines (269 loc) · 9.66 KB
/
updatestreamctl.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2012, 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 binlog
import (
"fmt"
"sync"
log "github.com/golang/glog"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/tb"
"github.com/youtube/vitess/go/vt/binlog/proto"
"github.com/youtube/vitess/go/vt/key"
"github.com/youtube/vitess/go/vt/mysqlctl"
myproto "github.com/youtube/vitess/go/vt/mysqlctl/proto"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
/* API and config for UpdateStream Service */
const (
DISABLED int64 = iota
ENABLED
)
var usStateNames = map[int64]string{
ENABLED: "Enabled",
DISABLED: "Disabled",
}
var (
streamCount = stats.NewCounters("UpdateStreamStreamCount")
updateStreamErrors = stats.NewCounters("UpdateStreamErrors")
updateStreamEvents = stats.NewCounters("UpdateStreamEvents")
keyrangeStatements = stats.NewInt("UpdateStreamKeyRangeStatements")
keyrangeTransactions = stats.NewInt("UpdateStreamKeyRangeTransactions")
tablesStatements = stats.NewInt("UpdateStreamTablesStatements")
tablesTransactions = stats.NewInt("UpdateStreamTablesTransactions")
)
// UpdateStream is the real implementation of proto.UpdateStream
type UpdateStream struct {
mycnf *mysqlctl.Mycnf
actionLock sync.Mutex
state sync2.AtomicInt64
mysqld mysqlctl.MysqlDaemon
stateWaitGroup sync.WaitGroup
dbname string
streams streamList
}
type streamList struct {
sync.Mutex
streams map[*sync2.ServiceManager]bool
}
func (sl *streamList) Init() {
sl.Lock()
sl.streams = make(map[*sync2.ServiceManager]bool)
sl.Unlock()
}
func (sl *streamList) Add(e *sync2.ServiceManager) {
sl.Lock()
sl.streams[e] = true
sl.Unlock()
}
func (sl *streamList) Delete(e *sync2.ServiceManager) {
sl.Lock()
delete(sl.streams, e)
sl.Unlock()
}
func (sl *streamList) Stop() {
sl.Lock()
for stream := range sl.streams {
stream.Stop()
}
sl.Unlock()
}
// UpdateStream is the singleton that gets initialized during
// startup and that gets called by all RPC server implementations
var UpdateStreamRpcService *UpdateStream
// RegisterUpdateStreamServiceFunc is the type to use for delayed
// registration of RPC servers until we have all the objects
type RegisterUpdateStreamServiceFunc func(proto.UpdateStream)
// RegisterUpdateStreamServices is the list of all registration
// callbacks to invoke
var RegisterUpdateStreamServices []RegisterUpdateStreamServiceFunc
// RegisterUpdateStreamService needs to be called to start listening
// to clients
func RegisterUpdateStreamService(mycnf *mysqlctl.Mycnf) {
// check we haven't been called already
if UpdateStreamRpcService != nil {
panic("Update Stream service already initialized")
}
// create the singleton
UpdateStreamRpcService = &UpdateStream{mycnf: mycnf}
stats.Publish("UpdateStreamState", stats.StringFunc(func() string {
return usStateNames[UpdateStreamRpcService.state.Get()]
}))
// and register all the instances
for _, f := range RegisterUpdateStreamServices {
f(UpdateStreamRpcService)
}
}
func logError() {
if x := recover(); x != nil {
log.Errorf("%s at\n%s", x.(error).Error(), tb.Stack(4))
}
}
// EnableUpdateStreamService enables the RPC service for UpdateStream
func EnableUpdateStreamService(dbname string, mysqld mysqlctl.MysqlDaemon) {
defer logError()
UpdateStreamRpcService.enable(dbname, mysqld)
}
// DisableUpdateStreamService disables the RPC service for UpdateStream
func DisableUpdateStreamService() {
defer logError()
UpdateStreamRpcService.disable()
}
// ServeUpdateStream sill serve one UpdateStream
func ServeUpdateStream(position string, sendReply func(reply *proto.StreamEvent) error) error {
return UpdateStreamRpcService.ServeUpdateStream(position, sendReply)
}
// IsUpdateStreamEnabled returns true if the RPC service is enabled
func IsUpdateStreamEnabled() bool {
return UpdateStreamRpcService.isEnabled()
}
// GetReplicationPosition returns the current replication position of
// the service
func GetReplicationPosition() (myproto.ReplicationPosition, error) {
return UpdateStreamRpcService.getReplicationPosition()
}
func (updateStream *UpdateStream) enable(dbname string, mysqld mysqlctl.MysqlDaemon) {
updateStream.actionLock.Lock()
defer updateStream.actionLock.Unlock()
if updateStream.isEnabled() {
return
}
if dbname == "" {
log.Errorf("Missing db name, cannot enable update stream service")
return
}
if updateStream.mycnf.BinLogPath == "" {
log.Errorf("Update stream service requires binlogs enabled")
return
}
updateStream.state.Set(ENABLED)
updateStream.mysqld = mysqld
updateStream.dbname = dbname
updateStream.streams.Init()
log.Infof("Enabling update stream, dbname: %s, binlogpath: %s", updateStream.dbname, updateStream.mycnf.BinLogPath)
}
func (updateStream *UpdateStream) disable() {
updateStream.actionLock.Lock()
defer updateStream.actionLock.Unlock()
if !updateStream.isEnabled() {
return
}
updateStream.state.Set(DISABLED)
updateStream.streams.Stop()
updateStream.stateWaitGroup.Wait()
log.Infof("Update Stream Disabled")
}
func (updateStream *UpdateStream) isEnabled() bool {
return updateStream.state.Get() == ENABLED
}
// ServeUpdateStream is part of the proto.UpdateStream interface
func (updateStream *UpdateStream) ServeUpdateStream(position string, sendReply func(reply *proto.StreamEvent) error) (err error) {
pos, err := myproto.DecodeReplicationPosition(position)
if err != nil {
return err
}
updateStream.actionLock.Lock()
if !updateStream.isEnabled() {
updateStream.actionLock.Unlock()
log.Errorf("Unable to serve client request: update stream service is not enabled")
return fmt.Errorf("update stream service is not enabled")
}
updateStream.stateWaitGroup.Add(1)
updateStream.actionLock.Unlock()
defer updateStream.stateWaitGroup.Done()
streamCount.Add("Updates", 1)
defer streamCount.Add("Updates", -1)
log.Infof("ServeUpdateStream starting @ %#v", pos)
evs := NewEventStreamer(updateStream.dbname, updateStream.mysqld, pos, func(reply *proto.StreamEvent) error {
if reply.Category == "ERR" {
updateStreamErrors.Add("UpdateStream", 1)
} else {
updateStreamEvents.Add(reply.Category, 1)
}
return sendReply(reply)
})
svm := &sync2.ServiceManager{}
svm.Go(evs.Stream)
updateStream.streams.Add(svm)
defer updateStream.streams.Delete(svm)
return svm.Join()
}
// StreamKeyRange is part of the proto.UpdateStream interface
func (updateStream *UpdateStream) StreamKeyRange(position string, keyspaceIdType key.KeyspaceIdType, keyRange *pb.KeyRange, charset *mproto.Charset, sendReply func(reply *proto.BinlogTransaction) error) (err error) {
pos, err := myproto.DecodeReplicationPosition(position)
if err != nil {
return err
}
updateStream.actionLock.Lock()
if !updateStream.isEnabled() {
updateStream.actionLock.Unlock()
log.Errorf("Unable to serve client request: Update stream service is not enabled")
return fmt.Errorf("update stream service is not enabled")
}
updateStream.stateWaitGroup.Add(1)
updateStream.actionLock.Unlock()
defer updateStream.stateWaitGroup.Done()
streamCount.Add("KeyRange", 1)
defer streamCount.Add("KeyRange", -1)
log.Infof("ServeUpdateStream starting @ %#v", pos)
// Calls cascade like this: BinlogStreamer->KeyRangeFilterFunc->func(*proto.BinlogTransaction)->sendReply
f := KeyRangeFilterFunc(keyspaceIdType, keyRange, func(reply *proto.BinlogTransaction) error {
keyrangeStatements.Add(int64(len(reply.Statements)))
keyrangeTransactions.Add(1)
return sendReply(reply)
})
bls := NewBinlogStreamer(updateStream.dbname, updateStream.mysqld, charset, pos, f)
svm := &sync2.ServiceManager{}
svm.Go(bls.Stream)
updateStream.streams.Add(svm)
defer updateStream.streams.Delete(svm)
return svm.Join()
}
// StreamTables is part of the proto.UpdateStream interface
func (updateStream *UpdateStream) StreamTables(position string, tables []string, charset *mproto.Charset, sendReply func(reply *proto.BinlogTransaction) error) (err error) {
pos, err := myproto.DecodeReplicationPosition(position)
if err != nil {
return err
}
updateStream.actionLock.Lock()
if !updateStream.isEnabled() {
updateStream.actionLock.Unlock()
log.Errorf("Unable to serve client request: Update stream service is not enabled")
return fmt.Errorf("update stream service is not enabled")
}
updateStream.stateWaitGroup.Add(1)
updateStream.actionLock.Unlock()
defer updateStream.stateWaitGroup.Done()
streamCount.Add("Tables", 1)
defer streamCount.Add("Tables", -1)
log.Infof("ServeUpdateStream starting @ %#v", pos)
// Calls cascade like this: BinlogStreamer->TablesFilterFunc->func(*proto.BinlogTransaction)->sendReply
f := TablesFilterFunc(tables, func(reply *proto.BinlogTransaction) error {
keyrangeStatements.Add(int64(len(reply.Statements)))
keyrangeTransactions.Add(1)
return sendReply(reply)
})
bls := NewBinlogStreamer(updateStream.dbname, updateStream.mysqld, charset, pos, f)
svm := &sync2.ServiceManager{}
svm.Go(bls.Stream)
updateStream.streams.Add(svm)
defer updateStream.streams.Delete(svm)
return svm.Join()
}
// HandlePanic is part of the proto.UpdateStream interface
func (updateStream *UpdateStream) HandlePanic(err *error) {
if x := recover(); x != nil {
log.Errorf("Uncaught panic:\n%v\n%s", x, tb.Stack(4))
*err = fmt.Errorf("uncaught panic: %v", x)
}
}
func (updateStream *UpdateStream) getReplicationPosition() (myproto.ReplicationPosition, error) {
updateStream.actionLock.Lock()
defer updateStream.actionLock.Unlock()
if !updateStream.isEnabled() {
return myproto.ReplicationPosition{}, fmt.Errorf("update stream service is not enabled")
}
return updateStream.mysqld.MasterPosition()
}