github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/stochastikctx/context.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package stochastikctx
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  	"github.com/whtcorpsinc/fidelpb/go-binlog"
    22  	"github.com/whtcorpsinc/milevadb/ekv"
    23  	"github.com/whtcorpsinc/milevadb/soliton"
    24  	"github.com/whtcorpsinc/milevadb/soliton/ekvcache"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    26  	"github.com/whtcorpsinc/milevadb/tenant"
    27  )
    28  
    29  // Context is an interface for transaction and executive args environment.
    30  type Context interface {
    31  	// NewTxn creates a new transaction for further execution.
    32  	// If old transaction is valid, it is committed first.
    33  	// It's used in BEGIN memex and DBS memexs to commit old transaction.
    34  	NewTxn(context.Context) error
    35  
    36  	// Txn returns the current transaction which is created before executing a memex.
    37  	// The returned ekv.Transaction is not nil, but it maybe pending or invalid.
    38  	// If the active parameter is true, call this function will wait for the pending txn
    39  	// to become valid.
    40  	Txn(active bool) (ekv.Transaction, error)
    41  
    42  	// GetClient gets a ekv.Client.
    43  	GetClient() ekv.Client
    44  
    45  	// SetValue saves a value associated with this context for key.
    46  	SetValue(key fmt.Stringer, value interface{})
    47  
    48  	// Value returns the value associated with this context for key.
    49  	Value(key fmt.Stringer) interface{}
    50  
    51  	// ClearValue clears the value associated with this context for key.
    52  	ClearValue(key fmt.Stringer)
    53  
    54  	GetStochastikVars() *variable.StochastikVars
    55  
    56  	GetStochastikManager() soliton.StochastikManager
    57  
    58  	// RefreshTxnCtx commits old transaction without retry,
    59  	// and creates a new transaction.
    60  	// now just for load data and batch insert.
    61  	RefreshTxnCtx(context.Context) error
    62  
    63  	// InitTxnWithStartTS initializes a transaction with startTS.
    64  	// It should be called right before we builds an interlock.
    65  	InitTxnWithStartTS(startTS uint64) error
    66  
    67  	// GetStore returns the causetstore of stochastik.
    68  	GetStore() ekv.CausetStorage
    69  
    70  	// PreparedCausetCache returns the cache of the physical plan
    71  	PreparedCausetCache() *ekvcache.SimpleLRUCache
    72  
    73  	// StoreQueryFeedback stores the query feedback.
    74  	StoreQueryFeedback(feedback interface{})
    75  
    76  	// HasDirtyContent checks whether there's dirty uFIDelate on the given causet.
    77  	HasDirtyContent(tid int64) bool
    78  
    79  	// StmtCommit flush all changes by the memex to the underlying transaction.
    80  	StmtCommit()
    81  	// StmtRollback provides memex level rollback.
    82  	StmtRollback()
    83  	// StmtGetMutation gets the binlog mutation for current memex.
    84  	StmtGetMutation(int64) *binlog.BlockMutation
    85  	// DBSTenantChecker returns tenant.DBSTenantChecker.
    86  	DBSTenantChecker() tenant.DBSTenantChecker
    87  	// AddBlockLock adds causet dagger to the stochastik dagger map.
    88  	AddBlockLock([]perceptron.BlockLockTpInfo)
    89  	// ReleaseBlockLocks releases causet locks in the stochastik dagger map.
    90  	ReleaseBlockLocks(locks []perceptron.BlockLockTpInfo)
    91  	// ReleaseBlockLockByBlockID releases causet locks in the stochastik dagger map by causet ID.
    92  	ReleaseBlockLockByBlockIDs(blockIDs []int64)
    93  	// CheckBlockLocked checks the causet dagger.
    94  	CheckBlockLocked(tblID int64) (bool, perceptron.BlockLockType)
    95  	// GetAllBlockLocks gets all causet locks causet id and EDB id hold by the stochastik.
    96  	GetAllBlockLocks() []perceptron.BlockLockTpInfo
    97  	// ReleaseAllBlockLocks releases all causet locks hold by the stochastik.
    98  	ReleaseAllBlockLocks()
    99  	// HasLockedBlocks uses to check whether this stochastik locked any blocks.
   100  	HasLockedBlocks() bool
   101  	// PrepareTSFuture uses to prepare timestamp by future.
   102  	PrepareTSFuture(ctx context.Context)
   103  }
   104  
   105  type basicCtxType int
   106  
   107  func (t basicCtxType) String() string {
   108  	switch t {
   109  	case QueryString:
   110  		return "query_string"
   111  	case Initing:
   112  		return "initing"
   113  	case LastInterDircuteDBS:
   114  		return "last_execute_dbs"
   115  	}
   116  	return "unknown"
   117  }
   118  
   119  // Context keys.
   120  const (
   121  	// QueryString is the key for original query string.
   122  	QueryString basicCtxType = 1
   123  	// Initing is the key for indicating if the server is running bootstrap or upgrade job.
   124  	Initing basicCtxType = 2
   125  	// LastInterDircuteDBS is the key for whether the stochastik execute a dbs command last time.
   126  	LastInterDircuteDBS basicCtxType = 3
   127  )
   128  
   129  type connIDCtxKeyType struct{}
   130  
   131  // ConnID is the key in context.
   132  var ConnID = connIDCtxKeyType{}
   133  
   134  // SetCommitCtx sets connection id into context
   135  func SetCommitCtx(ctx context.Context, sessCtx Context) context.Context {
   136  	return context.WithValue(ctx, ConnID, sessCtx.GetStochastikVars().ConnectionID)
   137  }