github.com/matrixorigin/matrixone@v1.2.0/pkg/util/status/server.go (about)

     1  // Copyright 2021 -2023 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 status
    16  
    17  import (
    18  	"encoding/json"
    19  	"net/http"
    20  	"sync"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/lockservice"
    23  	"github.com/matrixorigin/matrixone/pkg/logservice"
    24  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/disttae"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail/service"
    27  )
    28  
    29  const JsonIdent = "    "
    30  
    31  type CNInstance struct {
    32  	TxnClient     client.TxnClient
    33  	LockService   lockservice.LockService
    34  	logtailClient *disttae.PushClient
    35  }
    36  
    37  type Server struct {
    38  	mu struct {
    39  		sync.Mutex
    40  		LogtailServer  *service.LogtailServer
    41  		HAKeeperClient logservice.ClusterHAKeeperClient
    42  		CNInstances    map[string]*CNInstance
    43  	}
    44  }
    45  
    46  func NewServer() *Server {
    47  	s := &Server{}
    48  	s.mu.CNInstances = make(map[string]*CNInstance)
    49  	return s
    50  }
    51  
    52  func (s *Server) SetLogtailServer(logtailServer *service.LogtailServer) {
    53  	s.mu.Lock()
    54  	defer s.mu.Unlock()
    55  	s.mu.LogtailServer = logtailServer
    56  }
    57  
    58  func (s *Server) SetHAKeeperClient(c logservice.ClusterHAKeeperClient) {
    59  	s.mu.Lock()
    60  	defer s.mu.Unlock()
    61  	if s.mu.HAKeeperClient == nil {
    62  		s.mu.HAKeeperClient = c
    63  	}
    64  }
    65  
    66  func (s *Server) SetTxnClient(uuid string, c client.TxnClient) {
    67  	s.mu.Lock()
    68  	defer s.mu.Unlock()
    69  	_, ok := s.mu.CNInstances[uuid]
    70  	if !ok {
    71  		s.mu.CNInstances[uuid] = &CNInstance{}
    72  	}
    73  	s.mu.CNInstances[uuid].TxnClient = c
    74  }
    75  
    76  func (s *Server) SetLockService(uuid string, l lockservice.LockService) {
    77  	s.mu.Lock()
    78  	defer s.mu.Unlock()
    79  	_, ok := s.mu.CNInstances[uuid]
    80  	if !ok {
    81  		s.mu.CNInstances[uuid] = &CNInstance{}
    82  	}
    83  	s.mu.CNInstances[uuid].LockService = l
    84  }
    85  
    86  func (s *Server) SetLogTailClient(uuid string, c *disttae.PushClient) {
    87  	s.mu.Lock()
    88  	defer s.mu.Unlock()
    89  	_, ok := s.mu.CNInstances[uuid]
    90  	if !ok {
    91  		s.mu.CNInstances[uuid] = &CNInstance{}
    92  	}
    93  	s.mu.CNInstances[uuid].logtailClient = c
    94  }
    95  
    96  func (s *Server) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
    97  	data, err := s.Dump()
    98  	if err != nil {
    99  		w.Write([]byte(err.Error()))
   100  		return
   101  	}
   102  	w.Write(data)
   103  }
   104  
   105  func (s *Server) Dump() ([]byte, error) {
   106  	var status Status
   107  	s.mu.Lock()
   108  	defer s.mu.Unlock()
   109  	if s.mu.LogtailServer != nil {
   110  		status.LogtailServerStatus.fill(s.mu.LogtailServer)
   111  	}
   112  	if s.mu.HAKeeperClient != nil {
   113  		status.HAKeeperStatus.fill(s.mu.HAKeeperClient)
   114  	}
   115  	status.fillCNStatus(s.mu.CNInstances)
   116  	return json.MarshalIndent(status, "", JsonIdent)
   117  }