vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/tx/api.go (about)

     1  /*
     2  Copyright 2020 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 tx
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"time"
    23  
    24  	querypb "vitess.io/vitess/go/vt/proto/query"
    25  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    26  	"vitess.io/vitess/go/vt/servenv"
    27  	"vitess.io/vitess/go/vt/sqlparser"
    28  )
    29  
    30  type (
    31  	// ConnID as type int64
    32  	ConnID = int64
    33  
    34  	//DTID as type string
    35  	DTID = string
    36  
    37  	//EngineStateMachine is used to control the state the transactional engine -
    38  	//whether new connections and/or transactions are allowed or not.
    39  	EngineStateMachine interface {
    40  		Init() error
    41  		AcceptReadWrite() error
    42  		AcceptReadOnly() error
    43  		StopGently()
    44  	}
    45  
    46  	// ReleaseReason as type int
    47  	ReleaseReason int
    48  
    49  	//Properties contains all information that is related to the currently running
    50  	//transaction on the connection
    51  	Properties struct {
    52  		EffectiveCaller *vtrpcpb.CallerID
    53  		ImmediateCaller *querypb.VTGateCallerID
    54  		StartTime       time.Time
    55  		EndTime         time.Time
    56  		Queries         []string
    57  		Autocommit      bool
    58  		Conclusion      string
    59  		LogToFile       bool
    60  
    61  		Stats *servenv.TimingsWrapper
    62  	}
    63  )
    64  
    65  const (
    66  	// TxClose - connection released on close.
    67  	TxClose ReleaseReason = iota
    68  
    69  	// TxCommit - connection released on commit.
    70  	TxCommit
    71  
    72  	// TxRollback - connection released on rollback.
    73  	TxRollback
    74  
    75  	// TxKill - connection released on tx kill.
    76  	TxKill
    77  
    78  	// ConnInitFail - connection released on failed to start tx.
    79  	ConnInitFail
    80  
    81  	// ConnRelease - connection closed.
    82  	ConnRelease
    83  
    84  	// ConnRenewFail - reserve connection renew failed.
    85  	ConnRenewFail
    86  )
    87  
    88  func (r ReleaseReason) String() string {
    89  	return txResolutions[r]
    90  }
    91  
    92  // Name return the name of enum.
    93  func (r ReleaseReason) Name() string {
    94  	return txNames[r]
    95  }
    96  
    97  var txResolutions = map[ReleaseReason]string{
    98  	TxClose:       "closed",
    99  	TxCommit:      "transaction committed",
   100  	TxRollback:    "transaction rolled back",
   101  	TxKill:        "kill",
   102  	ConnInitFail:  "initFail",
   103  	ConnRelease:   "release connection",
   104  	ConnRenewFail: "connection renew failed",
   105  }
   106  
   107  var txNames = map[ReleaseReason]string{
   108  	TxClose:       "close",
   109  	TxCommit:      "commit",
   110  	TxRollback:    "rollback",
   111  	TxKill:        "kill",
   112  	ConnInitFail:  "initFail",
   113  	ConnRelease:   "release",
   114  	ConnRenewFail: "renewFail",
   115  }
   116  
   117  // RecordQuery records the query against this transaction.
   118  func (p *Properties) RecordQuery(query string) {
   119  	if p == nil {
   120  		return
   121  	}
   122  	p.Queries = append(p.Queries, query)
   123  }
   124  
   125  // InTransaction returns true as soon as this struct is not nil
   126  func (p *Properties) InTransaction() bool { return p != nil }
   127  
   128  // String returns a printable version of the transaction
   129  func (p *Properties) String(sanitize bool) string {
   130  	if p == nil {
   131  		return ""
   132  	}
   133  
   134  	printQueries := func() string {
   135  		sb := strings.Builder{}
   136  		for _, query := range p.Queries {
   137  			if sanitize {
   138  				query, _ = sqlparser.RedactSQLQuery(query)
   139  			}
   140  			sb.WriteString(query)
   141  			sb.WriteString(";")
   142  		}
   143  		return sb.String()
   144  	}
   145  
   146  	return fmt.Sprintf(
   147  		"'%v'\t'%v'\t%v\t%v\t%.6f\t%v\t%v\t\n",
   148  		p.EffectiveCaller,
   149  		p.ImmediateCaller,
   150  		p.StartTime.Format(time.StampMicro),
   151  		p.EndTime.Format(time.StampMicro),
   152  		p.EndTime.Sub(p.StartTime).Seconds(),
   153  		p.Conclusion,
   154  		printQueries(),
   155  	)
   156  }