github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/replica/getlag.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 ctrl "sigs.k8s.io/controller-runtime" 28 29 "github.com/1aal/kubeblocks/pkg/lorry/dcs" 30 "github.com/1aal/kubeblocks/pkg/lorry/engines" 31 "github.com/1aal/kubeblocks/pkg/lorry/engines/register" 32 "github.com/1aal/kubeblocks/pkg/lorry/operations" 33 "github.com/1aal/kubeblocks/pkg/lorry/util" 34 ) 35 36 type GetLag struct { 37 operations.Base 38 dcsStore dcs.DCS 39 dbManager engines.DBManager 40 logger logr.Logger 41 } 42 43 var getlag operations.Operation = &GetLag{} 44 45 func init() { 46 err := operations.Register("getlag", getlag) 47 if err != nil { 48 panic(err.Error()) 49 } 50 } 51 52 func (s *GetLag) Init(ctx context.Context) error { 53 s.dcsStore = dcs.GetStore() 54 if s.dcsStore == nil { 55 return errors.New("dcs store init failed") 56 } 57 58 dbManager, err := register.GetDBManager() 59 if err != nil { 60 return errors.Wrap(err, "get manager failed") 61 } 62 s.dbManager = dbManager 63 s.logger = ctrl.Log.WithName("getlag") 64 return nil 65 } 66 67 func (s *GetLag) IsReadonly(ctx context.Context) bool { 68 return false 69 } 70 71 func (s *GetLag) Do(ctx context.Context, req *operations.OpsRequest) (*operations.OpsResponse, error) { 72 sql := req.Parameters["sql"].(string) 73 if sql == "" { 74 return nil, errors.New("no sql provided") 75 } 76 77 resp := &operations.OpsResponse{ 78 Data: map[string]any{}, 79 } 80 resp.Data["operation"] = util.ExecOperation 81 k8sStore := s.dcsStore.(*dcs.KubernetesStore) 82 cluster := k8sStore.GetClusterFromCache() 83 84 lag, err := s.dbManager.GetLag(ctx, cluster) 85 if err != nil { 86 s.logger.Info("executing getlag error", "error", err) 87 return resp, err 88 } 89 90 resp.Data["lag"] = lag 91 return resp, err 92 }