github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/replica/checkstatus.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package replica 21 22 import ( 23 "context" 24 25 "github.com/go-logr/logr" 26 "github.com/pkg/errors" 27 "github.com/spf13/viper" 28 ctrl "sigs.k8s.io/controller-runtime" 29 30 "github.com/1aal/kubeblocks/pkg/lorry/dcs" 31 "github.com/1aal/kubeblocks/pkg/lorry/engines" 32 "github.com/1aal/kubeblocks/pkg/lorry/engines/register" 33 "github.com/1aal/kubeblocks/pkg/lorry/operations" 34 "github.com/1aal/kubeblocks/pkg/lorry/util" 35 ) 36 37 type CheckStatus struct { 38 operations.Base 39 dcsStore dcs.DCS 40 dbManager engines.DBManager 41 checkFailedCount int 42 failedEventReportFrequency int 43 logger logr.Logger 44 } 45 46 var checkstatus operations.Operation = &CheckStatus{} 47 48 func init() { 49 err := operations.Register("checkstatus", checkstatus) 50 if err != nil { 51 panic(err.Error()) 52 } 53 } 54 55 func (s *CheckStatus) Init(ctx context.Context) error { 56 s.dcsStore = dcs.GetStore() 57 if s.dcsStore == nil { 58 return errors.New("dcs store init failed") 59 } 60 61 dbManager, err := register.GetDBManager() 62 if err != nil { 63 return errors.Wrap(err, "get manager failed") 64 } 65 s.dbManager = dbManager 66 67 s.failedEventReportFrequency = viper.GetInt("KB_FAILED_EVENT_REPORT_FREQUENCY") 68 if s.failedEventReportFrequency < 300 { 69 s.failedEventReportFrequency = 300 70 } else if s.failedEventReportFrequency > 3600 { 71 s.failedEventReportFrequency = 3600 72 } 73 74 s.logger = ctrl.Log.WithName("checkstatus") 75 return nil 76 } 77 78 func (s *CheckStatus) IsReadonly(ctx context.Context) bool { 79 return true 80 } 81 82 func (s *CheckStatus) Do(ctx context.Context, req *operations.OpsRequest) (*operations.OpsResponse, error) { 83 resp := &operations.OpsResponse{ 84 Data: map[string]any{}, 85 } 86 resp.Data["operation"] = util.CheckStatusOperation 87 var message string 88 89 k8sStore := s.dcsStore.(*dcs.KubernetesStore) 90 cluster := k8sStore.GetClusterFromCache() 91 isHealthy := s.dbManager.IsCurrentMemberHealthy(ctx, cluster) 92 if !isHealthy { 93 message = "status check failed" 94 s.logger.Info(message) 95 resp.Data["event"] = util.OperationFailed 96 resp.Data["message"] = message 97 var err error 98 if s.checkFailedCount%s.failedEventReportFrequency == 0 { 99 s.logger.Info("status checks failed continuously", "times", s.checkFailedCount) 100 err = util.SentEventForProbe(ctx, resp.Data) 101 } 102 s.checkFailedCount++ 103 return resp, err 104 } 105 s.checkFailedCount = 0 106 resp.Data["event"] = util.OperationSuccess 107 return resp, nil 108 }