-
Notifications
You must be signed in to change notification settings - Fork 2k
/
replica_connector.go
101 lines (82 loc) · 2.96 KB
/
replica_connector.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
/*
Copyright 2020 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 vreplication
import (
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"context"
"vitess.io/vitess/go/sqltypes"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/vstreamer"
)
// NewReplicaConnector returns replica connector
//
// This is used by binlog server to make vstream connection
// using the vstream connection, it will parse the events from binglog
// to fetch the corresponding GTID for required recovery time
func NewReplicaConnector(connParams *mysql.ConnParams) *ReplicaConnector {
// Construct
config := tabletenv.NewDefaultConfig()
dbCfg := &dbconfigs.DBConfigs{
Host: connParams.Host,
Port: connParams.Port,
}
dbCfg.SetDbParams(*connParams, *connParams, *connParams)
config.DB = dbCfg
c := &ReplicaConnector{conn: connParams}
env := tabletenv.NewEnv(config, "source")
c.se = schema.NewEngine(env)
c.se.SkipMetaCheck = true
c.vstreamer = vstreamer.NewEngine(env, nil, c.se, nil, "")
c.se.InitDBConfig(dbconfigs.New(connParams))
// Open
c.vstreamer.Open()
return c
}
//-----------------------------------------------------------
type ReplicaConnector struct {
conn *mysql.ConnParams
se *schema.Engine
vstreamer *vstreamer.Engine
}
func (c *ReplicaConnector) shutdown() {
c.vstreamer.Close()
c.se.Close()
}
func (c *ReplicaConnector) Open(ctx context.Context) error {
return nil
}
func (c *ReplicaConnector) Close(ctx context.Context) error {
c.shutdown()
return nil
}
func (c *ReplicaConnector) VStream(ctx context.Context, startPos string, filter *binlogdatapb.Filter, send func([]*binlogdatapb.VEvent) error) error {
return c.vstreamer.Stream(ctx, startPos, nil, filter, send)
}
// VStreamRows streams rows from query result
func (c *ReplicaConnector) VStreamRows(ctx context.Context, query string, lastpk *querypb.QueryResult, send func(*binlogdatapb.VStreamRowsResponse) error) error {
var row []sqltypes.Value
if lastpk != nil {
r := sqltypes.Proto3ToResult(lastpk)
if len(r.Rows) != 1 {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "unexpected lastpk input: %v", lastpk)
}
row = r.Rows[0]
}
return c.vstreamer.StreamRows(ctx, query, row, send)
}