vitess.io/vitess@v0.16.2/go/vt/binlog/binlogplayer/client.go (about)

     1  /*
     2  Copyright 2019 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 binlogplayer
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/spf13/pflag"
    23  
    24  	"vitess.io/vitess/go/vt/log"
    25  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  	"vitess.io/vitess/go/vt/servenv"
    28  )
    29  
    30  /*
    31  This file contains the API and registration mechanism for binlog player client.
    32  */
    33  
    34  var binlogPlayerProtocol = "grpc"
    35  
    36  func init() {
    37  	servenv.OnParseFor("vtcombo", registerFlags)
    38  	servenv.OnParseFor("vttablet", registerFlags)
    39  }
    40  
    41  func registerFlags(fs *pflag.FlagSet) {
    42  	fs.StringVar(&binlogPlayerProtocol, "binlog_player_protocol", binlogPlayerProtocol, "the protocol to download binlogs from a vttablet")
    43  }
    44  
    45  // BinlogTransactionStream is the interface of the object returned by
    46  // StreamTables and StreamKeyRange
    47  type BinlogTransactionStream interface {
    48  	// Recv returns the next BinlogTransaction, or an error if the RPC was
    49  	// interrupted.
    50  	Recv() (*binlogdatapb.BinlogTransaction, error)
    51  }
    52  
    53  // Client is the interface all clients must satisfy
    54  type Client interface {
    55  	// Dial a server
    56  	Dial(tablet *topodatapb.Tablet) error
    57  
    58  	// Close the connection
    59  	Close()
    60  
    61  	// Ask the server to stream updates related to the provided tables.
    62  	// Should return context.Canceled if the context is canceled.
    63  	StreamTables(ctx context.Context, position string, tables []string, charset *binlogdatapb.Charset) (BinlogTransactionStream, error)
    64  
    65  	// Ask the server to stream updates related to the provided keyrange.
    66  	// Should return context.Canceled if the context is canceled.
    67  	StreamKeyRange(ctx context.Context, position string, keyRange *topodatapb.KeyRange, charset *binlogdatapb.Charset) (BinlogTransactionStream, error)
    68  }
    69  
    70  // ClientFactory is the factory method to create a Client
    71  type ClientFactory func() Client
    72  
    73  var clientFactories = make(map[string]ClientFactory)
    74  
    75  // RegisterClientFactory adds a new factory. Call during init().
    76  func RegisterClientFactory(name string, factory ClientFactory) {
    77  	if _, ok := clientFactories[name]; ok {
    78  		log.Fatalf("ClientFactory %s already exists", name)
    79  	}
    80  	clientFactories[name] = factory
    81  }