vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vreplication/replica_connector.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vreplication
    18  
    19  import (
    20  	"vitess.io/vitess/go/mysql"
    21  	"vitess.io/vitess/go/vt/dbconfigs"
    22  	"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
    23  
    24  	"context"
    25  
    26  	"vitess.io/vitess/go/sqltypes"
    27  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    28  	querypb "vitess.io/vitess/go/vt/proto/query"
    29  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    30  	"vitess.io/vitess/go/vt/vterrors"
    31  	"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
    32  	"vitess.io/vitess/go/vt/vttablet/tabletserver/vstreamer"
    33  )
    34  
    35  // NewReplicaConnector returns replica connector
    36  //
    37  // This is used by binlog server to make vstream connection
    38  // using the vstream connection, it will parse the events from binglog
    39  // to fetch the corresponding GTID for required recovery time
    40  func NewReplicaConnector(connParams *mysql.ConnParams) *ReplicaConnector {
    41  
    42  	// Construct
    43  	config := tabletenv.NewDefaultConfig()
    44  	dbCfg := &dbconfigs.DBConfigs{
    45  		Host: connParams.Host,
    46  		Port: connParams.Port,
    47  	}
    48  	dbCfg.SetDbParams(*connParams, *connParams, *connParams)
    49  	config.DB = dbCfg
    50  	c := &ReplicaConnector{conn: connParams}
    51  	env := tabletenv.NewEnv(config, "source")
    52  	c.se = schema.NewEngine(env)
    53  	c.se.SkipMetaCheck = true
    54  	c.vstreamer = vstreamer.NewEngine(env, nil, c.se, nil, "")
    55  	c.se.InitDBConfig(dbconfigs.New(connParams))
    56  
    57  	// Open
    58  
    59  	c.vstreamer.Open()
    60  
    61  	return c
    62  }
    63  
    64  //-----------------------------------------------------------
    65  
    66  type ReplicaConnector struct {
    67  	conn      *mysql.ConnParams
    68  	se        *schema.Engine
    69  	vstreamer *vstreamer.Engine
    70  }
    71  
    72  func (c *ReplicaConnector) shutdown() {
    73  	c.vstreamer.Close()
    74  	c.se.Close()
    75  }
    76  
    77  func (c *ReplicaConnector) Open(ctx context.Context) error {
    78  	return nil
    79  }
    80  
    81  func (c *ReplicaConnector) Close(ctx context.Context) error {
    82  	c.shutdown()
    83  	return nil
    84  }
    85  
    86  func (c *ReplicaConnector) VStream(ctx context.Context, startPos string, filter *binlogdatapb.Filter, send func([]*binlogdatapb.VEvent) error) error {
    87  	return c.vstreamer.Stream(ctx, startPos, nil, filter, send)
    88  }
    89  
    90  // VStreamRows streams rows from query result
    91  func (c *ReplicaConnector) VStreamRows(ctx context.Context, query string, lastpk *querypb.QueryResult, send func(*binlogdatapb.VStreamRowsResponse) error) error {
    92  	var row []sqltypes.Value
    93  	if lastpk != nil {
    94  		r := sqltypes.Proto3ToResult(lastpk)
    95  		if len(r.Rows) != 1 {
    96  			return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "unexpected lastpk input: %v", lastpk)
    97  		}
    98  		row = r.Rows[0]
    99  	}
   100  	return c.vstreamer.StreamRows(ctx, query, row, send)
   101  }