vitess.io/vitess@v0.16.2/go/vt/vttablet/endtoend/framework/server.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 framework
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"time"
    25  
    26  	"vitess.io/vitess/go/vt/log"
    27  	"vitess.io/vitess/go/vt/topo"
    28  	"vitess.io/vitess/go/yaml2"
    29  
    30  	"vitess.io/vitess/go/vt/topo/memorytopo"
    31  	"vitess.io/vitess/go/vt/vterrors"
    32  
    33  	"vitess.io/vitess/go/mysql"
    34  	"vitess.io/vitess/go/vt/dbconfigs"
    35  	"vitess.io/vitess/go/vt/vtgate/fakerpcvtgateconn"
    36  	"vitess.io/vitess/go/vt/vtgate/vtgateconn"
    37  	"vitess.io/vitess/go/vt/vttablet/tabletserver"
    38  	"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
    39  
    40  	querypb "vitess.io/vitess/go/vt/proto/query"
    41  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    42  )
    43  
    44  var (
    45  	// Target is the target info for the server.
    46  	Target *querypb.Target
    47  	// Server is the TabletServer for the framework.
    48  	Server *tabletserver.TabletServer
    49  	// ServerAddress is the http URL for the server.
    50  	ServerAddress string
    51  	// ResolveChan is the channel that sends dtids that are to be resolved.
    52  	ResolveChan = make(chan string, 1)
    53  	// TopoServer is the topology for the server
    54  	TopoServer *topo.Server
    55  )
    56  
    57  // StartCustomServer starts the server and initializes
    58  // all the global variables. This function should only be called
    59  // once at the beginning of the test.
    60  func StartCustomServer(connParams, connAppDebugParams mysql.ConnParams, dbName string, config *tabletenv.TabletConfig) error {
    61  	// Setup a fake vtgate server.
    62  	protocol := "resolveTest"
    63  	vtgateconn.SetVTGateProtocol(protocol)
    64  	vtgateconn.RegisterDialer(protocol, func(context.Context, string) (vtgateconn.Impl, error) {
    65  		return &txResolver{
    66  			FakeVTGateConn: fakerpcvtgateconn.FakeVTGateConn{},
    67  		}, nil
    68  	})
    69  
    70  	dbcfgs := dbconfigs.NewTestDBConfigs(connParams, connAppDebugParams, dbName)
    71  
    72  	Target = &querypb.Target{
    73  		Keyspace:   "vttest",
    74  		Shard:      "0",
    75  		TabletType: topodatapb.TabletType_PRIMARY,
    76  	}
    77  	TopoServer = memorytopo.NewServer("")
    78  
    79  	Server = tabletserver.NewTabletServer("", config, TopoServer, &topodatapb.TabletAlias{})
    80  	Server.Register()
    81  	err := Server.StartService(Target, dbcfgs, nil /* mysqld */)
    82  	if err != nil {
    83  		return vterrors.Wrap(err, "could not start service")
    84  	}
    85  
    86  	// Start http service.
    87  	ln, err := net.Listen("tcp", "127.0.0.1:0")
    88  	if err != nil {
    89  		return vterrors.Wrap(err, "could not start listener")
    90  	}
    91  	ServerAddress = fmt.Sprintf("http://%s", ln.Addr().String())
    92  	go http.Serve(ln, nil)
    93  	for {
    94  		time.Sleep(10 * time.Millisecond)
    95  		response, err := http.Get(fmt.Sprintf("%s/debug/vars", ServerAddress))
    96  		if err == nil {
    97  			response.Body.Close()
    98  			break
    99  		}
   100  	}
   101  	return nil
   102  }
   103  
   104  // StartServer starts the server and initializes
   105  // all the global variables. This function should only be called
   106  // once at the beginning of the test.
   107  func StartServer(connParams, connAppDebugParams mysql.ConnParams, dbName string) error {
   108  	config := tabletenv.NewDefaultConfig()
   109  	config.StrictTableACL = true
   110  	config.TwoPCEnable = true
   111  	config.TwoPCAbandonAge = 1
   112  	config.TwoPCCoordinatorAddress = "fake"
   113  	config.HotRowProtection.Mode = tabletenv.Enable
   114  	config.TrackSchemaVersions = true
   115  	config.GracePeriods.ShutdownSeconds = 2
   116  	config.SignalSchemaChangeReloadIntervalSeconds = tabletenv.Seconds(2.1)
   117  	config.SignalWhenSchemaChange = true
   118  	config.Healthcheck.IntervalSeconds = 0.1
   119  	config.Oltp.TxTimeoutSeconds = 5
   120  	config.Olap.TxTimeoutSeconds = 5
   121  	config.EnableViews = true
   122  	gotBytes, _ := yaml2.Marshal(config)
   123  	log.Infof("Config:\n%s", gotBytes)
   124  	return StartCustomServer(connParams, connAppDebugParams, dbName, config)
   125  }
   126  
   127  // StopServer must be called once all the tests are done.
   128  func StopServer() {
   129  	Server.StopService()
   130  }
   131  
   132  // txReolver transmits dtids to be resolved through ResolveChan.
   133  type txResolver struct {
   134  	fakerpcvtgateconn.FakeVTGateConn
   135  }
   136  
   137  func (conn *txResolver) ResolveTransaction(ctx context.Context, dtid string) error {
   138  	select {
   139  	case ResolveChan <- dtid:
   140  	default:
   141  	}
   142  	return nil
   143  }