github.com/matrixorigin/matrixone@v0.7.0/pkg/cnservice/server_heartbeat.go (about)

     1  // Copyright 2021 - 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 cnservice
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/logutil"
    22  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func (s *service) startCNStoreHeartbeat() error {
    27  	if s._hakeeperClient == nil {
    28  		if _, err := s.getHAKeeperClient(); err != nil {
    29  			return err
    30  		}
    31  	}
    32  	return s.stopper.RunNamedTask("cnservice-heartbeat", s.heartbeatTask)
    33  }
    34  
    35  func (s *service) heartbeatTask(ctx context.Context) {
    36  	if s.cfg.HAKeeper.HeatbeatInterval.Duration == 0 {
    37  		panic("invalid heartbeat interval")
    38  	}
    39  	defer logutil.LogAsyncTask(s.logger, "cnservice/heartbeat-task")()
    40  	defer func() {
    41  		s.logger.Info("cn heartbeat task stopped")
    42  	}()
    43  
    44  	ticker := time.NewTicker(s.cfg.HAKeeper.HeatbeatInterval.Duration)
    45  	defer ticker.Stop()
    46  
    47  	for {
    48  		select {
    49  		case <-ctx.Done():
    50  			return
    51  		case <-ticker.C:
    52  			s.heartbeat(ctx)
    53  			// see pkg/logservice/service_commands.go#130
    54  			select {
    55  			case <-ctx.Done():
    56  				return
    57  			default:
    58  			}
    59  		}
    60  	}
    61  }
    62  
    63  func (s *service) heartbeat(ctx context.Context) {
    64  	ctx2, cancel := context.WithTimeout(ctx, s.cfg.HAKeeper.HeatbeatTimeout.Duration)
    65  	defer cancel()
    66  
    67  	hb := logservicepb.CNStoreHeartbeat{
    68  		UUID:               s.cfg.UUID,
    69  		ServiceAddress:     s.cfg.ServiceAddress,
    70  		SQLAddress:         s.cfg.SQLAddress,
    71  		Role:               s.metadata.Role,
    72  		TaskServiceCreated: s.GetTaskRunner() != nil,
    73  	}
    74  	cb, err := s._hakeeperClient.SendCNHeartbeat(ctx2, hb)
    75  	if err != nil {
    76  		s.logger.Error("failed to send cn heartbeat", zap.Error(err))
    77  		return
    78  	}
    79  	s.handleCommands(cb.Commands)
    80  }
    81  
    82  func (s *service) handleCommands(cmds []logservicepb.ScheduleCommand) {
    83  	for _, cmd := range cmds {
    84  		if cmd.ServiceType != logservicepb.CNService {
    85  			s.logger.Fatal("received invalid command", zap.String("command", cmd.LogString()))
    86  		}
    87  		s.logger.Info("applying schedule command", zap.String("command", cmd.LogString()))
    88  		if cmd.CreateTaskService != nil {
    89  			s.createTaskService(cmd.CreateTaskService)
    90  		}
    91  	}
    92  }