github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/kafka/manager.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 kafka
    21  
    22  import (
    23  	"context"
    24  	"strings"
    25  
    26  	ctrl "sigs.k8s.io/controller-runtime"
    27  
    28  	"github.com/1aal/kubeblocks/pkg/lorry/dcs"
    29  	"github.com/1aal/kubeblocks/pkg/lorry/engines"
    30  )
    31  
    32  const (
    33  	publishTopic = "publishTopic"
    34  	topics       = "topics"
    35  )
    36  
    37  type Manager struct {
    38  	engines.DBManagerBase
    39  	Kafka        *Kafka
    40  	publishTopic string
    41  	topics       []string
    42  }
    43  
    44  var _ engines.DBManager = &Manager{}
    45  
    46  func NewManager(properties engines.Properties) (engines.DBManager, error) {
    47  	logger := ctrl.Log.WithName("Kafka")
    48  	k := NewKafka(logger)
    49  	// in kafka binding component, disable consumer retry by default
    50  	k.DefaultConsumeRetryEnabled = false
    51  
    52  	err := k.Init(context.TODO(), map[string]string(properties))
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	managerBase, err := engines.NewDBManagerBase(logger)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	mgr := &Manager{
    63  		DBManagerBase: *managerBase,
    64  		Kafka:         k,
    65  	}
    66  
    67  	val, ok := properties[publishTopic]
    68  	if ok && val != "" {
    69  		mgr.publishTopic = val
    70  	}
    71  
    72  	val, ok = properties[topics]
    73  	if ok && val != "" {
    74  		mgr.topics = strings.Split(val, ",")
    75  	}
    76  
    77  	return mgr, nil
    78  }
    79  
    80  func (mgr *Manager) IsCurrentMemberHealthy(ctx context.Context, cluster *dcs.Cluster) bool {
    81  	topic := "kb_health_check"
    82  
    83  	err := mgr.Kafka.BrokerOpen()
    84  	if err != nil {
    85  		mgr.Logger.Info("broker open failed", "error", err)
    86  		return false
    87  	}
    88  	defer mgr.Kafka.BrokerClose()
    89  
    90  	err = mgr.Kafka.BrokerCreateTopics(topic)
    91  	if err != nil {
    92  		mgr.Logger.Info("create topic failed", "error", err)
    93  		return false
    94  	}
    95  	return true
    96  }