vitess.io/vitess@v0.16.2/go/vt/mysqlctl/mysqlctlclient/interface.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 mysqlctlclient contains the generic client side of the remote
    18  // mysqlctl protocol.
    19  package mysqlctlclient
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  
    25  	"github.com/spf13/pflag"
    26  
    27  	"vitess.io/vitess/go/vt/log"
    28  	"vitess.io/vitess/go/vt/servenv"
    29  )
    30  
    31  var protocol = "grpc"
    32  
    33  func init() {
    34  	servenv.OnParseFor("mysqlctl", registerFlags)
    35  }
    36  
    37  func registerFlags(fs *pflag.FlagSet) {
    38  	fs.StringVar(&protocol, "mysqlctl_client_protocol", protocol, "the protocol to use to talk to the mysqlctl server")
    39  }
    40  
    41  // MysqlctlClient defines the interface used to send remote mysqlctl commands
    42  type MysqlctlClient interface {
    43  	// Start calls Mysqld.Start remotely.
    44  	Start(ctx context.Context, mysqldArgs ...string) error
    45  
    46  	// Shutdown calls Mysqld.Shutdown remotely.
    47  	Shutdown(ctx context.Context, waitForMysqld bool) error
    48  
    49  	// RunMysqlUpgrade calls Mysqld.RunMysqlUpgrade remotely.
    50  	RunMysqlUpgrade(ctx context.Context) error
    51  
    52  	// ReinitConfig calls Mysqld.ReinitConfig remotely.
    53  	ReinitConfig(ctx context.Context) error
    54  
    55  	// RefreshConfig calls Mysqld.RefreshConfig remotely.
    56  	RefreshConfig(ctx context.Context) error
    57  
    58  	// Close will terminate the connection. This object won't be used anymore.
    59  	Close()
    60  }
    61  
    62  // Factory functions are registered by client implementations.
    63  type Factory func(network, addr string) (MysqlctlClient, error)
    64  
    65  var factories = make(map[string]Factory)
    66  
    67  // RegisterFactory allows a client implementation to register itself
    68  func RegisterFactory(name string, factory Factory) {
    69  	if _, ok := factories[name]; ok {
    70  		log.Fatalf("RegisterFactory %s already exists", name)
    71  	}
    72  	factories[name] = factory
    73  }
    74  
    75  // New creates a client implementation as specified by a flag.
    76  func New(network, addr string) (MysqlctlClient, error) {
    77  	factory, ok := factories[protocol]
    78  	if !ok {
    79  		return nil, fmt.Errorf("unknown mysqlctl client protocol: %v", protocol)
    80  	}
    81  	return factory(network, addr)
    82  }