vitess.io/vitess@v0.16.2/go/vt/vttablet/faketmclient/fake_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 faketmclient
    18  
    19  // This file contains a "fake" implementation of the TabletManagerClient interface, which
    20  // may be useful for running tests without having to bring up a cluster. The implementation
    21  // is very minimal, and only works for specific use-cases. If you find that it doesn't work
    22  // for yours, feel free to extend this implementation.
    23  
    24  import (
    25  	"context"
    26  	"io"
    27  	"time"
    28  
    29  	"vitess.io/vitess/go/sqltypes"
    30  	"vitess.io/vitess/go/vt/hook"
    31  	"vitess.io/vitess/go/vt/logutil"
    32  	"vitess.io/vitess/go/vt/mysqlctl/tmutils"
    33  	"vitess.io/vitess/go/vt/vttablet/tmclient"
    34  
    35  	logutilpb "vitess.io/vitess/go/vt/proto/logutil"
    36  	querypb "vitess.io/vitess/go/vt/proto/query"
    37  	replicationdatapb "vitess.io/vitess/go/vt/proto/replicationdata"
    38  	tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
    39  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    40  )
    41  
    42  // NewFakeTabletManagerClient should be used to create a new FakeTabletManagerClient.
    43  // There is intentionally no init in this file with a call to RegisterTabletManagerClientFactory.
    44  // There shouldn't be any legitimate use-case where we would want to start a vitess cluster
    45  // with a FakeTMC, and we don't want to do it by accident.
    46  func NewFakeTabletManagerClient() tmclient.TabletManagerClient {
    47  	return &FakeTabletManagerClient{
    48  		tmc: tmclient.NewTabletManagerClient(),
    49  	}
    50  }
    51  
    52  // FakeTabletManagerClient implements tmclient.TabletManagerClient
    53  // TODO(aaijazi): this is a pretty complicated and inconsistent implementation. It can't
    54  // make up its mind on whether it wants to be a fake, a mock, or act like the real thing.
    55  // We probably want to move it more consistently towards being a mock, once we standardize
    56  // how we want to do mocks in vitess. We don't currently have a good way to configure
    57  // specific return values.
    58  type FakeTabletManagerClient struct {
    59  	// Keep a real TMC, so we can pass through certain calls.
    60  	// This is to let us essentially fake out only part of the interface, while deferring
    61  	// to the real implementation for things that we don't need to fake out yet.
    62  	tmc tmclient.TabletManagerClient
    63  }
    64  
    65  func (client *FakeTabletManagerClient) VDiff(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.VDiffRequest) (*tabletmanagerdatapb.VDiffResponse, error) {
    66  	return nil, nil
    67  }
    68  
    69  //
    70  // Various read-only methods
    71  //
    72  
    73  // Ping is part of the tmclient.TabletManagerClient interface.
    74  func (client *FakeTabletManagerClient) Ping(ctx context.Context, tablet *topodatapb.Tablet) error {
    75  	return nil
    76  }
    77  
    78  // Sleep is part of the tmclient.TabletManagerClient interface.
    79  func (client *FakeTabletManagerClient) Sleep(ctx context.Context, tablet *topodatapb.Tablet, duration time.Duration) error {
    80  	return nil
    81  }
    82  
    83  // ExecuteHook is part of the tmclient.TabletManagerClient interface.
    84  func (client *FakeTabletManagerClient) ExecuteHook(ctx context.Context, tablet *topodatapb.Tablet, hk *hook.Hook) (*hook.HookResult, error) {
    85  	var hr hook.HookResult
    86  	return &hr, nil
    87  }
    88  
    89  // GetSchema is part of the tmclient.TabletManagerClient interface.
    90  func (client *FakeTabletManagerClient) GetSchema(ctx context.Context, tablet *topodatapb.Tablet, request *tabletmanagerdatapb.GetSchemaRequest) (*tabletmanagerdatapb.SchemaDefinition, error) {
    91  	return client.tmc.GetSchema(ctx, tablet, request)
    92  }
    93  
    94  // GetPermissions is part of the tmclient.TabletManagerClient interface.
    95  func (client *FakeTabletManagerClient) GetPermissions(ctx context.Context, tablet *topodatapb.Tablet) (*tabletmanagerdatapb.Permissions, error) {
    96  	return &tabletmanagerdatapb.Permissions{}, nil
    97  }
    98  
    99  // LockTables is part of the tmclient.TabletManagerClient interface.
   100  func (client *FakeTabletManagerClient) LockTables(ctx context.Context, tablet *topodatapb.Tablet) error {
   101  	return nil
   102  }
   103  
   104  // UnlockTables is part of the tmclient.TabletManagerClient interface.
   105  func (client *FakeTabletManagerClient) UnlockTables(ctx context.Context, tablet *topodatapb.Tablet) error {
   106  	return nil
   107  }
   108  
   109  //
   110  // Various read-write methods
   111  //
   112  
   113  // SetReadOnly is part of the tmclient.TabletManagerClient interface.
   114  func (client *FakeTabletManagerClient) SetReadOnly(ctx context.Context, tablet *topodatapb.Tablet) error {
   115  	return nil
   116  }
   117  
   118  // SetReadWrite is part of the tmclient.TabletManagerClient interface.
   119  func (client *FakeTabletManagerClient) SetReadWrite(ctx context.Context, tablet *topodatapb.Tablet) error {
   120  	return nil
   121  }
   122  
   123  // ChangeType is part of the tmclient.TabletManagerClient interface.
   124  func (client *FakeTabletManagerClient) ChangeType(ctx context.Context, tablet *topodatapb.Tablet, dbType topodatapb.TabletType, semiSync bool) error {
   125  	return nil
   126  }
   127  
   128  // RefreshState is part of the tmclient.TabletManagerClient interface.
   129  func (client *FakeTabletManagerClient) RefreshState(ctx context.Context, tablet *topodatapb.Tablet) error {
   130  	return nil
   131  }
   132  
   133  // RunHealthCheck is part of the tmclient.TabletManagerClient interface.
   134  func (client *FakeTabletManagerClient) RunHealthCheck(ctx context.Context, tablet *topodatapb.Tablet) error {
   135  	return nil
   136  }
   137  
   138  // ReloadSchema is part of the tmclient.TabletManagerClient interface.
   139  func (client *FakeTabletManagerClient) ReloadSchema(ctx context.Context, tablet *topodatapb.Tablet, waitPosition string) error {
   140  	return nil
   141  }
   142  
   143  // PreflightSchema is part of the tmclient.TabletManagerClient interface.
   144  func (client *FakeTabletManagerClient) PreflightSchema(ctx context.Context, tablet *topodatapb.Tablet, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error) {
   145  	return make([]*tabletmanagerdatapb.SchemaChangeResult, len(changes)), nil
   146  }
   147  
   148  // ApplySchema is part of the tmclient.TabletManagerClient interface.
   149  func (client *FakeTabletManagerClient) ApplySchema(ctx context.Context, tablet *topodatapb.Tablet, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error) {
   150  	return &tabletmanagerdatapb.SchemaChangeResult{}, nil
   151  }
   152  
   153  // ExecuteQuery is part of the tmclient.TabletManagerClient interface.
   154  func (client *FakeTabletManagerClient) ExecuteQuery(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.ExecuteQueryRequest) (*querypb.QueryResult, error) {
   155  	return &querypb.QueryResult{}, nil
   156  }
   157  
   158  // ExecuteFetchAsDba is part of the tmclient.TabletManagerClient interface.
   159  func (client *FakeTabletManagerClient) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, req *tabletmanagerdatapb.ExecuteFetchAsDbaRequest) (*querypb.QueryResult, error) {
   160  	return &querypb.QueryResult{}, nil
   161  }
   162  
   163  // ExecuteFetchAsAllPrivs is part of the tmclient.TabletManagerClient interface.
   164  func (client *FakeTabletManagerClient) ExecuteFetchAsAllPrivs(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.ExecuteFetchAsAllPrivsRequest) (*querypb.QueryResult, error) {
   165  	return &querypb.QueryResult{}, nil
   166  }
   167  
   168  // ExecuteFetchAsApp is part of the tmclient.TabletManagerClient interface.
   169  func (client *FakeTabletManagerClient) ExecuteFetchAsApp(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, req *tabletmanagerdatapb.ExecuteFetchAsAppRequest) (*querypb.QueryResult, error) {
   170  	return &querypb.QueryResult{}, nil
   171  }
   172  
   173  //
   174  // Replication related methods
   175  //
   176  
   177  // ReplicationStatus is part of the tmclient.TabletManagerClient interface.
   178  func (client *FakeTabletManagerClient) ReplicationStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.Status, error) {
   179  	return &replicationdatapb.Status{}, nil
   180  }
   181  
   182  // FullStatus is part of the tmclient.TabletManagerClient interface.
   183  func (client *FakeTabletManagerClient) FullStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.FullStatus, error) {
   184  	return &replicationdatapb.FullStatus{}, nil
   185  }
   186  
   187  // StopReplication is part of the tmclient.TabletManagerClient interface.
   188  func (client *FakeTabletManagerClient) StopReplication(ctx context.Context, tablet *topodatapb.Tablet) error {
   189  	return nil
   190  }
   191  
   192  // StopReplicationMinimum is part of the tmclient.TabletManagerClient interface.
   193  func (client *FakeTabletManagerClient) StopReplicationMinimum(ctx context.Context, tablet *topodatapb.Tablet, stopPos string, waitTime time.Duration) (string, error) {
   194  	return "", nil
   195  }
   196  
   197  // PrimaryStatus is part of the tmclient.TabletManagerClient interface.
   198  func (client *FakeTabletManagerClient) PrimaryStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.PrimaryStatus, error) {
   199  	return &replicationdatapb.PrimaryStatus{}, nil
   200  }
   201  
   202  // StartReplication is part of the tmclient.TabletManagerClient interface.
   203  func (client *FakeTabletManagerClient) StartReplication(ctx context.Context, tablet *topodatapb.Tablet, semiSync bool) error {
   204  	return nil
   205  }
   206  
   207  // StartReplicationUntilAfter is part of the tmclient.TabletManagerClient interface.
   208  func (client *FakeTabletManagerClient) StartReplicationUntilAfter(ctx context.Context, tablet *topodatapb.Tablet, position string, duration time.Duration) error {
   209  	return nil
   210  }
   211  
   212  // GetReplicas is part of the tmclient.TabletManagerClient interface.
   213  func (client *FakeTabletManagerClient) GetReplicas(ctx context.Context, tablet *topodatapb.Tablet) ([]string, error) {
   214  	return nil, nil
   215  }
   216  
   217  // InitReplica is part of the tmclient.TabletManagerClient interface.
   218  func (client *FakeTabletManagerClient) InitReplica(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias, replicationPosition string, timeCreatedNS int64, semiSync bool) error {
   219  	return nil
   220  }
   221  
   222  // ReplicaWasPromoted is part of the tmclient.TabletManagerClient interface.
   223  func (client *FakeTabletManagerClient) ReplicaWasPromoted(ctx context.Context, tablet *topodatapb.Tablet) error {
   224  	return nil
   225  }
   226  
   227  // ResetReplicationParameters is part of the tmclient.TabletManagerClient interface.
   228  func (client *FakeTabletManagerClient) ResetReplicationParameters(ctx context.Context, tablet *topodatapb.Tablet) error {
   229  	return nil
   230  }
   231  
   232  // ReplicaWasRestarted is part of the tmclient.TabletManagerClient interface.
   233  func (client *FakeTabletManagerClient) ReplicaWasRestarted(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias) error {
   234  	return nil
   235  }
   236  
   237  // PrimaryPosition is part of the tmclient.TabletManagerClient interface.
   238  func (client *FakeTabletManagerClient) PrimaryPosition(ctx context.Context, tablet *topodatapb.Tablet) (string, error) {
   239  	return "", nil
   240  }
   241  
   242  // WaitForPosition is part of the tmclient.TabletManagerClient interface.
   243  func (client *FakeTabletManagerClient) WaitForPosition(ctx context.Context, tablet *topodatapb.Tablet, pos string) error {
   244  	return nil
   245  }
   246  
   247  // VExec is part of the tmclient.TabletManagerClient interface.
   248  func (client *FakeTabletManagerClient) VExec(ctx context.Context, tablet *topodatapb.Tablet, query, workflow, keyspace string) (*querypb.QueryResult, error) {
   249  	// This result satisfies a generic VExec command
   250  	result := sqltypes.MakeTestResult(
   251  		sqltypes.MakeTestFields("id", "int"),
   252  		"complete",
   253  	)
   254  	return sqltypes.ResultToProto3(result), nil
   255  }
   256  
   257  // VReplicationExec is part of the tmclient.TabletManagerClient interface.
   258  func (client *FakeTabletManagerClient) VReplicationExec(ctx context.Context, tablet *topodatapb.Tablet, query string) (*querypb.QueryResult, error) {
   259  	// This result satisfies 'select pos from _vt.vreplication...' called from split clone unit tests in go/vt/worker.
   260  	result := sqltypes.MakeTestResult(
   261  		sqltypes.MakeTestFields("pos", "varchar"),
   262  		"MariaDB/1-1-1",
   263  	)
   264  	return sqltypes.ResultToProto3(result), nil
   265  }
   266  
   267  // VReplicationWaitForPos is part of the tmclient.TabletManagerClient interface.
   268  func (client *FakeTabletManagerClient) VReplicationWaitForPos(ctx context.Context, tablet *topodatapb.Tablet, id int, pos string) error {
   269  	return nil
   270  }
   271  
   272  //
   273  // Reparenting related functions
   274  //
   275  
   276  // ResetReplication is part of the tmclient.TabletManagerClient interface.
   277  func (client *FakeTabletManagerClient) ResetReplication(ctx context.Context, tablet *topodatapb.Tablet) error {
   278  	return nil
   279  }
   280  
   281  // InitPrimary is part of the tmclient.TabletManagerClient interface.
   282  func (client *FakeTabletManagerClient) InitPrimary(ctx context.Context, tablet *topodatapb.Tablet, semiSync bool) (string, error) {
   283  	return "", nil
   284  }
   285  
   286  // PopulateReparentJournal is part of the tmclient.TabletManagerClient interface.
   287  func (client *FakeTabletManagerClient) PopulateReparentJournal(ctx context.Context, tablet *topodatapb.Tablet, timeCreatedNS int64, actionName string, masterAlias *topodatapb.TabletAlias, position string) error {
   288  	return nil
   289  }
   290  
   291  // DemotePrimary is part of the tmclient.TabletManagerClient interface.
   292  func (client *FakeTabletManagerClient) DemotePrimary(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.PrimaryStatus, error) {
   293  	return nil, nil
   294  }
   295  
   296  // UndoDemotePrimary is part of the tmclient.TabletManagerClient interface.
   297  func (client *FakeTabletManagerClient) UndoDemotePrimary(ctx context.Context, tablet *topodatapb.Tablet, semiSync bool) error {
   298  	return nil
   299  }
   300  
   301  // SetReplicationSource is part of the tmclient.TabletManagerClient interface.
   302  func (client *FakeTabletManagerClient) SetReplicationSource(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias, timeCreatedNS int64, waitPosition string, forceStartReplication bool, semiSync bool) error {
   303  	return nil
   304  }
   305  
   306  // StopReplicationAndGetStatus is part of the tmclient.TabletManagerClient interface.
   307  func (client *FakeTabletManagerClient) StopReplicationAndGetStatus(ctx context.Context, tablet *topodatapb.Tablet, stopReplicationMode replicationdatapb.StopReplicationMode) (*replicationdatapb.StopReplicationStatus, error) {
   308  	return &replicationdatapb.StopReplicationStatus{}, nil
   309  }
   310  
   311  // PromoteReplica is part of the tmclient.TabletManagerClient interface.
   312  func (client *FakeTabletManagerClient) PromoteReplica(ctx context.Context, tablet *topodatapb.Tablet, semiSync bool) (string, error) {
   313  	return "", nil
   314  }
   315  
   316  //
   317  // Backup related methods
   318  //
   319  
   320  type eofEventStream struct{}
   321  
   322  func (e *eofEventStream) Recv() (*logutilpb.Event, error) {
   323  	return nil, io.EOF
   324  }
   325  
   326  // Backup is part of the tmclient.TabletManagerClient interface.
   327  func (client *FakeTabletManagerClient) Backup(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.BackupRequest) (logutil.EventStream, error) {
   328  	return &eofEventStream{}, nil
   329  }
   330  
   331  // RestoreFromBackup is part of the tmclient.TabletManagerClient interface.
   332  func (client *FakeTabletManagerClient) RestoreFromBackup(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.RestoreFromBackupRequest) (logutil.EventStream, error) {
   333  	return &eofEventStream{}, nil
   334  }
   335  
   336  //
   337  // Management related methods
   338  //
   339  
   340  // Close is part of the tmclient.TabletManagerClient interface.
   341  func (client *FakeTabletManagerClient) Close() {
   342  }