vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/rpc_schema.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 tabletmanager
    18  
    19  import (
    20  	"vitess.io/vitess/go/vt/vterrors"
    21  
    22  	"context"
    23  
    24  	"vitess.io/vitess/go/mysql"
    25  	"vitess.io/vitess/go/vt/log"
    26  	"vitess.io/vitess/go/vt/mysqlctl/tmutils"
    27  	"vitess.io/vitess/go/vt/topo/topoproto"
    28  
    29  	tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
    30  )
    31  
    32  // GetSchema returns the schema.
    33  func (tm *TabletManager) GetSchema(ctx context.Context, request *tabletmanagerdatapb.GetSchemaRequest) (*tabletmanagerdatapb.SchemaDefinition, error) {
    34  	return tm.MysqlDaemon.GetSchema(ctx, topoproto.TabletDbName(tm.Tablet()), request)
    35  }
    36  
    37  // ReloadSchema will reload the schema
    38  // This doesn't need the action mutex because periodic schema reloads happen
    39  // in the background anyway.
    40  func (tm *TabletManager) ReloadSchema(ctx context.Context, waitPosition string) error {
    41  	if tm.DBConfigs.IsZero() {
    42  		// we skip this for test instances that can't connect to the DB anyway
    43  		return nil
    44  	}
    45  
    46  	if waitPosition != "" {
    47  		pos, err := mysql.DecodePosition(waitPosition)
    48  		if err != nil {
    49  			return vterrors.Wrapf(err, "ReloadSchema: can't parse wait position (%q)", waitPosition)
    50  		}
    51  		log.Infof("ReloadSchema: waiting for replication position: %v", waitPosition)
    52  		if err := tm.MysqlDaemon.WaitSourcePos(ctx, pos); err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	log.Infof("ReloadSchema requested via RPC")
    58  	return tm.QueryServiceControl.ReloadSchema(ctx)
    59  }
    60  
    61  // PreflightSchema will try out the schema changes in "changes".
    62  func (tm *TabletManager) PreflightSchema(ctx context.Context, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error) {
    63  	if err := tm.lock(ctx); err != nil {
    64  		return nil, err
    65  	}
    66  	defer tm.unlock()
    67  
    68  	// get the db name from the tablet
    69  	dbName := topoproto.TabletDbName(tm.Tablet())
    70  
    71  	// and preflight the change
    72  	return tm.MysqlDaemon.PreflightSchemaChange(ctx, dbName, changes)
    73  }
    74  
    75  // ApplySchema will apply a schema change
    76  func (tm *TabletManager) ApplySchema(ctx context.Context, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error) {
    77  	if err := tm.lock(ctx); err != nil {
    78  		return nil, err
    79  	}
    80  	defer tm.unlock()
    81  
    82  	// get the db name from the tablet
    83  	dbName := topoproto.TabletDbName(tm.Tablet())
    84  
    85  	// apply the change
    86  	scr, err := tm.MysqlDaemon.ApplySchemaChange(ctx, dbName, change)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	// and if it worked, reload the schema
    92  	tm.ReloadSchema(ctx, "") // nolint:errcheck
    93  	return scr, nil
    94  }