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

     1  // Copyright 2021 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  	"strings"
    20  	"sync/atomic"
    21  
    22  	"github.com/fagongzi/goetty/v2"
    23  	"github.com/matrixorigin/matrixone/pkg/config"
    24  	"github.com/matrixorigin/matrixone/pkg/logutil"
    25  )
    26  
    27  // RelationName counter for the new connection
    28  var initConnectionID uint32 = 1000
    29  
    30  // MOServer MatrixOne Server
    31  type MOServer struct {
    32  	addr  string
    33  	uaddr string
    34  	app   goetty.NetApplication
    35  	rm    *RoutineManager
    36  }
    37  
    38  func (mo *MOServer) GetRoutineManager() *RoutineManager {
    39  	return mo.rm
    40  }
    41  
    42  func (mo *MOServer) Start() error {
    43  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    44  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    45  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    46  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    47  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    48  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    49  	logutil.Infof("Server Listening on : %s ", mo.addr)
    50  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    51  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    52  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    53  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    54  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    55  	logutil.Infof("++++++++++++++++++++++++++++++++++++++++++++++++")
    56  	return mo.app.Start()
    57  }
    58  
    59  func (mo *MOServer) Stop() error {
    60  	return mo.app.Stop()
    61  }
    62  
    63  func nextConnectionID() uint32 {
    64  	return atomic.AddUint32(&initConnectionID, 1)
    65  }
    66  
    67  func NewMOServer(ctx context.Context, addr string, pu *config.ParameterUnit) *MOServer {
    68  	codec := NewSqlCodec()
    69  	rm, err := NewRoutineManager(ctx, pu)
    70  	if err != nil {
    71  		logutil.Panicf("start server failed with %+v", err)
    72  	}
    73  	// TODO asyncFlushBatch
    74  	addresses := []string{addr}
    75  	unixAddr := pu.SV.GetUnixSocketAddress()
    76  	if unixAddr != "" {
    77  		addresses = append(addresses, "unix://"+unixAddr)
    78  	}
    79  	app, err := goetty.NewApplicationWithListenAddress(
    80  		addresses,
    81  		rm.Handler,
    82  		goetty.WithAppLogger(logutil.GetGlobalLogger()),
    83  		goetty.WithAppSessionOptions(
    84  			goetty.WithSessionCodec(codec),
    85  			goetty.WithSessionLogger(logutil.GetGlobalLogger()),
    86  			goetty.WithSessionDisableCompactAfterGrow(),
    87  			goetty.WithSessionRWBUfferSize(1024*1024, 1024*1024)),
    88  		goetty.WithAppSessionAware(rm))
    89  	if err != nil {
    90  		logutil.Panicf("start server failed with %+v", err)
    91  	}
    92  	initVarByConfig(pu)
    93  	return &MOServer{
    94  		addr:  addr,
    95  		app:   app,
    96  		uaddr: pu.SV.UnixSocketAddress,
    97  		rm:    rm,
    98  	}
    99  }
   100  
   101  func initVarByConfig(pu *config.ParameterUnit) {
   102  	if strings.ToLower(pu.SV.SaveQueryResult) == "on" {
   103  		GSysVariables.sysVars["save_query_result"] = int8(1)
   104  	}
   105  	GSysVariables.sysVars["query_result_maxsize"] = pu.SV.QueryResultMaxsize
   106  	GSysVariables.sysVars["query_result_timeout"] = pu.SV.QueryResultTimeout
   107  }