vitess.io/vitess@v0.16.2/go/vt/wrangler/wrangler.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 wrangler contains the Wrangler object to manage complex 18 // topology actions. 19 package wrangler 20 21 import ( 22 "context" 23 24 "vitess.io/vitess/go/sqltypes" 25 "vitess.io/vitess/go/sync2" 26 "vitess.io/vitess/go/vt/logutil" 27 "vitess.io/vitess/go/vt/topo" 28 "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver" 29 "vitess.io/vitess/go/vt/vttablet/tmclient" 30 31 vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" 32 ) 33 34 var ( 35 // DefaultActionTimeout is a good default for interactive 36 // remote actions. We usually take a lock then do an action, 37 // lock actions use RemoteOperationTimeout, 38 // so basing this to be greater than RemoteOperationTimeout is good. 39 // Use this as the default value for Context that need a deadline. 40 DefaultActionTimeout = topo.RemoteOperationTimeout * 4 41 ) 42 43 // Wrangler manages complex actions on the topology, like reparents, 44 // backups, resharding, ... 45 // 46 // Multiple go routines can use the same Wrangler at the same time, 47 // provided they want to share the same logger / topo server / lock timeout. 48 type Wrangler struct { 49 logger logutil.Logger 50 ts *topo.Server 51 tmc tmclient.TabletManagerClient 52 vtctld vtctlservicepb.VtctldServer 53 sourceTs *topo.Server 54 // VExecFunc is a test-only fixture that allows us to short circuit vexec commands. 55 // DO NOT USE in production code. 56 VExecFunc func(ctx context.Context, workflow, keyspace, query string, dryRun bool) (map[*topo.TabletInfo]*sqltypes.Result, error) 57 // Limt the number of concurrent background goroutines if needed. 58 sem *sync2.Semaphore 59 } 60 61 // New creates a new Wrangler object. 62 func New(logger logutil.Logger, ts *topo.Server, tmc tmclient.TabletManagerClient) *Wrangler { 63 return &Wrangler{ 64 logger: logger, 65 ts: ts, 66 tmc: tmc, 67 vtctld: grpcvtctldserver.NewVtctldServer(ts), 68 sourceTs: ts, 69 } 70 } 71 72 // NewTestWrangler creates a new Wrangler object for use in tests. This should NOT be used 73 // in production. 74 func NewTestWrangler(logger logutil.Logger, ts *topo.Server, tmc tmclient.TabletManagerClient) *Wrangler { 75 return &Wrangler{ 76 logger: logger, 77 ts: ts, 78 tmc: tmc, 79 vtctld: grpcvtctldserver.NewTestVtctldServer(ts, tmc), 80 sourceTs: ts, 81 } 82 } 83 84 // TopoServer returns the topo.Server this wrangler is using. 85 func (wr *Wrangler) TopoServer() *topo.Server { 86 return wr.ts 87 } 88 89 // TabletManagerClient returns the tmclient.TabletManagerClient this 90 // wrangler is using. 91 func (wr *Wrangler) TabletManagerClient() tmclient.TabletManagerClient { 92 return wr.tmc 93 } 94 95 // VtctldServer returns the vtctlservicepb.VtctldServer implementation this 96 // wrangler is using. 97 func (wr *Wrangler) VtctldServer() vtctlservicepb.VtctldServer { 98 return wr.vtctld 99 } 100 101 // SetLogger can be used to change the current logger. Not synchronized, 102 // no calls to this wrangler should be in progress. 103 func (wr *Wrangler) SetLogger(logger logutil.Logger) { 104 wr.logger = logger 105 } 106 107 // Logger returns the logger associated with this wrangler. 108 func (wr *Wrangler) Logger() logutil.Logger { 109 return wr.logger 110 }