github.com/matrixorigin/matrixone@v1.2.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  	"sync/atomic"
    19  
    20  	"github.com/google/uuid"
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/logservice"
    23  	"github.com/matrixorigin/matrixone/pkg/objectio"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    25  )
    26  
    27  // FIXME: shit design
    28  var srv atomic.Pointer[Server]
    29  
    30  const (
    31  	TxnWorkSpaceIdType = 1
    32  	CnBlockIdType      = 2
    33  )
    34  
    35  func Get() *Server {
    36  	return srv.Load()
    37  }
    38  
    39  func Set(s *Server) {
    40  	srv.Store(s)
    41  }
    42  
    43  func NewServer(client logservice.CNHAKeeperClient) *Server {
    44  	s := Get()
    45  	if s != nil {
    46  		return s
    47  	}
    48  	s = &Server{
    49  		hakeeper:      client,
    50  		uuidCsChanMap: UuidProcMap{mp: make(map[uuid.UUID]uuidProcMapItem, 1024)},
    51  		cnSegmentMap:  CnSegmentMap{mp: make(map[objectio.Segmentid]int32, 1024)},
    52  	}
    53  	Set(s)
    54  	return s
    55  }
    56  
    57  // GetProcByUuid used the uuid to get a process from the srv.
    58  // if the process is nil, it means the process has done.
    59  // if forcedDelete, do an action to avoid another routine to put a new item.
    60  func (srv *Server) GetProcByUuid(u uuid.UUID, forcedDelete bool) (*process.Process, bool) {
    61  	srv.uuidCsChanMap.Lock()
    62  	defer srv.uuidCsChanMap.Unlock()
    63  	p, ok := srv.uuidCsChanMap.mp[u]
    64  	if !ok {
    65  		if forcedDelete {
    66  			srv.uuidCsChanMap.mp[u] = uuidProcMapItem{proc: nil}
    67  		}
    68  		return nil, false
    69  	}
    70  
    71  	result := p.proc
    72  	if p.proc == nil {
    73  		delete(srv.uuidCsChanMap.mp, u)
    74  	} else {
    75  		p.proc = nil
    76  		srv.uuidCsChanMap.mp[u] = p
    77  	}
    78  	return result, true
    79  }
    80  
    81  func (srv *Server) PutProcIntoUuidMap(u uuid.UUID, p *process.Process) error {
    82  	srv.uuidCsChanMap.Lock()
    83  	defer srv.uuidCsChanMap.Unlock()
    84  	if _, ok := srv.uuidCsChanMap.mp[u]; ok {
    85  		delete(srv.uuidCsChanMap.mp, u)
    86  		return moerr.NewInternalErrorNoCtx("remote receiver already done")
    87  	}
    88  
    89  	srv.uuidCsChanMap.mp[u] = uuidProcMapItem{proc: p}
    90  	return nil
    91  }
    92  
    93  func (srv *Server) DeleteUuids(uuids []uuid.UUID) {
    94  	srv.uuidCsChanMap.Lock()
    95  	defer srv.uuidCsChanMap.Unlock()
    96  	for i := range uuids {
    97  		p, ok := srv.uuidCsChanMap.mp[uuids[i]]
    98  		if !ok {
    99  			continue
   100  		}
   101  
   102  		if p.proc == nil {
   103  			delete(srv.uuidCsChanMap.mp, uuids[i])
   104  		} else {
   105  			p.proc = nil
   106  			srv.uuidCsChanMap.mp[uuids[i]] = p
   107  		}
   108  	}
   109  }
   110  
   111  func (srv *Server) PutCnSegment(sid *objectio.Segmentid, segmentType int32) {
   112  	srv.cnSegmentMap.Lock()
   113  	defer srv.cnSegmentMap.Unlock()
   114  	srv.cnSegmentMap.mp[*sid] = segmentType
   115  }
   116  
   117  func (srv *Server) DeleteTxnSegmentIds(sids []objectio.Segmentid) {
   118  	srv.cnSegmentMap.Lock()
   119  	defer srv.cnSegmentMap.Unlock()
   120  	for _, segmentName := range sids {
   121  		delete(srv.cnSegmentMap.mp, segmentName)
   122  	}
   123  }
   124  
   125  func (srv *Server) GetCnSegmentMap() map[string]int32 {
   126  	srv.cnSegmentMap.Lock()
   127  	defer srv.cnSegmentMap.Unlock()
   128  	new_mp := make(map[string]int32)
   129  	for k, v := range srv.cnSegmentMap.mp {
   130  		new_mp[string(k[:])] = v
   131  	}
   132  	return new_mp
   133  }
   134  
   135  func (srv *Server) GetCnSegmentType(sid *objectio.Segmentid) int32 {
   136  	srv.cnSegmentMap.Lock()
   137  	defer srv.cnSegmentMap.Unlock()
   138  	return srv.cnSegmentMap.mp[*sid]
   139  }
   140  
   141  // GenerateObject used to generate a new object name for CN
   142  func (srv *Server) GenerateObject() objectio.ObjectName {
   143  	segId := objectio.NewSegmentid()
   144  	return objectio.BuildObjectName(segId, 0)
   145  }