github.com/matrixorigin/matrixone@v0.7.0/pkg/frontend/session_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package frontend
    16  
    17  import (
    18  	"context"
    19  	"math"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/defines"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    28  	"github.com/matrixorigin/matrixone/pkg/txn/clock"
    29  
    30  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    31  	"github.com/matrixorigin/matrixone/pkg/config"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    33  
    34  	"github.com/fagongzi/goetty/v2/buf"
    35  	"github.com/golang/mock/gomock"
    36  	mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test"
    37  	plan2 "github.com/matrixorigin/matrixone/pkg/sql/plan"
    38  	"github.com/smartystreets/goconvey/convey"
    39  )
    40  
    41  func TestTxnHandler_NewTxn(t *testing.T) {
    42  	convey.Convey("new txn", t, func() {
    43  		ctrl := gomock.NewController(t)
    44  		defer ctrl.Finish()
    45  
    46  		ctx := context.TODO()
    47  		txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
    48  		txnOperator.EXPECT().Txn().Return(txn.TxnMeta{}).AnyTimes()
    49  		txnOperator.EXPECT().Rollback(gomock.Any()).Return(nil).AnyTimes()
    50  		txnOperator.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes()
    51  		txnClient := mock_frontend.NewMockTxnClient(ctrl)
    52  		cnt := 0
    53  		txnClient.EXPECT().New().DoAndReturn(
    54  			func(ootions ...client.TxnOption) (client.TxnOperator, error) {
    55  				cnt++
    56  				if cnt%2 != 0 {
    57  					return txnOperator, nil
    58  				} else {
    59  					return nil, moerr.NewInternalError(ctx, "startTxn failed")
    60  				}
    61  			}).AnyTimes()
    62  		eng := mock_frontend.NewMockEngine(ctrl)
    63  		eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    64  		eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    65  		eng.EXPECT().Rollback(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    66  		eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    67  		eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    68  		eng.EXPECT().Rollback(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    69  		eng.EXPECT().Hints().Return(engine.Hints{
    70  			CommitOrRollbackTimeout: time.Second,
    71  		}).AnyTimes()
    72  
    73  		txn := InitTxnHandler(eng, txnClient)
    74  		txn.ses = &Session{
    75  			requestCtx: ctx,
    76  		}
    77  		err := txn.NewTxn()
    78  		convey.So(err, convey.ShouldBeNil)
    79  		err = txn.NewTxn()
    80  		convey.So(err, convey.ShouldNotBeNil)
    81  		err = txn.NewTxn()
    82  		convey.So(err, convey.ShouldBeNil)
    83  	})
    84  }
    85  
    86  func TestTxnHandler_CommitTxn(t *testing.T) {
    87  	convey.Convey("commit txn", t, func() {
    88  		ctrl := gomock.NewController(t)
    89  		defer ctrl.Finish()
    90  
    91  		ctx := context.TODO()
    92  		txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
    93  		txnOperator.EXPECT().Txn().Return(txn.TxnMeta{}).AnyTimes()
    94  		cnt := 0
    95  		txnOperator.EXPECT().Commit(gomock.Any()).DoAndReturn(
    96  			func(ctx context.Context) error {
    97  				cnt++
    98  				if cnt%2 != 0 {
    99  					return nil
   100  				} else {
   101  					return moerr.NewInternalError(ctx, "commit failed")
   102  				}
   103  			}).AnyTimes()
   104  
   105  		txnClient := mock_frontend.NewMockTxnClient(ctrl)
   106  		eng := mock_frontend.NewMockEngine(ctrl)
   107  		eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   108  		eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   109  		eng.EXPECT().Rollback(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   110  		eng.EXPECT().Hints().Return(engine.Hints{
   111  			CommitOrRollbackTimeout: time.Second,
   112  		}).AnyTimes()
   113  
   114  		txnClient.EXPECT().New().Return(txnOperator, nil).AnyTimes()
   115  
   116  		txn := InitTxnHandler(eng, txnClient)
   117  		txn.ses = &Session{
   118  			requestCtx: ctx,
   119  		}
   120  		err := txn.NewTxn()
   121  		convey.So(err, convey.ShouldBeNil)
   122  		err = txn.CommitTxn()
   123  		convey.So(err, convey.ShouldBeNil)
   124  		err = txn.NewTxn()
   125  		convey.So(err, convey.ShouldBeNil)
   126  		err = txn.CommitTxn()
   127  		convey.So(err, convey.ShouldNotBeNil)
   128  	})
   129  }
   130  
   131  func TestTxnHandler_RollbackTxn(t *testing.T) {
   132  	convey.Convey("rollback txn", t, func() {
   133  		ctrl := gomock.NewController(t)
   134  		defer ctrl.Finish()
   135  
   136  		ctx := context.TODO()
   137  		txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
   138  		txnOperator.EXPECT().Txn().Return(txn.TxnMeta{}).AnyTimes()
   139  		cnt := 0
   140  		txnOperator.EXPECT().Rollback(gomock.Any()).DoAndReturn(
   141  			func(ctc context.Context) error {
   142  				cnt++
   143  				if cnt%2 != 0 {
   144  					return nil
   145  				} else {
   146  					return moerr.NewInternalError(ctx, "rollback failed")
   147  				}
   148  			}).AnyTimes()
   149  
   150  		txnClient := mock_frontend.NewMockTxnClient(ctrl)
   151  		eng := mock_frontend.NewMockEngine(ctrl)
   152  		eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   153  		eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   154  		eng.EXPECT().Rollback(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   155  		eng.EXPECT().Hints().Return(engine.Hints{
   156  			CommitOrRollbackTimeout: time.Second,
   157  		}).AnyTimes()
   158  
   159  		txnClient.EXPECT().New().Return(txnOperator, nil).AnyTimes()
   160  
   161  		txn := InitTxnHandler(eng, txnClient)
   162  		txn.ses = &Session{
   163  			requestCtx: ctx,
   164  		}
   165  		err := txn.NewTxn()
   166  		convey.So(err, convey.ShouldBeNil)
   167  		err = txn.RollbackTxn()
   168  		convey.So(err, convey.ShouldBeNil)
   169  		err = txn.NewTxn()
   170  		convey.So(err, convey.ShouldBeNil)
   171  		err = txn.RollbackTxn()
   172  		convey.So(err, convey.ShouldNotBeNil)
   173  	})
   174  }
   175  
   176  func TestSession_TxnBegin(t *testing.T) {
   177  	genSession := func(ctrl *gomock.Controller, gSysVars *GlobalSystemVariables) *Session {
   178  		ioses := mock_frontend.NewMockIOSession(ctrl)
   179  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
   180  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   181  		ioses.EXPECT().RemoteAddress().Return("").AnyTimes()
   182  		ioses.EXPECT().Ref().AnyTimes()
   183  		sv, err := getSystemVariables("test/system_vars_config.toml")
   184  		if err != nil {
   185  			t.Error(err)
   186  		}
   187  		proto := NewMysqlClientProtocol(0, ioses, 1024, sv)
   188  		txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
   189  		txnOperator.EXPECT().Txn().Return(txn.TxnMeta{}).AnyTimes()
   190  		txnOperator.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes()
   191  		txnClient := mock_frontend.NewMockTxnClient(ctrl)
   192  		txnClient.EXPECT().New().Return(txnOperator, nil).AnyTimes()
   193  		eng := mock_frontend.NewMockEngine(ctrl)
   194  		hints := engine.Hints{CommitOrRollbackTimeout: time.Second * 10}
   195  		eng.EXPECT().Hints().Return(hints).AnyTimes()
   196  		eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   197  		eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   198  		session := NewSession(proto, nil, config.NewParameterUnit(&config.FrontendParameters{}, eng, txnClient, nil, nil), gSysVars, false)
   199  		session.SetRequestContext(context.Background())
   200  		return session
   201  	}
   202  	convey.Convey("new session", t, func() {
   203  		ctrl := gomock.NewController(t)
   204  		defer ctrl.Finish()
   205  
   206  		gSysVars := &GlobalSystemVariables{}
   207  		InitGlobalSystemVariables(gSysVars)
   208  
   209  		ses := genSession(ctrl, gSysVars)
   210  		err := ses.TxnBegin()
   211  		convey.So(err, convey.ShouldBeNil)
   212  		err = ses.TxnCommit()
   213  		convey.So(err, convey.ShouldBeNil)
   214  		err = ses.TxnBegin()
   215  		convey.So(err, convey.ShouldBeNil)
   216  		err = ses.SetAutocommit(false)
   217  		convey.So(err, convey.ShouldNotBeNil)
   218  		err = ses.TxnCommit()
   219  		convey.So(err, convey.ShouldBeNil)
   220  		_, _ = ses.GetTxnHandler().GetTxn()
   221  		convey.So(err, convey.ShouldBeNil)
   222  
   223  		err = ses.TxnCommit()
   224  		convey.So(err, convey.ShouldBeNil)
   225  
   226  		err = ses.SetAutocommit(true)
   227  		convey.So(err, convey.ShouldBeNil)
   228  
   229  		err = ses.SetAutocommit(false)
   230  		convey.So(err, convey.ShouldBeNil)
   231  	})
   232  }
   233  
   234  func TestVariables(t *testing.T) {
   235  	genSession := func(ctrl *gomock.Controller, gSysVars *GlobalSystemVariables) *Session {
   236  		ioses := mock_frontend.NewMockIOSession(ctrl)
   237  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
   238  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   239  		ioses.EXPECT().RemoteAddress().Return("").AnyTimes()
   240  		ioses.EXPECT().Ref().AnyTimes()
   241  		sv, err := getSystemVariables("test/system_vars_config.toml")
   242  		if err != nil {
   243  			t.Error(err)
   244  		}
   245  		proto := NewMysqlClientProtocol(0, ioses, 1024, sv)
   246  		txnClient := mock_frontend.NewMockTxnClient(ctrl)
   247  		txnClient.EXPECT().New().AnyTimes()
   248  		session := NewSession(proto, nil, config.NewParameterUnit(&config.FrontendParameters{}, nil, txnClient, nil, nil), gSysVars, true)
   249  		session.SetRequestContext(context.Background())
   250  		return session
   251  	}
   252  
   253  	checkWant := func(ses, existSes, newSesAfterSession *Session,
   254  		v string,
   255  		sameSesWant1, existSesWant2, newSesAfterSesWant3,
   256  		saneSesGlobalWant4, existSesGlobalWant5, newSesAfterSesGlobalWant6 interface{}) {
   257  
   258  		//same session
   259  		v1_val, err := ses.GetSessionVar(v)
   260  		convey.So(err, convey.ShouldBeNil)
   261  		convey.So(sameSesWant1, convey.ShouldEqual, v1_val)
   262  		v1_ctx_val, err := ses.GetTxnCompileCtx().ResolveVariable(v, true, false)
   263  		convey.So(err, convey.ShouldBeNil)
   264  		convey.So(v1_ctx_val, convey.ShouldEqual, v1_val)
   265  
   266  		//exist session
   267  		v2_val, err := existSes.GetSessionVar(v)
   268  		convey.So(err, convey.ShouldBeNil)
   269  		convey.So(existSesWant2, convey.ShouldEqual, v2_val)
   270  		v2_ctx_val, err := existSes.GetTxnCompileCtx().ResolveVariable(v, true, false)
   271  		convey.So(err, convey.ShouldBeNil)
   272  		convey.So(v2_ctx_val, convey.ShouldEqual, v2_val)
   273  
   274  		//new session after session
   275  		v3_val, err := newSesAfterSession.GetSessionVar(v)
   276  		convey.So(err, convey.ShouldBeNil)
   277  		convey.So(newSesAfterSesWant3, convey.ShouldEqual, v3_val)
   278  		v3_ctx_val, err := newSesAfterSession.GetTxnCompileCtx().ResolveVariable(v, true, false)
   279  		convey.So(err, convey.ShouldBeNil)
   280  		convey.So(v3_ctx_val, convey.ShouldEqual, v3_val)
   281  
   282  		//same session global
   283  		v4_val, err := ses.GetGlobalVar(v)
   284  		convey.So(err, convey.ShouldBeNil)
   285  		convey.So(saneSesGlobalWant4, convey.ShouldEqual, v4_val)
   286  		v4_ctx_val, err := ses.GetTxnCompileCtx().ResolveVariable(v, true, true)
   287  		convey.So(err, convey.ShouldBeNil)
   288  		convey.So(v4_ctx_val, convey.ShouldEqual, v4_val)
   289  
   290  		//exist session global
   291  		v5_val, err := existSes.GetGlobalVar(v)
   292  		convey.So(err, convey.ShouldBeNil)
   293  		convey.So(existSesGlobalWant5, convey.ShouldEqual, v5_val)
   294  		v5_ctx_val, err := existSes.GetTxnCompileCtx().ResolveVariable(v, true, true)
   295  		convey.So(err, convey.ShouldBeNil)
   296  		convey.So(v5_ctx_val, convey.ShouldEqual, v5_val)
   297  
   298  		//new session after session global
   299  		v6_val, err := newSesAfterSession.GetGlobalVar(v)
   300  		convey.So(err, convey.ShouldBeNil)
   301  		convey.So(newSesAfterSesGlobalWant6, convey.ShouldEqual, v6_val)
   302  		v6_ctx_val, err := newSesAfterSession.GetTxnCompileCtx().ResolveVariable(v, true, true)
   303  		convey.So(err, convey.ShouldBeNil)
   304  		convey.So(v6_ctx_val, convey.ShouldEqual, v6_val)
   305  	}
   306  
   307  	checkWant2 := func(ses, existSes, newSesAfterSession *Session,
   308  		v string,
   309  		sameSesWant1, existSesWant2, newSesAfterSesWant3 interface{}) {
   310  
   311  		//same session
   312  		v1_val, err := ses.GetSessionVar(v)
   313  		convey.So(err, convey.ShouldBeNil)
   314  		convey.So(sameSesWant1, convey.ShouldEqual, v1_val)
   315  		v1_ctx_val, err := ses.GetTxnCompileCtx().ResolveVariable(v, true, false)
   316  		convey.So(err, convey.ShouldBeNil)
   317  		convey.So(v1_ctx_val, convey.ShouldEqual, v1_val)
   318  
   319  		//exist session
   320  		v2_val, err := existSes.GetSessionVar(v)
   321  		convey.So(err, convey.ShouldBeNil)
   322  		convey.So(existSesWant2, convey.ShouldEqual, v2_val)
   323  		v2_ctx_val, err := existSes.GetTxnCompileCtx().ResolveVariable(v, true, false)
   324  		convey.So(err, convey.ShouldBeNil)
   325  		convey.So(v2_ctx_val, convey.ShouldEqual, v2_val)
   326  
   327  		//new session after session
   328  		v3_val, err := newSesAfterSession.GetSessionVar(v)
   329  		convey.So(err, convey.ShouldBeNil)
   330  		convey.So(newSesAfterSesWant3, convey.ShouldEqual, v3_val)
   331  		v3_ctx_val, err := newSesAfterSession.GetTxnCompileCtx().ResolveVariable(v, true, false)
   332  		convey.So(err, convey.ShouldBeNil)
   333  		convey.So(v3_ctx_val, convey.ShouldEqual, v3_val)
   334  
   335  		//same session global
   336  		_, err = ses.GetGlobalVar(v)
   337  		convey.So(err, convey.ShouldNotBeNil)
   338  		convey.So(err, convey.ShouldBeError, moerr.NewInternalError(context.TODO(), errorSystemVariableSessionEmpty()))
   339  		_, err = ses.GetTxnCompileCtx().ResolveVariable(v, true, true)
   340  		convey.So(err, convey.ShouldNotBeNil)
   341  		convey.So(err, convey.ShouldBeError, moerr.NewInternalError(context.TODO(), errorSystemVariableSessionEmpty()))
   342  
   343  		//exist session global
   344  		_, err = existSes.GetGlobalVar(v)
   345  		convey.So(err, convey.ShouldNotBeNil)
   346  		convey.So(err, convey.ShouldBeError, moerr.NewInternalError(context.TODO(), errorSystemVariableSessionEmpty()))
   347  		_, err = existSes.GetTxnCompileCtx().ResolveVariable(v, true, true)
   348  		convey.So(err, convey.ShouldNotBeNil)
   349  		convey.So(err, convey.ShouldBeError, moerr.NewInternalError(context.TODO(), errorSystemVariableSessionEmpty()))
   350  
   351  		//new session after session global
   352  		_, err = newSesAfterSession.GetGlobalVar(v)
   353  		convey.So(err, convey.ShouldNotBeNil)
   354  		convey.So(err, convey.ShouldBeError, moerr.NewInternalError(context.TODO(), errorSystemVariableSessionEmpty()))
   355  		_, err = newSesAfterSession.GetTxnCompileCtx().ResolveVariable(v, true, true)
   356  		convey.So(err, convey.ShouldNotBeNil)
   357  		convey.So(err, convey.ShouldBeError, moerr.NewInternalError(context.TODO(), errorSystemVariableSessionEmpty()))
   358  	}
   359  
   360  	convey.Convey("scope global", t, func() {
   361  		ctrl := gomock.NewController(t)
   362  		defer ctrl.Finish()
   363  
   364  		gSysVars := &GlobalSystemVariables{}
   365  		InitGlobalSystemVariables(gSysVars)
   366  
   367  		ses := genSession(ctrl, gSysVars)
   368  		existSes := genSession(ctrl, gSysVars)
   369  
   370  		v1 := "testglobalvar_dyn"
   371  		_, v1_default, _ := gSysVars.GetGlobalSysVar(v1)
   372  		v1_want := 10
   373  		err := ses.SetSessionVar(v1, v1_want)
   374  		convey.So(err, convey.ShouldNotBeNil)
   375  
   376  		// no check after fail set
   377  		newSes2 := genSession(ctrl, gSysVars)
   378  		checkWant(ses, existSes, newSes2, v1, v1_default, v1_default, v1_default, v1_default, v1_default, v1_default)
   379  
   380  		err = ses.SetGlobalVar(v1, v1_want)
   381  		convey.So(err, convey.ShouldBeNil)
   382  
   383  		newSes3 := genSession(ctrl, gSysVars)
   384  		checkWant(ses, existSes, newSes3, v1, v1_want, v1_want, v1_want, v1_want, v1_want, v1_want)
   385  
   386  		v2 := "testglobalvar_nodyn"
   387  		_, v2_default, _ := gSysVars.GetGlobalSysVar(v2)
   388  		v2_want := 10
   389  		err = ses.SetSessionVar(v2, v2_want)
   390  		convey.So(err, convey.ShouldNotBeNil)
   391  
   392  		newSes4 := genSession(ctrl, gSysVars)
   393  		checkWant(ses, existSes, newSes4, v2, v2_default, v2_default, v2_default, v2_default, v2_default, v2_default)
   394  
   395  		err = ses.SetGlobalVar(v2, v2_want)
   396  		convey.So(err, convey.ShouldNotBeNil)
   397  
   398  		newSes5 := genSession(ctrl, gSysVars)
   399  		checkWant(ses, existSes, newSes5, v2, v2_default, v2_default, v2_default, v2_default, v2_default, v2_default)
   400  	})
   401  
   402  	convey.Convey("scope session", t, func() {
   403  		ctrl := gomock.NewController(t)
   404  		defer ctrl.Finish()
   405  
   406  		gSysVars := &GlobalSystemVariables{}
   407  		InitGlobalSystemVariables(gSysVars)
   408  
   409  		ses := genSession(ctrl, gSysVars)
   410  		existSes := genSession(ctrl, gSysVars)
   411  
   412  		v1 := "testsessionvar_dyn"
   413  		_, v1_default, _ := gSysVars.GetGlobalSysVar(v1)
   414  		v1_want := 10
   415  		err := ses.SetSessionVar(v1, v1_want)
   416  		convey.So(err, convey.ShouldBeNil)
   417  
   418  		newSes1 := genSession(ctrl, gSysVars)
   419  		checkWant2(ses, existSes, newSes1, v1, v1_want, v1_default, v1_default)
   420  
   421  		err = ses.SetGlobalVar(v1, v1_want)
   422  		convey.So(err, convey.ShouldNotBeNil)
   423  
   424  		newSes2 := genSession(ctrl, gSysVars)
   425  		checkWant2(ses, existSes, newSes2, v1, v1_want, v1_default, v1_default)
   426  
   427  		v2 := "testsessionvar_nodyn"
   428  		_, v2_default, _ := gSysVars.GetGlobalSysVar(v2)
   429  		v2_want := 10
   430  		err = ses.SetSessionVar(v2, v2_want)
   431  		convey.So(err, convey.ShouldNotBeNil)
   432  
   433  		newSes3 := genSession(ctrl, gSysVars)
   434  		checkWant2(ses, existSes, newSes3, v2, v2_default, v2_default, v2_default)
   435  
   436  		err = ses.SetGlobalVar(v2, v2_want)
   437  		convey.So(err, convey.ShouldNotBeNil)
   438  		newSes4 := genSession(ctrl, gSysVars)
   439  		checkWant2(ses, existSes, newSes4, v2, v2_default, v2_default, v2_default)
   440  
   441  	})
   442  
   443  	convey.Convey("scope both - set session", t, func() {
   444  		ctrl := gomock.NewController(t)
   445  		defer ctrl.Finish()
   446  
   447  		gSysVars := &GlobalSystemVariables{}
   448  		InitGlobalSystemVariables(gSysVars)
   449  
   450  		ses := genSession(ctrl, gSysVars)
   451  		existSes := genSession(ctrl, gSysVars)
   452  
   453  		v1 := "testbothvar_dyn"
   454  		_, v1_default, _ := gSysVars.GetGlobalSysVar(v1)
   455  		v1_want := 10
   456  		err := ses.SetSessionVar(v1, v1_want)
   457  		convey.So(err, convey.ShouldBeNil)
   458  
   459  		newSes2 := genSession(ctrl, gSysVars)
   460  		checkWant(ses, existSes, newSes2, v1, v1_want, v1_default, v1_default, v1_default, v1_default, v1_default)
   461  
   462  		v2 := "testbotchvar_nodyn"
   463  		err = ses.SetSessionVar(v2, 10)
   464  		convey.So(err, convey.ShouldNotBeNil)
   465  
   466  		err = ses.SetGlobalVar(v2, 10)
   467  		convey.So(err, convey.ShouldNotBeNil)
   468  	})
   469  
   470  	convey.Convey("scope both", t, func() {
   471  		ctrl := gomock.NewController(t)
   472  		defer ctrl.Finish()
   473  
   474  		gSysVars := &GlobalSystemVariables{}
   475  		InitGlobalSystemVariables(gSysVars)
   476  
   477  		ses := genSession(ctrl, gSysVars)
   478  		existSes := genSession(ctrl, gSysVars)
   479  
   480  		v1 := "testbothvar_dyn"
   481  		_, v1_default, _ := gSysVars.GetGlobalSysVar(v1)
   482  		v1_want := 10
   483  
   484  		err := ses.SetGlobalVar(v1, v1_want)
   485  		convey.So(err, convey.ShouldBeNil)
   486  
   487  		newSes2 := genSession(ctrl, gSysVars)
   488  		checkWant(ses, existSes, newSes2, v1, v1_default, v1_default, v1_want, v1_want, v1_want, v1_want)
   489  	})
   490  
   491  	convey.Convey("user variables", t, func() {
   492  		ctrl := gomock.NewController(t)
   493  		defer ctrl.Finish()
   494  
   495  		gSysVars := &GlobalSystemVariables{}
   496  		InitGlobalSystemVariables(gSysVars)
   497  
   498  		ses := genSession(ctrl, gSysVars)
   499  
   500  		vars := ses.CopyAllSessionVars()
   501  		convey.So(len(vars), convey.ShouldNotBeZeroValue)
   502  
   503  		err := ses.SetUserDefinedVar("abc", 1)
   504  		convey.So(err, convey.ShouldBeNil)
   505  
   506  		_, _, err = ses.GetUserDefinedVar("abc")
   507  		convey.So(err, convey.ShouldBeNil)
   508  	})
   509  }
   510  
   511  func TestSession_TxnCompilerContext(t *testing.T) {
   512  	genSession := func(ctrl *gomock.Controller, pu *config.ParameterUnit, gSysVars *GlobalSystemVariables) *Session {
   513  		ioses := mock_frontend.NewMockIOSession(ctrl)
   514  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
   515  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   516  		ioses.EXPECT().RemoteAddress().Return("").AnyTimes()
   517  		ioses.EXPECT().Ref().AnyTimes()
   518  		sv, err := getSystemVariables("test/system_vars_config.toml")
   519  		if err != nil {
   520  			t.Error(err)
   521  		}
   522  		proto := NewMysqlClientProtocol(0, ioses, 1024, sv)
   523  		session := NewSession(proto, nil, pu, gSysVars, false)
   524  		session.SetRequestContext(context.Background())
   525  		return session
   526  	}
   527  
   528  	convey.Convey("test", t, func() {
   529  		ctrl := gomock.NewController(t)
   530  		defer ctrl.Finish()
   531  
   532  		ctx := context.TODO()
   533  		txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
   534  		txnOperator.EXPECT().Commit(ctx).Return(nil).AnyTimes()
   535  		txnOperator.EXPECT().Rollback(ctx).Return(nil).AnyTimes()
   536  		txnClient := mock_frontend.NewMockTxnClient(ctrl)
   537  		txnClient.EXPECT().New().Return(txnOperator, nil).AnyTimes()
   538  		eng := mock_frontend.NewMockEngine(ctrl)
   539  		eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   540  		eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   541  		eng.EXPECT().Rollback(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   542  		eng.EXPECT().Hints().Return(engine.Hints{
   543  			CommitOrRollbackTimeout: time.Second,
   544  		}).AnyTimes()
   545  
   546  		db := mock_frontend.NewMockDatabase(ctrl)
   547  		db.EXPECT().Relations(gomock.Any()).Return(nil, nil).AnyTimes()
   548  
   549  		table := mock_frontend.NewMockRelation(ctrl)
   550  		table.EXPECT().Ranges(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
   551  		table.EXPECT().TableDefs(gomock.Any()).Return(nil, nil).AnyTimes()
   552  		table.EXPECT().GetPrimaryKeys(gomock.Any()).Return(nil, nil).AnyTimes()
   553  		table.EXPECT().GetHideKeys(gomock.Any()).Return(nil, nil).AnyTimes()
   554  		table.EXPECT().Stats(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
   555  		table.EXPECT().TableColumns(gomock.Any()).Return(nil, nil).AnyTimes()
   556  		table.EXPECT().GetTableID(gomock.Any()).Return(uint64(10)).AnyTimes()
   557  		db.EXPECT().Relation(gomock.Any(), gomock.Any()).Return(table, nil).AnyTimes()
   558  		eng.EXPECT().Database(gomock.Any(), gomock.Any(), gomock.Any()).Return(db, nil).AnyTimes()
   559  
   560  		pu := config.NewParameterUnit(&config.FrontendParameters{}, eng, txnClient, nil, nil)
   561  
   562  		gSysVars := &GlobalSystemVariables{}
   563  		InitGlobalSystemVariables(gSysVars)
   564  
   565  		ses := genSession(ctrl, pu, gSysVars)
   566  
   567  		tcc := ses.GetTxnCompileCtx()
   568  		defDBName := tcc.DefaultDatabase()
   569  		convey.So(defDBName, convey.ShouldEqual, "")
   570  		convey.So(tcc.DatabaseExists("abc"), convey.ShouldBeTrue)
   571  
   572  		_, _, err := tcc.getRelation("abc", "t1")
   573  		convey.So(err, convey.ShouldBeNil)
   574  
   575  		object, tableRef := tcc.Resolve("abc", "t1")
   576  		convey.So(object, convey.ShouldNotBeNil)
   577  		convey.So(tableRef, convey.ShouldNotBeNil)
   578  
   579  		pkd := tcc.GetPrimaryKeyDef("abc", "t1")
   580  		convey.So(len(pkd), convey.ShouldBeZeroValue)
   581  
   582  		hkd := tcc.GetHideKeyDef("abc", "t1")
   583  		convey.So(hkd, convey.ShouldBeNil)
   584  
   585  		stats := tcc.Stats(&plan2.ObjectRef{SchemaName: "abc", ObjName: "t1"}, &plan2.Expr{})
   586  		convey.So(stats, convey.ShouldBeNil)
   587  	})
   588  }
   589  
   590  func TestSession_GetTempTableStorage(t *testing.T) {
   591  	genSession := func(ctrl *gomock.Controller, pu *config.ParameterUnit, gSysVars *GlobalSystemVariables) *Session {
   592  		ioses := mock_frontend.NewMockIOSession(ctrl)
   593  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
   594  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   595  		ioses.EXPECT().RemoteAddress().Return("").AnyTimes()
   596  		ioses.EXPECT().Ref().AnyTimes()
   597  		sv, err := getSystemVariables("test/system_vars_config.toml")
   598  		if err != nil {
   599  			t.Error(err)
   600  		}
   601  		proto := NewMysqlClientProtocol(0, ioses, 1024, sv)
   602  		session := NewSession(proto, nil, pu, gSysVars, false)
   603  		session.SetRequestContext(context.Background())
   604  		return session
   605  	}
   606  
   607  	ctrl := gomock.NewController(t)
   608  	defer ctrl.Finish()
   609  	txnClient := mock_frontend.NewMockTxnClient(ctrl)
   610  	eng := mock_frontend.NewMockEngine(ctrl)
   611  	pu := config.NewParameterUnit(&config.FrontendParameters{}, eng, txnClient, nil, nil)
   612  	gSysVars := &GlobalSystemVariables{}
   613  
   614  	ses := genSession(ctrl, pu, gSysVars)
   615  	assert.Panics(t, func() {
   616  		_ = ses.GetTempTableStorage()
   617  	})
   618  }
   619  
   620  func TestIfInitedTempEngine(t *testing.T) {
   621  	genSession := func(ctrl *gomock.Controller, pu *config.ParameterUnit, gSysVars *GlobalSystemVariables) *Session {
   622  		ioses := mock_frontend.NewMockIOSession(ctrl)
   623  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
   624  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   625  		ioses.EXPECT().RemoteAddress().Return("").AnyTimes()
   626  		ioses.EXPECT().Ref().AnyTimes()
   627  		sv, err := getSystemVariables("test/system_vars_config.toml")
   628  		if err != nil {
   629  			t.Error(err)
   630  		}
   631  		proto := NewMysqlClientProtocol(0, ioses, 1024, sv)
   632  		session := NewSession(proto, nil, pu, gSysVars, false)
   633  		session.SetRequestContext(context.Background())
   634  		return session
   635  	}
   636  
   637  	ctrl := gomock.NewController(t)
   638  	defer ctrl.Finish()
   639  	txnClient := mock_frontend.NewMockTxnClient(ctrl)
   640  	eng := mock_frontend.NewMockEngine(ctrl)
   641  	pu := config.NewParameterUnit(&config.FrontendParameters{}, eng, txnClient, nil, nil)
   642  	gSysVars := &GlobalSystemVariables{}
   643  
   644  	ses := genSession(ctrl, pu, gSysVars)
   645  	assert.False(t, ses.IfInitedTempEngine())
   646  }
   647  
   648  func TestSetTempTableStorage(t *testing.T) {
   649  	genSession := func(ctrl *gomock.Controller, pu *config.ParameterUnit, gSysVars *GlobalSystemVariables) *Session {
   650  		ioses := mock_frontend.NewMockIOSession(ctrl)
   651  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
   652  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   653  		ioses.EXPECT().RemoteAddress().Return("").AnyTimes()
   654  		ioses.EXPECT().Ref().AnyTimes()
   655  		sv, err := getSystemVariables("test/system_vars_config.toml")
   656  		if err != nil {
   657  			t.Error(err)
   658  		}
   659  		proto := NewMysqlClientProtocol(0, ioses, 1024, sv)
   660  		session := NewSession(proto, nil, pu, gSysVars, false)
   661  		session.SetRequestContext(context.Background())
   662  		return session
   663  	}
   664  
   665  	ctrl := gomock.NewController(t)
   666  	defer ctrl.Finish()
   667  	txnClient := mock_frontend.NewMockTxnClient(ctrl)
   668  	eng := mock_frontend.NewMockEngine(ctrl)
   669  	pu := config.NewParameterUnit(&config.FrontendParameters{}, eng, txnClient, nil, nil)
   670  	gSysVars := &GlobalSystemVariables{}
   671  
   672  	ses := genSession(ctrl, pu, gSysVars)
   673  
   674  	ck := clock.NewHLCClock(func() int64 {
   675  		return time.Now().Unix()
   676  	}, math.MaxInt)
   677  	dnStore, _ := ses.SetTempTableStorage(ck)
   678  
   679  	assert.Equal(t, defines.TEMPORARY_TABLE_DN_ADDR, dnStore.ServiceAddress)
   680  }