vitess.io/vitess@v0.16.2/go/cmd/vtctld/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 agreedto 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 main
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/spf13/pflag"
    24  
    25  	"vitess.io/vitess/go/flagutil"
    26  	"vitess.io/vitess/go/timer"
    27  	"vitess.io/vitess/go/vt/log"
    28  	"vitess.io/vitess/go/vt/logutil"
    29  	"vitess.io/vitess/go/vt/schemamanager"
    30  	"vitess.io/vitess/go/vt/servenv"
    31  	"vitess.io/vitess/go/vt/vttablet/tmclient"
    32  	"vitess.io/vitess/go/vt/wrangler"
    33  )
    34  
    35  var (
    36  	schemaChangeDir             string
    37  	schemaChangeController      string
    38  	schemaChangeUser            string
    39  	schemaChangeCheckInterval   = flagutil.NewDurationOrIntVar("schema_change_check_interval", time.Minute, time.Second)
    40  	schemaChangeReplicasTimeout = wrangler.DefaultWaitReplicasTimeout
    41  )
    42  
    43  func init() {
    44  	servenv.OnParse(func(fs *pflag.FlagSet) {
    45  		fs.StringVar(&schemaChangeDir, "schema_change_dir", schemaChangeDir, "Directory containing schema changes for all keyspaces. Each keyspace has its own directory, and schema changes are expected to live in '$KEYSPACE/input' dir. (e.g. 'test_keyspace/input/*sql'). Each sql file represents a schema change.")
    46  		fs.StringVar(&schemaChangeController, "schema_change_controller", schemaChangeController, "Schema change controller is responsible for finding schema changes and responding to schema change events.")
    47  		fs.StringVar(&schemaChangeUser, "schema_change_user", schemaChangeUser, "The user who schema changes are submitted on behalf of.")
    48  
    49  		fs.Var(schemaChangeCheckInterval, "schema_change_check_interval", "How often the schema change dir is checked for schema changes (deprecated: if passed as a bare integer, the duration will be in seconds).")
    50  		fs.DurationVar(&schemaChangeReplicasTimeout, "schema_change_replicas_timeout", schemaChangeReplicasTimeout, "How long to wait for replicas to receive a schema change.")
    51  	})
    52  }
    53  
    54  func initSchema() {
    55  	// Start schema manager service if needed.
    56  	if schemaChangeDir != "" {
    57  		interval := time.Minute
    58  		if schemaChangeCheckInterval.Value() > time.Duration(0) {
    59  			interval = schemaChangeCheckInterval.Value()
    60  		}
    61  		timer := timer.NewTimer(interval)
    62  		controllerFactory, err :=
    63  			schemamanager.GetControllerFactory(schemaChangeController)
    64  		if err != nil {
    65  			log.Fatalf("unable to get a controller factory, error: %v", err)
    66  		}
    67  
    68  		timer.Start(func() {
    69  			controller, err := controllerFactory(map[string]string{
    70  				schemamanager.SchemaChangeDirName: schemaChangeDir,
    71  				schemamanager.SchemaChangeUser:    schemaChangeUser,
    72  			})
    73  			if err != nil {
    74  				log.Errorf("failed to get controller, error: %v", err)
    75  				return
    76  			}
    77  			ctx := context.Background()
    78  			wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
    79  			_, err = schemamanager.Run(
    80  				ctx,
    81  				controller,
    82  				schemamanager.NewTabletExecutor("vtctld/schema", wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), schemaChangeReplicasTimeout),
    83  			)
    84  			if err != nil {
    85  				log.Errorf("Schema change failed, error: %v", err)
    86  			}
    87  		})
    88  		servenv.OnClose(func() { timer.Stop() })
    89  	}
    90  }