vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/tabletenv/env.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 tabletenv maintains environment variables and types that
    18  // are common for all packages of tabletserver.
    19  package tabletenv
    20  
    21  import (
    22  	"vitess.io/vitess/go/tb"
    23  	"vitess.io/vitess/go/vt/log"
    24  	"vitess.io/vitess/go/vt/servenv"
    25  )
    26  
    27  // Env defines the functions supported by TabletServer
    28  // that the sub-componennts need to access.
    29  type Env interface {
    30  	CheckMySQL()
    31  	Config() *TabletConfig
    32  	Exporter() *servenv.Exporter
    33  	Stats() *Stats
    34  	LogError()
    35  }
    36  
    37  type testEnv struct {
    38  	config   *TabletConfig
    39  	exporter *servenv.Exporter
    40  	stats    *Stats
    41  }
    42  
    43  // NewEnv creates an Env that can be used for tabletserver subcomponents
    44  // without an actual TabletServer.
    45  func NewEnv(config *TabletConfig, exporterName string) Env {
    46  	exporter := servenv.NewExporter(exporterName, "Tablet")
    47  	return &testEnv{
    48  		config:   config,
    49  		exporter: exporter,
    50  		stats:    NewStats(exporter),
    51  	}
    52  }
    53  
    54  func (*testEnv) CheckMySQL()                    {}
    55  func (te *testEnv) Config() *TabletConfig       { return te.config }
    56  func (te *testEnv) Exporter() *servenv.Exporter { return te.exporter }
    57  func (te *testEnv) Stats() *Stats               { return te.stats }
    58  
    59  func (te *testEnv) LogError() {
    60  	if x := recover(); x != nil {
    61  		log.Errorf("Uncaught panic:\n%v\n%s", x, tb.Stack(4))
    62  		te.Stats().InternalErrors.Add("Panic", 1)
    63  	}
    64  }