github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/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 colexec
    16  
    17  import (
    18  	"context"
    19  	"encoding/binary"
    20  	"fmt"
    21  	"math"
    22  	"time"
    23  
    24  	"github.com/google/uuid"
    25  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    26  	"github.com/matrixorigin/matrixone/pkg/logservice"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    28  )
    29  
    30  var Srv *Server
    31  var CnAddr string
    32  
    33  func NewServer(client logservice.CNHAKeeperClient) *Server {
    34  	if Srv != nil {
    35  		return Srv
    36  	}
    37  	Srv = &Server{
    38  		mp:       make(map[uint64]*process.WaitRegister),
    39  		hakeeper: client,
    40  
    41  		uuidCsChanMap: UuidCsChanMap{mp: make(map[uuid.UUID]chan process.WrapCs)},
    42  	}
    43  	return Srv
    44  }
    45  
    46  func (srv *Server) GetConnector(id uint64) *process.WaitRegister {
    47  	srv.Lock()
    48  	defer srv.Unlock()
    49  	defer func() { delete(srv.mp, id) }()
    50  	return srv.mp[id]
    51  }
    52  
    53  func (srv *Server) RegistConnector(reg *process.WaitRegister) uint64 {
    54  	srv.Lock()
    55  	defer srv.Unlock()
    56  	srv.mp[srv.id] = reg
    57  	defer func() { srv.id++ }()
    58  	return srv.id
    59  }
    60  
    61  func (srv *Server) GetNotifyChByUuid(u uuid.UUID) (chan process.WrapCs, bool) {
    62  	srv.uuidCsChanMap.Lock()
    63  	defer srv.uuidCsChanMap.Unlock()
    64  	p, ok := srv.uuidCsChanMap.mp[u]
    65  	if !ok {
    66  		return nil, false
    67  	}
    68  	return p, true
    69  }
    70  
    71  func (srv *Server) PutNotifyChIntoUuidMap(u uuid.UUID, ch chan process.WrapCs) error {
    72  	srv.uuidCsChanMap.Lock()
    73  	defer srv.uuidCsChanMap.Unlock()
    74  	srv.uuidCsChanMap.mp[u] = ch
    75  	return nil
    76  }
    77  
    78  func (srv *Server) HandleRequest(ctx context.Context, req morpc.Message, _ uint64, cs morpc.ClientSession) error {
    79  	return nil
    80  }
    81  
    82  // SegmentId is part of Id for cn2s3 directly, for more info, refer to docs about it
    83  func (srv *Server) GenerateSegment() (string, error) {
    84  	srv.Lock()
    85  	defer srv.Unlock()
    86  	if srv.InitSegmentId {
    87  		if err := srv.incrementSegmentId(); err != nil {
    88  			return "", err
    89  		}
    90  	} else {
    91  		if err := srv.getNewSegmentId(); err != nil {
    92  			return "", err
    93  		}
    94  		srv.InitSegmentId = true
    95  	}
    96  	return fmt.Sprintf("%x.seg", (srv.CNSegmentId)[:]), nil
    97  }
    98  
    99  func (srv *Server) incrementSegmentId() error {
   100  	// increment SegmentId
   101  	b := binary.BigEndian.Uint32(srv.CNSegmentId[0:4])
   102  	// can't rise up to math.MaxUint32, we need to distinct the memory
   103  	// data in disttae, the batch's rowId's prefix is MaxUint64
   104  	if b < math.MaxUint32-1 {
   105  		b++
   106  		binary.BigEndian.PutUint32(srv.CNSegmentId[0:4], b)
   107  	} else {
   108  		if err := srv.getNewSegmentId(); err != nil {
   109  			return err
   110  		}
   111  	}
   112  	return nil
   113  }
   114  
   115  // getNewSegmentId returns Id given from hakeeper
   116  func (srv *Server) getNewSegmentId() error {
   117  	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
   118  	defer cancel()
   119  	Id, err := srv.hakeeper.AllocateID(ctx)
   120  	if err != nil {
   121  		return err
   122  	}
   123  	srv.CNSegmentId[0] = 0x80
   124  	for i := 1; i < 4; i++ {
   125  		srv.CNSegmentId[i] = 0
   126  	}
   127  	binary.BigEndian.PutUint64(srv.CNSegmentId[4:12], Id)
   128  	return nil
   129  }